4 Replies Latest reply on May 2, 2009 9:34 AM by Chunick

    Clock/Time and date

    WarDemonZ

      Hi,

       

      im trying to make a mini program in director and i would like it to show the date and time, i was just wondering what is the most efficient way of making an updating clock, i have updated the date so that it is written in a more appropriate format instead of month/day/year, but thought that if i use the _system.time function that will bring me back the time at this point, so to continually update it i would need for it to constantly check on something such as the exitframe parameter, but i know this can eat up the processor after a while, and seems a little excessive just for a clock. is there a more efficient method of making a clock that updates itself?

       

      also does director know the day it is? i know it understands the over all date ie 5/1/2009 is today, but does (or can it) it know that its friday?

       

      Thanks in advance

       

      Andy

        • 1. Re: Clock/Time and date
          Mike Blaustein Level 4

          Check out BuddyAPI's baSystemTime command.  It will give you the day, date, or time in any of a large number of formats.  Something like this would do just fine on a #text sprite:

           

          on exitFrame me
            sprite(me.spriteNum).member.text=baSystemTime("%A, %B %0d %Y ... %0H:%0M:%0S")
          end

          • 2. Re: Clock/Time and date
            Chunick Level 3

            BuddyAPI, psha! Lingo can handle dates and times just fine, right down to the seconds, in fact. Here's how:

             

            the systemDate is a date object. In the message window type this:

            put the systemDate

             

            ... and this is what's returned:

            -- date( 2009, 5, 1 )

             

            Let's set that to a variable:

            d = the systemDate

             

            Now, I'm going to access the year, month and day, as well as the seconds:

            put d.year

            -- 2009

            put d.month

            -- 5

            put d.day

            -- 1

            put d.seconds

            -- 82560

             

            now, the seconds is actually the number of seconds since the stroke of midnight from the previous day... so, we can actually format the time using the framesToHMS() function:

            put framesToHMS(d.seconds, 1, 0, 0)

            -- " 22:56:00.00 "

             

            ... and then it's an easy enough task to parse the values from the resulting string to remove 1/10s of a second:

            t = framesToHMS(d.seconds, 1, 0, 0)

            the itemDelimiter = "."

            t = t.item[1]

            delete char 1 of t

            put t

            "22:56:00"

             

            ... and is that AM or PM?...

            AMPMList = ["AM", "PM"]

            AMPM = AMPMList[(value(t[1..2]) > 12) + 1]
            if you don't want millitary time, then something like this would work:

            put string(value(t.char[1..2]) - 12) into char 1 to 2 of t

            put t

            -- "11:56:00"

             

            ... and what weekday is it? Assuming Sunday is first day of week:

            weekDay = ((d - date(1600, 1, 1)) - 1) mod 7 + 1

            put weekDay

            -- 6

            ... and if Monday is, then remove the +1 at the end for a value of 5.... and the dayList/dayAbbrevList would have to be adjusted as well.

             

            Now, to put the time and date together:

            monthList = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"]
            monthAbbrevList = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"]
            dayList = ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"]
            dayAbbrevList = ["Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"]

             

            put dayList[weekDay] & "," && monthList[d.month] && d.day & "," && d.year && "@" && t & AMPM

            -- Friday, May 1, 2009 @ 11:56:00PM"

             

            And as you can see, we have pretty much everything we need to parse, modify, edit to get whatever format we would like.. so, it's quite doable with Lingo, but you'll have to build your own code to format it how you like... and converting the seconds using framesToHMS() is just one method... you could simply work out the math to get hours, minutes, and seconds.

            1 person found this helpful
            • 3. Re: Clock/Time and date
              WarDemonZ Level 1

              wow thats fantastic, i hadnt properly delved into just what the system.date function can do but i think ill definitely have a go at it. i was just wondering where would be best to put this code though? because i obviously want the clock to update itself (maybe not as far as seconds) but in minutes so it would need to run the function to contantly check what time it is. i realise i could put this in the exitframe parameter but that would check the time every 30th of a second or so, and that will just slow the processor down (probably not by a huge amount but still)

               

              thank you greatly for that code but i was just wondering if u cud suggest what parameter to use it under

               

              Thanks again

               

              Andy

              • 4. Re: Clock/Time and date
                Chunick Level 3

                Just to clear things up:

                -- verbose syntax

                the systemDate

                 

                -- dot syntax

                _movie.systemDate()

                 

                As well, I found the code I had written to get the hours, minutes and seconds:

                t = _movie.systemDate()

                hours = t.seconds / 3600
                minutes = (t.seconds mod 3600) / 60
                seconds = t.seconds mod 60

                 

                For reference, you can find my code here: http://collab.directorforum.com/Undocumented_Lingo#the_systemDate

                ---------------------------------------------------------------------------

                ---------------------------------------------------------------------------

                Now, to answer your question; you can create yourself a parent script with a timeout object, like so:

                 

                -- create a new script member and change it's Script Type in the Property Inspector to 'Parent'

                -- name it "DateTime Object"

                -- start writing some code

                 

                on new me
                  t = timeOut.new("updateDateTime", 1000, #updateDateTime, me)
                  return me
                end

                 

                on updateDateTime me, tObj
                  -- maybe call another handler here to get the time and format it
                  dateTimeStr = me.getDateTime()
                  sendAllSprites(#updateClock, dateTimeStr)
                end

                 

                on getDateTime me
                  d = _movie.systemDate()
                  -- code here to get the date/time and format it and return it as a string
                  return someString
                end


                -- maybe set up a global variable and then create the object

                -- that way you can literally access it from anywhere in your project

                global gClockObj

                gClockObj = script("DateTime Object").new()

                 

                -- and if you have a handler somewhere else in your code named updateClock, the sendAllSprites in the code will send the message out and the updateClock handler will receive it...

                 

                -- handler example in some script somewhere in your code:

                on updateClock timeStr

                     member("clock output").text = timeStr

                end