Home Tutorials Forums Articles Blogs Movies Library Employment Press

Go Back   ActionScript.org Forums > ActionScript Forums Group > ActionScript 3.0

Reply
 
Thread Tools Rate Thread Display Modes
Old 12-30-2008, 04:48 AM   #1
fischerwest
Registered User
 
Join Date: Dec 2008
Posts: 1
Red face 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);
fischerwest is offline   Reply With Quote
Old 12-30-2008, 07:57 PM   #2
delfeld
Flashing my ass off.
 
delfeld's Avatar
 
Join Date: Oct 2008
Location: Nebraska, if you can believe it.
Posts: 63
Default 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.
delfeld is offline   Reply With Quote
Reply


Thread Tools
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Change color on several frames in the timeline dc2000 ActionScript 3.0 8 11-08-2008 09:09 PM
mapping a video time on frames DanglingChap ActionScript 3.0 1 02-26-2008 11:19 AM
changing a background image based on the time of day. krislewisjones ActionScript 2.0 0 02-21-2007 08:59 AM
[AS2] timebar based on time for game raskolnikov Gaming and Game Development 1 09-08-2005 01:03 PM
Moving MC based on time vlb1176 ActionScript 1.0 (and below) 6 06-30-2005 02:51 PM


All times are GMT. The time now is 07:34 AM.


Powered by vBulletin® Version 3.8.5
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Ad Management plugin by RedTyger
Copyright 2000-2010 ActionScript.org. All Rights Reserved.
Your use of this site is subject to our Privacy Policy and Terms of Use.