Solved

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

Posted on 2009-06-30
16
1,742 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
Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

 

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
 

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

NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
3D rotation for a product 3 339
HTML 5 or Flash 7 486
Failed download 57 563
Rotating Adobe pdf document 4 312
Sometimes you know that one object has a specific child in it, but you can't find the child. This happened to me when I was trying to code some actionScript to make a toolbar work with its embedded buttons.  My partner had created the toolbar usi…
I have been doing hardcore actionscripting for some time; and needless to say I have faced a lot of problems in just understanding others' code rather than understanding what the code executes. A programmer's life can become hell when there are a lo…
The goal of the tutorial is to teach the user how to use the auto adjust feature and what the different options do. When your video is not working right you can choose the auto adjust feature to help choose your settings.
This Micro Tutorial will teach to how to utilize bit rate in Adobe Flash Media Live Encoder.

777 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