【问题标题】:C++ Time returned is two hours outC++ 时间返回两个小时
【发布时间】:2012-10-13 08:27:47
【问题描述】:

有点 c++ 新手,所以我们开始吧;

我有一个解析日期/时间的方法,但是该日期/时间总是以 00:00:00 作为 hh:mm:ss 传递给我。因此,我想添加当前系统时间的值来代替这些值。我有这样做的方法,第一种方法是以 UTC 格式返回正确的时间。

bool CTRHTranslationRTNS::ParseDateSysTime(const char* pszString, time_t& tValue)
{
    ASSERT(pszString != NULL);

    // DateTime fields.
    enum { YEAR, MONTH, DAY, HOURS, MINS, SECS, NUM_FIELDS };

    CStringArray astrFields;

    // Split the string into the date and time fields.
    int nFields = CStringParser::Split(pszString, "- :T", astrFields);

    // Not DD/MM/YYYY HH:MM:SS format.
    if (nFields != NUM_FIELDS)
        return false;

    int anFields[NUM_FIELDS] = { 0 };

    // Parse field numbers.
    for (int i = 0; i < NUM_FIELDS; ++i)
        anFields[i] = atoi(astrFields[i]);

    tm oTime = { 0 };

        //Add System Time instead
        time_t sysyemTimeNow;
        struct tm * ptm;
        time ( &sysyemTimeNow );
        ptm = gmtime ( &sysyemTimeNow );

    // Copy fields to time struct.
    oTime.tm_mday  = anFields[DAY];
    oTime.tm_mon   = anFields[MONTH] - 1;
    oTime.tm_year  = anFields[YEAR] - 1900;
    oTime.tm_hour  = ptm->tm_hour;
    oTime.tm_min   = ptm->tm_min;
    oTime.tm_sec   = ptm->tm_sec;
    oTime.tm_isdst = -1;

    // Convert to time_t.
    tValue = mktime(&oTime);

    // Invalid field values.
    if (tValue < 0)
        return false;

    return true;
}

在第二种方法中,我对日期/时间进行了一些格式化,这会导致从时间中删除 2 小时。

string CTRHTranslationRTNS::ConvertDateSysTimeToDateInUTC(const string& bossDate)
{
    time_t dealDate;
    if (ParseDateSysTime(bossDate.c_str(), dealDate))
    {
        struct tm * ptm = gmtime(&dealDate);
        char buffer [80];
        strftime(buffer,80,"%Y-%m-%d %H:%M:%S",ptm);
        return string(buffer);
    }
    else
    {
        throw exception(string("Invalid date/SysTime value: ").append(bossDate).c_str());   
    }   
}

为了清楚起见,ParseDateSysTime 方法返回正确的 UTC 值为 11:53 的时间,但只要

struct tm * ptm = gmtime(&dealDate);

称为时间更改为 08:53。它表明这是调用 gmtime() 方法的产物,但我不确定。

非常感谢

格雷厄姆

【问题讨论】:

  • 到底是什么问题?
  • 为什么没有添加时间的时间有两个小时的差异。 UTC 时间应该是 11:53,但在第二个函数中调用 gmtime 时,它​​会在 08:53 出来

标签: c++ ctime


【解决方案1】:

reson是第一个函数中使用的mktime()方法使用当地时间,而gmtime()使用UTC时间

请参阅http://www.cplusplus.com/reference/clibrary/ctime/mktime/http://www.cplusplus.com/reference/clibrary/ctime/gmtime/ 了解更多说明。

【讨论】:

    【解决方案2】:

    试试这个功能:

    CTime Time2UTC(CTime original)
    {
        CString Formatted = original.FormatGmt(L"%Y%m%d%H%M%S");
        int Year, Month, Day, Hour, Minute;
        if (Formatted != L"" && Formatted.GetLength() >= 12)
        {
            Year = _wtol(Formatted.Left(4));
            Month = _wtol(Formatted.Mid(4, 2));
            Day = _wtol(Formatted.Mid(6,2));
            Hour = _wtol(Formatted.Mid(8, 2));
            Minute = _wtol(Formatted.Mid(10, 2));
            CTime result(Year, Month, Day, Hour, Minute, 0);
            return result;
        }
        else
            return (CTime)NULL;
    }
    

    【讨论】:

    • 谢谢!省去了我的麻烦。我得说,主要框架的日期和时间类,比如CTime,甚至都懒得构建函数来让你做一些事情,比如将UTC时间分解成组件,或者(在另一个 Microsoft 日期/时间类的情况,COleDateTime) 从 Posix 时间戳等构造一个日期/时间对象。真的很可悲。对于使用这些对象的程序员来说,想要做一些神秘的事情,比如正确地处理时区,或者在一个 API 和另一个 API 之间进行转换,这将是不寻常的。谁想要那个...
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-09-15
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多