"EnTerr" wrote:
All this pain and suffering. Seems like a case of "if something is too hard, perhaps you are approaching it the wrong way".
How about using roDateTime.toISOString()?
You get a timestamp which (a) can be used directly for comparisons (because of the format used, string lexical order gives you proper chronological order) and (b) can be converted server-side to native timestamp, if time differences are to be calculated.
"TheEndless" wrote:
Neither ToISOString nor FromISO8601String supports milliseconds, so if millisecond precision is important, that wouldn't work.
BrightScript Debugger> ti = createObject("roDateTime")
BrightScript Debugger> ? left(ti.ToISOString(), 19) + "." + right("000" + ti.getMilliseconds().toStr(), 3) + "Z"
2015-08-07T19:48:35.076Z
"EnTerr" wrote:
Ugh! I was mislead by "2009-01-01T01:00:00.000" while skimming the doc
BrightScript Debugger> ti.FromISO8601String("2009-01-02T03:04:05.678")
BrightScript Debugger> ? ti.toISOstring(), ti.getMilliseconds()
2009-01-02T03:04:05Z 678
"EnTerr" wrote:
they might have fixed it by now:BrightScript Debugger> ti.FromISO8601String("2009-01-02T03:04:05.678")
BrightScript Debugger> ? ti.toISOstring(), ti.getMilliseconds()
2009-01-02T03:04:05Z 678
BrightScript Debugger> ti.FromISO8601String("2009-01-02T03:04:05.678Z")
BrightScript Debugger> ? ti.toISOstring(), ti.getMilliseconds()
1970-01-01T00:00:00Z 0
Set the date/time using a string in the ISO 8601 format. For example "YYYY-MM-DD HH:MM:SS" e.g "2009-01-01 01:00:00.000" or "2009-01-01T01:00:00.000". Note that this function is unaware of the local time zone, so these time formats are effectively UTC even though the ISO 8601 spec says they should be in local time. The above formats are also the only formats recognized by this function, even though the ISO 8601 spec contains other valid formats.
"TheEndless" wrote:
Interesting.. It doesn't work with the Z, though...BrightScript Debugger> ti.FromISO8601String("2009-01-02T03:04:05.678Z")
BrightScript Debugger> ? ti.toISOstring(), ti.getMilliseconds()
1970-01-01T00:00:00Z 0
Hi guys) maybe it'll help somebody someday)
timestamp = CreateObject("roLongInteger") time = CreateObject("roDateTime") ts = time.AsSeconds() tms = time.GetMilliseconds() timestamp.SetLongInt( ts ) timestamp.SetLongInt( timestamp.GetLongInt() * 1000 ) timestamp.SetLongInt( timestamp.GetLongInt() + tms )
now timestamp.GetLongInt() should return timestamp in milliseconds
Note that you don't need to do all the explicit Get/SetLongInt stuff.
I.e. you can do regular arithmetic on LongInteger types as long as your expression either implicitly or explicitly has that type (as opposed to plain Integer).
For example, either:
time = CreateObject("roDateTime") ts = time.AsSecondsLong() * 1000 + time.GetMilliseconds()
or
time = CreateObject("roDateTime") ts = time.AsSeconds() * 1000& + time.GetMilliseconds()
will work.
dt = CreateObject("roDateTime")
seconds = dt.AsSeconds()
print "***>>> seconds: " seconds
milliSecondsPart = dt.GetMilliseconds()
print "***>>> milliSecondsPart: " milliSecondsPart
'This way to do is wrong
totalMilliSeconds = (seconds * 1000) + milliSecondsPart
print "***>>> totalMilliSeconds (wrong): " totalMilliSeconds
x = CreateObject("roLongInteger")
x.SetLongInt(seconds)
x = (x * 1000) + dt.GetMilliseconds()
print "***>>> totalMilliSeconds: " x
Log:
***>>> seconds: 1675287438
***>>> milliSecondsPart: 967
***>>> totalMilliSeconds (wrong): 250193527
***>>> totalMilliSeconds: 1675287438967