wdenk | 012771d | 2002-03-08 21:31:05 +0000 | [diff] [blame] | 1 | #ifndef _LINUX_TIME_H |
| 2 | #define _LINUX_TIME_H |
| 3 | |
AKASHI Takahiro | 80a75bc | 2019-11-13 09:44:50 +0900 | [diff] [blame] | 4 | #include <rtc.h> |
AKASHI Takahiro | 9825d55 | 2019-11-13 09:44:52 +0900 | [diff] [blame] | 5 | #include <vsprintf.h> |
wdenk | 012771d | 2002-03-08 21:31:05 +0000 | [diff] [blame] | 6 | #include <linux/types.h> |
| 7 | |
| 8 | #define _DEFUN(a,b,c) a(c) |
| 9 | #define _CONST const |
| 10 | #define _AND , |
| 11 | |
| 12 | #define _REENT_ONLY |
| 13 | |
Igor Prusov | 700a06c | 2023-11-09 20:10:02 +0300 | [diff] [blame] | 14 | #define MSEC_PER_SEC 1000L |
| 15 | #define USEC_PER_MSEC 1000L |
| 16 | #define NSEC_PER_USEC 1000L |
| 17 | #define NSEC_PER_MSEC 1000000L |
| 18 | #define USEC_PER_SEC 1000000L |
| 19 | #define NSEC_PER_SEC 1000000000L |
| 20 | #define PSEC_PER_SEC 1000000000000LL |
| 21 | #define FSEC_PER_SEC 1000000000000000LL |
| 22 | |
wdenk | 012771d | 2002-03-08 21:31:05 +0000 | [diff] [blame] | 23 | #define SECSPERMIN 60L |
| 24 | #define MINSPERHOUR 60L |
| 25 | #define HOURSPERDAY 24L |
| 26 | #define SECSPERHOUR (SECSPERMIN * MINSPERHOUR) |
| 27 | #define SECSPERDAY (SECSPERHOUR * HOURSPERDAY) |
| 28 | #define DAYSPERWEEK 7 |
| 29 | #define MONSPERYEAR 12 |
| 30 | |
| 31 | #define YEAR_BASE 1900 |
| 32 | #define EPOCH_YEAR 1970 |
| 33 | #define EPOCH_WDAY 4 |
| 34 | |
| 35 | #define isleap(y) ((((y) % 4) == 0 && ((y) % 100) != 0) || ((y) % 400) == 0) |
| 36 | |
| 37 | |
| 38 | /* Used by other time functions. */ |
| 39 | struct tm { |
| 40 | int tm_sec; /* Seconds. [0-60] (1 leap second) */ |
| 41 | int tm_min; /* Minutes. [0-59] */ |
| 42 | int tm_hour; /* Hours. [0-23] */ |
| 43 | int tm_mday; /* Day. [1-31] */ |
| 44 | int tm_mon; /* Month. [0-11] */ |
| 45 | int tm_year; /* Year - 1900. */ |
| 46 | int tm_wday; /* Day of week. [0-6] */ |
| 47 | int tm_yday; /* Days in year.[0-365] */ |
| 48 | int tm_isdst; /* DST. [-1/0/1]*/ |
| 49 | |
| 50 | # ifdef __USE_BSD |
| 51 | long int tm_gmtoff; /* Seconds east of UTC. */ |
| 52 | __const char *tm_zone; /* Timezone abbreviation. */ |
| 53 | # else |
| 54 | long int __tm_gmtoff; /* Seconds east of UTC. */ |
| 55 | __const char *__tm_zone; /* Timezone abbreviation. */ |
| 56 | # endif |
| 57 | }; |
| 58 | |
| 59 | static inline char * |
| 60 | _DEFUN (asctime_r, (tim_p, result), |
| 61 | _CONST struct tm *tim_p _AND |
| 62 | char *result) |
| 63 | { |
| 64 | static _CONST char day_name[7][3] = { |
| 65 | "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat" |
| 66 | }; |
| 67 | static _CONST char mon_name[12][3] = { |
| 68 | "Jan", "Feb", "Mar", "Apr", "May", "Jun", |
| 69 | "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" |
| 70 | }; |
| 71 | |
| 72 | sprintf (result, "%.3s %.3s %.2d %.2d:%.2d:%.2d %d\n", |
| 73 | day_name[tim_p->tm_wday], |
| 74 | mon_name[tim_p->tm_mon], |
| 75 | tim_p->tm_mday, tim_p->tm_hour, tim_p->tm_min, |
| 76 | tim_p->tm_sec, 1900 + tim_p->tm_year); |
| 77 | return result; |
| 78 | } |
| 79 | |
| 80 | static inline struct tm * |
| 81 | _DEFUN (localtime_r, (tim_p, res), |
| 82 | _CONST time_t * tim_p _AND |
| 83 | struct tm *res) |
| 84 | { |
| 85 | static _CONST int mon_lengths[2][MONSPERYEAR] = { |
| 86 | {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}, |
| 87 | {31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31} |
| 88 | } ; |
| 89 | |
| 90 | static _CONST int year_lengths[2] = { |
| 91 | 365, |
| 92 | 366 |
| 93 | } ; |
| 94 | |
| 95 | long days, rem; |
| 96 | int y; |
| 97 | int yleap; |
| 98 | _CONST int *ip; |
| 99 | |
| 100 | days = ((long) *tim_p) / SECSPERDAY; |
| 101 | rem = ((long) *tim_p) % SECSPERDAY; |
| 102 | while (rem < 0) |
| 103 | { |
| 104 | rem += SECSPERDAY; |
| 105 | --days; |
| 106 | } |
wdenk | 012771d | 2002-03-08 21:31:05 +0000 | [diff] [blame] | 107 | |
| 108 | /* compute hour, min, and sec */ |
| 109 | res->tm_hour = (int) (rem / SECSPERHOUR); |
| 110 | rem %= SECSPERHOUR; |
| 111 | res->tm_min = (int) (rem / SECSPERMIN); |
| 112 | res->tm_sec = (int) (rem % SECSPERMIN); |
| 113 | |
| 114 | /* compute day of week */ |
| 115 | if ((res->tm_wday = ((EPOCH_WDAY + days) % DAYSPERWEEK)) < 0) |
| 116 | res->tm_wday += DAYSPERWEEK; |
| 117 | |
| 118 | /* compute year & day of year */ |
| 119 | y = EPOCH_YEAR; |
| 120 | if (days >= 0) |
| 121 | { |
| 122 | for (;;) |
| 123 | { |
| 124 | yleap = isleap(y); |
| 125 | if (days < year_lengths[yleap]) |
| 126 | break; |
| 127 | y++; |
| 128 | days -= year_lengths[yleap]; |
| 129 | } |
| 130 | } |
| 131 | else |
| 132 | { |
| 133 | do |
| 134 | { |
| 135 | --y; |
| 136 | yleap = isleap(y); |
| 137 | days += year_lengths[yleap]; |
| 138 | } while (days < 0); |
| 139 | } |
| 140 | |
| 141 | res->tm_year = y - YEAR_BASE; |
| 142 | res->tm_yday = days; |
| 143 | ip = mon_lengths[yleap]; |
| 144 | for (res->tm_mon = 0; days >= ip[res->tm_mon]; ++res->tm_mon) |
| 145 | days -= ip[res->tm_mon]; |
| 146 | res->tm_mday = days + 1; |
| 147 | |
| 148 | /* set daylight saving time flag */ |
| 149 | res->tm_isdst = -1; |
| 150 | |
| 151 | return (res); |
| 152 | } |
| 153 | |
| 154 | static inline char * |
| 155 | _DEFUN (ctime_r, (tim_p, result), |
| 156 | _CONST time_t * tim_p _AND |
| 157 | char * result) |
| 158 | |
| 159 | { |
| 160 | struct tm tm; |
| 161 | return asctime_r (localtime_r (tim_p, &tm), result); |
| 162 | } |
| 163 | |
AKASHI Takahiro | 80a75bc | 2019-11-13 09:44:50 +0900 | [diff] [blame] | 164 | #ifdef CONFIG_LIB_DATE |
| 165 | time64_t mktime64(const unsigned int year, const unsigned int mon, |
| 166 | const unsigned int day, const unsigned int hour, |
| 167 | const unsigned int min, const unsigned int sec); |
| 168 | #endif |
| 169 | |
wdenk | 012771d | 2002-03-08 21:31:05 +0000 | [diff] [blame] | 170 | #endif |