Re: Getting the GMT time
Re: Getting the GMT time
- Subject: Re: Getting the GMT time
- From: p3consulting <email@hidden>
- Date: Sun, 18 Apr 2004 14:49:57 +0200
Le avr. 17, 2004, ` 18:49, Steve Palmer a icrit :
>
OK, so what if I want to go the other way? Say I have a date which is
>
in GMT and I want to obtain the date in PST? I thought this would work:
>
>
NSDate * anObject = // an NSDate object
>
NSCalendarDate * anDate = [anObject dateWithCalendarFormat:nil
>
timeZone:[NSTimeZone timeZoneWithAbbreviation:@"PST"]];
>
>
but since my machine is in PST this just returns me the date unchanged.
>
If I substitute PST with GMT, it adds 8 hours to the date which is
>
wrong. What I want is for it to subtract 8 hours. I've looked through
>
the date/time APIs but I don't see anything obvious. What am I missing?
>
>
Since you don't show us how the "anObject" NSDate is created, we can
just assume
from the result you got that is was created in the "PST" timezone
then the initialization of "anDate" converts it from PST to GMT
correctly adding N hours
// cc -framework Foundation -o timezone -Wall timezone.m
#import <Foundation/Foundation.h>
int main(int argc,char **argv)
{
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
NSCalendarDate *today = [NSCalendarDate date]; // date with "locale"
time zone
NSLog(@"today %@ timeZone %@",today,[[today timeZone] name]);
[today setTimeZone:[NSTimeZone timeZoneWithAbbreviation:@"PST"]];
NSLog(@"today %@ timeZone %@",today,[[today timeZone] name]);
// midnight at GMT
today = [NSCalendarDate dateWithYear:2004 month:4 day:17 hour:0
minute:0 second:0 timeZone:[NSTimeZone
timeZoneWithAbbreviation:@"GMT"]] ;
NSLog(@"today %@ timeZone %@",today,[[today timeZone] name]);
[today setTimeZone:[NSTimeZone timeZoneWithAbbreviation:@"PST"]];
NSLog(@"today %@ timeZone %@",today,[[today timeZone] name]);
[pool release] ;
return 0 ;
}
Outputs:
2004-04-18 14:40:10.362 timezone[16602] today 2004-04-18 14:40:10 +0200
timeZone Europe/Brussels
2004-04-18 14:40:10.366 timezone[16602] today 2004-04-18 05:40:10 -0700
timeZone America/Los_Angeles
2004-04-18 14:40:10.368 timezone[16602] today 2004-04-17 00:00:00 +0000
timeZone GMT
2004-04-18 14:40:10.370 timezone[16602] today 2004-04-16 17:00:00 -0700
timeZone America/Los_Angeles
which seems correct to me apart the fact that I got a 7 hours
difference between GMT and PST instead of the 8 you see on your machine
(probably summer time settings)
Pascal Pochet
email@hidden
_______________________________________________
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.