提问人:Hot Licks 提问时间:7/7/2011 最后编辑:halferHot Licks 更新时间:10/5/2022 访问量:48395
处理 NSDateFormatter 语言环境“功能”的最佳方法是什么?
What is the best way to deal with the NSDateFormatter locale "feature"?
问:
似乎有一个“功能”出乎意料地咬了你一口:如果你做一个简单的“固定”格式操作,例如:NSDateFormatter
NSDateFormatter* fmt = [[NSDateFormatter alloc] init];
[fmt setDateFormat:@"yyyyMMddHHmmss"];
NSString* dateStr = [fmt stringFromDate:someDate];
[fmt release];
然后它在美国和大多数地区都可以正常工作,直到......将手机设置为 24 小时区域的人将“设置”中的“12/24 小时切换”设置为 12。然后,上面开始将“AM”或“PM”附加到结果字符串的末尾。
(例如,参见 NSDateFormatter,我做错了什么还是这是一个错误?)
(见 https://developer.apple.com/library/content/qa/qa1480/_index.html)
显然,苹果已经宣布这是“坏的”——按设计损坏,他们不会修复它。
规避显然是为特定区域(通常是美国)设置日期格式化程序的区域设置,但这有点混乱:
NSLocale *loc = [[NSLocale alloc] initWithLocaleIdentifier:@"en_US"];
[df setLocale: loc];
[loc release];
连体衣还不错,但我正在处理大约十个不同的应用程序,我看到的第一个应用程序有 43 个这种情况的实例。
那么,对于宏/重写类/任何东西,有什么聪明的想法可以最大限度地减少更改所有内容的努力,而不会使代码变得模糊?(我的第一反应是用一个可以在 init 方法中设置语言环境的版本覆盖 NSDateFormatter。需要更改两行 -- alloc/init 行和添加的 import。
##Added
这就是我到目前为止想出的——似乎适用于所有场景:
@implementation BNSDateFormatter
-(id)init {
static NSLocale* en_US_POSIX = nil;
NSDateFormatter* me = [super init];
if (en_US_POSIX == nil) {
en_US_POSIX = [[NSLocale alloc] initWithLocaleIdentifier:@"en_US_POSIX"];
}
[me setLocale:en_US_POSIX];
return me;
}
@end
##Update 关于OMZ的提议,这是我发现的——
以下是类别版本 -- h 文件:
#import <Foundation/Foundation.h>
@interface NSDateFormatter (Locale)
- (id)initWithSafeLocale;
@end
M 类文件:
#import "NSDateFormatter+Locale.h"
@implementation NSDateFormatter (Locale)
- (id)initWithSafeLocale {
static NSLocale* en_US_POSIX = nil;
self = [super init];
if (en_US_POSIX == nil) {
en_US_POSIX = [[NSLocale alloc] initWithLocaleIdentifier:@"en_US_POSIX"];
}
NSLog(@"Category's locale: %@ %@", en_US_POSIX.description, [en_US_POSIX localeIdentifier]);
[self setLocale:en_US_POSIX];
return self;
}
@end
代码:
NSDateFormatter* fmt;
NSString* dateString;
NSDate* date1;
NSDate* date2;
NSDate* date3;
NSDate* date4;
fmt = [[NSDateFormatter alloc] initWithSafeLocale];
[fmt setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
dateString = [fmt stringFromDate:[NSDate date]];
NSLog(@"dateString = %@", dateString);
date1 = [fmt dateFromString:@"2001-05-05 12:34:56"];
NSLog(@"date1 = %@", date1.description);
date2 = [fmt dateFromString:@"2001-05-05 22:34:56"];
NSLog(@"date2 = %@", date2.description);
date3 = [fmt dateFromString:@"2001-05-05 12:34:56PM"];
NSLog(@"date3 = %@", date3.description);
date4 = [fmt dateFromString:@"2001-05-05 12:34:56 PM"];
NSLog(@"date4 = %@", date4.description);
[fmt release];
fmt = [[BNSDateFormatter alloc] init];
[fmt setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
dateString = [fmt stringFromDate:[NSDate date]];
NSLog(@"dateString = %@", dateString);
date1 = [fmt dateFromString:@"2001-05-05 12:34:56"];
NSLog(@"date1 = %@", date1.description);
date2 = [fmt dateFromString:@"2001-05-05 22:34:56"];
NSLog(@"date2 = %@", date2.description);
date3 = [fmt dateFromString:@"2001-05-05 12:34:56PM"];
NSLog(@"date3 = %@", date3.description);
date4 = [fmt dateFromString:@"2001-05-05 12:34:56 PM"];
NSLog(@"date4 = %@", date4.description);
[fmt release];
结果:
2011-07-11 17:44:43.243 DemoApp[160:307] Category's locale: <__NSCFLocale: 0x11a820> en_US_POSIX
2011-07-11 17:44:43.257 DemoApp[160:307] dateString = 2011-07-11 05:44:43 PM
2011-07-11 17:44:43.264 DemoApp[160:307] date1 = (null)
2011-07-11 17:44:43.272 DemoApp[160:307] date2 = (null)
2011-07-11 17:44:43.280 DemoApp[160:307] date3 = (null)
2011-07-11 17:44:43.298 DemoApp[160:307] date4 = 2001-05-05 05:34:56 PM +0000
2011-07-11 17:44:43.311 DemoApp[160:307] Extended class's locale: <__NSCFLocale: 0x11a820> en_US_POSIX
2011-07-11 17:44:43.336 DemoApp[160:307] dateString = 2011-07-11 17:44:43
2011-07-11 17:44:43.352 DemoApp[160:307] date1 = 2001-05-05 05:34:56 PM +0000
2011-07-11 17:44:43.369 DemoApp[160:307] date2 = 2001-05-06 03:34:56 AM +0000
2011-07-11 17:44:43.380 DemoApp[160:307] date3 = (null)
2011-07-11 17:44:43.392 DemoApp[160:307] date4 = (null)
手机 [使其成为 iPod Touch] 设置为英国,12/24 开关设置为 12。这两个结果有明显的区别,我判断类别版本是错误的。请注意,类别版本中的日志正在执行(并且代码中的停止被命中),因此这不仅仅是代码以某种方式未被使用的情况。
##A 好奇的观察
略微修改了类别实现:
#import "NSDateFormatter+Locale.h"
@implementation NSDateFormatter (Locale)
- (id)initWithSafeLocale {
static NSLocale* en_US_POSIX2 = nil;
self = [super init];
if (en_US_POSIX2 == nil) {
en_US_POSIX2 = [[NSLocale alloc] initWithLocaleIdentifier:@"en_US_POSIX"];
}
NSLog(@"Category's locale: %@ %@", en_US_POSIX2.description, [en_US_POSIX2 localeIdentifier]);
[self setLocale:en_US_POSIX2];
NSLog(@"Category's object: %@ and object's locale: %@ %@", self.description, self.locale.description, [self.locale localeIdentifier]);
return self;
}
@end
基本上只是更改了静态语言环境变量的名称(以防与子类中声明的静态冲突)并添加了额外的 NSLog。但是看看 NSLog 打印了什么:
2011-07-15 16:35:24.322 DemoApp[214:307] Category's locale: <__NSCFLocale: 0x160550> en_US_POSIX
2011-07-15 16:35:24.338 DemoApp[214:307] Category's object: <NSDateFormatter: 0x160d90> and object's locale: <__NSCFLocale: 0x12be70> en_GB
2011-07-15 16:35:24.345 DemoApp[214:307] dateString = 2011-07-15 04:35:24 PM
2011-07-15 16:35:24.370 DemoApp[214:307] date1 = (null)
2011-07-15 16:35:24.378 DemoApp[214:307] date2 = (null)
2011-07-15 16:35:24.390 DemoApp[214:307] date3 = (null)
2011-07-15 16:35:24.404 DemoApp[214:307] date4 = 2001-05-05 05:34:56 PM +0000
正如你所看到的,setLocale 根本没有。格式化程序的区域设置仍处于en_GB状态。类别中的 init 方法似乎有些“奇怪”。
答:
您可以创建一个带有附加初始值设定项的类别,该初始值设定项负责分配区域设置,也可能是一个格式字符串,而不是子类化,因此在初始化后立即拥有一个现成的格式化程序。NSDateFormatter
@interface NSDateFormatter (LocaleAdditions)
- (id)initWithPOSIXLocaleAndFormat:(NSString *)formatString;
@end
@implementation NSDateFormatter (LocaleAdditions)
- (id)initWithPOSIXLocaleAndFormat:(NSString *)formatString {
self = [super init];
if (self) {
NSLocale *locale = [[NSLocale alloc] initWithLocaleIdentifier:@"en_US_POSIX"];
[self setLocale:locale];
[locale release];
[self setFormat:formatString];
}
return self;
}
@end
然后,您可以在代码中的任何位置使用:NSDateFormatter
NSDateFormatter* fmt = [[NSDateFormatter alloc] initWithPOSIXLocaleAndFormat:@"yyyyMMddHHmmss"];
您可能希望以某种方式为您的类别方法添加前缀以避免名称冲突,以防万一 Apple 决定在未来版本的操作系统中添加此类方法。
如果您始终使用相同的日期格式,您还可以添加类别方法,这些方法返回具有特定配置的单例实例(类似于 )。但请注意,这不是线程安全的,当您使用来自多个线程的同一实例时,您必须使用锁或块。+sharedRFC3339DateFormatter
NSDateFormatter
@synchronized
评论
咄!!
有时你会有“啊哈!”的时刻,有时更像是“咄!这是后者。在“super”的类别中被编码为 .这初始化了 的 SUPERCLASS,但不初始化对象本身。initWithSafeLocale
init
self = [super init];
NSDateFormatter
init
NSDateFormatter
显然,当跳过此初始化时,“反弹”,可能是因为对象中缺少一些数据结构。更改 to 会导致初始化发生,并且再次感到高兴。setLocale
init
self = [self init];
NSDateFormatter
setLocale
以下是该类别 .m 的“最终”来源:
#import "NSDateFormatter+Locale.h"
@implementation NSDateFormatter (Locale)
- (id)initWithSafeLocale {
static NSLocale* en_US_POSIX = nil;
self = [self init];
if (en_US_POSIX == nil) {
en_US_POSIX = [[NSLocale alloc] initWithLocaleIdentifier:@"en_US_POSIX"];
}
[self setLocale:en_US_POSIX];
return self;
}
@end
评论
我可以提出一些完全不同的东西,因为老实说,所有这些都在某种程度上陷入了兔子洞。
您应该使用一个带有 set 和 forced to 的日期(来自服务器/API)。NSDateFormatter
dateFormat
locale
en_US_POSIX
然后,您应该为UI使用不同的/属性 - 这样您就不会自己进行显式设置,从而错误地假设将使用该格式。NSDateFormatter
timeStyle
dateStyle
dateFormat
这意味着 UI 由用户首选项驱动(上午 / 下午 与 24 小时,以及根据用户选择正确格式化的日期字符串 - 从 iOS 设置),而“进入”您的应用程序的日期始终被正确“解析”为供您使用。NSDate
评论
timeZone
这是 swift 版本中该问题的解决方案。在 swift 中,我们可以使用 extension 而不是 category。 因此,在这里,我创建了 DateFormatter 的扩展,并在其中创建了 initWithSafeLocale 返回带有相关 Locale 的 DateFormatter,在我们的例子中是en_US_POSIX,除此之外还提供了几种日期形成方法。
斯威夫特 4
extension DateFormatter { private static var dateFormatter = DateFormatter() class func initWithSafeLocale(withDateFormat dateFormat: String? = nil) -> DateFormatter { dateFormatter = DateFormatter() var en_US_POSIX: Locale? = nil; if (en_US_POSIX == nil) { en_US_POSIX = Locale.init(identifier: "en_US_POSIX") } dateFormatter.locale = en_US_POSIX if dateFormat != nil, let format = dateFormat { dateFormatter.dateFormat = format }else{ dateFormatter.dateFormat = "yyyy-MM-dd HH:mm:ss" } return dateFormatter } // ------------------------------------------------------------------------------------------ class func getDateFromString(string: String, fromFormat dateFormat: String? = nil) -> Date? { if dateFormat != nil, let format = dateFormat { dateFormatter = DateFormatter.initWithSafeLocale(withDateFormat: format) }else{ dateFormatter = DateFormatter.initWithSafeLocale() } guard let date = dateFormatter.date(from: string) else { return nil } return date } // ------------------------------------------------------------------------------------------ class func getStringFromDate(date: Date, fromDateFormat dateFormat: String? = nil)-> String { if dateFormat != nil, let format = dateFormat { dateFormatter = DateFormatter.initWithSafeLocale(withDateFormat: format) }else{ dateFormatter = DateFormatter.initWithSafeLocale() } let string = dateFormatter.string(from: date) return string } }
使用说明:
let date = DateFormatter.getDateFromString(string: "11-07-2001”, fromFormat: "dd-MM-yyyy") print("custom date : \(date)") let dateFormatter = DateFormatter.initWithSafeLocale(withDateFormat: "yyyy-MM-dd HH:mm:ss") let dt = DateFormatter.getDateFromString(string: "2001-05-05 12:34:56") print("base date = \(dt)") dateFormatter.dateFormat = "yyyy-MM-dd HH:mm:ss" let dateString = dateFormatter.string(from: Date()) print("dateString = " + dateString) let date1 = dateFormatter.date(from: "2001-05-05 12:34:56") print("date1 = \(String(describing: date1))") let date2 = dateFormatter.date(from: "2001-05-05 22:34:56") print("date2 = \(String(describing: date2))") let date3 = dateFormatter.date(from: "2001-05-05 12:34:56PM") print("date3 = \(String(describing: date3))") let date4 = dateFormatter.date(from: "2001-05-05 12:34:56 PM") print("date4 = \(String(describing: date4))")
评论
- (NSDateFormatterBehavior)formatterBehavior