Copyright: It is originally original. You can modify, disseminate, and commercial use it at will. It must be noted that it is from this blog and will be useful to you!
From: http://blog.csdn.net/wanfustudio Author: wanfustudio
Question: I often see the following questions on csdn:
1. How does tm_hour calculate the number of Angel days in a year?
2. The specified day of a month is the day of the specified year.
3. What day is the offset added to a date?
4 .......
There are still many questions to be detailed.
# Include <stdio. h>
# Include <string. h>
# Include <time. h>
Const char * week [] =
{
"Sunday ",
"Monday ",
"Tuesday ",
"Wednesday ",
"Thursday ",
"Friday ",
"Saturday"
};
# If 0
Struct TM {
Int tm_sec;/* seconds after the minute-[0, 59] */
Int tm_min;/* minutes after the hour-[0, 59] */
Int tm_hour;/* hours since midnight-[0, 23] */
Int tm_mday;/* day of the month-[1, 31] */
Int tm_mon;/* months since January-[0, 11] */
Int tm_year;/* years since 1900 */
Int tm_wday;/* Days since Sunday-[0, 6] */
Int tm_yday;/* Days since January 1-[0,365] */
Int tm_isdst;/* daylight savings time flag */
};
# Endif
Void getweekday (INT year, int num );
Void getsomeday (INT year, int Mon, int Day );
Void getnexttime (INT year, int Mon, int day, int num );
Int main ()
{
Getweekday (2007,337 );
Getsomeday (2007,12, 4 );
Getnexttime (2007,12, 4,30 );
Return 0;
}
Void getsomeday (INT year, int Mon, int Day)
{
Time_t t; // The number of seconds from which the storage needs to be calculated
Struct TM test; // fill in this structure
Memset (& test, 0, sizeof (TM); // first clear
Test. tm_year = years-1900;
Test. tm_mon = mon-1;
Test. tm_mday = Day;
T = mktime (& Test); // converts it to seconds.
Struct TM * Now = localtime (& T); // switch back to TM
Printf ("% d/N", now-> tm_yday); // day
Printf ("% s/n", week [now-> tm_wday]); // day of the week
}
Void getnexttime (INT year, int Mon, int day, int num)
{
Struct TM now, TMP;
Time_t T;
Memset (& now, 0, sizeof (struct TM ));
Memset (& TMP, 0, sizeof (struct TM ));
Now. tm_year = year-1900; // specifies the year
Now. tm_mon = mon-1; // specify the month
Now. tm_mday = day + num; // specifies the day
T = mktime (& now); // converts to seconds
Struct TM * pnext = localtime (& T );
Printf ("% s/n", asctime (pnext); // which day
Printf ("% s/n", week [pnext-> tm_wday]); // day of the week
}
Void getweekday (INT year, int num)
{
Time_t t; // The number of seconds from which the storage needs to be calculated
Struct TM test; // fill in this structure
Memset (& test, 0, sizeof (TM); // first clear
Test. tm_year = years-1900;
Test. tm_mday = num + 1;
T = mktime (& Test); // converts it to seconds.
Struct TM * Now = localtime (& T); // switch back to TM
Printf ("% d/N", now-> tm_yday); // day
Printf ("% s/n", week [now-> tm_wday]); // day of the week
}