12-30-2008, 04:48 AM
|
#1
|
|
Registered User
Join Date: Dec 2008
Posts: 1
|
change frames based on UTC time
I'd like to place a time-based ad on my site - essentially advertising a "happy hour" every tuesday. Before happy hour, the flash file would show one frame. During happy hour it would show something different and link to a promo. After happy hour it shows something else.
I can get this working with the user's local time using the code below, but I'd really like to use UTC so that the happy hour is the same for everyone. I've tried changing all the .get references to .getUTC to no avail.
Any help would be greatly appreciated.
alarmDay = 2;
alarmHour = 14;
alarmMinute = 11;
kMillisecondsInSecond = 1000;
kMillisecondsInMinute = kMillisecondsInSecond*60;
kMillisecondsInHour = kMillisecondsInMinute*60;
kMillisecondsInDay = kMillisecondsInHour*24;
mySetAlarm = function (alarmDay, alarmHour, alarmMinute) {
var d = new Date();
var dm = new Date(d.getFullYear(), d.getMonth(), d.getDate());
var ms = dm.getTime();
var day = dm.getDay();
var dayD = (alarmDay+7-day)%7;
ms += dayD*kMillisecondsInDay;
ms += alarmHour*kMillisecondsInHour;
ms += alarmMinute*kMillisecondsInMinute;
var df = new Date(ms);
trace(" Setting alarm for "+df);
var msDiff = df.getTime()-d.getTime();
alarmHandle = setInterval(myAlarmAction, msDiff, alarmDay, alarmHour, alarmMinute);
};
mySetAlarm(alarmDay, alarmHour, alarmMinute);
myAlarmAction = function (alarmDay, alarmHour, alarmMinute) {
clearInterval(alarmHandle);
gotoAndStop(10);
};
mySetAlarm(alarmDay, alarmHour, alarmMinute);
alarm2Day = 2;
alarm2Hour = 14;
alarm2Minute = 12;
kMillisecondsInSecond = 1000;
kMillisecondsInMinute = kMillisecondsInSecond*60;
kMillisecondsInHour = kMillisecondsInMinute*60;
kMillisecondsInDay = kMillisecondsInHour*24;
mySetAlarm2 = function (alarm2Day, alarm2Hour, alarm2Minute) {
var d = new Date();
var dm = new Date(d.getFullYear(), d.getMonth(), d.getDate());
var ms = dm.getTime();
var day = dm.getDay();
var dayD = (alarm2Day+7-day)%7;
ms += dayD*kMillisecondsInDay;
ms += alarm2Hour*kMillisecondsInHour;
ms += alarm2Minute*kMillisecondsInMinute;
var df = new Date(ms);
trace(" Setting alarm for "+df);
var msDiff = df.getTime()-d.getTime();
alarm2Handle = setInterval(myAlarm2Action, msDiff, alarm2Day, alarm2Hour, alarm2Minute);
};
mySetAlarm2(alarm2Day, alarm2Hour, alarm2Minute);
myAlarm2Action = function (alarm2Day, alarm2Hour, alarm2Minute) {
clearInterval(alarm2Handle);
gotoAndStop(20);
};
mySetAlarm2(alarm2Day, alarm2Hour, alarm2Minute);
|
|
|
12-30-2008, 07:57 PM
|
#2
|
|
Flashing my ass off.
Join Date: Oct 2008
Location: Nebraska, if you can believe it.
Posts: 63
|
Offset timer alarm based on UTC or local time
Simply add the getTimezoneOffset() (this is in minutes) to your result.
Your code is a bit AS2-ish. With AS3, it is easier to make this into a class, and inherit that class from a Flash library object's "linkage".
The class below is not very robust, since there is only a single alarm per class instance. However, it does show you how to convert to UTC time.
Code:
package
{
import flash.display.MovieClip;
import flash.events.TimerEvent;
import flash.utils.Timer;
/**
* ...
* @author Delfeld, copyright 2008
*/
public class alarmTestAS extends MovieClip
{
private var alarm:Timer;
private var endingDate:Date;
public function alarmTestAS()
{
getAlarmDate(0, 0, 0, 2, true);
setAlarm();
}
private function setAlarm():void
{
// end time - immediate time = offset time, timer delay, timer interval, etc.
alarm = new Timer(endingDate.time - (new Date()).time,1);
alarm.addEventListener(TimerEvent.TIMER_COMPLETE, timerHandler, false, 0, true);
alarm.start();
}
private function getAlarmDate(dyOffset:int, hrOffset:int, mnOffset:int, secOffset:int, asUTC:Boolean = false):void
{
endingDate = new Date();
//endingDate.setMinutes(endingDate.minutes + endingDate.timezoneOffset);
// Change time to an offset of the current UTC time.
// Note that this will still maintain your current clock's offset in the endingDate object,
// but the time - delivered via a non-UTC method - will be offset from the UTC time.
if (asUTC)
{
trace(" UTC offset minutes: " + endingDate.timezoneOffset);
endingDate.setUTCHours(
endingDate.getUTCHours() + hrOffset,
endingDate.getUTCMinutes() + mnOffset + endingDate.getTimezoneOffset(),
endingDate.getUTCSeconds() + secOffset
);
endingDate.setUTCDate(
endingDate.getUTCDate() + dyOffset
);
}
// Change time to an offset of the current local time.
else
{
endingDate.setHours(
endingDate.getHours() + hrOffset,
endingDate.getMinutes() + mnOffset, //+ endingDate.getTimezoneOffset()
endingDate.getSeconds() + secOffset
);
endingDate.setDate(
endingDate.getDate() + dyOffset
);
}
trace(" alarm time: \t" + endingDate.toString());
trace(" local time: \t" + (new Date()).toString());
trace(" UTC time: \t\t" + (new Date()).toUTCString());
}
private function timerHandler(e:TimerEvent):void
{
alarm.stop();
e.target.removeEventListener(e.type, timerHandler);
trace("alarmTestAS.timerHandler: " + alarm.delay + " ms");
}
}
}
Last edited by delfeld; 12-30-2008 at 08:25 PM.
|
|
|
| Thread Tools |
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
All times are GMT. The time now is 07:34 AM.
|
|