Re: NSLog displays inconsistent format for NSDate
Re: NSLog displays inconsistent format for NSDate
- Subject: Re: NSLog displays inconsistent format for NSDate
- From: "Gary L. Wade via Cocoa-dev" <email@hidden>
- Date: Tue, 20 Aug 2019 20:00:11 -0700
If you want to output a date in a predictable format, you should use a date
formatter. When I ran a sample app under the simulator for tvOS 13 in Xcode 11
beta 5, I got these results for the specified locales:
2019-08-20 19:51:05.637667-0700 TestDateDescription[32894:2579867] ar_AE: The
date is now 2019-08-21 02:51:05 +0000
2019-08-20 19:51:42.350744-0700 TestDateDescription[32899:2580909] fr_FR: The
date is now 2019-08-21 02:51:42 +0000
2019-08-20 19:52:13.121429-0700 TestDateDescription[32905:2581496] en_US: The
date is now 2019-08-21 02:52:13 +0000
This was the line of Swift I used:
NSLog("%@: The date is now %@", Locale.current.identifier, NSDate())
For a predictable format in logs, I’d suggest using NSISO8601DateFormatter.
--
Gary L. Wade
http://www.garywade.com/ <http://www.garywade.com/>
> On Aug 20, 2019, at 12:50 PM, Carl Hoefs via Cocoa-dev
> <email@hidden> wrote:
>
> When printing out an NSDate using NSLog from within Xcode I get:
>
> "Tue Aug 20 12:32:40 2019"
>
> When the same program is run from within a shell (bash) window:
>
> "2019-08-20 19:32:48 +0000"
>
> Is the NSDate output format somehow determined by the environment? My system
> is set to Local Time Zone (America/Los_Angeles (PDT) offset -25200
> (Daylight)).
>
> A code snippet that reproduces the issue follows.
>
> -Carl
>
>
>
> - (void) testDate
> {
> NSCalendar *calendar = [NSCalendar currentCalendar];
> unsigned unitFlags =
> NSCalendarUnitYear|NSCalendarUnitMonth|NSCalendarUnitDay|NSCalendarUnitHour|NSCalendarUnitMinute|NSCalendarUnitSecond|NSCalendarUnitTimeZone;
> NSDateComponents *dateComponents = [calendar components:unitFlags
> fromDate:[NSDate date]];
> dateComponents.timeZone = NSTimeZone.localTimeZone;
> NSDate *configuredDate = [calendar dateFromComponents:dateComponents];
> NSLog(@"Configured date: %@",configuredDate);
> }
>
> Xcode:
> 2019-08-20 12:32:40.828863-0700 tester[3926:1353] Configured date: Tue Aug 20
> 12:32:40 2019
>
> Shell:
> 2019-08-20 12:33:08.356 tester[3928:1359] Configured date: 2019-08-20
> 19:32:48 +0000
>
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden