背景:最近 BI 的同學(xué)反映路翻,根據(jù)我們 iOS 客戶端收集的埋點(diǎn)數(shù)據(jù)顯示咪啡,有一部分?jǐn)?shù)據(jù)的埋點(diǎn)時(shí)間不對(duì)洁灵,但是我在工程代碼中斷點(diǎn)調(diào)試時(shí)卻沒(méi)有發(fā)現(xiàn)任何異常情況。
BI 同學(xué)提供的“證據(jù)”如下圖所示还棱,圖中圈出來(lái)的 tracktime (00280629104535)本應(yīng)顯示的時(shí)間理應(yīng)是 20160629104535载慈,機(jī)智的 BI 同學(xué)分析其原因可能是,我們記錄時(shí)間的代碼在某些機(jī)器上可能有問(wèn)題珍手,也就是說(shuō)機(jī)型適配問(wèn)題办铡,但根據(jù)我多年的開(kāi)發(fā)經(jīng)驗(yàn)來(lái)看(不要笑),問(wèn)題應(yīng)該不在于此琳要,其中必定另有蹊蹺寡具。
于是,我看了一下我們記錄埋點(diǎn)時(shí)間的相關(guān)代碼:
// tt
NSDateFormatter *dateformatter=[[NSDateFormatter alloc] init];
[dateformatter setDateFormat:@"yyyyMMddHHmmss"];
[mutDict setString:[dateformatter stringFromDate:[NSDate date]] forKey:MTCLICK_KEY_TT];
第一眼看上去貌似很正常稚补,打個(gè)斷點(diǎn)童叠,打印出來(lái)的時(shí)間也沒(méi)問(wèn)題。那么問(wèn)題在哪呢课幕?難道真是機(jī)型問(wèn)題拯钻?但從沒(méi)聽(tīng)說(shuō)過(guò)機(jī)型跟這時(shí)間格式有毛關(guān)系啊。
那就只能先從 NSDateFormatter 入手了撰豺,首先求助于官方文檔 Data Formatting Guide,在 Date Formatters 章節(jié)中的 Use Format Strings to Specify Custom Formats 發(fā)現(xiàn)其中有這樣一段話:
There are two things to note about this example:
- It uses yyyy to specify the year component. A common mistake is to use YYYY. yyyy specifies the calendar year whereas YYYY specifies the year (of “Week of Year”), used in the ISO year-week calendar. In most cases, yyyy and YYYY yield the same number, however they may be different. Typically you should use the calendar year.
2.The representation of the time may be 13:00. In iOS, however, if the user has switched 24-Hour Time to Off, the time may be 1:00 pm.
讀到此處拼余,我感慨萬(wàn)分污桦,到處都是坑啊匙监!頓時(shí)想起年初時(shí)的那個(gè)優(yōu)惠券有效期的 bug凡橱,就是因?yàn)闀r(shí)間格式的年份用了 “YYYY”,而不是 “yyyy”亭姥,而導(dǎo)致了一個(gè)平時(shí)看不出來(lái)到跨年的時(shí)候才出現(xiàn)的問(wèn)題稼钩。yyyy specifies the calendar year whereas YYYY specifies the year (of “Week of Year”), used in the ISO year-week calendar.
這句話很關(guān)鍵,由此可以看出之前這個(gè)問(wèn)題的根本就在于日歷(calendar)的區(qū)別达罗。
現(xiàn)在我們?cè)倩剡^(guò)頭來(lái)看看這次埋點(diǎn) bug 的“證據(jù)”: 20160629104535 -> (00280629104535)坝撑,問(wèn)題好像也是出在年份上,那么 0028 代表什么呢粮揉?于是我先后求助了度娘巡李、stackoverflow、bing扶认,最終在 bing 上找到了這樣一篇文章 How TechCrunch Japan broke our app - Handling local calendars in Swift侨拦,里面專門提到了一個(gè)關(guān)于日本日歷(Japan calendar)和公歷(Gregorian calendar)之間區(qū)別的例子,當(dāng)我看到這樣一段令人感動(dòng)的話時(shí):
It means that any NSDate instances retrieved from CoreData would always point to the right point in time, i.e. 04 Jan 0028 in Japan calendar now always points to the same point in time as 04 Jan 2016 in Gregorian calendar (having the same Unix timestamp 1451865600).
原來(lái)公歷2016年相當(dāng)于日本平成28年辐宾,我頓時(shí)感覺(jué)豁然開(kāi)朗狱从,仿佛終于找到心目中的 the one 了膨蛮。
找到問(wèn)題所在后,接下來(lái)就是試驗(yàn)驗(yàn)證了季研。
還是那段代碼:
NSDateFormatter *dateformatter=[[NSDateFormatter alloc] init];
[dateformatter setDateFormat:@"yyyyMMddHHmmss"];
NSLog(@"%@", [dateformatter stringFromDate:[NSDate date]]);
打開(kāi)模擬器的設(shè)置->通用->語(yǔ)言與地區(qū)->日歷敞葛,我們依次選擇“公歷”、“日本日歷”训貌、“佛教日歷”制肮,并運(yùn)行工程,console 打印出來(lái)的結(jié)果是如下递沪。
試驗(yàn)結(jié)果說(shuō)明之前的百分百推斷屬實(shí)豺鼻。那現(xiàn)在我們?cè)撛趺唇鉀Q這個(gè)問(wèn)題呢?打開(kāi) NSDateFormatter.h 文件和參考文檔款慨,我們可以看到一個(gè)屬性 @property (null_resettable, copy) NSCalendar *calendar;
儒飒,然后我們?cè)俅蜷_(kāi) NSCalendar 類的參考文檔,可以看到 Calendar Identifiers
常量的一些聲明:
NSString * const NSCalendarIdentifierGregorian
NSString * const NSCalendarIdentifierBuddhist
NSString * const NSCalendarIdentifierChinese
NSString * const NSCalendarIdentifierCoptic
NSString * const NSCalendarIdentifierEthiopicAmeteMihret
NSString * const NSCalendarIdentifierEthiopicAmeteAlem
NSString * const NSCalendarIdentifierHebrew
NSString * const NSCalendarIdentifierISO8601
NSString * const NSCalendarIdentifierIndian
NSString * const NSCalendarIdentifierIslamic
NSString * const NSCalendarIdentifierIslamicCivil
NSString * const NSCalendarIdentifierJapanese
NSString * const NSCalendarIdentifierPersian
NSString * const NSCalendarIdentifierRepublicOfChina
NSString * const NSCalendarIdentifierIslamicTabular
NSString * const NSCalendarIdentifierIslamicUmmAlQura
顯而易見(jiàn)的是檩奠,這里我們應(yīng)該選擇 NSCalendarIdentifierGregorian(公歷)桩了。
NSDateFormatter *dateformatter=[[NSDateFormatter alloc] init];
[dateformatter setDateFormat:@"yyyyMMddHHmmss"];
[dateformatter setCalendar:[NSCalendar calendarWithIdentifier:NSCalendarIdentifierGregorian]];
NSLog(@"%@", [dateformatter stringFromDate:[NSDate date]]);
重新修改代碼,再次驗(yàn)證后結(jié)果如下埠戳。
試驗(yàn)結(jié)果證明設(shè)置 NSDateFormatter 對(duì)象的 calendar 屬性為 identifier 為公歷NSCalendarIdentifierGregorian
的 NSCalendar 對(duì)象就可以解決這個(gè)問(wèn)題了。
NSDateFormatter 是我們經(jīng)常需要使用的一個(gè)類整胃,但在使用過(guò)程中需要注意很多有關(guān)地區(qū)颗圣,時(shí)間,性能方面的問(wèn)題屁使,要想知道如何一一避免那些坑在岂,多讀官方文檔是一個(gè)上佳的選擇。
One more thing蛮寂,人蠢就要多讀書啦蔽午!不然連日本日歷是什么都不知道是什么。Just kidding! ??
參考資料:
(1)How TechCrunch Japan broke our app - Handling local calendars in Swift:http://blog.famanson.com/2016/01/07/handling-local-calendars-is-a-pain/
(2)官方文檔: Data Formatting Guide
拓展延伸:
(1)日歷有哪幾種酬蹋?什么是日本日歷及老?什么是佛教日歷?
(2)如何正確使用 NSDateFormatter 范抓?