1

我们正在构建一个应用程序,该应用程序需要在设备上存储条目的日期,该应用程序将是国际化的,因此我们遇到了两个困境/挑战。

  1. 用户偏好

    如果用户选择 12 小时而不是 24 小时格式,我们会从 [NSDate date] 返回一个像这样的日期 2012-07-17 11:26:03 AM 在 SQLite 数据库中排序不是最佳的,因为我们无法存储它作为一个日期。

  2. 用户区域设置

    通常这是可以的,但是在这里我们有一个很棒的东西,叫做英国夏季。它在一个周期中每 10 月 25 日至 30 日增加一个小时,并在一个周期中每 3 月 25 日至 31 日删除一个小时,因此如果一年中的 8 个月没有进行调整,则时间落后一小时。

我需要实现的是一个一致的日期,格式如下:2012-07-17 11:26:03,无论设备位于何处,还要考虑 GMT+1 的位置。

任何帮助都是极好的。

编辑*

NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
dateFormatter.dateFormat = @"yyyy-MM-dd'T'HH:mm";
NSTimeZone *gmt = [NSTimeZone timeZoneWithAbbreviation:@"GMT+01:00"];
[dateFormatter setTimeZone:gmt];
NSString *timeStamp = [dateFormatter stringFromDate:[NSDate date]];
NSDate *localDate = [dateFormatter dateFromString:timeStamp];


NSLocale* currentLocale = [NSLocale currentLocale]; 
NSString* countryCode = [currentLocale objectForKey:NSLocaleCountryCode];
NSLog(@"Country Code: %@", countryCode);
NSLog(@"Current Loacle: %@", currentLocale);
if(([countryCode isEqualToString: @"GB"])){
    NSDate *mydate = [NSDate date];
    NSDate *fiddlyFoo = [mydate dateByAddingTimeInterval:3600];
    NSLog(@"Print GMT +1 %@",fiddlyFoo); 
} else {
    NSLog(@"Print GMT Local   %@",localDate); 
}
4

1 回答 1

4

我现在正在做这样的事情。请注意,NSDate“知道”当前时区和夏令时等。因此,您只需要以可排序的表示形式获取 GMT 版本的时间。我建议使用 RFC 3339,但您可以根据需要使用它的变体:

这段代码:

NSCalendar *gregorian = [[NSCalendar alloc]initWithCalendarIdentifier:NSGregorianCalendar];

// Create a local date for London, for testing purposes
NSDateComponents *comps = [NSDateComponents new];
[comps setTimeZone:[NSTimeZone timeZoneWithName:@"Europe/London"]];
[comps setDay:1];
[comps setMonth:7];
[comps setYear:2012];
[comps setHour:14];
NSDate *date = [gregorian dateFromComponents:comps];

// Just want to show this date is 2PM in London July 1st
NSDateFormatter *curFormat = [NSDateFormatter new];
[curFormat setDateStyle:NSDateFormatterFullStyle];
[curFormat setTimeStyle:NSDateFormatterFullStyle];
NSLog(@"Reference date is good no: %@", [curFormat stringFromDate:date]);

// So now we get the date as a rfc3339 string, referenced to GMT
NSString *timeString;
{
    NSDateFormatter *rfc3339 = [NSDateFormatter new];
    [rfc3339 setDateFormat:@"yyyy-MM-dd'T'HH:mm:ss'Z'"];
    rfc3339.timeZone = [NSTimeZone timeZoneWithName:@"UTC"];
    timeString = [rfc3339 stringFromDate:date];
}
// referenced to UTC (sortable with any other time), can save in SQL DB etc
NSLog(@"Date as rfc3339 string: %@", timeString);

// Now lets convert it back into a BST time
NSDate *newDate;
{
    NSDateFormatter *rfc3339 = [NSDateFormatter new];
    [rfc3339 setDateFormat:@"yyyy-MM-dd'T'HH:mm:ss'Z'"];
    rfc3339.timeZone = [NSTimeZone timeZoneWithName:@"UTC"];
    newDate = [rfc3339 dateFromString:timeString];

    // we want to show this as a string 2012-07-17 11:26:03
    NSDateFormatter *newFormatter = [NSDateFormatter new];
    [newFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"]; // local time
    [newFormatter setTimeZone:[NSTimeZone timeZoneWithName:@"Europe/London"]];

    NSLog(@"Local string using 24 hour clock: %@", [newFormatter stringFromDate:newDate]);
}

生成这个输出,我相信这是你想要的:

TimeTester[58558:f803] Reference date is good no: Sunday, July 1, 2012 9:00:00 AM Eastern Daylight Time
TimeTester[58558:f803] Date as rfc3339 string: 2012-07-01T13:00:00Z
TimeTester[58558:f803] Local string using 24 hour clock: 2012-07-01 14:00:00
于 2012-07-17T11:44:23.927 回答