Ios 什么';处理时区的移动行业标准是什么?
我制作的移动应用程序使用来自我无法控制的.NET服务器的web服务 在安排不同时区的活动时,我们总是遇到麻烦 服务器在EST时间,来自web服务的大部分数据都是以这种方式显示的 我们最终不得不添加显示日期字段,这非常令人困惑 此外,我的代码本质上是相同的,只是这取决于我是在与服务器通话还是在设备上计算。也是一种痛苦 以下是我处理EST时间的一个示例: 数据输入: 数据输出: 问题:Ios 什么';处理时区的移动行业标准是什么?,ios,.net,timezone,standards,web-standards,Ios,.net,Timezone,Standards,Web Standards,我制作的移动应用程序使用来自我无法控制的.NET服务器的web服务 在安排不同时区的活动时,我们总是遇到麻烦 服务器在EST时间,来自web服务的大部分数据都是以这种方式显示的 我们最终不得不添加显示日期字段,这非常令人困惑 此外,我的代码本质上是相同的,只是这取决于我是在与服务器通话还是在设备上计算。也是一种痛苦 以下是我处理EST时间的一个示例: 数据输入: 数据输出: 问题: 我是否做错了什么?我可以向我的.NET合作伙伴推荐什么来处理时区?与其说是移动行业标准,不如说是集成和国际时间标准
我是否做错了什么?我可以向我的.NET合作伙伴推荐什么来处理时区?与其说是移动行业标准,不如说是集成和国际时间标准:时间戳应该使用始终使用偏移量或UTC(zulu)时间。GMT是一个时区,就像EST一样,所以UTC是首选的世界时。碰巧格林尼治标准时间不遵守夏令时,所以它通常是可互换的。您的合作伙伴应发送UTC标准中的时间戳:,看起来像是
2014-11-09T10:37:27+00:00
,或者zulu:2014-11-09T10:37:27Z
。有时人们会使用一些变体,例如2014-11-09 10:37:27UTC
,但您可以为其中任何一个构建日期格式化程序
日期格式将是dateFormatter.dateFormat=@“yyyy-MM-dd'HH:MM:ss.SSSz”
。我通常将UTC日期格式工厂放入NSDateFormatter类别中
如果一天是唯一重要的事情,而准确的时间确实不重要,那么您的方法应该足够了,除了您还需要格式化时间戳产生的任何内容,而不需要时间,或者使用日历进行计算
- (void)initWithWebServiceData:(NSDictionary *)data {
self.scheduleDate = [self dateFromDotNetJSONString:data[@"ScheduleDate"]];
//...and other stuff
}
- (NSDate *)dateFromDotNetJSONString:(NSString *)stringInEST {
int secondsESToffset = 14400;
static NSRegularExpression *dateRegEx = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
dateRegEx = [[NSRegularExpression alloc] initWithPattern:@"^\\/date\\((-?\\d++)(?:([+-])(\\d{2})(\\d{2}))?\\)\\/$" options:NSRegularExpressionCaseInsensitive error:nil];
});
NSTextCheckingResult *regexResult = [dateRegEx firstMatchInString:stringInEST options:0 range:NSMakeRange(0, [stringInEST length])];
if (regexResult) {
// milliseconds
NSTimeInterval seconds = [[stringInEST substringWithRange:[regexResult rangeAtIndex:1]] doubleValue] / 1000.0;
// timezone offset
if ([regexResult rangeAtIndex:2].location != NSNotFound) {
NSString *sign = [stringInEST substringWithRange:[regexResult rangeAtIndex:2]];
// hours
seconds += [[NSString stringWithFormat:@"%@%@", sign, [stringInEST substringWithRange:[regexResult rangeAtIndex:3]]] doubleValue] * 60.0 * 60.0;
// minutes
seconds += [[NSString stringWithFormat:@"%@%@", sign, [stringInEST substringWithRange:[regexResult rangeAtIndex:4]]] doubleValue] * 60.0;
}
return [NSDate dateWithTimeIntervalSince1970:seconds - secondsESToffset];
}
return nil;
}
- (void)requestRegistrationForEvent
{
NSString *fullDateTime = [NSString stringWithFormat:@"%@ %@", [self.scheduleDate webServiceStringValue], [self.scheduleDate dateTimeText]];
[self.dataController requestRegistrationForEvent:[self.eventID stringValue]
onDate:fullDateTime];
}
- (NSString *)webServiceStringValue {
NSString *webServiceStringValue = nil;
if ([self isKindOfClass:NSDate.class]) {
NSDate *date = (NSDate *)self;
//format the date how the web service expects it
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"M/d/yyyy"];
NSTimeZone *gmt = [NSTimeZone timeZoneWithAbbreviation:@"GMT"];
[dateFormatter setTimeZone:gmt];
webServiceStringValue = [dateFormatter stringFromDate:date];
}
return webServiceStringValue;
}
- (NSString *)dateTimeText {
NSString *text = @"";
NSDate *date;
if ([self isKindOfClass:NSDate.class]) {
date = (NSDate *)self;
NSDateFormatter *timeFormatter = [[NSDateFormatter alloc] init];
[timeFormatter setDateFormat:@"h:mm a"];
NSTimeZone *gmt = [NSTimeZone timeZoneWithAbbreviation:@"GMT"];
[timeFormatter setTimeZone:gmt];
text = [timeFormatter stringFromDate:date];
text = [text lowercaseString];
text = [text stringByReplacingOccurrencesOfString:@" " withString:@""];
}
return text;
}