一.将年月日转换为秒
1.闰年与平年;
闰年分为普通闰年和世纪闰年;公历年份是4的倍数而不是100的倍数,一般是普通闰年;公历年份是400的倍数,是世纪闰年,闰年二月份有29天。
平年是指既不能被4整除或者能被100整除,叫做平年,平年二月份只有28天,比闰年少一天。
if(year % 400 == 0)
printf("闰年\n");
else{
if((year % 4 ==0)&&(year %100 != 0))
printf("闰年\n");
else
printf("平年\n");
}
相关时间结构体的定义,如图所示:
/*显示时间相关的定义*/
#define SECONDS_IN_A_DAY (86400) //1天等于86400秒
#define SECONDS_IN_A_HOUR (3600)
#define SECONDS_IN_A_MINUTE (60)
#define DAYS_IN_A_YEARS (365)
#define YEAR_RANGE_START (1970)
#define YEAR_RANGE_END (2099)
/*设置时间有关的结构体*/
struct rtc_datetime{
unsigned char year;
unsigned char month;
unsigned char day;
unsigned char hour;
unsigned char minute;
unsigned char second;
};
2 . 将年月日时分秒转换为秒,首先判断年份是否是润年还是平年(闰年366天,平年365天,闰年就在平年的基础上加1),然后所得的天数应该减1(不能将当天当成24小时算上去,应该根据当天的时间来计算出当天的秒);最后将他们统一加起来(U其实是无符号的整型);
/*判断是否是闰年还是平年,返回值为value值 1 为闰年 0为平年*/
unsigned char isleapyear(unsigned short year)
{
unsigned char value = 0;
if(year % 400 == 0)
value = 1;
else{
if((year % 4 ==0)&&(year %100!= 0))
value = 1;
else
value = 0;
}
return value;
}
unsigned int coverdate_to_seconds(struct rtc_datetime *datetime)
{
unsigned short i = 0;
unsigned int seconds = 0;
unsigned int days = 0;
unsigned short monthdays[] = {0U, 0U, 31U, 59U, 90U, 120U, 151U, 181U, 212U, 243U, 273U, 304U, 334U};
for(i = 1970; i < datetime->year; i++)
{
days += DAYS_IN_A_YEAR; /* 平年,每年365天 */
if(isleapyear(i)) days += 1;/* 闰年多加一天 */
}
days += monthdays[datetime->month];
if(rtc_isleapyear(i) && (datetime->month >= 3)) days += 1;/* 闰年,并且当前月份大于等于3月的话加一天 */
days += datetime->day - 1;
seconds = days * SECONDS_IN_A_DAY +
datetime->hour * SECONDS_IN_A_HOUR +
datetime->minute * SECONDS_IN_A_MINUTE +
datetime->second;
return seconds;
}
二. 将秒转换为年月日时分秒
1.秒转换为年月日时分秒,首先计算天数,(当前天数等于计算的天数加1,前面减去1了的,后面需要加上1),之后依次计算时分秒;再然后计算出当前年份(也是先判断是否时润年和平年,之后天数依次减去那一年的时间,直到小于一年的天数,退出循环,年份一直++这个判断就比较绝),至于月份吗;(也是先判断是否为闰年,闰年将加数组里面的第三个值更改为29,之后也是--,直到小于数组里面的值,就退出循环,此时已经得到月份
/*判断是否为闰年或者平年*/
unsigned char isleapyear(unsigned short year)
{
unsigned char value = 0;
if(year % 400 == 0)
value = 1;
else{
if((year % 4 ==0)&&(year %100!= 0))
value = 1;
else
value = 0;
}
return value;
}
void rtc_convertseconds_to_datetime(u64 seconds, struct rtc_datetime *datetime)
{
u64 x;
u64 secondsRemaining, days;
unsigned short daysInYear;
/* 每个月的天数 */
unsigned char daysPerMonth[] = {0U, 31U, 28U, 31U, 30U, 31U, 30U, 31U, 31U, 30U, 31U, 30U, 31U};
secondsRemaining = seconds; /* 剩余秒数初始化 */
days = secondsRemaining / SECONDS_IN_A_DAY + 1; /* 根据秒数计算天数,加1是当前天数 */
secondsRemaining = secondsRemaining % SECONDS_IN_A_DAY; /*计算天数以后剩余的秒数 */
/* 计算时、分、秒 */
datetime->hour = secondsRemaining / SECONDS_IN_A_HOUR;
secondsRemaining = secondsRemaining % SECONDS_IN_A_HOUR;
datetime->minute = secondsRemaining / 60;
datetime->second = secondsRemaining % SECONDS_IN_A_MINUTE;
/* 计算年 */
daysInYear = DAYS_IN_A_YEAR;
datetime->year = YEAR_RANGE_START;
while(days > daysInYear)
{
/* 根据天数计算年 */
days -= daysInYear;
datetime->year++;
/* 处理闰年 */
if (!rtc_isleapyear(datetime->year))
daysInYear = DAYS_IN_A_YEAR;
else /*闰年,天数加一 */
daysInYear = DAYS_IN_A_YEAR + 1;
}
/*根据剩余的天数计算月份 */
if(rtc_isleapyear(datetime->year)) /* 如果是闰年的话2月加一天 */
daysPerMonth[2] = 29;
for(x = 1; x <= 12; x++)
{
if (days <= daysPerMonth[x])
{
datetime->month = x;
break;
}
else
{
days -= daysPerMonth[x];
}
}
datetime->day = days;
}
这样就可以实现将秒转换为时间了。