我有char date [] =“2011-04-01”;它如何转换为C或C++中的时间戳?C将char []转换为时间戳;
1
A
回答
3
试试这个:
char date[] = "2011-04-01";
date[4] = date[7] = '\0';
struct tm tmdate = {0};
tmdate.tm_year = atoi(&date[0]) - 1900;
tmdate.tm_mon = atoi(&date[5]) - 1;
tmdate.tm_mday = atoi(&date[8]);
time_t t = mktime(&tmdate);
+0
请注意,此解决方案假定“2011-04-01”是_local_日期,并且DST不会生效为“tmdate.tm_mday == 0”。因此'time_t t'可能从午夜起1个小时。 – chux 2017-01-26 18:09:38
4
警告:strptime是POSIX - 功能(可能无法在操作系统 “视窗” 平台可通过time.h中)。
#include <time.h>
struct tm time;
strptime("2011-04-01", "%Y-%m-%d", &time);
time_t loctime = mktime(&time); // timestamp in current timezone
time_t gmttime = timegm(&time); // timestamp in GMT
+1
未完全初始化/分配“时间”可能是一个问题:“未指定...将更新结构的当前内容或覆盖结构的所有内容”。建议'struct tm time = {0};'并使用'time-> isdst == -1;'。 – chux 2017-01-26 18:16:25
相关问题
- 1. 将时间戳转换为时间
- 2. 将时间戳转换为时区
- 3. 转换为UTC时间戳
- 4. 将时间戳长转换为时间戳错误
- 5. 将Java时间戳转换为MySQL时间戳反之亦然
- 6. 如何将时间戳转换为php中的unix时间戳?
- 7. 将json时间戳转换为正则java时间戳
- 8. Python:将Varbinary类型的时间戳转换为unix时间戳
- 9. 将UTC中的时间戳转换为时间戳
- 10. 如何将Evernote API时间戳转换为Postgresql时间戳
- 11. 将字符串时间戳转换为PHP中的时间戳
- 12. C#将Char转换为KeyValue
- 13. 将NTP时间戳转换为utc
- 14. PHP将日期转换为时间戳
- 15. 将MYSQL时间戳转换为time_t
- 16. 将unix时间戳转换为julian
- 17. 将EPOCH时间戳转换为日期
- 18. 将VarChar转换为时间戳
- 19. 将php时间戳转换为actionScript
- 20. 将时间戳转换为毫秒 - MATLAB
- 21. 将LocalDatetime转换为时间戳
- 22. 将日期转换为时间戳
- 23. 将datetime转换为unix时间戳
- 24. 将日期转换为jQuery时间戳
- 25. 如何将int64转换为时间戳
- 26. 将时间戳转换为NSDate
- 27. 将时间戳转换为纳秒
- 28. 将UTC转换为Unix时间戳
- 29. Bash将日期转换为时间戳
- 30. 如何将System :: Char转换为C++ char
也许这个回答能帮助:http://stackoverflow.com/questions/1002542/how-to-convert-datetime-to-unix-timestamp-in-c – 2011-04-22 10:01:21
你的标题说,C,你的问题说: C或C++,并且您只标记了C++。你用C编程还是用C++编程? – Puppy 2011-04-22 10:36:07