-2

I have app where i need to get the current time (CST). I'm trying to use the following code but the NDate is the same as GMT:

NSDate* date = [NSDate date];
    NSLog(@"Time is %@", [date description]);

    [NSTimeZone setDefaultTimeZone:[NSTimeZone timeZoneWithAbbreviation:@"CST"]];
    NSDate* nDate = [NSDate date];
    NSLog(@"Central time is%@", [nDate description])
Dameon Bryant
  • 39
  • 1
  • 6
  • An NSDate is *supposed* to be GMT. If it isn't you screwed up somewhere. – Hot Licks May 24 '15 at 01:13
  • NSLog will print to your console, therefore it will use your system (Mac) timezone. – Otávio May 24 '15 at 01:13
  • 2
    @HotLicks, `NSDate` is not "supposed to be GMT". It's not supposed to be in any time zone. Time zones as a concept are completely orthogonal to what `NSDate` represents. Even `-[NSDate description]` is not "supposed to be GMT". It's unspecified, which is part of what makes that method unsuitable for anything other than debugging. – Ken Thomases May 24 '15 at 04:17
  • possible duplicate of [Unexpected value from NSDate](http://stackoverflow.com/questions/17294289/unexpected-value-from-nsdate) – Amin Negm-Awad May 24 '15 at 04:50
  • @KenThomases - In theory. In practice the value inside an NSDate is GMT/UTC, and the `description` representation is always with zero timezone offset. There is, I suspect, a timezone offset field in the object, but it's been hard-wired to zero for about 5 years now. – Hot Licks May 24 '15 at 09:21
  • First, it doesn't matter what the value inside an `NSDate` is. But you're wrong about that, anyway. The value is simply a number of seconds since a particular reference date. Even if that reference date is especially round when represented in GMT, that doesn't make the time indicated by adding an interval to it have a time zone. Times don't have a time zone except when represented as strings or components relative to a calendar. It just doesn't make sense. – Ken Thomases May 24 '15 at 10:47

3 Answers3

0

but the NSDate is the same as GMT

That's right, and if you look at the printout, it says that it is showing you GMT. Therefore, it is giving you the right answer — the date-time, right now, in terms of GMT. The current time CST is the GMT date-time it is showing you.

That is what description does. If that isn't what you wanted, why did you use description?

matt
  • 515,959
  • 87
  • 875
  • 1,141
0

An NSDate doesn't have a time zone. It's a moment in time. A moment exists across all time zones. The string it gives from -description has to pick a time zone in order to represent the moment, because that's the way we write dates and times, but which it picks is arbitrary. The time zone used for the description in no way represents any fundamental fact about the nature of the NSDate object.

If you want to format a date into a string, don't use -description because it's not well-defined. (For example, which time zone it uses has changed before with the version of OS X and could change again.) Use an NSDateFormatter. You can configure an NSDateFormatter with a time zone by setting its timeZone property.

If you want to get information about the date, like month, day, hour, minute, etc., use NSCalendar and NSDateComponents.

Ken Thomases
  • 88,520
  • 7
  • 116
  • 154
0

Beside the fact that the time has not timezone itself, but you have to add a calendar to set it in relation to a time zone as I described here, you should not use a tz abbreviation as described here.

Community
  • 1
  • 1
Amin Negm-Awad
  • 16,582
  • 3
  • 35
  • 50