Tôi phải thiếu một cái gì đó nhỏ của mình nhưng không thể tìm ra nó. Đang cố gắng tạo ngày để so sánh, nhưng tôi dường như không thể bù đắp currentDate từ GMT sang EST:NSDate - Offsetting Múi giờ
// 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
Cảm ơn!
Edit:
Tôi gọi một phương thức trong một lớp học riêng biệt (cố gắng để làm portable này) bằng cách sử dụng dòng sau:
[Expiration expires:[[NSDate alloc] initWithString:@"2011-01-07 12:00:00 +0000"] within:1.0]
trong phương pháp hết hạn, tôi có những dòng này:
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
tôi sau đó so sánh hai giá trị này như sau:
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 //
}
}
Điều này có hữu ích không? Cảm ơn sự kiên nhẫn của bạn!
Edit:
Dưới đây là toàn bộ hết hạn: trong phương pháp vì nó hiện đang đứng ...
+(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;
}
tôi thì gọi đó là từ một lớp khác với
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";
Khi chạy trong trình mô phỏng (cài đặt ứng dụng mới), NSLog hiển thị ...
[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
Bạn có thể cụ thể hơn về việc bạn thực sự muốn làm gì không? –
Tôi đang cố gắng so sánh ngày/giờ hiện tại với ngày/giờ được cung cấp cho các ứng dụng demo giới hạn thời gian. Phần mà đó là cho tôi rắc rối là làm so sánh trong múi giờ của tôi (EST) thay vì GMT. Dễ sử dụng múi giờ của riêng tôi hơn chuyển đổi thành GMT khi tôi gọi phương thức chứa. – Eric
Mã đó trông chính xác, và đầu ra cũng vậy. Tôi đoán vấn đề của bạn là bạn dự kiến sẽ thấy '10:43:48 -0500' trong nhật ký thay vì '15:43:48 + 0000'. Vấn đề là - OK. Họ là * cùng một lúc *. Đó chỉ là cách nó in ra khi bạn gọi NSLog. –