加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

根据Unix时间戳计算时间

发布时间:2020-12-15 16:16:27 所属栏目:安全 来源:网络整理
导读:根据Unix时间戳计算时间,不分大小月,每月30天,每年按360天计算。开始时间1970/01/01 00:00:00,输入秒数,显示时间 输入:10 返回:1970/01/01 00:00:10 输入:12345678 返回:1970/05/23 21:21:18 string CalculationDate(long long second ){ int year

根据Unix时间戳计算时间,不分大小月,每月30天,每年按360天计算。开始时间1970/01/01 00:00:00,输入秒数,显示时间

输入:10
返回:1970/01/01 00:00:10

输入:12345678
返回:1970/05/23 21:21:18

string CalculationDate(long long second)
{
    int year = 1970,month = 1,day = 1;
    int hour = 0,minute = 0;
    while(second > 60)
    {
        second -= 60;
        minute += 1;
        if (minute == 60)
        {
            minute = 0;
            hour += 1;
            if (hour == 24)
            {
                hour = 0;
                day += 1;
                if (day == 31)
                {
                    day = 1;
                    month += 1;
                    if (month == 13)
                    {
                        month = 1;
                        year += 1;
                    }
                }
            }
        }
    }
    char buff[] = "1970/01/01 00:00:10";
    sprintf(buff,"%d/%02d/%02d %02d:%02d:%02d",year,month,day,hour,minute,second);
    return buff;
}

void Test()
{
    cout<<CalculationDate(10)<<endl;
    cout<<CalculationDate(12345678)<<endl;
}

int main()
{
    Test();
    system("pause");
    return 0;
}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读