Re: NSDate and Date formatters
Re: NSDate and Date formatters
- Subject: Re: NSDate and Date formatters
- From: Daniel Todd Currie <email@hidden>
- Date: Wed, 14 Jan 2004 03:36:47 -0800
Adrian,
What time zone are you in (what time zone does your computer think
you're in)?
I am in US PST, which is GMT -8. [NSDate
dateWithTimeIntervalSinceReferenceDate:0] returns 2000-12-31 16:00 for
me, which is exactly what it should be, since the reference date is
based on GMT. Remember we are working with dates, not time intervals.
Perhaps I simply don't have all the details, but what do you need this
counter for anyway? The system clock is a perfectly good timer... I
would think an easier way to do this would be to init an NSDate where
you init the counter, and then you can get your counter value with:
[[NSDate date] timeIntervalSinceDate:initDate]
This would eschew all the confusion over reference dates and worries
about your user's time zone.
Just my 2 cents anyway...
// Dan
On 2004 Jan 14, at 02:59, Adrian Bool wrote:
Hi,
My app has a counter which I just want to display in
Hours:Minutes:Seconds using an NSDateFormatter.
The data for this counter is just an int (seconds) which is
incremented each second by an NSTimer. No problems there.
Here is where I initialize everything,
// init the counter
seconds = 0 ;
NSDateFormatter *dateFormat = [[NSDateFormatter alloc]
initWithDateFormat:@"%H:%M:%S" allowNaturalLanguage:NO];
[timeRemainingTextField setFormatter: dateFormat] ;
[timeRemainingTextField setObjectValue:
[NSCalendarDate dateWithTimeIntervalSinceReferenceDate: seconds]] ;
Now I would expect my app would start off showing '00:00:00' as I have
have zero seconds from the reference date. Unfortunately I get
'01:00:00' which is rather annoying!
In the docs for NSDateFornetter there are two ways of specifying the
hour,
%H Hour based on a 24-hour clock as a decimal number (00-23)
%I Hour based on a 12-hour clock as a decimal number (01-12)
Where %H definitely seems to be the one I want - although I have tried
both and see no change.
I can get things to look correct by subtracting 60*60 from 'seconds'
just before I create the new NSCalendarDate, but that feels dodgy!
The Reference Date is supposed to be the 'start' of 1st Jan 2001.
Where 'start' is as accurate description as I can find. For the
normal unix time (from time(3) man page) it is defined as,
The time() function returns the value of time in seconds since 0
hours, 0
minutes, 0 seconds, January 1, 1970, Coordinated Universal Time,
without
including leap seconds.
The big question is - when did 1st Jan 2001 start for Cocoa? At
midnight on 31st Dec 2000 or at 1am on 1st Jan 2001?
Any suggestions for this unfortunate confused person!?
Thanks
aid
_______________________________________________
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.
_______________________________________________
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.