时间转换 [英] Time Conversion

查看:83
本文介绍了时间转换的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有2个时间值:

系统时间和用户输入。

1)系统时间是从1970年1月1日起的秒数按

计算

time_t秒;

SYSTEMTIME stime;

time(& secs);

2)用户的输入格式为hr:min:sec,这是一个字符串

值。

但是已经获得了单独的值使用

sscanf(storedTimeValue,"%d:%d:%d",& hour,& minutes,& seconds);

所以现在我有3个小时,分钟和秒的整数值。

3)我有一个函数来计算2

时间值之间的时差,

但是这两个时间值都需要从1970年1月1日开始。

有没有办法,我可以将给定时间的用户转换成时间


从1970年1月1日开始,

这样我就可以得到系统时间与用户之间的差额

给定时间。

I have 2 time values:
System time and an input from the user.
1) System time is in the form of seconds from 1/1/1970 calculated by
using
time_t secs;
SYSTEMTIME stime;
time(&secs);
2) The input from the user is in form hr:min:sec which is a string
value.
But the seperate values have been obtained by using
sscanf(storedTimeValue, "%d:%d:%d", &hour,&minutes,&seconds);
So now I have 3 integer values for hour,minutes and seconds.
3) I have a function which calculates the time difference between 2
time values,
but both these time values need to be seconds from 1/1/1970.
Is there any way by which,I could convert the user given time into time

in seconds from 1/1/1970,
so that I can get the difference between the system time and the user
given time.

推荐答案

" moni" < mo ******* @ gmail.comwrote:
"moni" <mo*******@gmail.comwrote:

我有2个时间值:系统时间和用户输入。
I have 2 time values: System time and an input from the user.


1)系统时间是从1970年1月1日起的秒数形式
1) System time is in the form of seconds from 1/1/1970



ISO C并不能保证。它也没有给你任何方式做数学

on time_t。但一切都没有丢失......

ISO C doesn''t guarantee that. Nor does it give you any way to do maths
on time_t. But all is not lost...


time_t secs;

SYSTEMTIME stime;
time_t secs;
SYSTEMTIME stime;



这种类型在C中不存在。它也没有必要。

This type doesn''t exist in C. It''s also not necessary.


时间(安培;秒);
time(&secs);


2)用户的输入格式为hr:min:sec,这是一个字符串

值。但是通过使用
2) The input from the user is in form hr:min:sec which is a string
value. But the seperate values have been obtained by using


sscanf(storedTimeValue,"%d:%d:%d",& hour,&)获得了单独的值。 ;分钟,&安培;秒);
sscanf(storedTimeValue, "%d:%d:%d", &hour,&minutes,&seconds);


所以现在我有3个小时,分钟和秒的整数值。
So now I have 3 integer values for hour,minutes and seconds.



好​​。请注意,您仍然需要相关日期,但是从时间()得到的时间可以得到

Good. Note that you''ll still need the relevant date, but that can be had
from the time_t you got from time().


3)我有一个函数来计算2

时间值之间的时差,但这两个时间值都需要是1970年1月1日的秒数。
3) I have a function which calculates the time difference between 2
time values, but both these time values need to be seconds from 1/1/1970.



然后你的功能错了。用于计算两个time_t'之间的

差异的正确函数在< time.h>中,并且它被称为

difftime()。无论time_t的格式如何,这都必须有效,并且因此优于自制函数,该函数假定为1970年1月1日的
和分辨率1秒。

Then you have the wrong function. The right function to calculate the
difference between two time_t''s is in <time.h>, and it''s called
difftime(). This must work regardless of the format of a time_t, and is
therefore superior to a home-made function which assumes an epoch of
1970/01/01 and a resolution of 1 second.


有没有办法,我可以将给定时间的用户转换成时间

,以秒为单位,从1/1开始/ 1970,
Is there any way by which,I could convert the user given time into time
in seconds from 1/1/1970,



否。但是,有一种方法可以将它转换为time_t。它有点好处,但是它可以在任何地方使用。

首先你将time_t转换为struct tm,使用gmtime()或

localtime()。接下来,将此

struct tm的tm_hour,tm_min和tm_sec成员设置为从sscanf()获得的值。然后使用mktime()将此

struct tm转换回第二个time_t。最后你使用difftime()减去时间_t'和
,而鲍勃是你的叔叔。所有这些

函数都是ISO标准C,所有(显然除了sscanf()除外)可以在< time.h>中找到



Richard

No. However, there _is_ a way to convert it into a time_t. It''s a bit
roundabout, but it does work anywhere.
First you convert your time_t into a struct tm, using either gmtime() or
localtime(). Next you set the tm_hour, tm_min and tm_sec members of this
struct tm to the values you got from sscanf(). Then you convert this
struct tm back to a second time_t, using mktime(). Finally you subtract
both time_t''s using difftime(), and Bob''s your uncle. All of these
functions are ISO Standard C, and all (except sscanf(), obviously) can
be found in <time.h>.

Richard


Richard Bos说:


< snip>
Richard Bos said:

<snip>

ISO C并没有给你任何方式在time_t上做数学。
ISO C doesn''t [...] give you any way to do maths on time_t.



....除了difftime。


-

Richard Heathfield

Usenet是一个奇怪的地方 - dmr 29/7/1999
http://www.cpax.org.uk

电子邮件:rjh在上面的域名(但显然放弃了www)

....except for difftime.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at above domain (but drop the www, obviously)




struct tm * newtime,* oldtime;


time_t结果;

time_t long_time;

double elapsed_time;

sscanf(stringtoconvert,"%d:%d:%d",& hour,& minutes,& seconds);

time(& long_time); / *获取时间长整数。 * /


newtime = localtime(& long_time); / *转换为当地时间。 * /

oldtime = localtime(& long_time);


newtime-> tm_isdst = 0;

newtime-> tm_hour =小时;

newtime-> tm_min =分钟;

newtime-> tm_sec =秒;

/ / newtime-> tm_year = 2006;


result = mktime(& newtime);


elapsed_time = difftime(结果,long_time) ;

printf(时间是%d,%d,结果,long_time);


这里结果总是为-1,即。 mktime总是返回-1。


你能告诉我原因吗?


Thanx很多..


Richard Heathfield写道:

struct tm *newtime, *oldtime;

time_t result;
time_t long_time;
double elapsed_time;
sscanf(stringtoconvert, "%d:%d:%d", &hour,&minutes,&seconds);
time( &long_time ); /* Get time as long integer. */

newtime = localtime( &long_time ); /* Convert to local time. */
oldtime = localtime( &long_time );


newtime->tm_isdst = 0;
newtime->tm_hour = hour;
newtime->tm_min = minutes;
newtime->tm_sec = seconds;
//newtime->tm_year = 2006;

result = mktime(&newtime);

elapsed_time = difftime( result, long_time );
printf("time is %d, %d", result, long_time);

Here the result always come s to -1, ie. mktime is always returning -1.

Can you tell me the reason?

Thanx alot..

Richard Heathfield wrote:

Richard Bos说:


< snip>
Richard Bos said:

<snip>

ISO C并没有给你任何方式在time_t上做数学。
ISO C doesn''t [...] give you any way to do maths on time_t.



...除了difftime。


-

Richard Heathfield
Usenet是一个奇怪的地方 - dmr 29/7/1999
http://www.cpax.org.uk

电子邮件:rjh在上面的域名(但显然放弃了www)


...except for difftime.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at above domain (but drop the www, obviously)


这篇关于时间转换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆