What’s the easiest way to conver this string
2017-11-10T18:02:43.6072734+01:00
into Xojo’s date format ?
Thanks
R
What’s the easiest way to conver this string
2017-11-10T18:02:43.6072734+01:00
into Xojo’s date format ?
Thanks
R
Public Function FromISO8601Date1(t as text) as xojo.Core.Date
#if debugbuild
#pragma BreakOnExceptions false
#endif
// these should be formed as as ISO 8601 format
// <date>T<time><TZ>
// where
// <dates> are stated as YYYY-MM-DD
// <times> are stated as HH:MM:SS.NNNN
// HH is hours (NOT 24 hour format)
// MM is minutes
// SS is seconds
// NNNN is nanoseconds (may be more than 4 digits long)
// <TZ> is formed as
// Z if the TZ is GMT (offset = 0)
// -hhmm if the tz is GMT- (offset < 0)
// +hhmm if the tz is GMT+ (offset > 0)
// hh is hours before GMT
// mm is minutes before GMT
// so we can handle offsets that are not whole or half hours
// 1 2 3
// 01234567890123456789012345678901234567890
// ie/ YYYY-MM-DDTHH:MM:SS.NNNNZ
// 1 2 3
// 01234567890123456789012345678901234567890
// YYYY-MM-DDTHH:MM:SS.NNNN+hhmm
// 1 2 3
// 01234567890123456789012345678901234567890
// YYYY-MM-DDTHH:MM:SS.NNNN-hhmm
dim year, month, day as integer
dim hour, minutes, seconds, nanos as integer
dim gmthrs, gmtmins as integer
dim tmp as text
// everything except a few spots has to be a number
dim digits as text = "0123456789"
for i as integer = 0 to t.length()-1
if i = 4 then continue
if i = 7 then continue
if i = 10 then continue
if i = 13 then continue
if i = 16 then continue
if i = 19 then continue
if i >= 20 and t.mid(i,1) = "." then continue
if i >= 20 and t.mid(i,1) = "Z" then continue
if i >= 20 and t.mid(i,1) = "+" then continue
if i >= 20 and t.mid(i,1) = "-" then continue
if digits.IndexOf(t.mid(i,1)) < 0 then raise new UnsupportedFormatException
next
// check for -'s in the right spots
tmp = t.mid(4,1)
if tmp <> "-" then raise new UnsupportedFormatException
tmp = t.mid(7,1)
if tmp <> "-" then raise new UnsupportedFormatException
// check for T in the right spots
tmp = t.mid(10,1)
if tmp <> "T" then raise new UnsupportedFormatException
// check for :'s in the right spots
tmp = t.mid(13,1)
if tmp <> ":" then raise new UnsupportedFormatException
tmp = t.mid(16,1)
if tmp <> ":" then raise new UnsupportedFormatException
// check for .'s in the right spots
tmp = t.mid(19,1)
if tmp <> "." then raise new UnsupportedFormatException
// convert data
tmp = t.Mid(0,4)
if tmp.Length <> 4 then raise new UnsupportedFormatException
year = Integer.FromText(tmp)
tmp = t.Mid(5,2)
if tmp.Length <> 2 then raise new UnsupportedFormatException
month = Integer.FromText(tmp)
tmp = t.Mid(8,2)
if tmp.Length <> 2 then raise new UnsupportedFormatException
day = Integer.FromText(tmp)
tmp = t.Mid(11,2)
if tmp.Length <> 2 then raise new UnsupportedFormatException
hour = Integer.FromText(tmp)
tmp = t.Mid(14,2)
if tmp.Length <> 2 then raise new UnsupportedFormatException
minutes = Integer.FromText(tmp)
tmp = t.Mid(17,2)
if tmp.Length <> 2 then raise new UnsupportedFormatException
seconds = Integer.FromText(tmp)
// ok from here we read either to a +/-/Z
dim i as integer = 20
tmp = ""
while true
if t.mid(i,1) = "+" then exit
if t.mid(i,1) = "-" then exit
if t.mid(i,1) = "Z" then exit
tmp = tmp + t.mid(i,1)
i = i + 1
wend
nanos = Integer.FromText(tmp)
// i is ON the separator whatever it was (Z + or -)
dim mult as integer = 1
if t.mid(i,1) = "Z" then
i = i + 1
else
if t.mid(i,1) = "-" then
mult = -1
end if
i = i + 1
if t.length() - i <> 4 then raise new UnsupportedFormatException
tmp = t.mid(i,2)
if tmp.Length <> 2 then raise new UnsupportedFormatException
gmthrs = Integer.FromText(tmp)
i = i + 2
tmp = t.mid(i,2)
if tmp.Length <> 2 then raise new UnsupportedFormatException
gmtmins = Integer.FromText(tmp)
i = i + 2
end if
if t.length <> i then raise new UnsupportedFormatException
dim gmtoffset as integer = mult * (gmthrs * 3600) + (gmtmins * 60)
dim tz as new xojo.core.TimeZone(gmtoffset)
//(year, month, day, hour, minute, seconds, nanoseconds, timezone As TimeZone)
return new xojo.core.date(year, month, day, hour, minutes, seconds, nanos, tz)
End Function
I get a compile error at “TimeUtilities”
Utilitaires.FromISO8601ToDate, line 139
This item does not exist
Dim gmtoffset As Integer = mult * (gmthrs * TimeUtilities.Hours) + (gmtmins * TimeUtilities.Minutes)
copy & paste the code again
They’re just two constants (3600 and 60) that can be replaced by literals
[Insert my regular statement about not copy and pasting code without reading it to see how it works]
such beautifully formatted source code coming from Norman must be working directly.
so first check is copy-paste and check for compile …
I just copied it out of some other code I have and literally never looked at the fact I had two constants in there that would make it not compile
Norman, thank you so much !