日期时间的一些模板

#include<stdio.h>
#include<algorithm>
#include<cstring>
#include<string>
#include<cmath>
#include<vector>
#include<queue>

using namespace std;

#define ll   long long
#define MAXN 100010
#define inf  1000000007

ll now[7],add[7],ans[7];
ll ye,mo,da,h,m,s;
bool jud(ll a)
{
    if(a%4==0&&a%100!=0||a%400==0)
        return 1;
    return 0;
}
ll is_mo[14]={0,31,29,31,30,31,30,31,31,30,31,30,31};
ll no_mo[14]={0,31,28,31,30,31,30,31,31,30,31,30,31};
ll calda(ll ye,ll mo, ll da)//算天 0年1月1天开始
{
    ll ans=0;
    for(int i=0;i<ye;i++)
        ans=ans+365+jud(i);
    if(jud(ye))
        for(int i=1;i<mo;i++)
            ans=ans+is_mo[i];
    else
        for(int i=1;i<mo;i++)
            ans=ans+no_mo[i];
    ans=ans+da;
    return ans;
}
void  da_chang(ll n) //n 天 转化年 0年1月1日 开始
{
    ll ye=0;
    while(n>=365+jud(ye))
    {
        n=n-(365+jud(ye));
        ye++;
    }
    ll mo=1;
    if(jud(ye))
        while(n>=is_mo[mo])
        {
            n=n-is_mo[mo];
            mo++;
        }
    else
        while(n>=no_mo[mo])
        {
            n=n-no_mo[mo];
            mo++;
        }
    ll da=1;
    da=da+n;      
    printf("%lld %lld %lld
",ye,mo,da);
}
ll calsec(ll ye,ll mo, ll da,ll h, ll m,ll s)//下午时间>12 算时间 0年1月1日 0时0分0秒 开始
{
    ll ans=0;
    for(int i=0;i<ye;i++)
        ans=ans+(365+jud(i))*24*3600;
    if(jud(ye))
        for(int i=1;i<mo;i++)
            ans=ans+is_mo[i]*3600*24;
    else
        for(int i=1;i<mo;i++)
            ans=ans+no_mo[i]*3600*24;
    ans=ans+(da-1)*24*3600;
    ans=ans+h*3600+m*60+s;
    return ans;
}

int main()
{
    int t;
    scanf("%d",&t);
    while(t--)
    {
        ll n;
        scanf("%lld",&n);
        da_chang(n);
    }
    return 0;
}
View Code
原文地址:https://www.cnblogs.com/cherryMJY/p/6706155.html