Solved

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

Posted on 2009-06-30
16
1,719 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
<<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
Comment Utility
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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 

Author Comment

by:jgravois
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
Do you know of a source I can use?
0
 
LVL 22

Accepted Solution

by:
rascalpants earned 500 total points
Comment Utility
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
Comment Utility
Thanks
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

I know the transition can be hard. We got used to the ease of use ActionScript 2 had, but honestly, it became problematic later on, especially if designers were involved in the project and found it easy to add code as they saw fit. So, this artic…
I come across a lot of question about how to access things in the document class from a movieclip, or accessing something from a movieclip in the document class. It took me a while to figure this out but once I did it makes life so much easier. …
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.
This Micro Tutorial will teach to how to utilize bit rate in Adobe Flash Media Live Encoder.

771 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

12 Experts available now in Live!

Get 1:1 Help Now