看流星社区

 找回密码
 注册账号
查看: 2705|回复: 0

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

[复制链接]

该用户从未签到

发表于 2015-6-16 20:07:50 | 显示全部楼层 |阅读模式
在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时间
点击按钮快速添加回复内容: 支持 高兴 激动 给力 加油 苦寻 生气 回帖 路过 感恩
您需要登录后才可以回帖 登录 | 注册账号

本版积分规则

小黑屋|手机版|Archiver|看流星社区 |网站地图

GMT+8, 2024-4-19 09:14

Powered by Kanliuxing X3.4

© 2010-2019 kanliuxing.com

快速回复 返回顶部 返回列表