| 1 | /* @title: Date Time */ |
| 2 | #pragma once |
| 3 | #include <types/types.h> |
| 4 | |
| 5 | /* We'll define these because this header + the implementations for various |
| 6 | * functions using struct date_time will need the annotations in types */ |
| 7 | typedef uint16_t year_t; |
| 8 | typedef uint16_t day_t; |
| 9 | |
| 10 | /* We use this one for storage */ |
| 11 | struct date_time { |
| 12 | year_t year; |
| 13 | day_t day; |
| 14 | uint32_t sec; |
| 15 | }; |
| 16 | |
| 17 | struct date_time_zone { |
| 18 | int16_t utc_offset_min; |
| 19 | int16_t dst_offset_min; |
| 20 | }; |
| 21 | |
| 22 | /* We use this one for processing */ |
| 23 | struct date_time_expanded { |
| 24 | year_t year; |
| 25 | uint8_t month; /* 0 - 11 */ |
| 26 | uint8_t day_of_month; /* 1 - 31 */ |
| 27 | uint8_t day_of_week; /* 0 - 6 */ |
| 28 | uint8_t hour; /* 0 - 23 */ |
| 29 | uint8_t minute; /* 0 - 59 */ |
| 30 | uint8_t second; /* 0 - 59 */ |
| 31 | }; |
| 32 | |
| 33 | static inline bool is_leap_year(year_t year) { |
| 34 | return (year % 4 == 0 && (year % 100 != 0 || year % 400 == 0)); |
| 35 | } |
| 36 | |
| 37 | /* NOTE: month is 1-12 here */ |
| 38 | static inline uint32_t days_in_month(int year, int month) { |
| 39 | if (month == 2) { |
| 40 | return is_leap_year(year) ? 29 : 28; |
| 41 | } |
| 42 | return (month == 4 || month == 6 || month == 9 || month == 11) ? 30 : 31; |
| 43 | } |
| 44 | |
| 45 | struct date_time_expanded date_time_expand(const struct date_time *dt); |
| 46 | void date_time_compact(const struct date_time_expanded *expanded, |
| 47 | struct date_time *out); |
| 48 | |
| 49 | /* TODO: timezones and functions related to this once anything needs this */ |
| 50 | |