Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
我正在尝试使用 ctime 格式化 10 位 Unix 时间戳(当前为字符串)。
但是,ctime() 需要一个 time_t 类型的参数,而不是字符串。
我必须做什么才能使用 ctime?换句话说,我可以轻松地将字符串转换为 time_t 吗?
你是说你有像 1346426869 这样的字符串并希望它是 time_t?
time_t raw_time = atoi("1346426869"); printf("current time is %s",ctime(&raw_time)); > current time is Fri Aug 31 11:27:49 2012
time_t 类型只是一个整数。它是自纪元以来的秒数。您需要先解析字符串。
从这里开始:
http://www.gnu.org/software/libc/manual/html_node/General-Time-String-Parsing.html#General-Time-String-Parsing
并从那里继续前进。