Re: Simple date format problem? iOS 5
Re: Simple date format problem? iOS 5
- Subject: Re: Simple date format problem? iOS 5
- From: Dave DeLong <email@hidden>
- Date: Thu, 09 Aug 2012 18:26:32 -0700
I'm going to venture out on a limb and say you shouldn't be using NSDateFormatter for this. NSDateFormatter is really only useful when you're converting a date to-and-from a human-readable form. The way you're using it, it's not human-readable, and will cause you problems in very subtle ways.
For example, if you have any users in Thailand, their files will likely all start with "2555", and not "2012", because the current year in the Buddhist calendar (which is the default calendar of the Thai locales) is 2555. Other users of yours around the world might have a year of 24, or 5772, et cetera. This is because NSDateFormatter is initialized with the current locale and current calendar. Since you're not resetting those properties yourself, they can cause you trouble.
Since you want an unlocalized date, you should just use [NSString stringWithFormat:] to build the name yourself, after breaking the NSDate up into its NSDateComponents using the Gregorian calendar.
Dave
On Aug 9, 2012, at 5:25 PM, Alex Kac <email@hidden> wrote:
> We archive our database files and use the following code to name them. We've been using the same code for about 3 years without any issues but today I have one user that is exhibiting very strange results. I'm assuming I'm missing something very basic, simple, but I'm just not seeing it.
>
> Here is the code:
>
> //rename our sqlite file
> //basically, we tack on a date & time to the current file name
> NSString *fileExtension = [[self coreDataStorePath] pathExtension];
> NSDateFormatter *fileArchiveDateFormat = [[NSDateFormatter alloc] init];
> [fileArchiveDateFormat setDateFormat: @"yyyyMMdd_HHmmss"];
> NSString *newPath = [[[[self coreDataStorePath] stringByDeletingPathExtension] stringByAppendingString: [fileArchiveDateFormat stringFromDate:[NSDate date]]] stringByAppendingPathExtension: fileExtension];
> [fileArchiveDateFormat release];
>
> It should result in a filename like this:
>
> "pim20120801_011922.informantdata",
>
> and for every other customer I've ever seen it does. For this one user it gives me:
>
> "pim20120801_011922 PM_.informantdata",
>
> I don't know where the extra _ is coming from, or where the AM/PM indicator is coming from. This is the only place in our code where we name these files as my first thought was I had some rogue code somewhere. I've also double checked the Apple docs on date formats and pretty sure what we have there is correct.
>
> The [self coreDataStorePath] returns "pim.informantdata", so that is the what string its starting from (with the /Documents path of course).
>
> Again, I'm pretty certain something simple will help me here - perhaps I need to
> [fileArchiveDateFormat setLocale: [[[NSLocale alloc] initWithLocaleIdentifier:@"en_US_POSIX"] autorelease]];
>
> ? I've also considered adding [fileArchiveDateFormat setFormatterBehavior:NSDateFormatterBehavior10_4];
>
> I could try it and not bother you fine folk - but this is iOS where I can't just give the user a build to test for me. I kind of have to "know" and of course it all works fine for me. I suppose the two things above are at the least safe to add..
> _______________________________________________
>
> 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
_______________________________________________
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