Roku Developer Program

Developers and content creators—a complete solution for growing an audience directly.
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
sjb64
Level 7

Re: Timestamp in milliseconds

Was too focused thinking about formatting that I didn't even think about the datetime functions, much better and simpler approach.
0 Kudos
dunerunner
Level 7

Re: Timestamp in milliseconds

"belltown" wrote:
There are too many significant digits to store in any of the Roku number types, but you can get the milliseconds value as a string:

dt = CreateObject ("roDateTime")
ms = dt.AsSeconds ().ToStr () + Right ("00" + dt.GetMilliseconds ().ToStr (), 3)
Print ms



The problem is I need the timestamp to be a numeric value to make calculations with it and only after that I need to convert it to string.
The direct conversion to string happens inside my ToMilliseconds function.
0 Kudos
sjb64
Level 7

Re: Timestamp in milliseconds

Use a rotimespan with it's Mark ability or roappmanagers uptime field , then your only using deltas from the app start in your calculation? The seconds would be smaller, and you could still pull datetime for display using the method Belltown showed. I don't know your exact needs, but could this work?
0 Kudos
dunerunner
Level 7

Re: Timestamp in milliseconds

I am writing an analytics library and need to track the exact time of user actions and send them to server. Sometimes I need to calculate the difference bertween two timestamps. that is why I need it to be numeric. If there were no calculations with the timestamps I would just store them as strings.
0 Kudos
sjb64
Level 7

Re: Timestamp in milliseconds

I'd use an roDateTime to sent to the server, you shouldn't need MS for that, but if you do send seconds and MS as 2 fields if possible, or use the concatentation things from before to send to the server as a string, then use the roTimeSpan or roAppManager uptime to for the difference calculations.
0 Kudos
dunerunner
Level 7

Re: Timestamp in milliseconds

Thank you for all your help, but the answer I came up with looks like this:

ToMilliseconds = Function(time, timestring = "")
If time <> invalid
timestamp = time.AsSeconds()
ms = time.GetMilliseconds()
timestampString = timestamp.ToStr() + ms.ToStr()
Else
timestampString = timestring
End If
low = timestampString.Mid(timestampString.Len() - 9, 9).ToInt()
high = timestampString.Mid(0, timestampString.Len() - 9).ToInt()
output# = high
output# = output# * 1000000000
output# = output# + low

return output#
End Function


And then:
time = CreateObject("roDateTime")
timestamp = GetGlobalAA().util.ToMilliseconds(time)
print timestamp

part1 = Int(timestamp / 1000000).toStr().Trim()

coolString = part1 + "000000"
coolTimestamp = GetGlobalAA().util.ToMilliseconds(invalid, coolString)

part2 = Str(timestamp - coolTimestamp).Trim()

print part1
print part2

timeStampString = part1 + part2
print "--------------"
print timeStampString


BTW BrightScript is the best language in the World Smiley LOL
0 Kudos
RokuMarkn
Level 8

Re: Timestamp in milliseconds

"dunerunner" wrote:



timestampString = timestamp.ToStr() + ms.ToStr()



I don't think that will work. Depending on the value of ms, you'll be appending either 1, 2 or 3 digits to timestamp.

--Mark
0 Kudos
sjb64
Level 7

Re: Timestamp in milliseconds

Belltown's suggestion should fix that:

"belltown" wrote:
There are too many significant digits to store in any of the Roku number types, but you can get the milliseconds value as a string:

dt = CreateObject ("roDateTime")
ms = dt.AsSeconds ().ToStr () + Right ("00" + dt.GetMilliseconds ().ToStr (), 3)
Print ms
0 Kudos
dunerunner
Level 7

Re: Timestamp in milliseconds

Thanks, fixed it.
0 Kudos
EnTerr
Level 11

Re: Timestamp in milliseconds

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.
0 Kudos