Your Digital Media Has Never Looked So Good

 
dunerunner
Topic Author
Posts: 10
Joined: Wed Jul 22, 2015 3:12 am

Timestamp in milliseconds

Thu Aug 06, 2015 8:30 am

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 :lol:
 
sjb64
Posts: 108
Joined: Thu Apr 16, 2015 1:13 pm
Location: Memphis, TN

Re: Timestamp in milliseconds

Thu Aug 06, 2015 8:34 am

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)
FlixRaider channel
 
dunerunner
Topic Author
Posts: 10
Joined: Wed Jul 22, 2015 3:12 am

Re: Timestamp in milliseconds

Thu Aug 06, 2015 8:58 am

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?
 
sjb64
Posts: 108
Joined: Thu Apr 16, 2015 1:13 pm
Location: Memphis, TN

Re: Timestamp in milliseconds

Thu Aug 06, 2015 9:27 am

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.
FlixRaider channel
 
dunerunner
Topic Author
Posts: 10
Joined: Wed Jul 22, 2015 3:12 am

Re: Timestamp in milliseconds

Thu Aug 06, 2015 9:28 am

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.
 
sjb64
Posts: 108
Joined: Thu Apr 16, 2015 1:13 pm
Location: Memphis, TN

Re: Timestamp in milliseconds

Thu Aug 06, 2015 9:40 am

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.
FlixRaider channel
 
dunerunner
Topic Author
Posts: 10
Joined: Wed Jul 22, 2015 3:12 am

Re: Timestamp in milliseconds

Thu Aug 06, 2015 9:42 am

I would be very grateful if you look into this. But if I find an answer, I will surely post it :)
 
sjb64
Posts: 108
Joined: Thu Apr 16, 2015 1:13 pm
Location: Memphis, TN

Re: Timestamp in milliseconds

Thu Aug 06, 2015 9:48 am

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.
FlixRaider channel
 
dunerunner
Topic Author
Posts: 10
Joined: Wed Jul 22, 2015 3:12 am

Re: Timestamp in milliseconds

Thu Aug 06, 2015 9:53 am

The timestamp value is stable. I used sleep() to check it. It's always the same
 
belltown
Posts: 1465
Joined: Thu Dec 09, 2010 1:43 pm
Contact:

Re: Timestamp in milliseconds

Thu Aug 06, 2015 10:32 am

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
https://github.com/belltown/
 
sjb64
Posts: 108
Joined: Thu Apr 16, 2015 1:13 pm
Location: Memphis, TN

Re: Timestamp in milliseconds

Thu Aug 06, 2015 11:09 am

Was too focused thinking about formatting that I didn't even think about the datetime functions, much better and simpler approach.
FlixRaider channel
 
dunerunner
Topic Author
Posts: 10
Joined: Wed Jul 22, 2015 3:12 am

Re: Timestamp in milliseconds

Fri Aug 07, 2015 6:03 am

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.
 
sjb64
Posts: 108
Joined: Thu Apr 16, 2015 1:13 pm
Location: Memphis, TN

Re: Timestamp in milliseconds

Fri Aug 07, 2015 6:11 am

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?
FlixRaider channel
 
dunerunner
Topic Author
Posts: 10
Joined: Wed Jul 22, 2015 3:12 am

Re: Timestamp in milliseconds

Fri Aug 07, 2015 6:27 am

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.
 
sjb64
Posts: 108
Joined: Thu Apr 16, 2015 1:13 pm
Location: Memphis, TN

Re: Timestamp in milliseconds

Fri Aug 07, 2015 7:02 am

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.
FlixRaider channel

Who is online

Users browsing this forum: No registered users and 1 guest