北京时间与Unix时间戳相互转换

#include<bits/stdc++.h>
#define ull unsigned long long
#define ll long long
using namespace std;
int T;
string s1, s2;
int x;
char ch;

void solve1() {
    struct tm t;
    t.tm_year = x;
    scanf("%d-%d", &t.tm_mon, &t.tm_mday);
    scanf("%d:%d:%d", &t.tm_hour, &t.tm_min, &t.tm_sec);
    t.tm_year -= 1900; t.tm_mon -= 1;
    printf("%d\n", mktime(&t));
}

void solve2() {
    time_t xx = x;
    struct tm *t;
    t = localtime(&xx);
    printf("%d-%02d-%02d %02d:%02d:%02d\n", t->tm_year + 1900, t->tm_mon + 1, t->tm_mday, t->tm_hour, t->tm_min, t->tm_sec);
}

int main() {    
    cin >> T; while(T--) {
        cin >> x;
        ch = getchar();
        if(ch == '-') {
            solve1();
        }
        else solve2();
    }
    return 0;
}

posted @ 2023-06-06 10:28  starlightlmy  阅读(101)  评论(0编辑  收藏  举报