MCPcopy Create free account
hub / github.com/acl-dev/acl / nolocks_localtime

Function nolocks_localtime

lib_acl/src/stdlib/string/acl_localtime.c:75–128  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

73}
74
75static void nolocks_localtime(struct tm *tmp, time_t t, time_t tz, int dst)
76{
77 const time_t secs_min = 60;
78 const time_t secs_hour = 3600;
79 const time_t secs_day = 3600*24;
80
81 t -= tz; /* Adjust for timezone. */
82 t += 3600 * dst; /* Adjust for daylight time. */
83 time_t days = t / secs_day; /* Days passed since epoch. */
84 time_t seconds = t % secs_day; /* Remaining seconds. */
85
86 tmp->tm_isdst = dst;
87 tmp->tm_hour = (int) (seconds / secs_hour);
88 tmp->tm_min = (seconds % secs_hour) / secs_min;
89 tmp->tm_sec = (seconds % secs_hour) % secs_min;
90
91 /* 1/1/1970 was a Thursday, that is, day 4 from the POV of the tm
92 * structure where sunday = 0, so to calculate the day of the week
93 * we have to add 4
94 * and take the modulo by 7.
95 */
96 tmp->tm_wday = (days + 4) % 7;
97
98 /* Calculate the current year. */
99 tmp->tm_year = 1970;
100 while(1) {
101 /* Leap years have one day more. */
102 time_t days_this_year = 365 + is_leap_year(tmp->tm_year);
103 if (days_this_year > days) {
104 break;
105 }
106 days -= days_this_year;
107 tmp->tm_year++;
108 }
109
110 tmp->tm_yday = (int) days; /* Number of day of the current year. */
111
112 /* We need to calculate in which month and day of the month we are.
113 * To do so we need to skip days according to how many days there are
114 * in each month, and adjust for the leap year that has one more day
115 * in February.
116 */
117 int mdays[12] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
118 mdays[1] += is_leap_year(tmp->tm_year);
119
120 tmp->tm_mon = 0;
121 while(days >= mdays[tmp->tm_mon]) {
122 days -= mdays[tmp->tm_mon];
123 tmp->tm_mon++;
124 }
125
126 tmp->tm_mday = (int) days + 1; /* Add 1 since our 'days' is zero-based. */
127 tmp->tm_year -= 1900; /* Surprisingly tm_year is year-1900. */
128}
129
130static int daylight_active;
131

Callers 1

acl_localtime_rFunction · 0.85

Calls 1

is_leap_yearFunction · 0.85

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…