dunerunner
Visitor
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-06-2015
08:30 AM
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:
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
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

30 REPLIES 30
sjb64
Roku Guru
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-06-2015
08:34 AM
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)
dunerunner
Visitor
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-06-2015
08:58 AM
Re: Timestamp in milliseconds
So I used your advice and got something like this:
And the values are:
1438876614488
1438876
483647
1438876 483647
What am I doing wrong?
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?
sjb64
Roku Guru
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-06-2015
09:27 AM
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.
Not sure why part 2 is coming up with a totally different number, i'll have to look at that a bit.
dunerunner
Visitor
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-06-2015
09:28 AM
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.
Somehow the second part always returns 483647 for me.
sjb64
Roku Guru
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-06-2015
09:40 AM
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.
I'll look later and if you find the culprit I'd be interested in knowing what it was.
dunerunner
Visitor
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-06-2015
09:42 AM
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 🙂
sjb64
Roku Guru
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-06-2015
09:48 AM
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.
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.
dunerunner
Visitor
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-06-2015
09:53 AM
Re: Timestamp in milliseconds
The timestamp value is stable. I used sleep() to check it. It's always the same
belltown
Roku Guru
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-06-2015
10:32 AM
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