c++学习总结:获取13位系统时间戳

大憨熊 提交于 2019-12-27 21:19:12

【推荐】2019 Java 开发者跳槽指南.pdf(吐血整理) >>>

在iOS中使用NSDate来处理时间相关的操作,这在iOS客户端开发中非常方便。如果中间层使用c++来写的话,为了保证中间层代码的纯净,不能在c++中混编OC代码,这时候就要使用c++的方法来产生13位时间戳,向外传递给OC调用,如下代码展示了如何生成13位时间戳的方式,

#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时间的方法乳腺下所示,

//将中间层传过来的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时间


易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!