Forum Discussion

scaper12123's avatar
9 years ago

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.

3 Replies

  • 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
  • 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()
  • "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.