将历史时间转换为GMT [英] Convert historical time to GMT

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

问题描述

我需要将格式为2011061411322100的一些字符串时间转换为GMT - 我的第一次尝试是在下面。然而,问题是,时代来自另一台PC,是一个历史时间。所以我没有得到实时的时间,所以我不能简单地从我的代码运行的盒子上的本地时间获取GMT。

I need to convert some string times in the format "2011061411322100" into GMT - my first attempt is below. However, the problem is that the times are coming from another PC and is a historical time. So I am not getting the times in real time so I cannot simply get the GMT from the local time on the box that my code is running.

问题是,如果我的代码在时间更改期间运行,时间更改将发生在我的盒子上,但不是在我的时间。不过,我可以随时查询方框以获取当前时间。

The problem is that if my code is running during a time change, the time change will have occurred on my box but not on the remote box where I am getting the times. I can however, query the box to get the current time at any time.

因此,要提供更多详细信息:

So, to give more detail:


  1. 我在远程机器上启动作业

  2. 作业完成

  3. 正在运行

  4. 我将时间转换为GMT

  1. I start the job on a remote box
  2. The job completes
  3. I get some times related to the job running
  4. I convert the time to GMT

发生在1.和2.我被拧紧。我的GMT转换会中断。我猜,2)我需要得到当前的遥控盒时间,看看是否有差异> 58分钟,然后应用到转换。但我不能找出一个可靠的方法这样做。

If a time change (daylight savings) occurs between 1. and 2. I am screwed. My GMT conversion will break. I guess after 2) I need to get the current Remote Box time and see if there is a difference of >58 mins and then apply that to the conversion. But I cannot figure out a reliable method of doing this.

string GMTConverter::strToGMT(const string& timeToConvert)
{
    // Set time zone from TZ environment variable. 
    _tzset();

    struct tm tmTime;


    //2011 06 14 11 32 21 00
     // (strToInt is just a wrapper for atoi) 
    int year = strToint(timeToConvert.substr(0, 4) );
    int month = strToint(timeToConvert.substr(4, 2) );
    int day = strToint(timeToConvert.substr(6, 2) );
    int hour = strToint(timeToConvert.substr(8, 2) );
    int min = strToint(timeToConvert.substr(10, 2) );
    int sec = strToint(timeToConvert.substr(12, 2) );

    cout<<"Time after parsing: "<<year<<"/"<<month<<"/"<<day<<" "<<hour<<":"<<min<<":"<<sec<<endl;

    // add to tm struct and return
    tmTime.tm_hour = hour; 
    tmTime.tm_min = min; 
    tmTime.tm_sec = sec; 
    tmTime.tm_mday = day; 
    tmTime.tm_mon = (month-1); 
    tmTime.tm_year = (year - 1900); 

    cout <<"Time in TM: "<<tmTime.tm_year<<"/"<<tmTime.tm_mon<<"/"<<tmTime.tm_mday<<" "<<tmTime.tm_hour<<":"<<tmTime.tm_min<<":"<<tmTime.tm_sec<<endl;

    char currDateTime[64];

     // For logging
    strftime(currDateTime, 63, "%c", &tmTime);
    cout <<"Actual time:"<<currDateTime<<endl;

    time_t remotePCTime = mktime( &tmTime );

    struct tm *gmt = gmtime( &remotePCTime );
    cout << "gmt = " << asctime( gmt ) << endl;

    char datebuf_2[12];
    char timebuf_2[13];
    strftime( datebuf_2, 13, "%Y-%m-%d\0", gmt );
    strftime( timebuf_2, 13, "%H:%M:%S\0", gmt );

    return string(datebuf_2) + "T" + string(timebuf_2) + "." + string("000");
}


推荐答案

为您发送的时间戳使用UTC(没有夏令时)。使用任何具有固有模糊性的时间系统(每年有一个小时的重叠,你可以在不同的时间获得相同的时间戳)将使得不可能有一个愚蠢的方法,因为信息丢失。

The obvious reliable solution would be to use UTC (which has no daylight savings) for the time stamp you're sending over. Using any time system that has inherent ambiguity (there is one hour of overlap each year where you can get the same time stamps on a different time) will make it impossible to have a fool-proof method, since information is lost.

如果您无法控制远程计算机发送的时间格式,您只能尝试从您所拥有的信息中进行推断,例如,如果结束时间是低于开始时间,加1小时。如果工作花费超过一小时,但至少时间不会向后移动,这再次引起歧义。

If you have no control over the time format that the remote machine is sending, you can only try to extrapolate from the information that you do have, for instance, if the end time is lower than the start time, add one hour. This again introduces ambiguity if the job took longer than one hour, but at least time won't move backwards.

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

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