C ++返回的时间是两个小时 [英] C++ Time returned is two hours out

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

问题描述

一位C ++新手,所以我们开始;

Bit of a c++ newbie so here we go;

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

I have a method that is parsing a date/time, however that date/time is passed to me always with 00:00:00 as the hh:mm:ss. As such i want to add in the values of the current systime in place of those values. I have methods that do this, and the first method is returning the correct time in UTC format.

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个小时.

In the second method I do some formatting on the date/time and this results in 2 hours being removed from the time.

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的时间,但是会尽快返回

Just to be clear, the ParseDateSysTime method returns the time with the correct UTC value of 11:53, but as soon as

struct tm * ptm = gmtime(&dealDate);

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

is called the time changes to 08:53. It suggests this is a product of calling the gmtime() method but i am not sure.

非常感谢

格雷厄姆

推荐答案

Reson是第一个函数中使用的 mktime()方法使用本地时间,但是 gmtime()使用 UTC时间.

The reson is the mktime() method used in the first function uses local time, but gmtime() uses UTC time.

请参见 http://www.cplusplus.com/reference/clibrary/ctime/mktime/ http://www.cplusplus.com/reference/clibrary/ctime/gmtime/进行进一步说明.

See http://www.cplusplus.com/reference/clibrary/ctime/mktime/ and http://www.cplusplus.com/reference/clibrary/ctime/gmtime/ for further explanation.

这篇关于C ++返回的时间是两个小时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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