Home Tutorials Forums Articles Blogs Movies Library Employment Press

Go Back   ActionScript.org Forums > ActionScript Forums Group > ActionScript 3.0

Reply
 
Thread Tools Rate Thread Display Modes
Old 02-12-2010, 03:49 PM   #1
danee987
Senior Member
 
Join Date: Apr 2005
Posts: 140
Question countdown timer/timezone logic??

Hoping one of you programming geniouses could help me on this. I created a countdown timer which needs to countdown an event accurately no matter where you are located in the world. So based on my understanding of timezones etc.. and flash the only way I can get Paris time if I am not in Paris is by using my machine clock to get a time offset between that and Paris. But this isn't working as I expect it and im totally confused:S

I would think that if an event is happening say in 5 hours, that no matter what time or timezone I have on my machine my countdown should still say the event is due in 5 hours. But if I change my timezone or time my countdown also changes which to me doesn't make sense although I could be wrong??? My code is below could anybody help me out? Maybe my logic is off?? Could anyone help?

ActionScript Code:
var _endDate:Date = new Date(2010,1,13); //This is 1 because Paris is one hour ahead of UTC var timeZoneFromUTC:Number = 1; countdownTimer=new Timer(10); countdownTimer.addEventListener(TimerEvent.TIMER,_updateTime); countdownTimer.start(); function _updateTime(e:TimerEvent){ _now = new Date(); //Converting our desired timezone difference from hours to minutes var $timeZoneFromUTCMinutes:Number = timeZoneFromUTC * 60;             //Converting timezone offset and timezone from UTC minutes into seconds. Then adding them together //This is the total offset (in seconds) from your current location to the desired location. var $secondsOffset:Number = (_now.getTimezoneOffset() * 60) + ($timeZoneFromUTCMinutes * 60)             //Converting offset from seconds to milliseconds to feed to the formula below var $milliSecsOffset:Number = $secondsOffset * 1000;             _timeLeft =(_endDate.getTime() - _now.getTime()) + -($milliSecsOffset); _seconds = Math.floor(_timeLeft / 1000); _minutes = Math.floor(_seconds / 60 ); _hours = Math.floor(_minutes / 60); _days = Math.floor(_hours / 24); _seconds %= 60; _minutes %= 60; _hours %=  24 trace (_days) trace (_hours) trace(_minutes) trace (_seconds) if(_days==0 && _hours==0 && _minutes==0 && _seconds==0){    dispatchEvent(new Event("timeUp"));    countdownTimer.stop();    trace ("times up!!!") }else dispatchEvent(new Event("active")); }
danee987 is offline   Reply With Quote
Old 02-13-2010, 10:03 AM   #2
henke37
Senior Member
 
henke37's Avatar
 
Join Date: Mar 2009
Location: Sweden
Posts: 10,682
Send a message via Skype™ to henke37
Default

There is plenty of properties for UTC time in the Date class, read the manual. Try using those.
__________________
Signature: I wrote a pair of articles about the timeline.
henke37 is online now   Reply With Quote
Old 02-15-2010, 04:21 PM   #3
danee987
Senior Member
 
Join Date: Apr 2005
Posts: 140
Default

Hi Henke thanks for the help but I guess my problem is that from what I have read and learned in the manual I THINK my logic is accurate. But my code is still not working correctly. The logic I am using is this one:

Set destination timezone (note: could be positive or negative number).
Get difference between local user time and utc time.(note:Western hemisphere is a positive number, Eastern is a negative).
Get difference between utc time and destination timezone.
Add both differences together and convert to milliseconds to pass to countdown formula.
Begin countdown code

I guess my first question would be does this logic make sense? If programmed correctly should this countdown a time accurately no matter where viewer and timezone is located? It seems to me like it should.

Second question, it doesn't seem like I am adding differences of numbers correctly on my code especially if a number is a negative. Could someone confirm this? From the testing I've done it seems like I am coding that incorrectly but nothing I've tried seems to give me an expected result.

Thanks in advance!

Last edited by danee987; 02-15-2010 at 04:39 PM.
danee987 is offline   Reply With Quote
Old 02-18-2010, 06:00 PM   #4
danee987
Senior Member
 
Join Date: Apr 2005
Posts: 140
Default

anyone? :S
danee987 is offline   Reply With Quote
Old 02-24-2010, 09:33 PM   #5
danee987
Senior Member
 
Join Date: Apr 2005
Posts: 140
Default

I think I figured it out. So far it has behaved as expected. Just wanted to post the class I made in case anyone needs something similar:

ActionScript Code:
package time{     import flash.events.Event;     import flash.events.EventDispatcher;     import flash.utils.Timer;     import flash.events.TimerEvent;     import flash.text.TextField;                 /**      *      * This class is used to create countdowns for events.      *      * @example      * <br>      * var dueDate:Date = new Date(2010,3,1);<br><br>      *      * var myCounter:CountdownTimer = new CountdownTimer(dueDate);<br><br>      *      * myCounter.timeZoneFromUTC = 2;//Very important setting. See the description of this property on this documentation.<br><br>      *      * myCounter.addEventListener("active", onCounterActivate);<br><br>      *      * function onCounterActivate(e:Event):void {<br><br>      *      * var daysString:String = String(myCounter.days);<br>      * var hoursString:String = String(myCounter.hours);<br>      * var minutesString:String = String(myCounter.minutes);<br>      * var secondsString:String = String(myCounter.seconds);<br>      *      * if(daysString.length<2){<br><br>      *      *  daysString="0"+ daysString;<br><br>      *      *  }<br><br>      *      *  if(hoursString.length<2){<br><br>      *      *  hoursString="0"+ hoursString;<br><br>      *      *  }<br><br>      *      *  if(minutesString.length<2){<br><br>      *      *  minutesString="0"+ minutesString;<br><br>      *      *  }<br><br>      *      *  if(secondsString.length<2){<br><br>      *      *  secondsString="0"+ secondsString;<br><br>      *      *  }<br><br>      *      *  trace( daysString + ":" + hoursString + ":" + minutesString + ":" + secondsString);<br><br>      * }      *      */     public class CountdownTimer extends EventDispatcher{                 private var _endDate:Date                 private var _now:Date;         private var _timeLeft:Number;         private var _seconds:Number;         private var _minutes:Number;         private var _hours:Number;         private var _days:Number;         private var _timeZoneFromUTC:Number;         private var countdownTimer:Timer                 /**          *          * The seconds left for the countdown to end.          *          */         public function get seconds():Number {                         return _seconds;                 }                 /**          *          * The minutes left for the countdown to end.          *          */         public function get minutes():Number {                         return _minutes;                     }                 /**          *          * The hours left for the countdown to end.          *          */         public function get hours():Number {                         return _hours;                 }                 /**          *          * The days left for the countdown to end.          *          */         public function get days():Number {                         return _days;                 }                 /**          * The current hour difference between the UTC time zone and a desired location. Default is 0.          * This setting needs to be accurate so the countdown timer works as intended. You can          * check online to get the correct timezone difference for your desired location. It may          * be a good idea to search for UTC time zone first to know where this timezon is located. From          * there it would be easier to count to the timezone you wish to countdown from and apply that number          * difference on this property. One thing to note the timezone difference is reversed from what you will find online. Timezones to the West should be a POSITIVE number and timezones to the east should be NEGATIVE.          *          */         public function get timeZoneFromUTC():Number {                         return _timeZoneFromUTC;                     }                 /**          * @private          */         public function set timeZoneFromUTC($t:Number):void  {                         _timeZoneFromUTC = $t;                     }                 /**          *          * Creates an instance of the class.          *          * @param   $endDate The Date object to countdown to          * @param   $timeZoneFromUTC The timezone in which the event is located. It is necessary to have          * the correct timezone in order for the countdown to work correctly.          */         public function CountdownTimer($endDate:Date, $timeZoneFromUTC:Number = 0):void{                         _endDate = $endDate             timeZoneFromUTC = $timeZoneFromUTC;             //<--1000 milliseconds = 1 second (this is how often we want the timer to go off             countdownTimer=new Timer(10);             //we are listening for an event called TIMER on the TimerEvent class which will be triggered every 1000 seconds             countdownTimer.addEventListener(TimerEvent.TIMER,_updateTime);             countdownTimer.start();             //and everytime is triggered it will call on the updateTime function below                     }                 /* PRIVATE */                 private function _updateTime(e:TimerEvent):void {                         _now = new Date();                                     //Already gives offset in minutes no need to convert.             var $timeZoneOffset:Number = _now.getTimezoneOffset();                         //Converting our desired timezone difference from hours to minutes             var $timeZoneFromUTCMinutes:Number = timeZoneFromUTC * 60;                         //converting to seconds             var $timeZoneOffsetInseconds:Number = $timeZoneOffset * 60;             //converting to seconds             var $timeZoneFromUTCInSeconds:Number = $timeZoneFromUTCMinutes * 60;                         //Converting timezone offset and timezone from UTC minutes into seconds. Then adding them together             //This is the total offset (in seconds) from your current location to the desired location.             var $secondsOffset:Number;                                     $secondsOffset = $timeZoneOffsetInseconds - $timeZoneFromUTCInSeconds;                                 //Converting offset from seconds to milliseconds to feed to the formula below             var $milliSecsOffset:Number = $secondsOffset * 1000;                                     _timeLeft = (_endDate.getTime() - _now.getTime())  - $milliSecsOffset;                         _seconds = Math.floor(_timeLeft / 1000);             _minutes = Math.floor(_seconds / 60 );             _hours = Math.floor(_minutes / 60);             _days = Math.floor(_hours / 24);                                                 _seconds %= 60; //this is same as    seconds = seconds % 60;             _minutes %= 60; //this is same as    minutes = minutes % 60;                         _hours %=  24                         trace (_days)             trace (_hours)             trace(_minutes)             trace (_seconds)                         if(_days==0 && _hours==0 && _minutes==0 && _seconds==0){                 dispatchEvent(new Event("timeUp"));                 countdownTimer.stop();                 trace ("times up!!!")             }                         else dispatchEvent(new Event("active"));                                 }     } }
danee987 is offline   Reply With Quote
Reply


Thread Tools
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT. The time now is 03:43 PM.

///
Follow actionscriptorg on Twitter

 


Powered by vBulletin® Version 3.8.5
Copyright ©2000 - 2014, Jelsoft Enterprises Ltd.
Ad Management plugin by RedTyger
Copyright 2000-2013 ActionScript.org. All Rights Reserved.
Your use of this site is subject to our Privacy Policy and Terms of Use.