[Functions]D. Liang 5.24 Displaying date and time.c

Description

Write a program to reads an integer n, which means the total seconds since midnight Jan 1,1970(eg., 1103203148 seconds).
Display the date and time at that second.

Input

An integer n (long) . n is the total seconds since midnight Jan 1,1970(eg., 1103203148 seconds).

Output

Output the date and time in two lines, using the following format:

year-month-day

hour:minute:second

Sample Input

1297090629

Sample Output

2011-2-7
14:57:9

Analysis

1. There are many cases, it is easy to be judged as high complexity by matrix without function, and random test will output some strange values when the code is compiled;
2. This problem seems to be complex, but the thinking is also clear. You can first convert the number of seconds into the number of years, and then calculate the number of months, days, hours, minutes and seconds in turn. In the middle, you need to use the cycle and selection structure, in which you need to determine whether a leap year or not.
3. It is cleverer to use an array to store the number of days of each month when judging the number of days of months, so many statements of selection and judgment are omitted .

The solution code is as follows:

//   Date:2020/4/3
//   Author:xiezhg5
#include <stdio.h>
//#include "time.h"              //C语言中有一个时间库函数
void DispDateTime(long long n); // 显示日期时间
int IsLeapYear(int year); // 判断 year 是否闰年
int SecsOfYear(int year); // 返回某一年的总秒数
int main(void) {
	long long t;
	scanf("%lld", &t);   //用户输入一个长整型的数
	DispDateTime(t);    //调用DispDateTime函数
	return 0;
}
// 显示日期时间
void DispDateTime(long long n) {
	int year, month, day, hour, min, sec; // 年、月、日、时、分、秒
	long long secs = 0; // 用于计算经过的秒数,如果用32位的int,到2038年就溢出了
	int DaysOfMonth[13] = { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }; //定义数组存放每月的天数,很巧妙
// 开始计算年
	year = 1970;      //从1970年算起
	do {
		secs += SecsOfYear(year); // 从1970年1月1日零点开始,逐年累计秒数,大于 n 时退出循环
		year++;
	} while (secs < n);
	year--; // 退出循环说明累计年数超过1年,减1得到期望的年数
	secs -= SecsOfYear(year); // 减去多累加的1年秒数,得到至该年1月1日零时止的总秒数
	n -= secs; // 得到从该年1月1日零时起,剩余的秒数
// 开始计算 月
	secs = 0;  // 计数清0
	month = 1; // 从1月开始
	if (IsLeapYear(year))
		DaysOfMonth[2] = 29; //如果是闰年,2月有29天
	do {
		// 86400 为1天的秒数
		secs += 86400 * DaysOfMonth[month]; // 从1月开始,逐月累计秒数,大于 n 时退出循环
		month++;
	} while (secs < n);
	month--; // 退出循环说明累计月数超过1个月,减1得到期望的月数
	secs -= 86400 * DaysOfMonth[month]; // 减去多累加的1个月秒数
	n -= secs; // 得到从该月1日零时起,剩余的秒数
// 计算 日、时、分、秒
	day = n / 86400 +1;  // 算出天数
	hour = (n / 3600) % 24; // 小时
	min = (n % 3600) / 60; // 分钟
	sec = n % 60;   // 秒
// 输出结果
	printf("%d-%d-%d\n", year, month, day);
	printf("%d:%d:%d\n", hour, min, sec);
}
// 判断 year 是否闰年
int IsLeapYear(int year) {
	if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0)
		return 1;
	return 0;
}

// 返回某一年的总秒数
int SecsOfYear(int year) {
	if (IsLeapYear(year))
		return(31622400); // 3600*24*366 闰年的秒数
	return(31536000);  // 3600*24*365 平年的秒数
}

Expand:

Time function in C – time:

time

C Date and time utilities
Defined in header <time.h>

time_t time( time_t *arg );

Returns the current calendar time encoded as a time_t object, and also stores it in the time_t object pointed to by arg (unless arg is a null pointer)

Parameters

arg - pointer to a time_t object where the time will be stored, or a null pointer
Return value
Current calendar time encoded as time_t object on success, (time_t)(-1) on error. If arg is not a null pointer, the return value is also stored in the object pointed to by arg.

Notes

The encoding of calendar time in time_t is unspecified, but most systems conform to POSIX specification and return a value of integral type holding the number of seconds since the Epoch. Implementations in which time_t is a 32-bit signed integer (many historical implementations) fail in the year 2038.

Example

Run this code
#include <stdio.h>
#include <time.h>
#include <stdint.h>
 
int main(void)
{
    time_t result = time(NULL);
    if(result != (time_t)(-1))
        printf("The current time is %s(%jd seconds since the Epoch)\n",
               asctime(gmtime(&result)), (intmax_t)result);
}

Possible output:

The current time is Fri Apr 24 15:05:25 2015
(1429887925 seconds since the Epoch)

**Reference:**https://en.cppreference.com/w/c/chrono/time

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值