本地时间问题 [英] Problem with localtime
问题描述
有人可以向我解释当地时间的情况吗?我知道它使用静态tm结构,它返回一个指针。这里发生的是我传递两个不同的值,它给出了相同的结果。
我附上了我用来测试它的代码。如果用printf替换trace,则会有控制台输出。以下是我看到的输出:
Can anyone explain what is going on with localtime to me? I am aware that it uses a static tm structure that it returns a pointer to. What happens here is I pass two different values to it and it gives the same results with both.
I enclosed the code I use to test this. If you substitute trace with printf you will have console output. Here is the output I see from it:
16:11:37.571 PrimaryThread time 1 data :
16:11:37.575 PrimaryThread now is 1553123249
16:11:37.578 PrimaryThread year = 19
16:11:37.582 PrimaryThread month = 2
16:11:37.585 PrimaryThread day = 30
16:11:37.588 PrimaryThread hour = 16
16:11:37.591 PrimaryThread min = 11
16:11:37.595 PrimaryThread sec = 37
16:11:37.598 PrimaryThread time 2 data :
16:11:37.601 PrimaryThread now is 1553987249
16:11:37.604 PrimaryThread year = 19
16:11:37.607 PrimaryThread month = 2
16:11:37.610 PrimaryThread day = 30
16:11:37.613 PrimaryThread hour = 16
16:11:37.618 PrimaryThread min = 11
16:11:37.621 PrimaryThread sec = 37
16:11:37.624 PrimaryThread difference in times is 864000 seconds
正如你所能,时间值传递是不同的,它相差十天,但是来自localtime a的值同样的。这就是我所困惑的。如果有可用的话我会愿意使用不同的功能。
-edit-我找到了不同的功能。这是localtime_s,它需要一个tm结构作为参数。当我更改为该功能时,此测试正常。顺便说一句,这是vs2017和windows 10.
我尝试过:
As you can from this, the time value pass is different and it differs by the equivalent of ten days but the values from localtime are the same. That's what I am puzzled about. I would be willing to use a different function if there is one available.
-edit- I found a different function. That is localtime_s and it takes a tm structure as an argument. When I changed to that function this test works correctly. BTW, this is with vs2017 and windows 10.
What I have tried:
void TraceTime( time_t tv )
{
struct tm * ptm = localtime( &tv );
trace( _T( "now is %I64d\n" ), tv );
trace( _T( "year = %2d\n" ), ptm->tm_year - 100 );
trace( _T( "month = %2d\n" ), ptm->tm_mon );
trace( _T( "day = %2d\n" ), ptm->tm_mday );
trace( _T( "hour = %2d\n" ), ptm->tm_hour );
trace( _T( "min = %2d\n" ), ptm->tm_min );
trace( _T( "sec = %2d\n" ), ptm->tm_sec );
trace( _T( "value = %d\n" ), value );
}
void DoLocaltimeTest()
{
time_t now2 = 1553987249;
time_t now1 = 1553123249;
trace( _T( "time 1 data :\n" ) );
TraceTime( now1 );
trace( _T( "time 2 data :\n" ) );
TraceTime( now2 );
INT64 diff = now2 - now1;
trace( _T( "difference in times is %I64d seconds\n" ), diff );
}
推荐答案
localtime
不是线程安全的,所以你如果你从不同的线程调用它可能会遇到麻烦(你的跟踪工具是多线程的吗?)
在我的Linux机器上,代码
localtime
is not thread safe, so you might experience troubles if you are calling it from different threads (is your the trace tool multithreaded?)
On my Linux box, the code
#include <stdlib.h>
#include <stdio.h>
#include <time.h>
#define trace printf
#define _T(s) s
void TraceTime( time_t tv )
{
struct tm * ptm = localtime( &tv );
trace( _T( "now is %ld\n" ), tv );
trace( _T( "year = %2d\n" ), ptm->tm_year - 100 );
trace( _T( "month = %2d\n" ), ptm->tm_mon );
trace( _T( "day = %2d\n" ), ptm->tm_mday );
trace( _T( "hour = %2d\n" ), ptm->tm_hour );
trace( _T( "min = %2d\n" ), ptm->tm_min );
trace( _T( "sec = %2d\n" ), ptm->tm_sec );
//trace( _T( "value = %d\n" ), value );
}
void DoLocaltimeTest()
{
time_t now2 = 1553987249;
time_t now1 = 1553123249;
trace( _T( "time 1 data :\n" ) );
TraceTime( now1 );
trace( _T( "time 2 data :\n" ) );
TraceTime( now2 );
long long diff = now2 - now1;
trace( _T( "difference in times is %lld seconds\n" ), diff );
}
int main()
{
DoLocaltimeTest();
return 0;
}
行为正常
behaves correctly
time 1 data :
now is 1553123249
year = 19
month = 2
day = 21
hour = 0
min = 7
sec = 29
time 2 data :
now is 1553987249
year = 19
month = 2
day = 31
hour = 0
min = 7
sec = 29
difference in times is 864000 seconds
这篇关于本地时间问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!