【发布时间】:2011-01-05 18:39:15
【问题描述】:
我一定是错过了她的一些小事,但我想不通。试图创建一个日期进行比较,但我似乎无法将 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 //
}
}
这有帮助吗?感谢您的耐心等待!
编辑:
这里是整个 expires:within 方法,因为它目前的状态......
+(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
【问题讨论】:
-
您能否更具体地说明您真正想要做什么?
-
我正在尝试将当前日期/时间与提供的限时演示应用程序的日期/时间进行比较。给我带来麻烦的部分是在我的时区(EST)而不是 GMT 进行比较。调用包含方法时,使用我自己的时区比转换为 GMT 更容易。
-
该代码看起来完全正确,输出也是如此。我猜您的问题是您希望在日志中看到
10:43:48 -0500而不是15:43:48 +0000。问题是——没关系。它们同时。当你调用 NSLog 时,它就是这样打印出来的。 -
好吧,我现在感觉自己像个真正的白痴...... NSLog 在应用偏移量 -0500 后,在 GMT 时打印过期。我期待看到它是 07:00:00。我想我现在明白了。感谢您所有的帮助。现在我只需要弄清楚如何让它在使用应用程序时调用此方法。再次感谢!
-
P.S.我想出了如何让最后一部分如何工作,这样可以节省一个新线程!
标签: objective-c nsdate