Welcome!

Welcome to the official BlackBerry Support Community Forums.

This is your resource to discuss support topics with your peers, and learn from each other.

inside custom component

Adobe AIR Development

Reply
Developer
Posts: 660
Registered: ‎10-08-2009
My Device: Bold 9900
My Carrier: Telus
Accepted Solution

display time

Hi, I am posting because I can't find how to do this anywhere!

 

I am trying to display current system time and I've been successful at displaying it. However, If possible, I want it to display live time of the system. I'm thinking about using a timer to update either every second or minute.

 

Is there any easier and logical way to do this?

----------------------------------------------------------
Twitter: @gyubok
Developer
Posts: 274
Registered: ‎11-01-2010
My Device: PlayBook?
My Carrier: WiFi

Re: display time

[ Edited ]

It sounds like you want to use an EnterFrameEvent to update, instead of making your own timers.

 

I'm not sure if I understand the question properly though.

 

EDIT:

 

addEventListener(Event.ENTER_FRAME, updateSystemTime);

 

function updateSystemTime(event:Event)

{

//your code here

}

 

Or something like that.

 

Developer
Posts: 660
Registered: ‎10-08-2009
My Device: Bold 9900
My Carrier: Telus

Re: display time

Thanks for the reply!

 

Will this update the display time (i.e. change from 11:42 to 11:43 in one minute)?

----------------------------------------------------------
Twitter: @gyubok
Developer
Posts: 274
Registered: ‎11-01-2010
My Device: PlayBook?
My Carrier: WiFi

Re: display time

All the enterFrame function will do is run your code every frame, so it depends what you're using to grab the system time and display it. Whatever you're doing there, just put that code in the enterFrame and it will keep getting called and update itself whenever the system time changes.

 

Developer
Posts: 2,462
Registered: ‎11-04-2010
My Device: Bold 9700

Re: display time

[ Edited ]

hey gyubok,

 

to add on to what RottenOgre stated here is a sample code to show and update the system time. there is also a TimeFormatter class in QNX but im not a huge fan of it and seems to be off sometimes. So i created my own:

 

 

package
{
	import flash.display.Sprite;
	import flash.display.StageAlign;
	import flash.display.StageScaleMode;
	import flash.events.TimerEvent;
	import flash.text.TextFieldAutoSize;
	import flash.utils.Timer;
	
	import qnx.ui.text.Label;
	
	
	[SWF(width="1024", height="600", backgroundColor="#E8E8E8", frameRate="30")]
	public class TimeDisplayTest extends Sprite
	{
		private var timeLabel:Label;
		private var myTimer:Timer;
		private var currentDate:Date;
		
		public function TimeDisplayTest()
		{
			super();
			
			// support autoOrients
			stage.align = StageAlign.TOP_LEFT;
			stage.scaleMode = StageScaleMode.NO_SCALE;
			
			displayTime();
		}
		
		private function displayTime():void
		{
			/*
			 * We set up our label, timer and date
			 * Label will display our time onto the screen
			 * Timer will be used to "listen" for each second's tick
			 * based on the tick we call our update function
			*/
			
			timeLabel = new Label();
			myTimer = new Timer(1000);
			currentDate = new Date();
			
			myTimer.start();
			
			timeLabel.setPosition(100,100);
			timeLabel.autoSize = TextFieldAutoSize.LEFT;
			timeLabel.text = formatTime(currentDate);
			
			myTimer.addEventListener(TimerEvent.TIMER, updateTime);
			
			addChild(timeLabel);
			
		}
		
		private function updateTime(e:TimerEvent):void
		{
			trace("tick");
			
			/*
			 * Every second we update the time label with its new time
			 * we update the currentDate with a new date every call
			*/
			
			currentDate = new Date();
			
			timeLabel.text = formatTime(currentDate);
		}
		
		private function formatTime(curDate:Date):String
		{
			var formattedTime:String;
			
			var curHour:int = curDate.getHours();
			var curMins:int = curDate.getMinutes();
			var curSecs:int = curDate.getSeconds();
			
			/*
			 * To get the ending AM or PM we do a simple test for the hour
			*/
			
			var curEnding:String = ((curHour > 11) ? "PM" : "AM");
			
			/*
			 * I'm not a fan of military time so I like my numbers less than 13
			*/
			
			curHour = ((curHour > 12) ? curHour - 12 : curHour);
			
			/*
			 * Return the formatted time including adding zeros such as "02" instead of "2"
			*/
			
			formattedTime = formatStartingZero(curHour) + ":" + formatStartingZero(curMins) + ":" + formatStartingZero(curSecs) + " " + curEnding;
			
			
			return formattedTime;
		}
		
		private function formatStartingZero(num:int):String
		{
			/*
			 * Function to format and return the number based on its value of being less than 10
			*/
			
			var formattedNum:String;
			
			formattedNum = ((num < 10) ? "0" + num.toString() : num.toString());
			
			return formattedNum;
			
		}
	}
}

 

There are comments to explain the code. if you need further help let me know. this should be what you are looking for. good luck!

 

J. Rab (Blog) (Twitter)
--
1. If you liked my post or found it useful please click on the thumbs up and provide a Like!
2. If my post solved your problem please click on the Accept as Solution button. Much appreciated!

Approved Apps: OnTrack | ssShots | Hangman
Highlighted
Developer
Posts: 6,473
Registered: ‎12-08-2010
My Device: PlayBook, Z10
My Carrier: none

Re: display time

By the way, if you are going to do it this way, note that while using the ENTER_FRAME would lead you to update too often (wasting battery power, unless you drop the frame rate down), using a 1.0 second timer would lead to inconsistent updates, with one increment missed every so often (like maybe once per every minute or two).

 

The problem is that, even under a realtime OS like QNX :-), the ActionScript Timer timing is not going to be perfect and is not synchronized to the actual system time. 

 

Sometimes two ticks will be just slightly farther apart than one second, and if the first one occurs at, say, 12:34:56.98s and the second occurs at 12:34:58.02s, you'll never draw 12:34:57 onscreen.  A similar effect can occur where you draw the same time twice in one second.  From the user point of view, these "stutters" will make your clock look unreliable.

 

If you want your time to change consistently, once per second, make sure your event comes faster than once per second.  You probably will want at least four times per second if you want a fairly smooth-feeling display.  You can make that a bit more efficient by not actually doing an update if the time has not changed.  I'll leave it to you to experiment, both to observe the above effects, and to see the performance of your modified version.


Peter Hansen -- (BB10 and dev-related blog posts at http://peterhansen.ca.)
Author of White Noise and Battery Guru for BB10 and for PlayBook | Get more from your battery!
Developer
Posts: 2,462
Registered: ‎11-04-2010
My Device: Bold 9700

Re: display time

four times per second seems a lot to me. id rather have the slight inconsistencies but to each his own hah. my approach would probably be to display the hours and minutes on screen rather than all three hours, minutes, and seconds. The user doesnt really need to see the seconds anyway (unless ppl really like seeing the time change hah). but as from a debugging standpoint i put in the seconds just to show it working. if we chop the second part off i dont think we'd "noticed" too many inconsistencies. by notice i mean visually (it'd still be happening as you had stated).

J. Rab (Blog) (Twitter)
--
1. If you liked my post or found it useful please click on the thumbs up and provide a Like!
2. If my post solved your problem please click on the Accept as Solution button. Much appreciated!

Approved Apps: OnTrack | ssShots | Hangman
Developer
Posts: 6,473
Registered: ‎12-08-2010
My Device: PlayBook, Z10
My Carrier: none

Re: display time

I agree with the sentiment that most of the time nobody needs to see seconds, and especially in a portable device it's likely a real waste of power most of the time.  Still, if you're doing some sort of countdown or timed event, it may be exactly what is most suitable.

 

As for how often to do it, you really have to try it to see the effects of too-infrequent updates.  If you think 1s is adequate, or even a little less, try sitting and watching your time count up, second by second, every second, for a minute or two.  I tend to do things like this sometimes... and I don't like how my UI feels to the user when I see the hiccups that result.

 

Also note that if you think it's okay to use a value just under 1.0s, like 0.7s, try it and you'll see that you're wrong.  In many ways that's actually worse than being right around 1.0s.  You need to be able to update within a couple hundred milliseconds of the correct time change to avoid any visible issues.


Peter Hansen -- (BB10 and dev-related blog posts at http://peterhansen.ca.)
Author of White Noise and Battery Guru for BB10 and for PlayBook | Get more from your battery!
Developer
Posts: 2,462
Registered: ‎11-04-2010
My Device: Bold 9700

Re: display time

i can understand where you are coming from. but the way im looking at the situation is a simple clock being displayed on screen. worst case scenario your display time is off by 1 minute for about second but it will correct itself after a second. Dont think it'd bother the user. However i do agree that if you are in a game environment and you rely on a timer and there is a delay i as a user wouldn't be as lenient waiting for an update versus just a simple clock.

J. Rab (Blog) (Twitter)
--
1. If you liked my post or found it useful please click on the thumbs up and provide a Like!
2. If my post solved your problem please click on the Accept as Solution button. Much appreciated!

Approved Apps: OnTrack | ssShots | Hangman
Developer
Posts: 660
Registered: ‎10-08-2009
My Device: Bold 9900
My Carrier: Telus

Re: display time

Worked flawlessly!

 

Thanks for so much help!!!

----------------------------------------------------------
Twitter: @gyubok