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: 
dunerunner
Visitor

Timestamp in milliseconds

Hi, everyone.

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 Smiley LOL
0 Kudos
30 REPLIES 30
sjb64
Roku Guru

Re: Timestamp in milliseconds

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)
0 Kudos
dunerunner
Visitor

Re: Timestamp in milliseconds

So I used your advice and got something like this:

time = CreateObject("roDateTime")
timestamp = GetGlobalAA().util.ToMilliseconds(time)

print timestamp

part1 = Int(timestamp / 1000000).toStr()
part2 = Str(timestamp MOD 1000000)

print part1
print part2
timeStampString = part1 + part2

print timeStampString


And the values are:
1438876614488
1438876
483647
1438876 483647

What am I doing wrong?
0 Kudos
sjb64
Roku Guru

Re: Timestamp in milliseconds

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

Re: Timestamp in milliseconds

The problem is the second part is not equal to the timestamp second part, I can format the string after that.

Somehow the second part always returns 483647 for me.
0 Kudos
sjb64
Roku Guru

Re: Timestamp in milliseconds

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

Re: Timestamp in milliseconds

I would be very grateful if you look into this. But if I find an answer, I will surely post it 🙂
0 Kudos
sjb64
Roku Guru

Re: Timestamp in milliseconds

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

Re: Timestamp in milliseconds

The timestamp value is stable. I used sleep() to check it. It's always the same
0 Kudos
belltown
Roku Guru

Re: Timestamp in milliseconds

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