jgravois
asked on
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
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>
ASKER
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
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
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
rp
ASKER
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!
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!
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
so maybe there is nothing wrong with your code at all... maybe it is just your XML values.
rp
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
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>
ASKER
<<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.
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.
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
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
ASKER
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
So, this code tests to see if "today" is after the DST Start date and before the DST End date.
jon
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
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
ASKER
Line 18 returns UTC time (that has no Daylight Savings Time). I use this throughout to dynamically generate the current time for all locations.
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
...
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
you do understand that this line...
var local:Date = new Date();
is taking its data from the client machine, right?
rp
var local:Date = new Date();
is taking its data from the client machine, right?
rp
ASKER
Do you know of a source I can use?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thanks
so this means that your localDate is not working for you.
rp