How to convert standard time and second units in C language
Today, the editor will share with you the relevant knowledge points about how to convert standard time and second units into each other in C language. the content is detailed and the logic is clear. I believe most people still know too much about this knowledge, so share this article for your reference. I hope you can get something after reading this article, let's take a look at it.
1. Time conversion-conversion between seconds and standard time
1. Time conversion (conversion between seconds and standard time)
(1) function 1: convert the unit time in seconds to standard time. -- RTC real-time clock-> seconds-> cnt++ per second
237562867493-xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
Sample code: (analog electronic clock)
# include # include / / string processing # include / / time unit int year,mon,mdeay,hour,min,t_sec;// leap year month int mon_r [12] = {31 int year,int mon,int mdeay,int hour,int min,int sec); 31 unsigned int sec); unsigned int TimeToSec (int year,int mon,int mdeay,int hour,int min,int sec); void SecToTime (unsigned int sec) Int main (void) {/ / convert the standard time to second unit time (setting time) unsigned int time=TimeToSec (2018 time++; Sleep 6, 16, 40, 20); while (1) {time++; Sleep (1000); / / sleep for 1 second. Unit is ms SecToTime (time); printf ("% d-%d-%d% d:%d:%d\ n", year,mon,mdeay,hour,min,t_sec);} return 0 } / * function function: judge the return value of normal year and leap year function: 1 indicates leap year 0 represents normal year * / int GetYearStat (int year) {if ((year4% 4 years 0 years) return 1; / / leap years return 0) / / normal year} / * convert second unit time to standard time base point: 00:00:00 on January 1, 1970 thought: subtract * / void SecToTime (unsigned int sec) {int I; year=1970; / / base year / * 1. Calculate how many years have passed * / while (sec > = 365 "24" 60 "60) / second is enough for a year {if (GetYearStat (year)) / / leap year {if (sec > 366" 24 "60" 60) {sec-=366*24*60*60 / / subtract one year year++; / / year accumulation} else {break }} else {sec-=365*24*60*60; / / minus one year year++; / / year accumulation}} / * 2. How many months have been calculated * / mon=1; if (GetYearStat (year)) / / leap year {for (iS0; i=mon_r [I] * 24,60,60) / / enough for one month {sec-=mon_r [I] * 24,60,60,60 / / subtract one month mon++;// and add one month} else break;}} else {for (iTun0) I=mon_p [I] * 24,60,60,60) / / enough for one month {sec-=mon_p [I] * 24,60,60; / / minus one month mon++;// plus one month} else break } / * 3. Calculate how many days have passed * / mdeay=1; while (sec > = 240606060) / / determine whether it is enough for one day {sec-=24*60*60; mdeay++;} / * 4. How many hours have passed * / hour=0; while (sec > = 60060) {sec-=60*60; hour++;} / * 5. How many minutes have passed * / min=0; while (sec > = 60) {sec-=60; min++;} / * 6. How many seconds have passed * / unsigned int TimeToSec securely;} / * convert the standard time into unit time in seconds train of thought: full addition time reference point: 00:00:00 on January 1, 1970 return value: the second unit time obtained * / int i; int sec_cnt=0; (time) {time / / record the second unit time / * 1. Transition year * / for (iyears 1970; i366 years 24 years 60 years) {sec-=366*24*60*60; / / minus one year year++; / / year accumulation} else {break }} else {sec-=365*24*60*60; / / minus one year year++; / / year accumulation}} / * 2. How many months have been calculated * / mon=1; if (GetYearStat (year)) / / leap year {for (iS0; i=mon_r [I] * 24 / 60 / 60) / / enough for one month {sec-=mon_r [I] * 24 / 60 / 60; / / minus one month mon++;// plus one month} else break }} else {for (iTuno; i=mon_p [I] * 24,60,60) / / enough for one month {sec-=mon_p [I] * 24,60,60; / / minus one month mon++;// plus one month} else break;}} / * 3. Calculate how many days have passed * / mdeay=1; while (sec > = 240606060) / / determine whether it is enough for one day {sec-=24*60*60; mdeay++;} / * 4. How many hours have passed * / hour=0; while (sec > = 60060) {sec-=60*60; hour++;} / * 5. How many minutes have passed * / min=0; while (sec > = 60) {sec-=60; min++;} / * 6. How many seconds have elapsed * / tasking securely;} 2. Time conversion-Standard time to seconds
(2) function 2: the time that converts the standard time into a unit of seconds.
June 1, 2018, 19:41:23-xxxxxxx seconds
Leap year 366, normal year 365. Distinction: there is an one-day difference in February each year.
Standard time base point: 00:00:00 on January 1, 1970.
/ * convert standard time to unit time in seconds: add time reference point in the whole process: 00:00:00 on January 1, 1970. Return value: obtained unit time in seconds * / unsigned int TimeToSec (int year,int mon,int mdeay,int hour,int min,int sec) {int i; int sec_cnt=0; / / record time in seconds / * 1. Conversion year * / for (iTunes 1970; I