• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
syncing NSTimer with system clock...
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

syncing NSTimer with system clock...


  • Subject: syncing NSTimer with system clock...
  • From: Daniel Hedrick <email@hidden>
  • Date: Tue, 21 Jan 2003 22:14:29 -0600

I'm using the following code to send the message "changeTime:" in sync with the system clock at a resolution of 1.0 second:

//begin code block...
NSCalendarDate* now = [NSCalendarDate calendarDate];
//reinit 'now' with granularity of seconds...
now = [NSCalendarDate dateWithYear: [now yearOfCommonEra]
month: [now monthOfYear]
day: [now dayOfMonth]
hour: [now hourOfDay]
minute: [now minuteOfHour]
second: [now secondOfMinute]
timeZone: [now timeZone]];

NSTimer* timer = [[NSTimer alloc] initWithFireDate: now
interval: 1.0
target: self
selector: @selector(changeTime:)
userInfo: nil
repeats: YES];

[[NSRunLoop currentRunLoop] addTimer: timer forMode: NSDefaultRunLoopMode];
[timer release];
//end code block

This looks like it works most of the time, and in fact the subsecond precision is around 0.001, which is well within the parameters I need.

The problem is that sometimes the selector appears to fire exactly one second off from system time (as observed if I turn on "view seconds" in the menubar clock). Sometimes changeTime: reads 10:07:24 when the menubar clock reads 10:07:23. This happens on a per-run basis.

Any ideas on why this is? Any better suggestions for configuring a timer to fire the selector in sync with the system clock?

Erp! Just as I was about to send this, I tried to run another test, and while my NSLog inside "changeTime:" reported the following:

2003-01-21 22:12:29.001 PhotoClock[3848] fireDate = <CFDate 0x9ebb0 [0xa01303fc]>{time = 64901549}
2003-01-21 22:12:30.001 PhotoClock[3848] fireDate = <CFDate 0x9ebb0 [0xa01303fc]>{time = 64901550}
2003-01-21 22:12:31.001 PhotoClock[3848] fireDate = <CFDate 0x9ebb0 [0xa01303fc]>{time = 64901551}

The menubar clock, and in fact the Clock.app, appeared to be about a half-second behind my timer. However, they were both in lockstep harmony. Any ideas?

-daniel
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives: http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.

  • Follow-Ups:
    • Re: syncing NSTimer with system clock...
      • From: Andreas Mayer <email@hidden>
  • Prev by Date: Re: @protocol vs NSClassFromString
  • Next by Date: Re: Volume info in Cocoa
  • Previous by thread: Re: Toolbar woes...
  • Next by thread: Re: syncing NSTimer with system clock...
  • Index(es):
    • Date
    • Thread