MFC下北京时间与时间戳相互转换

//时间戳转北京时间字串
CString getLocalDate(__int64 timestamp)
{
    timestamp += 28800;//GTM偏移8个时区得到北京时间
    tm p;
    gmtime_s(&p, &timestamp);
    char s[80];
    strftime(s, 80, "%Y-%m-%d %H:%M:%S", &p);
    return CString(s);
}

//北京时间字串转时间戳
__int64 getTimestamp(CString strInputTime)
{
    COleVariant vtime(strInputTime);
    vtime.ChangeType(VT_DATE);
    COleDateTime cOleTime = vtime;
    SYSTEMTIME systime;
    VariantTimeToSystemTime(cOleTime, &systime);
    //时间戳最小值为北京时间:1970-01-01 08:00:00
    if (systime.wYear <= 1970 && systime.wMonth <= 1 && systime.wDay <= 1 && systime.wHour <= 7 && systime.wMinute <= 59 && systime.wSecond <= 59)
        return 0;
    CTime cTimeFromDB(systime);
    __int64 timestamp = cTimeFromDB.GetTime();//CTime->时间戳
    return timestamp;
}

 

posted @ 2021-07-01 11:08  任小七  阅读(888)  评论(0编辑  收藏  举报