已知某些操作系统记录系统时间的方式是一个整数,这个整数是当前系统时间距离1970年1月1日零点的秒数,编写一个函数,给这个函数一个整数,返回当前的系统时间,比如100,返回1970:01:01:00
·
#include <stdio.h>
int isrun(int year)
{
if (year % 4 == 0 && year % 100 != 0 || year % 400 == 0)
return 0;
return 1;
}
int monthdays(int year, int month)
{
int monthdays[12] = { 31,28,31,30,31,30,31,31,30,31,30,31 };
if (month == 2 && isrun(year))
return 29;
else
return monthdays[month - 1];
}
void a(long long totalsec)
{
const long long daysec = 86400;
const int hoursec = 3600;
const int minsec = 60;
int totalday = totalsec / daysec;
int leftsec = totalsec % daysec;
int hour = leftsec / hoursec;
leftsec = leftsec % hoursec;
int min = leftsec / minsec;
int sec = leftsec % minsec;
int year = 1970;
while (1)
{
int yearday = isrun(year) ?
366 : 365;
if (totalday < yearday)
break;
totalday = totalday - yearday;
year++;
}
int month = 1;
while (1)
{
int monthday = monthdays(year, month);
if (totalday < monthday)
break;
totalday = totalday - monthday;
month++;
}
int day = totalday + 1;
printf("%04d:%02d:%02d:%02d:%02d:%02d:", year, month, day, hour, min, sec);
}
int main()
{
long long sec;
printf("距离1970年1月1日零点的秒数: ");
scanf_s("%lld", &sec);
a(sec);
return 0;
}
感觉有点复杂,在写几遍消化消化
openEuler 是由开放原子开源基金会孵化的全场景开源操作系统项目,面向数字基础设施四大核心场景(服务器、云计算、边缘计算、嵌入式),全面支持 ARM、x86、RISC-V、loongArch、PowerPC、SW-64 等多样性计算架构
更多推荐


所有评论(0)