C语言--已知秒钟数求对应的日期

#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;
}

  • 3
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值