我有时间在几秒钟内long tv_sec ,我想使用函数stftime使用格式%Y-%m-%d %X获得正确的日期和时间。 所以我有几秒钟的时间戳,我想用stftime转换它。
这是我的试用版
struct tm dateStruct; memset(dateStruct, 0, sizeof(struct tm)); gmtime(×tampInSecs,dateStruct); char buffer[80]; strftime(buffer,80,"%Y-%m-%d %X", dateStruct); printf("Formatted date & time : |%s|\n", buffer );I have time in seconds long tv_sec, and I want to get the correct date, and time, using the format %Y-%m-%d %X, using the function stftime. So I have the time stamps in seconds and I want to convert it using stftime.
This is my trial
struct tm dateStruct; memset(dateStruct, 0, sizeof(struct tm)); gmtime(×tampInSecs,dateStruct); char buffer[80]; strftime(buffer,80,"%Y-%m-%d %X", dateStruct); printf("Formatted date & time : |%s|\n", buffer );最满意答案
确保timestampInSecs是time_t类型并尝试这样:
char buffer[80]; strftime(buffer,80,"%Y-%m-%d %X", gmtime(×tampInSecs)); printf("Formatted date & time : |%s|\n", buffer );编辑
示例:如果您有timestampInSecs表示从2000年1月1日起的秒数,您必须使用从1/1/1970 (Linux time.h中的纪元)的所有秒来补偿变量。从1970年1月1日到1月1日我们有:
10957天 262968小时 946684800秒最后,您必须将946684800总和为您的变量才能使用strftime ,如我的代码所示。
然后你必须做的是计算1/1/1970至你的开始日期和时间( timestampInSecs=0的日期和时间)之间的秒数
Ensure that timestampInSecs is time_t type and try this:
char buffer[80]; strftime(buffer,80,"%Y-%m-%d %X", gmtime(×tampInSecs)); printf("Formatted date & time : |%s|\n", buffer );EDIT
Example: if you have timestampInSecs that represent seconds from 1/1/2000 you must compensate the variable with all seconds from 1/1/1970 (epoch in Linux time.h) From 1/1/1970 to 1/1/2000 we have:
10957 days 262968 hours 946684800 secondsAt the end you have to sum 946684800 to your variable to be able to use strftime as shown in my code.
Then what you must do is to calculate seconds between 1/1/1970 and your start date and time (the date and time of timestampInSecs=0)
更多推荐
发布评论