I recently ran into an issue with BrightScript when trying to get the timestamp in milliseconds. I use this (hack)function to get the value:
ToMilliseconds = Function(time)
timestamp = time.AsSeconds()
ms = time.GetMilliseconds()
timestampString = timestamp.ToStr() + ms.ToStr()
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
But now I’ve encountered a problem when I try to convert this value to JSONstring.
Basically, I need a value that looks like “1438866985492”, but I get “1.438867e+12”. If anyone could help me with this issue I would be so happy
An old trick from years ago when you don’t have formatting abiltiies it to take (number/1,000,000 (as integer) as a string) + (number mod 1,000,000 as a string)
If I recall correctly, Str() adds a space for the sign position on positive numbers, tostr() does not, so switch to tostr() or use a trim function to clear the leading space.
Not sure why part 2 is coming up with a totally different number, i’ll have to look at that a bit.
Am at a loss on that one, will play with it when I get a chance, but I don’t have a clue why mod gives an incorrect answer. The error isn’t a power of 2 so not an overflow issue I don’t think, and totalmilliseconds is a 32 bit integer so we are within the limits and shouldn’t be overflowing anyway.
I’ll look later and if you find the culprit I’d be interested in knowing what it was.
Am curious, if you print timestamp many times in a row is it stable? I know it’s a ridiculous thought but if timestamp was pointing to the function, not the result of the function, it would be fluid and changing as your code ran.
Also, once this works, remember to pad part2 to 6 places with 0’s on the left, just in case it had some leading zeros trimmed.
“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.
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?
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.
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.
“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
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.