Solved

ActionScript 3: Dynamic Date/Time wrong in certain time zones

Posted on 2009-06-30
16
1,730 Views
Last Modified: 2013-11-11
I am creating a "World Time" Clock for our company's locations in Adobe Flash. I am creating a new Date from the local server and using the UTCOffset to find out the current UTC time. I am then using an XML feed to provide the UTC offset and the Daylight Savings Time period for each area. Everything is working fine except the two time zones with positive UTC offsets are showing their day as Wednesday when it should be Tuesday.

I am going CRAZY!!!!! Could someone tell me what I am doing wrong?

Thanks in Advance,
Jon
//////////// ACTIONSCRIPT 3 CODE ///////////////////////////////////

var display:String = "";

var spacer:String = "     ";

 

//////////////////////// LOAD XML //////////////////

var feedXML:XML;

var a:uint = 0;

var xml:XML;

var ldr:URLLoader = new URLLoader();

var req:URLRequest = new URLRequest("ClockFeed.xml");

ldr.addEventListener(Event.COMPLETE, xmlLoaded);

ldr.load(req);

 

/////////////////////// SETTING TIMES ////////////////////////////////

var dayLabels:Array = new Array("Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat");

 

var local:Date = new Date();

var utc: Date = new Date(local.getTime() + (local.getTimezoneOffset() * 60000));

 

var utcTime:Number=utc.getTime();

 

/////////////////// FUNCTIONS ///////////////////////////////

function xmlLoaded(evt:Event):void

{

	feedXML = new XML(evt.target.data);

	a = feedXML.*.item.length();

	//trace(feedXML);

	

	var b:uint = 0;

	

	while (b < a)

	{

		var locationTitle:String = feedXML.*.item.title[b];

		var locationOffset:String = feedXML.*.item.UTC_Offset[b];

		var locationStartDST:String = feedXML.*.item.DST_Start[b];

		var locationEndDST:String = feedXML.*.item.DST_End[b];

		//trace(locationTitle, locationOffset, locationStartDST, locationEndDST)

		

		if(locationStartDST == "None")

		{

			var localDateCipher:Number = utcTime + (3600000 * Number(locationOffset));

			var localDate:Date = new Date(localDateCipher);

			//trace(localDate+" for "+locationTitle+" -- Line 39");

		} else {

			

			var startDate:Date = new Date(locationStartDST);

			var endDate:Date = new Date(locationEndDST);

			

			if ((startDate < local) && (endDate > local))

			{

				var localDateCipher:Number = utcTime + (3600000 * (Number(locationOffset)+1));

				var localDate:Date = new Date(localDateCipher);

				//trace(localDate+" for "+locationTitle+" -- Line 49");

			} else {

				var localDateCipher:Number = utcTime + (3600000 * Number(locationOffset));

				var localDate:Date = new Date(localDateCipher);

				//trace(localDate+" for "+locationTitle+" -- Line 53");

			} // end inner if

		}// end if

		

		if (locationTitle == "NewYork")

		{

			var locale:String = "New York";

		} else {

			var locale:String = locationTitle;

		}// end if

		

		var day:String = dayLabels[localDate.getUTCDay()];

		var h:Number = localDate.getHours();

		var m = String(localDate.getMinutes());

		var hours:String;

		var AMPM:String;

		

		if (h == 0) {

			hours = "12";

			AMPM = "AM";

		} else if (h == 12) {

			hours = "12";

			AMPM = "PM";

		} else if (h > 12) {

			hours = String(h - 12);

			AMPM = "PM";

		} else if (h < 12) {

			hours = String(h);

			AMPM = "AM";

		} // end if

 

		if(hours.length==1)

		{

			hours="0"+hours;

		} // end if

		

		if(m.length==1)

		{

			m="0"+m;

		} // end if

		

		display += "<b>"+locale + "</b>: " + day+ "  "+ hours+":"+m+" "+AMPM+spacer;

		trace(locale + ": " + day+ "  "+ hours+":"+m+" "+AMPM);

	b++;

	} // end while

	this.displayText.htmlText = display;

} // end function xmlLoaded

 

 

///////////////////////// XML FILE CODE ///////////////////////////////

<?xml version="1.0"?>

<rss version="2.0">

  <channel>

    <title>Stock and Clock Feeder</title>

    <link></link>

    <description>All of the necessary information to feed Flash</description>

    <item>

       <title>Memphis</title>

       <description>GFS Americas</description>

       <UTC_Offset>-6</UTC_Offset>

       <DST_Start>Sun Mar 8 2009 02:00:00 AM</DST_Start>

       <DST_End>Sun Nov 1 2009 02:00:00 AM</DST_End>

    </item>

    <item>

       <title>NewYork</title>

       <description>GFS America</description>

       <UTC_Offset>-5</UTC_Offset>

       <DST_Start>Sun Mar 8 2009 02:00:00 AM</DST_Start>

       <DST_End>Sun Nov 1 2009 02:00:00 AM</DST_End>

    </item>

    <item>

       <title>Dublin</title>

       <description>GFS Europe</description>

       <UTC_Offset>0</UTC_Offset>

       <DST_Start>Sun Mar 29 2009 01:00:00 AM</DST_Start>

       <DST_End>Sun Oct 25 2009 02:00:00 AM</DST_End>

    </item>

	<item>

       <title>Delhi</title>

       <description>GFS APAC</description>

       <UTC_Offset>5.5</UTC_Offset>

       <DST_Start>None</DST_Start>

       <DST_End>None</DST_End>

    </item>

	<item>

       <title>Dalian</title>

       <description>GFS APAC</description>

       <UTC_Offset>8</UTC_Offset>

       <DST_Start>None</DST_Start>

       <DST_End>None</DST_End>

    </item>

  </channel>

</rss>

Open in new window

0
Comment
Question by:jgravois
  • 9
  • 7
16 Comments
 
LVL 22

Expert Comment

by:rascalpants
ID: 24758544
can you post your trace() outputs for this...  obviously, you start with this line of code (  var day:String = dayLabels[localDate.getUTCDay()];  )  and work backwards...

so this means that your localDate is not working for you.


rp
0
 

Author Comment

by:jgravois
ID: 24762062
Here is the trace() output:
Thu Jul 2 11:17:00 GMT-0500 2009
Memphis: Thu  06:17 AM
New York: Thu  07:17 AM
Dublin: Thu  12:17 PM
Delhi: Thu  04:47 PM
Dalian: Fri  07:17 PM
0
 
LVL 22

Expert Comment

by:rascalpants
ID: 24764262
can you post it again and tell me what it should be...  sorry... I thought I could get more information from your trace statements, but I am not sure what they actually should be.


rp
0
 

Author Comment

by:jgravois
ID: 24764305
Memphis has a -6 UTCoffset
New York has a -5 UTC offset
Dublin has no offset
Delhi has a +5.5 UTC offset
Dalian has a +8 UTC offset
Therefore if the following trace is outputed
Thu Jul 2 11:17:00 GMT-0500 2009
Memphis: Thu  06:17 AM
New York: Thu  07:17 AM
Dublin: Thu  12:17 PM
Delhi: Thu  04:47 PM
Dalian: Fri  07:17 PM  
Dalian should be Thu and not Fri
 
Hope this answers the question
 
Thanks!
0
 
LVL 22

Expert Comment

by:rascalpants
ID: 24765188
okay... so i just googled "Dalian time" and it gave me 12:40am Friday (CST)

so maybe there is nothing wrong with your code at all...  maybe it is just your XML values.


rp
0
 
LVL 22

Expert Comment

by:rascalpants
ID: 24765295
when I replicate your project, this is the trace I get...

Memphis: Thu 11:51 AM
New York: Thu 12:51 PM
Dublin: Thu 05:51 PM
Delhi: Fri 10:21 PM
Dalian: Fri 12:51 AM

this seems correct.

the only thing in your code that I needed to fix was to move some variables that where getting duplicated... which are on lines 30 - 32 in the code below


rp

//////////// ACTIONSCRIPT 3 CODE ///////////////////////////////////

var display:String = "";

var spacer:String = "     ";

 

//////////////////////// LOAD XML //////////////////

var feedXML:XML;

var a:uint = 0;

var xml:XML;

var ldr:URLLoader = new URLLoader();

var req:URLRequest = new URLRequest("ClockFeed.xml");

ldr.addEventListener(Event.COMPLETE, xmlLoaded);

ldr.load(req);

 

/////////////////////// SETTING TIMES ////////////////////////////////

var dayLabels:Array = new Array("Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat");

 

var local:Date = new Date();

var utc: Date = new Date(local.getTime() + (local.getTimezoneOffset() * 60000));

 

var utcTime:Number=utc.getTime();

 

/////////////////// FUNCTIONS ///////////////////////////////

function xmlLoaded(evt:Event):void

{

	feedXML = new XML(evt.target.data);

	a = feedXML.*.item.length();

	//trace(feedXML);

	

	var b:uint = 0;

	var localDate:Date;

	var localDateCipher:Number;

	var locale:String;

	

	while (b < a)

	{

		var locationTitle:String = feedXML.*.item.title[b];

		var locationOffset:String = feedXML.*.item.UTC_Offset[b];

		var locationStartDST:String = feedXML.*.item.DST_Start[b];

		var locationEndDST:String = feedXML.*.item.DST_End[b];

		//trace(locationTitle, locationOffset, locationStartDST, locationEndDST)

		

		if(locationStartDST == "None")

		{

			localDateCipher = utcTime + (3600000 * Number(locationOffset));

			localDate = new Date(localDateCipher);

			//trace(localDate+" for "+locationTitle+" -- Line 39");

		} else {

			

			var startDate:Date = new Date(locationStartDST);

			var endDate:Date = new Date(locationEndDST);

			

			if ((startDate < local) && (endDate > local))

			{

				localDateCipher = utcTime + (3600000 * (Number(locationOffset)+1));

				localDate = new Date(localDateCipher);

				//trace(localDate+" for "+locationTitle+" -- Line 49");

			} else {

				localDateCipher = utcTime + (3600000 * Number(locationOffset));

				localDate = new Date(localDateCipher);

				//trace(localDate+" for "+locationTitle+" -- Line 53");

			} // end inner if

		}// end if

		

		if (locationTitle == "NewYork")

		{

			locale = "New York";

		} else {

			locale = locationTitle;

		}// end if

		

		var day:String = dayLabels[localDate.getUTCDay()];

		var h:Number = localDate.getHours();

		var m = String(localDate.getMinutes());

		var hours:String;

		var AMPM:String;

		

		if (h == 0) {

			hours = "12";

			AMPM = "AM";

		} else if (h == 12) {

			hours = "12";

			AMPM = "PM";

		} else if (h > 12) {

			hours = String(h - 12);

			AMPM = "PM";

		} else if (h < 12) {

			hours = String(h);

			AMPM = "AM";

		} // end if

 

		if(hours.length==1)

		{

			hours="0"+hours;

		} // end if

		

		if(m.length==1)

		{

			m="0"+m;

		} // end if

		

		display += "<b>"+locale + "</b>: " + day+ "  "+ hours+":"+m+" "+AMPM+spacer;

		trace(locale + ": " + day+ "  "+ hours+":"+m+" "+AMPM);

	b++;

	} // end while

	this.displayText.htmlText = display;

} // end function xmlLoaded

 

 

////  XML feed ///

 

<?xml version="1.0"?>

<rss version="2.0">

  <channel>

    <title>Stock and Clock Feeder</title>

    <link></link>

    <description>All of the necessary information to feed Flash</description>

    <item>

       <title>Memphis</title>

       <description>GFS Americas</description>

       <UTC_Offset>-6</UTC_Offset>

       <DST_Start>Sun Mar 8 2009 02:00:00 AM</DST_Start>

       <DST_End>Sun Nov 1 2009 02:00:00 AM</DST_End>

    </item>

    <item>

       <title>NewYork</title>

       <description>GFS America</description>

       <UTC_Offset>-5</UTC_Offset>

       <DST_Start>Sun Mar 8 2009 02:00:00 AM</DST_Start>

       <DST_End>Sun Nov 1 2009 02:00:00 AM</DST_End>

    </item>

    <item>

       <title>Dublin</title>

       <description>GFS Europe</description>

       <UTC_Offset>0</UTC_Offset>

       <DST_Start>Sun Mar 29 2009 01:00:00 AM</DST_Start>

       <DST_End>Sun Oct 25 2009 02:00:00 AM</DST_End>

    </item>

	<item>

       <title>Delhi</title>

       <description>GFS APAC</description>

       <UTC_Offset>5.5</UTC_Offset>

       <DST_Start>None</DST_Start>

       <DST_End>None</DST_End>

    </item>

	<item>

       <title>Dalian</title>

       <description>GFS APAC</description>

       <UTC_Offset>8</UTC_Offset>

       <DST_Start>None</DST_Start>

       <DST_End>None</DST_End>

    </item>

  </channel>

</rss> 

Open in new window

0
 

Author Comment

by:jgravois
ID: 24765902
<<quote>>
Memphis: Thu 11:51 AM
New York: Thu 12:51 PM
Dublin: Thu 05:51 PM
Delhi: Fri 10:21 PM
Dalian: Fri 12:51 AM

this seems correct.
<</quote>>
if Delhi is 5.5 hours ahead of UTC and Dalian is 8 hours ahead of UTC, the day should still be "Thu" and not "Fri." The time has always been correct...I can't figure out why the day is wrong.
0
 
LVL 22

Expert Comment

by:rascalpants
ID: 24766290
what is this code doing...

var startDate:Date = new Date(locationStartDST);
var endDate:Date = new Date(locationEndDST);

if ((startDate < local) && (endDate > local))
{


if you run this code without the if then conditional statement, you get the right trace values...

localDateCipher = utcTime + (3600000 * Number(locationOffset));
localDate = new Date(localDateCipher);


what is that conditional for?

we have narrowed it down to those lines of code.


rp



0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

Author Comment

by:jgravois
ID: 24766526
There is no "OOB" solution for Daylight Savings Time in ActionScript and I don't want to hard code the offset because that would mean that I would have to edit the .fla to reflect when a country enters or leaves DST. So, I use an XML file which can be edited to represent the DST start date and end date since these can change (as they did a year ago in the USA).
So, this code tests to see if "today" is after the DST Start date and before the DST End date.
 
jon
0
 
LVL 22

Expert Comment

by:rascalpants
ID: 24767129
okay, but because you are using the local time/date of the client Machine, which would already account for DST, you comparison is not going to work very well.

my PC is using DST already, so adding one more to the equation would be one too many...utcTime is a value that is based on a time that already has DST integrated into it from the local machine.

Probably a better way to do all of this is the get the date object form a server-side script that will be the same for everyone that views the application. Because you have to think about people who travel and may have set there clock ahead a few hours... then returned home and their application will be wrong, cause they never set their clock back.

I don't think you can rely on the user's clock to run this application properly.
plus, you will have to add another conditional that checks to see if the server's clock is running on DST, and then check to see if the country is in the time frame of when they use DST.  then adjust your value based on if those conditional all are met.

rp



0
 

Author Comment

by:jgravois
ID: 24767144
Line 18 returns UTC time (that has no Daylight Savings Time). I use this throughout to dynamically generate the current time for all locations.
0
 
LVL 22

Expert Comment

by:rascalpants
ID: 24767164
I just updated the above comment...  read it again
...

and it doesn't matter...  my machine IS using DST, so our results whould always be different.

you need to pull the date object from a single source where you know if that source is already using DST or not, and adjust for the need to use DST in other countries.


rp
0
 
LVL 22

Expert Comment

by:rascalpants
ID: 24767176
you do understand that this line...

var local:Date = new Date();

is taking its data from the client machine, right?


rp
0
 

Author Comment

by:jgravois
ID: 24767240
Do you know of a source I can use?
0
 
LVL 22

Accepted Solution

by:
rascalpants earned 500 total points
ID: 24767353
your server.  just have your server-side code pass in a FlashVar of the date, in the normal date format, and also you can pass in if it is currently using DST in the location of your server.

rp
0
 

Author Closing Comment

by:jgravois
ID: 31598346
Thanks
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Firefox video player - embedded native versus VLC player 3 1,064
Online Course Not Responding 3 89
How to block online streaming via TMG2010 ? 2 122
Running Mp4 or Flash Video 1 101
Here are some practices and techniques that can be adopted into your Flash/Flex application development process. Note: Not all "performance tips" provide an immediately-recognizable benefit.   This article does not include timing validation data,…
I have found that much of my time doing support ends up being a constant repetition of the same steps to different people.  Early on I stated using web pages with Frequently Asked Questions (FAQs) to alleviate most of the burden.  Sometimes this jus…
In this tutorial viewers will learn how to create a basic motion tween animation in Flash Open a new document in Flash: Draw/import an image: Press CTRL + F8 to convert it into a graphic symbol: Select a frame (how long you want the tween to last): …
The goal of the tutorial is to teach the user how to set there setting in Adobe Flash Media Live Encoder and YouTube for optimal video and audio quality.

912 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

24 Experts available now in Live!

Get 1:1 Help Now