PTypes: date/time: calendar

Top: Basic types: Date/time: Date/calendar manipulation

>     #include <ptime.h>

>     typedef large datetime;

>     bool     isleapyear(int year);
>     int      daysinmonth(int year, int month);
>     int      daysinyear(int year, int month);
>     int      dayofweek(datetime d);
>     bool     isdatevalid(int year, int month, int day);
>     datetime encodedate(int year, int month, int day);
>     bool     decodedate(datetime d, int& year, int& month, int& day);

bool isleapyear(int year) determines whether year is a leap year.

int daysinmonth(int year, int month) returns the number of days in month (1 - 12). The parameter year is needed to determine whether it's a leap year in order to return the correct number of days for February. The returned value can be in the range 28 - 31. If month is out of the allowed range (1 - 12) this function returns 0.

int daysinyear(int year, int month) returns the number of days since the beginning of the year up to the month (1 - 12), inclusive. If month is 12 the function will return the total number of days in the year. If month is out of the allowed range (1 - 12) this function returns 0.

int dayofweek(datetime d) returns the day of the week (0 - 6) for the given date d. 0 corresponds to Sunday, 1 - Monday etc. This function does not check the value of d for validity, it always returns some result in the range 0 - 6.

bool isdatevalid(int year, int month, int day) checks the values of year, month and day for validity. This function takes into account that day must be in the correct range depending on year and month. Also, it checks whether year is in the allowed range 1 - 9999.

datetime encodedate(int year, int month, int day) returns a datetime value, i.e. the number of milliseconds since the beginning of the calendar up to the midnight of month/day/year. This value can then be added to the value returned by encodetime() to form the exact time stamp with millisecond precision, or it can be used alone as a calendar date value without the time correction.

bool decodedate(datetime d, int& year, int& month, int& day) is the inverse version of encodedate(): it breaks the value of d into its year, month and day. This function returns false if the value of d is invalid.

See also: Datetime type, Time manipulation

PTypes home