Roku Developer Program

Join our online forum to talk to Roku developers and fellow channel creators. Ask questions, share tips with the community, and find helpful resources.
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
TheEndless
Channel Surfer

Re: Timestamp in milliseconds

"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.

Neither ToISOString nor FromISO8601String supports milliseconds, so if millisecond precision is important, that wouldn't work.
My Channels: http://roku.permanence.com - Twitter: @TheEndlessDev
Instant Watch Browser (NetflixIWB), Aquarium Screensaver (AQUARIUM), Clever Clocks Screensaver (CLEVERCLOCKS), iTunes Podcasts (ITPC), My Channels (MYCHANNELS)
0 Kudos
EnTerr
Roku Guru

Re: Timestamp in milliseconds

"TheEndless" wrote:
Neither ToISOString nor FromISO8601String supports milliseconds, so if millisecond precision is important, that wouldn't work.

Ugh! I was mislead by "2009-01-01T01:00:00.000" while skimming the doc

PS. let me compensate for that:
BrightScript Debugger> ti = createObject("roDateTime")
BrightScript Debugger> ? left(ti.ToISOString(), 19) + "." + right("000" + ti.getMilliseconds().toStr(), 3) + "Z"
2015-08-07T19:48:35.076Z
0 Kudos
TheEndless
Channel Surfer

Re: Timestamp in milliseconds

"EnTerr" wrote:
Ugh! I was mislead by "2009-01-01T01:00:00.000" while skimming the doc

If that format is indeed in the documentation, then it's misleading and they should fix it, as roDateTime.FromISO8601String() will fail to parse it, returning 1/1/1970 instead. The fact that FromISO8601String doesn't support ISO dates with milliseconds is a bug, in my opinion, and has tripped me up on a number of occasions.
My Channels: http://roku.permanence.com - Twitter: @TheEndlessDev
Instant Watch Browser (NetflixIWB), Aquarium Screensaver (AQUARIUM), Clever Clocks Screensaver (CLEVERCLOCKS), iTunes Podcasts (ITPC), My Channels (MYCHANNELS)
0 Kudos
EnTerr
Roku Guru

Re: Timestamp in milliseconds

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
0 Kudos
TheEndless
Channel Surfer

Re: Timestamp in milliseconds

"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

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
My Channels: http://roku.permanence.com - Twitter: @TheEndlessDev
Instant Watch Browser (NetflixIWB), Aquarium Screensaver (AQUARIUM), Clever Clocks Screensaver (CLEVERCLOCKS), iTunes Podcasts (ITPC), My Channels (MYCHANNELS)
0 Kudos
belltown
Roku Guru

Re: Timestamp in milliseconds

The documentation for FromISO8601String() is definitely confusing. It gives an example of the format supported: "YYYY-MM-DD HH:MM:SS" (note, no milliseconds), but when it goes on to give examples of the example, it includes milliseconds: "2009-01-01 01:00:00.000" or "2009-01-01T01:00:00.000". It says those are the only formats recognized. Neither the example format nor the examples of the example format include the "Z" at the end. ToISOString() supposedly does have a "Z" at the end.

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.
0 Kudos
RokuKC
Roku Employee
Roku Employee

Re: Timestamp in milliseconds

"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


Thanks for the bug report. Issue confirmed.
0 Kudos
anat
Newbie

Re: Timestamp in milliseconds

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

0 Kudos
RokuKC
Roku Employee
Roku Employee

Re: 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.

hdonapati
Reel Rookie

Re: Timestamp in milliseconds

 

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

 

0 Kudos