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

Get Milliseconds from Local Time

I am trying to get the miliseconds from the date time object. I suspect it's something to do with type casting but not sure. I'm using the asSeconds() function to get the seconds and then multiplying by 1000. I'm getting some weird results:

start_time = CreateObject("roDateTime")
start_time.Mark()
start_time.ToLocalTime()

start_time_seconds = CreateObject("roInt")
start_time_miliseconds = CreateObject("roInt")
'convert to miliseconds'
start_time_seconds = start_time.asSeconds()
print "start time seconds: " + itostr(start_time_seconds)
start_time_miliseconds = start_time_seconds * 1000
print type(start_time_miliseconds)
print tostr(start_time_miliseconds)

end_time_miliseconds = CreateObject("roInt")
end_time_miliseconds = (1 * 3600000) + start_time_miliseconds
print type(end_time_miliseconds)

print itostr(end_time_miliseconds)


Telnet results. I'm not sure why I am getting -58130832 as this should be positive. Any help would be appreciated. Thanks!
start time seconds: 1361446502
Integer
-58130832
Integer
-50930832
0 Kudos
7 REPLIES 7
RokuMarkn
Visitor

Re: Get Milliseconds from Local Time

asSeconds returns an integer. The current time (seconds from the Unix epoch) is about 1361404800 (one billion). If you multiply this by 1000 you will overflow a 32 bit integer and get nonsense results. You can solve this by using an explict floating point variable:

start_time_seconds = start_time.asSeconds()
start_time_miliseconds# = start_time_seconds * 1000


A couple of other comments about your code:
Pre-creating the variables with CreateObject is not necessary. You can omit the two "roInt" creations.
Converting a time to local time is almost always a bad idea, unless it's for display purposes. Twice a year, local time has discontinuities due to DST. GMT time does not have this problem.

--Mark
0 Kudos
edatl
Visitor

Re: Get Milliseconds from Local Time

Thank you for your response. I ran the changes and I am still getting an error :

start_time = CreateObject("roDateTime")
start_time.Mark()
'start_time.ToLocalTime()

'convert to miliseconds'
start_time_seconds = start_time.asSeconds()
print "start time seconds: " + itostr(start_time_seconds)
start_time_miliseconds# = start_time_seconds * 1000
print type(start_time_miliseconds)
print Str(start_time_miliseconds)
end_time_miliseconds# = (hours * 3600000) + start_time_miliseconds
print Str(end_time_miliseconds)


Use of uninitialized variable. (runtime error &he9) in ...9S0W/pkg:/source/smsUtils.brs(205)

205: print Str(start_time_miliseconds)

The print statement print type(start_time_miliseconds) comes back with <uninitialized> as the output.
I have removed local time as stated.
0 Kudos
RokuMarkn
Visitor

Re: Get Milliseconds from Local Time

The # at the end is part of the variable name. You've omitted it in a couple of places, including the print statement. In other words, start_time_milliseconds and start_time_milliseconds# are two different variables.

--Mark
0 Kudos
edatl
Visitor

Re: Get Milliseconds from Local Time

Thank you again for your quick response, almost there I hope!

start_time = CreateObject("roDateTime")
start_time.Mark()
'start_time.ToLocalTime()

'convert to miliseconds'
start_time_seconds = start_time.asSeconds()
print "start time seconds: " + itostr(start_time_seconds)
start_time_miliseconds# = start_time_seconds * 1000
print type(start_time_miliseconds#)
print Str(start_time_miliseconds#)
end_time_miliseconds# = (2 * 3600000) + start_time_miliseconds#
print Str(end_time_miliseconds#)


start time seconds: 1361479201
Double
-2.543183e+07
-1.823183e+07


I am still getting a negative number returned with the exponential notation. What am I doing wrong here? Also, once I get the correct value, how can I output a string that will not have the exponential notation and just the regular number? Str() does not seem to be the way.
0 Kudos
RokuMarkn
Visitor

Re: Get Milliseconds from Local Time

Sorry, my code wasn't quite right. You need to convert to a float before you do the multiply, otherwise it does an integer multiply and still overflows. So start_time_seconds should also be changed to have a # at the end.

To print a float, just say "print variable". No need to use Str.

--Mark
0 Kudos
edatl
Visitor

Re: Get Milliseconds from Local Time

Thank you Mark, this worked great!

Now if I want to put the float into a string, I used Str() and it gave me the exponential notation.

get_string$ = "?user=1&" + Str(float_value#)

1.361486e+12
Is there a way I can use do this without writing a function to regex and add the correct number of zeros?
0 Kudos
renojim
Community Streaming Expert

Re: Get Milliseconds from Local Time

Unless Mark points one out, I don't think there's a Double-to-String function. Here's one I whipped up quickly:
Function DoubleToString(x# as Double) as String
onemill# = 1000000
xhi = Int(x#/onemill#).toStr()
xlo = Int((x#-onemill#*Int(x#/onemill#))).toStr()
xlo = String(6-xlo.Len(),"0") + xlo
return xhi+xlo
End Function

Barely tested, so caveat emptor.

-JT
Roku Community Streaming Expert

Help others find this answer and click "Accept as Solution."
If you appreciate my answer, maybe give me a Kudo.

I am not a Roku employee.
0 Kudos
Need Assistance?
Welcome to the Roku Community! Feel free to search our Community for answers or post your question to get help.

Become a Roku Streaming Expert!

Share your expertise, help fellow streamers, and unlock exclusive rewards as part of the Roku Community. Learn more.