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

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

jgravoisAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

rascalpantsCommented:
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
jgravoisAuthor Commented:
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
rascalpantsCommented:
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
Big Business Goals? Which KPIs Will Help You

The most successful MSPs rely on metrics – known as key performance indicators (KPIs) – for making informed decisions that help their businesses thrive, rather than just survive. This eBook provides an overview of the most important KPIs used by top MSPs.

jgravoisAuthor Commented:
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
rascalpantsCommented:
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
rascalpantsCommented:
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
jgravoisAuthor Commented:
<<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
rascalpantsCommented:
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
jgravoisAuthor Commented:
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
rascalpantsCommented:
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
jgravoisAuthor Commented:
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
rascalpantsCommented:
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
rascalpantsCommented:
you do understand that this line...

var local:Date = new Date();

is taking its data from the client machine, right?


rp
0
jgravoisAuthor Commented:
Do you know of a source I can use?
0
rascalpantsCommented:
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
jgravoisAuthor Commented:
Thanks
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Adobe Flash

From novice to tech pro — start learning today.