常用函数-Time

 

 #pragma pack(push,1)
/*
在这中间定义的结构体,已单字节对齐
*/
#pragma pack(pop)

 

/************************************************************************
函数功能: 将时间time_t转化为YYYY-MM-DD hh:mm:ss
输入参数: time_t  tTime             --  给定时间的标准time_t格式
输出参数: string& strTime           --  字符串
返回值  :
说明    :
************************************************************************/
void time2string(time_t tTime, string& strTime)
{
    struct tm tmTime;
    localtime_s(&tmTime, &tTime);

    char cTime[100] = {'\n'};
    _snprintf_s(cTime, sizeof(cTime)-1, 
        "%04d-%02d-%02d %02d:%02d:%02d", 
        tmTime.tm_year + 1900,
        tmTime.tm_mon + 1,
        tmTime.tm_mday,
        tmTime.tm_hour,
        tmTime.tm_min, tmTime.tm_sec);
    strTime = cTime;
}

 

/*
用于tcp传输时间结构体
*/
typedef struct tagTimePack
{
    unsigned short    nYear;
    unsigned char     nMonth;
    unsigned char     nDay;
    unsigned char     nSer;
    unsigned char     nHour;
    unsigned char     nMinute;
    unsigned char     nSecond;

    std::string toString()
    {
        char buff[32] = {'\0'};
        _snprintf_s(buff, sizeof(buff)-1, 
            "%04d-%02d-%02d %02d:%02d:%02d", 
            nYear, nMonth, nDay, nHour, nMinute, nSecond);
        std::string str(buff);
        return str;
    }
} TimePack, * LPTimePack;

 

/************************************************************************
函数功能: 将时间的time_t的表示,转换为自定义的TimePack格式
输入参数: time_t  tTime             --  给定时间的标准time_t格式
输出参数: const TimePack& timePack  --  给定时间的自定义TimePack格式
返回值  : 
说明    :
************************************************************************/
void DPC::time2pack(time_t tTime, TimePack& timePack)
{
    struct tm tmTime;
    localtime_s(&tmTime, &tTime);
    timePack.nYear      = tmTime.tm_year + 1900;
    timePack.nMonth     = tmTime.tm_mon + 1;
    timePack.nDay       = tmTime.tm_mday;
    timePack.nHour      = tmTime.tm_hour;
    timePack.nMinute    = tmTime.tm_min;
    timePack.nSecond    = tmTime.tm_sec;
};

 

/************************************************************************
函数功能: 将时间的自定义的TimePack表示,转换为标准的time_t格式
输入参数: const TimePack& timePack  --  给定时间的自定义TimePack格式

输出参数: time_t& tTime             --  时间的标准time_t格式
返回值  : 
说明    :
************************************************************************/
void DPC::pack2time(const TimePack& timePack, time_t& tTime)
{
    tm mytm;
    mytm.tm_year  = timePack.nYear    - 1900;
    mytm.tm_mon   = timePack.nMonth   - 1;
    mytm.tm_mday  = timePack.nDay;
    mytm.tm_hour  = timePack.nHour;
    mytm.tm_min   = timePack.nMinute;
    mytm.tm_sec   = timePack.nSecond;

    // 如果在循环再频繁被调用,可考虑使用 dcs_mktime
    tTime = mktime(&mytm);
};

 

//*************************************************************************
// 函数名称: OleTimeToTimet        
// 返 回 值: time_t     
// 参    数: const COleDateTime & oleTime  
// 函数说明: COleDatetime转time_t
//*************************************************************************
time_t OleTimeToTimet(const COleDateTime& oleTime)
{
    if (COleDateTime::valid != oleTime.GetStatus()){
      return 0;
    }

    SYSTEMTIME systime;
    oleTime.GetAsSystemTime(systime);
    struct tm atm;

    atm.tm_sec    = systime.wSecond;
    atm.tm_min    = systime.wMinute;
    atm.tm_hour = systime.wHour;
    atm.tm_mday = systime.wDay;
    atm.tm_mon    = systime.wMonth - 1;      
    atm.tm_year = systime.wYear - 1900;   
    atm.tm_isdst = -1;

    return mktime(&atm);
}

 

/************************************************************************
函数功能: 将时间的time_t的表示,转换为string格式
输入参数: time_t  tTime    --  给定时间的标准time_t格式
输出参数: string& strTime  --
返回值  :
说明    : 格式:YYYYMMDDHHMMSSmmm
************************************************************************/
void DPC::get_local_time(string& strTime)
{
    SYSTEMTIME st;
    GetLocalTime(&st);

    char cTime[100] = { '\0' };
    _snprintf_s(cTime, sizeof(cTime) - 1,
        "%04d%02d%02d%02d%02d%02d%03d",
        st.wYear,
        st.wMonth,
        st.wDay,
        st.wHour,
        st.wMinute,
        st.wSecond,
        st.wMilliseconds);

    strTime = cTime;
}

 

转载于:https://www.cnblogs.com/osbreak/p/10061516.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值