#include<stdio.h>
struct datatime
{
int w_year;
int w_month;
int w_date;
int hour;
int min;
int sec;
};
struct datatime calendar = { 0 };
int mon_table[12] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };//平年每月的天数
int main()
{
Data_Get(1666453038);
printf("%d ", calendar.w_year);
printf("%d ", calendar.w_month);
printf("%d ", calendar.w_date);
printf("%d ", calendar.hour);
printf("%d ", calendar.min);
printf("%d ", calendar.sec);
return 0;
}
int Is_Leap_Year(int year)
{
if (year % 4 == 0 && year % 100 != 0)return 1;
else if (year % 400 == 0)return 1;
else return 0;
}
int Data_Get(int timecount)
{
static int daycnt = 0;
int temp, temp1 = 0;
temp = timecount/86400;
if (daycnt != temp)
{
daycnt = temp;
temp1 = 1970;//从1970年开始
while (temp >= 365)//计算年份
{
if (Is_Leap_Year(temp1))
{
if(temp>=366)temp -= 366;//闰年
else break ;
}
else temp -= 365;
temp1++;
}
calendar.w_year = temp1;//得到年份
temp1 = 0;//
while (temp >= 28)//计算月份
{
if (Is_Leap_Year(calendar.w_year) && temp1 == 1)//判断是不是闰年2月份
{
if (temp >= 29)temp -= 29;
else break;
}
else
{
if (temp >= mon_table[temp1])temp -= mon_table[temp1];
else break;
}
temp1++;
}
calendar.w_month = temp1 + 1; //得到月份
calendar.w_date = temp + 1; //得到日期
}
temp = timecount % 86400; //得到秒钟数
calendar.hour = temp / 3600; //小时
calendar.min = (temp % 3600) / 60; //分钟
calendar.sec = (temp % 3600) % 60; //秒钟
return 0;
}
C语言--已知秒钟数求对应的日期
最新推荐文章于 2023-10-24 11:52:23 发布