本文介绍了如何将时间计算为整数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,


我需要以秒为单位计算当前时间值:当前时间

是12/16/2003,它'自1970年1月1日以来,整数的时间值应该在1071560616左右.b $ b秒。在Tcl中很容易得到这样的值,但是我注意到它在C中很难做。好吧,ANSI提供了诸如

time(),asctime等功能。 time.h文件中的(),localtime(),gmtime(),ctime()。但是,

我无法在几秒钟内将回报转换为整数。


我确实想到了计算方式:


年* 365 +月* 12 +天* 24 + .....


来自struct tm成员的值。但我不能处理好闰年和闰月。有人可以帮忙吗?谢谢!

Huey

Hi All,

I need to compute the current time value in seconds: the current time
is 12/16/2003, and it''s time value in integer should around 1071560616
seconds since 1/1/1970. It is easy to get such a value in Tcl, but i
noticed it is uneasy to do in C. Well, ANSI provides functions such as
time(), asctime(), localtime(), gmtime(), ctime() in time.h file. But,
I can''t convert the returns into integer in seconds.

I did think of the way as computing by:

year*365 + month * 12 + day *24 + .....

with the values derived from struct tm members. but I can''t handle
the leap year and leap month well. Anybody can help? Thanks!
Huey

推荐答案




假设您输入的格式为mm / dd / yyyy:

支票,必须/必须扩展

最终处理小时,分钟和秒以及夏令时。

我建议你阅读描述一个结构小心:):/ b

time_t StringToTimestamp(const char * time_string)

{

struct tm time_in = {0} ;

char * tmp = NULL;


time_in.tm_year = strtoul(time_string + 6,& tmp,10) - 1900;

time_in.tm_mon = strtoul(time_string,& tmp,10) - 1;

time_in.tm_mday = strtoul(time_string + 3,& tmp,10);

/ *以秒为单位转换日期(自1970年1月1日起)* /

返回mktime(& time_in);

} / * StringToTimest amp * /


HTH

Robert



Assuming your input is in the format mm/dd/yyyy:
Checks, necessary headers and calling function omitted, may/must be expanded
eventually to handle hours, minutes and seconds as well as daylight saving.
I suggest you read the description of a struct tm carefully :)

time_t StringToTimestamp(const char *time_string)
{
struct tm time_in = {0};
char *tmp = NULL;

time_in.tm_year = strtoul(time_string + 6, &tmp, 10) - 1900;
time_in.tm_mon = strtoul(time_string, &tmp, 10) - 1;
time_in.tm_mday = strtoul(time_string + 3, &tmp, 10);
/* Convert the date in seconds (since 1/1/1970) */
return mktime(&time_in);
} /* StringToTimestamp */

HTH
Robert





这里有一种方式:


#include< stdio.h>

#include< time.h>

#include< stdlib.h>


int main (无效){

struct tm epoch_strt;

time_t basetime;

time_t curtime;

double nsecs;


epoch_strt.tm_sec = 0;

epoch_strt.tm_min = 0;

epoch_strt.tm_hour = 0;

epoch_strt.tm_mday = 1;

epoch_strt.tm_mon = 1;

epoch_strt.tm_year = 70;

epoch_strt.tm_isdst = - 1;


basetime = mktime(& epoch_strt);

if(basetime ==(time_t)-1){

fprintf(stderr,无法转换时间\ n);

退出(EXIT_FAILURE);

}


curtime = time(NULL);

if(curtime ==(time_t)-1){

fprintf(stderr,无法获取当前时间\ n) ;

退出(EXIT_FAI (LURE);

}


nsecs = difftime(curtime,basetime);


printf(" Seconds从纪元开始:%。0f \ n",nsecs);


返回0;

}


-nrk。



Here''s one way:

#include <stdio.h>
#include <time.h>
#include <stdlib.h>

int main(void) {
struct tm epoch_strt;
time_t basetime;
time_t curtime;
double nsecs;

epoch_strt.tm_sec = 0;
epoch_strt.tm_min = 0;
epoch_strt.tm_hour = 0;
epoch_strt.tm_mday = 1;
epoch_strt.tm_mon = 1;
epoch_strt.tm_year = 70;
epoch_strt.tm_isdst = -1;

basetime = mktime(&epoch_strt);
if ( basetime == (time_t)-1 ) {
fprintf(stderr, "Cannot convert time\n");
exit(EXIT_FAILURE);
}

curtime = time(NULL);
if ( curtime == (time_t)-1 ) {
fprintf(stderr, "Cannot get current time\n");
exit(EXIT_FAILURE);
}

nsecs = difftime(curtime, basetime);

printf("Seconds since start of epoch: %.0f\n", nsecs);

return 0;
}

-nrk.




假设您输入的格式为mm / dd / yyyy:
检查,必要的标题和调用函数省略,可能/必须最终扩展以处理小时,分钟和秒以及夏令时。我建议你仔细阅读一个struct tm的描述。

time_t StringToTimestamp(const char * time_string)
{struct / time tm time_in = {0};
char * tmp = NULL;

time_in.tm_year = strtoul(time_string + 6,& tmp,10) - 1900;
time_in.tm_mon = strtoul(time_string,& ; tmp,10) - 1;
time_in.tm_mday = strtoul(time_string + 3,& tmp,10);
/ *以秒为单位转换日期(自1970年1月1日起)* /
返回mktime(& time_in);
} / * StringToTimestamp * /

HTH
Robert



Assuming your input is in the format mm/dd/yyyy:
Checks, necessary headers and calling function omitted, may/must be
expanded eventually to handle hours, minutes and seconds as well as
daylight saving. I suggest you read the description of a struct tm
carefully :)

time_t StringToTimestamp(const char *time_string)
{
struct tm time_in = {0};
char *tmp = NULL;

time_in.tm_year = strtoul(time_string + 6, &tmp, 10) - 1900;
time_in.tm_mon = strtoul(time_string, &tmp, 10) - 1;
time_in.tm_mday = strtoul(time_string + 3, &tmp, 10);
/* Convert the date in seconds (since 1/1/1970) */
return mktime(&time_in);
} /* StringToTimestamp */

HTH
Robert




mktime的回报是否保证是自纪元以来的秒数?


-nrk。



Is the return of mktime guaranteed to be seconds since epoch?

-nrk.


这篇关于如何将时间计算为整数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-29 11:11