بتوقيت جرينتش الوقت على اي فون
سؤال
كيف أحصل على وقت GMT؟
NSDate *c =[NSDate date];
يعطي وقت النظام، وليس بتوقيت جرينتش.
المحلول
- (NSDate*) convertToUTC:(NSDate*)sourceDate
{
NSTimeZone* currentTimeZone = [NSTimeZone localTimeZone];
NSTimeZone* utcTimeZone = [NSTimeZone timeZoneWithAbbreviation:@"UTC"];
NSInteger currentGMTOffset = [currentTimeZone secondsFromGMTForDate:sourceDate];
NSInteger gmtOffset = [utcTimeZone secondsFromGMTForDate:sourceDate];
NSTimeInterval gmtInterval = gmtOffset - currentGMTOffset;
NSDate* destinationDate = [[[NSDate alloc] initWithTimeInterval:gmtInterval sinceDate:sourceDate] autorelease];
return destinationDate;
}
نصائح أخرى
هذه نسخة أبسط من إجابة رامين
+ (NSDate *) GMTNow
{
NSDate *sourceDate = [NSDate date];
NSTimeZone* currentTimeZone = [NSTimeZone localTimeZone];
NSInteger currentGMTOffset = [currentTimeZone secondsFromGMT];
[sourceDate addTimeInterval:currentGMTOffset];
return sourceDate;
}
إذا كانت لأغراض العرض التي تريد عرضها، استخدم NSDateFormatter مثل ذلك:
NSDate *myDate = [NSDate date];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setTimeZone:[NSTimeZone timeZoneWithAbbreviation:@"GMT"]];
// Set date style:
[dateFormatter setDateStyle:NSDateFormatterShortStyle];
[dateFormatter setTimeStyle:NSDateFormatterShortStyle];
NSString *GMTDateString = [dateFormatter stringFromDate: myDate];
إذا تم إجراء حسابات التاريخ، فقد تكون هذه الفئات مفيدة. بعد أن قام بتحويل تاريخك إلى "تطبيع" (وهذا هو نفس الشهر واليوم والسنة ولكن في +1200 UTC)، إذا قمت بعد ذلك بإجراء حسابات لاحقة باستخدام NSCALENDAR والتي يتم تعيينها أيضا على UTC (+[NSCalendar normalizedCalendar]
)، سوف يعمل كل شيء.
@implementation NSDate (NormalizedAdditions)
+ (NSDate *)normalizedDateFromDateInCurrentCalendar:(NSDate *)inDate
{
NSDateComponents *todayComponents = [[NSCalendar currentCalendar] components:(NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit)
fromDate:inDate];
[todayComponents setTimeZone:[NSTimeZone timeZoneWithAbbreviation:@"UTC"]];
[todayComponents setHour:12];
return [[NSCalendar normalizedCalendar] dateFromComponents:todayComponents];
}
+ (NSDate *)normalizedDate
{
return [self normalizedDateFromDateInCurrentCalendar:[NSDate date]];
}
@end
@implementation NSCalendar (NormalizedAdditions)
+ (NSCalendar *)normalizedCalendar
{
static NSCalendar *gregorian = nil;
if (!gregorian) {
gregorian = [[NSCalendar alloc]
initWithCalendarIdentifier:NSGregorianCalendar];
[gregorian setTimeZone:[NSTimeZone timeZoneWithAbbreviation:@"UTC"]];
}
return gregorian;
}
@end
+ (NSDate*) convertToGMT:(NSDate*)sourceDate
{
NSTimeZone* currentTimeZone = [NSTimeZone localTimeZone];
NSTimeInterval gmtInterval = [currentTimeZone secondsFromGMTForDate:sourceDate];
NSDate* destinationDate = [[[NSDate alloc] initWithTimeInterval:gmtInterval sinceDate:sourceDate] autorelease];
return destinationDate;
}
NSDate يخزن المنطقة الزمنية داخليا - هناك عدد قليل من الوظائف التي يمكنك الاتصال بها وتمريرها في منطقة هما الهدف إذا كنت تريد jsut تمثيل سلسلة للتاريخ، وانظر وثائق أبل
لا تنتمي إلى StackOverflow