c++學習總結:獲取13位系統時間戳

在iOS中使用NSDate來處理時間相關的操做,這在iOS客戶端開發中很是方便。若是中間層使用c++來寫的話,爲了保證中間層代碼的純淨,不能在c++中混編OC代碼,這時候就要使用c++的方法來產生13位時間戳,向外傳遞給OC調用,以下代碼展現瞭如何生成13位時間戳的方式,c++

#if defined(__APPLE__)
#define sprintf_s snprintf
#endif
time_t t = time(NULL);
char timeInterval[32] = {0};
sprintf_s(timeInterval, sizeof(timeInterval), "%ld000", t);
//timeInterval即爲13位的時間戳。

這時候timeInterval即爲c++中間層傳遞給OC的13位時間戳,將其轉換成OC時間的方法乳腺下所示,code

//將中間層傳過來的c++時間戳strtime轉換成iOS中的時間
NSDateFormatter *formatter =[[NSDateFormatter alloc] init];
[formatter setDateFormat:@"MM/dd HH:mm"];
//atol()將const char*字符串轉換爲長整形long
NSDate *currentDate = [NSDate dateWithTimeIntervalSince1970:atol(timeInterval)];
NSString *currentTime = [formatter stringFromDate:currentDate];
//currentTime就是將13位長整形轉換爲的OC時間
相關文章
相關標籤/搜索