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

How to check for time differences?

Is there a quick and simple way to compare two roDateTime objects and find the time difference between them? I intend to do this to get the duration of a timer. While I'm thinking about it, is there also an easy and simple way to display a countdown to match the timer or does that require a manual touch?

EDIT: What I'm trying is using the AsSeconds functionality of roDateTime to determine the time between the two values. Specifically, this is the code I have:
'stream_dt represents the date and time of a planned event, current_dt represents the system's date and time
'values are pre-adjusted to account for time-zone differences
timeLeft = AsSeconds(stream_dt) - AsSeconds(current_dt)

I'm not sure if this will work, however. If anybody has any suggestions or if they believe this idea WOULD work, I would love to hear it in order to boost my confidence here.
I AM THE ARCHMAGE... who is also rather new to Brightscript so forgive me for seeming inept at times.
0 Kudos
3 REPLIES 3
RokuMarkn
Visitor

Re: How to check for time differences?

Assuming both roDateTimes are in GMT, this will work. However AsSeconds is a member function, so the syntax should be

timeLeft = stream_dt.AsSeconds() - current_dt.AsSeconds()


--Mark
0 Kudos
belltown
Roku Guru

Re: How to check for time differences?

You're on the right track. Do what Mark said. Here's a bit more explanation of why.

roDateTime is a component. When you create an instance of a component, e.g. dt = CreateObject("roDateTime"), you end up with an object, so dt would be an object instance of the roDateTime component. Components implement interfaces. An interface provides functionality to a component through the use of methods. If you look at the roDateTime component documentation, you'll see that it implements the ifDateTime interface, which is where you'd look to find out about the functionality implemented by the roDateTime component. The interface documentation lists the methods it supports, for example, AsSeconds().

The important thing about using interface methods is that they have to be called on an object, by specifying the object's identifier followed by a period, the interface name, and a set of parentheses containing parameters, if any.

So to get the number of seconds from the Unix epoch for an roDateTime, you'd do something like this:


dt = CreateObject("roDateTime")
numberOfSeconds = dt.AsSeconds()


To find the difference in seconds between two roDateTime objects, you could do this:


differenceSeconds = dt2.AsSeconds() - dt1.AsSeconds()
0 Kudos
scaper12123
Visitor

Re: How to check for time differences?

"belltown" wrote:
To find the difference in seconds between two roDateTime objects, you could do this:


differenceSeconds = dt2.AsSeconds() - dt1.AsSeconds()


Thank you for the advice. Much appreciated. I'm getting something now, at the very least. In testing, I've found this tactic to be a reliable method to determine the time left until an event starts. Although, for some strange reason, I'm getting an unknown error whenever I try to call on the xml that uses the same code as my program (Because I don't know how to make connections between xml and the brightscript code). My problem is solved for now, though. Thank you.
I AM THE ARCHMAGE... who is also rather new to Brightscript so forgive me for seeming inept at times.
0 Kudos