Lines Matching refs:c

37 static CalendarComponent* chain_free(CalendarComponent *c) {  in chain_free()  argument
38 while (c) { in chain_free()
39 CalendarComponent *n = c->next; in chain_free()
40 free(c); in chain_free()
41 c = n; in chain_free()
48 CalendarSpec* calendar_spec_free(CalendarSpec *c) { in calendar_spec_free() argument
50 if (!c) in calendar_spec_free()
53 chain_free(c->year); in calendar_spec_free()
54 chain_free(c->month); in calendar_spec_free()
55 chain_free(c->day); in calendar_spec_free()
56 chain_free(c->hour); in calendar_spec_free()
57 chain_free(c->minute); in calendar_spec_free()
58 chain_free(c->microsecond); in calendar_spec_free()
59 free(c->timezone); in calendar_spec_free()
61 return mfree(c); in calendar_spec_free()
78 static void normalize_chain(CalendarComponent **c) { in normalize_chain() argument
79 assert(c); in normalize_chain()
82 for (CalendarComponent *i = *c; i; i = i->next) { in normalize_chain()
106 for (CalendarComponent *i = *c; i; i = i->next) in normalize_chain()
125 *c = next; in normalize_chain()
128 static void fix_year(CalendarComponent *c) { in fix_year() argument
131 while (c) { in fix_year()
132 if (c->start >= 0 && c->start < 70) in fix_year()
133 c->start += 2000; in fix_year()
135 if (c->stop >= 0 && c->stop < 70) in fix_year()
136 c->stop += 2000; in fix_year()
138 if (c->start >= 70 && c->start < 100) in fix_year()
139 c->start += 1900; in fix_year()
141 if (c->stop >= 70 && c->stop < 100) in fix_year()
142 c->stop += 1900; in fix_year()
144 c = c->next; in fix_year()
148 static void calendar_spec_normalize(CalendarSpec *c) { in calendar_spec_normalize() argument
149 assert(c); in calendar_spec_normalize()
151 if (streq_ptr(c->timezone, "UTC")) { in calendar_spec_normalize()
152 c->utc = true; in calendar_spec_normalize()
153 c->timezone = mfree(c->timezone); in calendar_spec_normalize()
156 if (c->weekdays_bits <= 0 || c->weekdays_bits >= BITS_WEEKDAYS) in calendar_spec_normalize()
157 c->weekdays_bits = -1; in calendar_spec_normalize()
159 if (c->end_of_month && !c->day) in calendar_spec_normalize()
160 c->end_of_month = false; in calendar_spec_normalize()
162 fix_year(c->year); in calendar_spec_normalize()
164 normalize_chain(&c->year); in calendar_spec_normalize()
165 normalize_chain(&c->month); in calendar_spec_normalize()
166 normalize_chain(&c->day); in calendar_spec_normalize()
167 normalize_chain(&c->hour); in calendar_spec_normalize()
168 normalize_chain(&c->minute); in calendar_spec_normalize()
169 normalize_chain(&c->microsecond); in calendar_spec_normalize()
172 static bool chain_valid(CalendarComponent *c, int from, int to, bool end_of_month) { in chain_valid() argument
175 if (!c) in chain_valid()
182 if (c->start < from || c->start > to) in chain_valid()
186 if (c->repeat > to - from) in chain_valid()
195 if (c->stop >= 0) { in chain_valid()
196 if (c->stop < from || c ->stop > to) in chain_valid()
199 if (c->start + c->repeat > c->stop) in chain_valid()
202 if (end_of_month && c->start - c->repeat < from) in chain_valid()
205 if (!end_of_month && c->start + c->repeat > to) in chain_valid()
209 if (c->next) in chain_valid()
210 return chain_valid(c->next, from, to, end_of_month); in chain_valid()
215 _pure_ bool calendar_spec_valid(CalendarSpec *c) { in calendar_spec_valid() argument
216 assert(c); in calendar_spec_valid()
218 if (c->weekdays_bits > BITS_WEEKDAYS) in calendar_spec_valid()
221 if (!chain_valid(c->year, MIN_YEAR, MAX_YEAR, false)) in calendar_spec_valid()
224 if (!chain_valid(c->month, 1, 12, false)) in calendar_spec_valid()
227 if (!chain_valid(c->day, 1, 31, c->end_of_month)) in calendar_spec_valid()
230 if (!chain_valid(c->hour, 0, 23, false)) in calendar_spec_valid()
233 if (!chain_valid(c->minute, 0, 59, false)) in calendar_spec_valid()
236 if (!chain_valid(c->microsecond, 0, 60*USEC_PER_SEC-1, false)) in calendar_spec_valid()
242 static void format_weekdays(FILE *f, const CalendarSpec *c) { in format_weekdays() argument
257 assert(c); in format_weekdays()
258 assert(c->weekdays_bits > 0 && c->weekdays_bits <= BITS_WEEKDAYS); in format_weekdays()
262 if (c->weekdays_bits & (1 << x)) { in format_weekdays()
291 static bool chain_is_star(const CalendarComponent *c, bool usec) { in chain_is_star() argument
294 if (!c) in chain_is_star()
297 for (; c; c = c->next) in chain_is_star()
298 if (c->start == 0 && c->stop < 0 && c->repeat == USEC_PER_SEC) in chain_is_star()
303 static void _format_chain(FILE *f, int space, const CalendarComponent *c, bool start, bool usec) { in _format_chain() argument
308 if (start && chain_is_star(c, usec)) { in _format_chain()
313 assert(c->start >= 0); in _format_chain()
315 fprintf(f, "%0*i", space, c->start / d); in _format_chain()
316 if (c->start % d > 0) in _format_chain()
317 fprintf(f, ".%06i", c->start % d); in _format_chain()
319 if (c->stop > 0) in _format_chain()
320 fprintf(f, "..%0*i", space, c->stop / d); in _format_chain()
321 if (c->stop % d > 0) in _format_chain()
322 fprintf(f, ".%06i", c->stop % d); in _format_chain()
324 if (c->repeat > 0 && !(c->stop > 0 && c->repeat == d)) in _format_chain()
325 fprintf(f, "/%i", c->repeat / d); in _format_chain()
326 if (c->repeat % d > 0) in _format_chain()
327 fprintf(f, ".%06i", c->repeat % d); in _format_chain()
329 if (c->next) { in _format_chain()
331 _format_chain(f, space, c->next, false, usec); in _format_chain()
335 static void format_chain(FILE *f, int space, const CalendarComponent *c, bool usec) { in format_chain() argument
336 _format_chain(f, space, c, /* start = */ true, usec); in format_chain()
339 int calendar_spec_to_string(const CalendarSpec *c, char **p) { in calendar_spec_to_string() argument
345 assert(c); in calendar_spec_to_string()
352 if (c->weekdays_bits > 0 && c->weekdays_bits <= BITS_WEEKDAYS) { in calendar_spec_to_string()
353 format_weekdays(f, c); in calendar_spec_to_string()
357 format_chain(f, 4, c->year, false); in calendar_spec_to_string()
359 format_chain(f, 2, c->month, false); in calendar_spec_to_string()
360 fputc(c->end_of_month ? '~' : '-', f); in calendar_spec_to_string()
361 format_chain(f, 2, c->day, false); in calendar_spec_to_string()
363 format_chain(f, 2, c->hour, false); in calendar_spec_to_string()
365 format_chain(f, 2, c->minute, false); in calendar_spec_to_string()
367 format_chain(f, 2, c->microsecond, true); in calendar_spec_to_string()
369 if (c->utc) in calendar_spec_to_string()
371 else if (c->timezone) { in calendar_spec_to_string()
373 fputs(c->timezone, f); in calendar_spec_to_string()
374 } else if (IN_SET(c->dst, 0, 1)) { in calendar_spec_to_string()
380 if (!isempty(tzname[c->dst])) { in calendar_spec_to_string()
382 fputs(tzname[c->dst], f); in calendar_spec_to_string()
398 static int parse_weekdays(const char **p, CalendarSpec *c) { in parse_weekdays() argument
424 assert(c); in parse_weekdays()
440 c->weekdays_bits |= 1 << day_nr[i].nr; in parse_weekdays()
447 c->weekdays_bits |= 1 << j; in parse_weekdays()
561 static int const_chain(int value, CalendarComponent **c) { in const_chain() argument
564 assert(c); in const_chain()
574 .next = *c, in const_chain()
577 *c = cc; in const_chain()
582 static int calendarspec_from_time_t(CalendarSpec *c, time_t time) { in calendarspec_from_time_t() argument
619 c->utc = true; in calendarspec_from_time_t()
620 c->year = TAKE_PTR(year); in calendarspec_from_time_t()
621 c->month = TAKE_PTR(month); in calendarspec_from_time_t()
622 c->day = TAKE_PTR(day); in calendarspec_from_time_t()
623 c->hour = TAKE_PTR(hour); in calendarspec_from_time_t()
624 c->minute = TAKE_PTR(minute); in calendarspec_from_time_t()
625 c->microsecond = TAKE_PTR(us); in calendarspec_from_time_t()
629 static int prepend_component(const char **p, bool usec, unsigned nesting, CalendarComponent **c) { in prepend_component() argument
635 assert(c); in prepend_component()
684 .next = *c, in prepend_component()
688 *c = cc; in prepend_component()
692 return prepend_component(p, usec, nesting + 1, c); in prepend_component()
698 static int parse_chain(const char **p, bool usec, CalendarComponent **c) { in parse_chain() argument
704 assert(c); in parse_chain()
710 r = const_chain(0, c); in parse_chain()
713 (*c)->repeat = USEC_PER_SEC; in parse_chain()
715 *c = NULL; in parse_chain()
726 *c = TAKE_PTR(cc); in parse_chain()
730 static int parse_date(const char **p, CalendarSpec *c) { in parse_date() argument
737 assert(c); in parse_date()
757 r = calendarspec_from_time_t(c, time); in parse_date()
774 c->end_of_month = true; in parse_date()
786 c->month = TAKE_PTR(first); in parse_date()
787 c->day = TAKE_PTR(second); in parse_date()
789 } else if (c->end_of_month) in parse_date()
793 c->end_of_month = true; in parse_date()
807 c->year = TAKE_PTR(first); in parse_date()
808 c->month = TAKE_PTR(second); in parse_date()
809 c->day = TAKE_PTR(third); in parse_date()
813 static int parse_calendar_time(const char **p, CalendarSpec *c) { in parse_calendar_time() argument
820 assert(c); in parse_calendar_time()
874 c->hour = TAKE_PTR(h); in parse_calendar_time()
875 c->minute = TAKE_PTR(m); in parse_calendar_time()
876 c->microsecond = TAKE_PTR(s); in parse_calendar_time()
883 _cleanup_(calendar_spec_freep) CalendarSpec *c = NULL; in calendar_spec_from_string()
889 c = new(CalendarSpec, 1); in calendar_spec_from_string()
890 if (!c) in calendar_spec_from_string()
893 *c = (CalendarSpec) { in calendar_spec_from_string()
900 c->utc = true; in calendar_spec_from_string()
932 c->dst = j; in calendar_spec_from_string()
938 c->timezone = strdup(last_space + 1); in calendar_spec_from_string()
939 if (!c->timezone) in calendar_spec_from_string()
953 r = const_chain(0, &c->microsecond); in calendar_spec_from_string()
958 r = const_chain(0, &c->minute); in calendar_spec_from_string()
961 r = const_chain(0, &c->microsecond); in calendar_spec_from_string()
966 r = const_chain(0, &c->hour); in calendar_spec_from_string()
969 r = const_chain(0, &c->minute); in calendar_spec_from_string()
972 r = const_chain(0, &c->microsecond); in calendar_spec_from_string()
977 r = const_chain(1, &c->day); in calendar_spec_from_string()
980 r = const_chain(0, &c->hour); in calendar_spec_from_string()
983 r = const_chain(0, &c->minute); in calendar_spec_from_string()
986 r = const_chain(0, &c->microsecond); in calendar_spec_from_string()
995 r = const_chain(1, &c->month); in calendar_spec_from_string()
998 r = const_chain(1, &c->day); in calendar_spec_from_string()
1001 r = const_chain(0, &c->hour); in calendar_spec_from_string()
1004 r = const_chain(0, &c->minute); in calendar_spec_from_string()
1007 r = const_chain(0, &c->microsecond); in calendar_spec_from_string()
1013 c->weekdays_bits = 1; in calendar_spec_from_string()
1015 r = const_chain(0, &c->hour); in calendar_spec_from_string()
1018 r = const_chain(0, &c->minute); in calendar_spec_from_string()
1021 r = const_chain(0, &c->microsecond); in calendar_spec_from_string()
1027 r = const_chain(1, &c->month); in calendar_spec_from_string()
1030 r = const_chain(4, &c->month); in calendar_spec_from_string()
1033 r = const_chain(7, &c->month); in calendar_spec_from_string()
1036 r = const_chain(10, &c->month); in calendar_spec_from_string()
1039 r = const_chain(1, &c->day); in calendar_spec_from_string()
1042 r = const_chain(0, &c->hour); in calendar_spec_from_string()
1045 r = const_chain(0, &c->minute); in calendar_spec_from_string()
1048 r = const_chain(0, &c->microsecond); in calendar_spec_from_string()
1058 r = const_chain(1, &c->month); in calendar_spec_from_string()
1061 r = const_chain(7, &c->month); in calendar_spec_from_string()
1064 r = const_chain(1, &c->day); in calendar_spec_from_string()
1067 r = const_chain(0, &c->hour); in calendar_spec_from_string()
1070 r = const_chain(0, &c->minute); in calendar_spec_from_string()
1073 r = const_chain(0, &c->microsecond); in calendar_spec_from_string()
1078 r = parse_weekdays(&p, c); in calendar_spec_from_string()
1082 r = parse_date(&p, c); in calendar_spec_from_string()
1087 r = parse_calendar_time(&p, c); in calendar_spec_from_string()
1096 calendar_spec_normalize(c); in calendar_spec_from_string()
1098 if (!calendar_spec_valid(c)) in calendar_spec_from_string()
1102 *spec = TAKE_PTR(c); in calendar_spec_from_string()
1121 const CalendarComponent *c, in find_matching_component() argument
1135 if (!c) in find_matching_component()
1138 bool end_of_month = spec->end_of_month && c == spec->day; in find_matching_component()
1140 while (c) { in find_matching_component()
1144 start = find_end_of_month(tm, spec->utc, c->start); in find_matching_component()
1145 stop = find_end_of_month(tm, spec->utc, c->stop); in find_matching_component()
1150 start = c->start; in find_matching_component()
1151 stop = c->stop; in find_matching_component()
1161 } else if (c->repeat > 0) { in find_matching_component()
1164 k = start + c->repeat * DIV_ROUND_UP(*val - start, c->repeat); in find_matching_component()
1172 c = c->next; in find_matching_component()
1247 struct tm c; in find_next() local
1256 c = *tm; in find_next()
1261 (void) mktime_or_timegm(&c, spec->utc); in find_next()
1262 c.tm_isdst = spec->dst; in find_next()
1264 c.tm_year += 1900; in find_next()
1265 r = find_matching_component(spec, spec->year, &c, &c.tm_year); in find_next()
1266 c.tm_year -= 1900; in find_next()
1269 c.tm_mon = 0; in find_next()
1270 c.tm_mday = 1; in find_next()
1271 c.tm_hour = c.tm_min = c.tm_sec = tm_usec = 0; in find_next()
1275 if (tm_within_bounds(&c, spec->utc) <= 0) in find_next()
1278 c.tm_mon += 1; in find_next()
1279 r = find_matching_component(spec, spec->month, &c, &c.tm_mon); in find_next()
1280 c.tm_mon -= 1; in find_next()
1283 c.tm_mday = 1; in find_next()
1284 c.tm_hour = c.tm_min = c.tm_sec = tm_usec = 0; in find_next()
1286 if (r < 0 || (r = tm_within_bounds(&c, spec->utc)) < 0) { in find_next()
1287 c.tm_year++; in find_next()
1288 c.tm_mon = 0; in find_next()
1289 c.tm_mday = 1; in find_next()
1290 c.tm_hour = c.tm_min = c.tm_sec = tm_usec = 0; in find_next()
1296 r = find_matching_component(spec, spec->day, &c, &c.tm_mday); in find_next()
1298 c.tm_hour = c.tm_min = c.tm_sec = tm_usec = 0; in find_next()
1299 if (r < 0 || (r = tm_within_bounds(&c, spec->utc)) < 0) { in find_next()
1300 c.tm_mon++; in find_next()
1301 c.tm_mday = 1; in find_next()
1302 c.tm_hour = c.tm_min = c.tm_sec = tm_usec = 0; in find_next()
1308 if (!matches_weekday(spec->weekdays_bits, &c, spec->utc)) { in find_next()
1309 c.tm_mday++; in find_next()
1310 c.tm_hour = c.tm_min = c.tm_sec = tm_usec = 0; in find_next()
1314 r = find_matching_component(spec, spec->hour, &c, &c.tm_hour); in find_next()
1316 c.tm_min = c.tm_sec = tm_usec = 0; in find_next()
1317 if (r < 0 || (r = tm_within_bounds(&c, spec->utc)) < 0) { in find_next()
1318 c.tm_mday++; in find_next()
1319 c.tm_hour = c.tm_min = c.tm_sec = tm_usec = 0; in find_next()
1328 r = find_matching_component(spec, spec->minute, &c, &c.tm_min); in find_next()
1330 c.tm_sec = tm_usec = 0; in find_next()
1331 if (r < 0 || (r = tm_within_bounds(&c, spec->utc)) < 0) { in find_next()
1332 c.tm_hour++; in find_next()
1333 c.tm_min = c.tm_sec = tm_usec = 0; in find_next()
1339 c.tm_sec = c.tm_sec * USEC_PER_SEC + tm_usec; in find_next()
1340 r = find_matching_component(spec, spec->microsecond, &c, &c.tm_sec); in find_next()
1341 tm_usec = c.tm_sec % USEC_PER_SEC; in find_next()
1342 c.tm_sec /= USEC_PER_SEC; in find_next()
1344 if (r < 0 || (r = tm_within_bounds(&c, spec->utc)) < 0) { in find_next()
1345 c.tm_min++; in find_next()
1346 c.tm_sec = tm_usec = 0; in find_next()
1352 *tm = c; in find_next()