NSDate - 抵消时区

时间:2011-01-05 18:39:16

标签: objective-c nsdate

我一定是想念她的小事,但无法弄明白。试图创建一个比较日期,但我似乎无法将currentDate从GMT抵消到EST:

// current date (gmt) //
NSDate *currentDate = [NSDate date];

NSTimeZone *currentDateTimeZone = [NSTimeZone timeZoneWithAbbreviation:@"EST"];

NSDateFormatter *currentDateFormat = [[NSDateFormatter alloc]init];
[currentDateFormat setTimeZone:currentDateTimeZone];
[currentDateFormat setDateFormat:@"yyyy-MM-dd HH:mm:ss zzz"];

NSString *currentDateString = [currentDateFormat stringFromDate:currentDate];
NSLog(@"currentDateString: %@", currentDateString); // returns 2011-01-05 13:30:30 EST

NSDate *currentDateWithOffset = [currentDateFormat dateFromString:currentDateString];

NSLog(@"currentDateWithOffset: %@", currentDateWithOffset); // returns 2011-01-05 18:30:30 +0000

谢谢!

编辑:

我正在使用以下行在一个单独的类中调用一个方法(尝试使其可移植):

[Expiration expires:[[NSDate alloc] initWithString:@"2011-01-07 12:00:00 +0000"] within:1.0]

在expires方法中,我有以下几行:

NSComparisonResult comparison = [currentDateWithOffset compare:expires]; // check for a fixed date to disable the demo

double withinRange = [installDate timeIntervalSinceDate:currentDateWithOffset]; // check for number of seconds between "within" and the install date

然后我将这两个值进行比较:

if(withinRange >= within && withinRange > 0.0) {
    // app is expired //
}
else {
    // app is still enabled (so far...) //
    if(comparison == NSOrderedDescending || comparison == NSOrderedSame) {
        // app is expired //
    }
    else {
        // app is still enabled //
    }
}

这有帮助吗?谢谢你的耐心等待!

编辑:

这是整个过期:在目前的方法中......

+(BOOL)expire:(NSDate*)expires within:(double)within {
   // default expired value //
    BOOL expired = NO;

    // convert within value from days to seconds //
    within *= 24.0 * 60.0 * 60.0;

    // current date (gmt) //
    NSDate *currentDate = [NSDate date];

    // install date //
    NSDate *installDate = [[NSUserDefaults standardUserDefaults]objectForKey:@"installDate"];

    // check for a value in installDate //
    if (nil == installDate) {
        // app is running for the first time //
        [[NSUserDefaults standardUserDefaults]setObject:currentDate forKey:@"installDate"];
        [[NSUserDefaults standardUserDefaults]synchronize];
        installDate = currentDate;
    }

    if([installDate timeIntervalSinceNow] < (-within)) {
        expired = YES;
    }
    else {
        if([expires timeIntervalSinceNow] < 0) {
            expired = YES;
        }
    }

    NSLog(@"installDate:%@", installDate);
    NSLog(@"expires:%@", expires);
    NSLog(@"currentDate:%@", currentDate);

    return expired; 
}

然后我用

从另一个类调用它
message.text = (YES == [Expiration expire:[[NSDate alloc] initWithString:@"2011-01-07 12:00:00 -0500"] within:(0.015625/2)]) ? @"This App is Expired" : @"This App is Active";

在模拟器中运行时(全新的应用安装),NSLog显示了这个......

[Session started at 2011-01-06 10:43:46 -0500.]
2011-01-06 10:43:48.146 TimeBasedDemo[14717:207] installDate:2011-01-06 15:43:48 +0000
2011-01-06 10:43:48.147 TimeBasedDemo[14717:207] expires:2011-01-07 17:00:00 +0000
2011-01-06 10:43:48.147 TimeBasedDemo[14717:207] currentDate:2011-01-06 15:43:48 +0000

4 个答案:

答案 0 :(得分:17)

这些答案都没有给我一个NSDate对象,其中包含当前的本地日期。所以我就是这样做的:

NSDate *currentDateWithOffset = [NSDate dateWithTimeIntervalSinceNow:[[NSTimeZone localTimeZone] secondsFromGMT]];

答案 1 :(得分:5)

NSDate对象代表一个时刻,而不考虑时区和日历考虑因素。打印或解析日期时,时区信息是相关的,但 NSDate

假设您正在创建这样的到期日期:

NSDate *exp=[[NSDate alloc] initWithString:@"2011-01-07 12:00:00 +0000"]

那就是说你想要在1月7日格林尼治标准时间中午到期。如果你想让它在美国东部时间中午到期,请用-0500创建它。当你进行比较时,你应该所要做的就是弄乱当前的时间。

然后

是一个简单的方法来查看时间是否过去了
if ([exp timeIntervalSinceNow]<0) { /* it's expired */ }

您可以看到自安装日期起是否已经过within秒,如下所示:

if ([installDate timeIntervalSinceNow]<(-within)]) { /* it's expired */}

答案 2 :(得分:0)

在Cocoa中,NSDate是未应用时区信息的日期的抽象表示。请注意,currentDateWithOffset 与日期字符串的日期相同,只是在不同的时区(提前5小时)。这是预期的行为,因为NSDate不会保留用于创建它的时区。

答案 3 :(得分:0)

我更多地修补了一下,找到了一种“欺骗”偏移以满足我需要的方法。从其他阅读来看,我猜测NSCalendar可能是一个更好的长期解决方案,但是现在我最终改变了

NSDate *currentDateWithOffset = [currentDateFormat dateFromString:currentDateString];

NSDate *currentDateWithOffset = [[NSDate alloc] initWithString:[NSString stringWithFormat:@"%@ +0000", currentDateString]];

这让我得到了我需要的结果,并且在后来我正在使用的比较中起作用。感谢所有背景信息!