29 #ifdef HAVE_SYS_TIME_H
51 log_warn(
LD_BUG,
"%s", err);
70 log_warn(
LD_BUG,
"%s", err);
77 #define IS_LEAPYEAR(y) (!(y % 4) && ((y % 100) || !(y % 400)))
84 return (year2/4 - year1/4) - (year2/100 - year1/100)
85 + (year2/400 - year1/400);
90 { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
105 int64_t year, days, hours, minutes, seconds;
106 int i, invalid_year, dpm;
114 if (tm->tm_year < INT32_MAX-1900) {
115 year = tm->tm_year + 1900;
120 invalid_year = (year < 1970 || tm->tm_year >= INT32_MAX-1900);
122 if (tm->tm_mon >= 0 && tm->tm_mon <= 11) {
124 if (tm->tm_mon == 1 && !invalid_year &&
IS_LEAPYEAR(tm->tm_year)) {
133 tm->tm_mon < 0 || tm->tm_mon > 11 ||
134 tm->tm_mday < 1 || tm->tm_mday > dpm ||
135 tm->tm_hour < 0 || tm->tm_hour > 23 ||
136 tm->tm_min < 0 || tm->tm_min > 59 ||
137 tm->tm_sec < 0 || tm->tm_sec > 60) {
138 log_warn(
LD_BUG,
"Out-of-range argument to tor_timegm");
141 days = 365 * (year-1970) +
n_leapdays(1970,(
int)year);
142 for (i = 0; i < tm->tm_mon; ++i)
146 days += tm->tm_mday - 1;
147 hours = days*24 + tm->tm_hour;
149 minutes = hours*60 + tm->tm_min;
150 seconds = minutes*60 + tm->tm_sec;
155 #if SIZEOF_TIME_T < 8
156 if (seconds < TIME_MIN || seconds > TIME_MAX) {
157 log_warn(
LD_BUG,
"Result does not fit in tor_timegm");
161 *time_out = (time_t)seconds;
169 {
"Sun",
"Mon",
"Tue",
"Wed",
"Thu",
"Fri",
"Sat" };
172 {
"Jan",
"Feb",
"Mar",
"Apr",
"May",
"Jun",
173 "Jul",
"Aug",
"Sep",
"Oct",
"Nov",
"Dec" };
188 strftime(buf, RFC1123_TIME_LEN+1,
"___, %d ___ %Y %H:%M:%S GMT", &tm);
211 int i, m, invalid_year;
212 unsigned tm_mday, tm_year, tm_hour, tm_min, tm_sec;
215 if (strlen(buf) != RFC1123_TIME_LEN)
217 memset(&tm, 0,
sizeof(tm));
218 if (
tor_sscanf(buf,
"%3s, %2u %3s %u %2u:%2u:%2u GMT", weekday,
219 &tm_mday, month, &tm_year, &tm_hour,
220 &tm_min, &tm_sec) < 7) {
222 log_warn(
LD_GENERAL,
"Got invalid RFC1123 time %s", esc);
228 for (i = 0; i < 12; ++i) {
236 log_warn(
LD_GENERAL,
"Got invalid RFC1123 time %s: No such month", esc);
242 invalid_year = (tm_year >= INT32_MAX || tm_year < 1970);
245 if (m == 1 && !invalid_year &&
IS_LEAPYEAR(tm_year)) {
249 if (invalid_year || tm_mday < 1 || tm_mday > dpm ||
250 tm_hour > 23 || tm_min > 59 || tm_sec > 60) {
252 log_warn(
LD_GENERAL,
"Got invalid RFC1123 time %s", esc);
256 tm.tm_mday = (int)tm_mday;
257 tm.tm_year = (int)tm_year;
258 tm.tm_hour = (int)tm_hour;
259 tm.tm_min = (int)tm_min;
260 tm.tm_sec = (int)tm_sec;
262 if (tm.tm_year < 1970) {
269 "Got invalid RFC1123 time %s. (Before 1970)", esc);
288 strftime(buf, ISO_TIME_LEN+1,
"%Y-%m-%d %H:%M:%S",
tor_localtime_r(&t, &tm));
298 strftime(buf, ISO_TIME_LEN+1,
"%Y-%m-%d %H:%M:%S",
tor_gmtime_r(&t, &tm));
327 tor_snprintf(buf+ISO_TIME_LEN, 8,
".%06d", (
int)tv->tv_usec);
339 unsigned int year=0, month=0, day=0, hour=0, minute=0, second=0;
341 char extra_char, separator_char;
342 n_fields =
tor_sscanf(cp,
"%u-%2u-%2u%c%2u:%2u:%2u%c",
345 &hour, &minute, &second, &extra_char);
346 if (strict ? (n_fields != 7) : (n_fields < 7)) {
348 log_warn(
LD_GENERAL,
"ISO time %s was unparseable", esc);
352 if (separator_char != (nospace ?
'T' :
' ')) {
354 log_warn(
LD_GENERAL,
"ISO time %s was unparseable", esc);
358 if (year < 1970 || month < 1 || month > 12 || day < 1 || day > 31 ||
359 hour > 23 || minute > 59 || second > 60 || year >= INT32_MAX) {
361 log_warn(
LD_GENERAL,
"ISO time %s was nonsensical", esc);
365 st_tm.tm_year = (int)year-1900;
366 st_tm.tm_mon = month-1;
368 st_tm.tm_hour = hour;
369 st_tm.tm_min = minute;
370 st_tm.tm_sec = second;
373 if (st_tm.tm_year < 70) {
379 log_warn(
LD_GENERAL,
"Got invalid ISO time %s. (Before 1970)", esc);
415 unsigned tm_mday, tm_year, tm_hour, tm_min, tm_sec;
418 memset(tm, 0,
sizeof(*tm));
421 if ((cp = strchr(date,
','))) {
426 if (
tor_sscanf(cp,
"%2u %3s %4u %2u:%2u:%2u GMT",
427 &tm_mday, month, &tm_year,
428 &tm_hour, &tm_min, &tm_sec) == 6) {
431 }
else if (
tor_sscanf(cp,
"%2u-%3s-%2u %2u:%2u:%2u GMT",
432 &tm_mday, month, &tm_year,
433 &tm_hour, &tm_min, &tm_sec) == 6) {
440 if (
tor_sscanf(date,
"%3s %3s %2u %2u:%2u:%2u %4u",
441 wkday, month, &tm_mday,
442 &tm_hour, &tm_min, &tm_sec, &tm_year) == 7) {
448 tm->tm_mday = (int)tm_mday;
449 tm->tm_year = (int)tm_year;
450 tm->tm_hour = (int)tm_hour;
451 tm->tm_min = (int)tm_min;
452 tm->tm_sec = (int)tm_sec;
459 for (i = 0; i < 12; ++i) {
465 if (tm->tm_year < 0 ||
466 tm->tm_mon < 0 || tm->tm_mon > 11 ||
467 tm->tm_mday < 1 || tm->tm_mday > 31 ||
468 tm->tm_hour < 0 || tm->tm_hour > 23 ||
469 tm->tm_min < 0 || tm->tm_min > 59 ||
470 tm->tm_sec < 0 || tm->tm_sec > 60)
484 long sec = 0, min = 0, hour = 0, day = 0;
487 if (interval < -LONG_MAX)
489 else if (interval < 0)
490 interval = -interval;
492 if (interval >= 86400) {
493 day = interval / 86400;
496 if (interval >= 3600) {
497 hour = interval / 3600;
500 if (interval >= 60) {
507 return tor_snprintf(out, out_len,
"%ld days, %ld hours, %ld minutes",
510 return tor_snprintf(out, out_len,
"%ld hours, %ld minutes", hour, min);
512 return tor_snprintf(out, out_len,
"%ld minutes, %ld seconds", min, sec);
char * esc_for_log(const char *s)
Headers for util_malloc.c.
int tor_snprintf(char *str, size_t size, const char *format,...)
int tor_sscanf(const char *buf, const char *pattern,...)
Definitions for timing-related constants.
static const char * MONTH_NAMES[]
void format_iso_time_nospace(char *buf, time_t t)
void format_rfc1123_time(char *buf, time_t t)
int parse_iso_time(const char *cp, time_t *t)
int parse_http_time(const char *date, struct tm *tm)
void format_iso_time_nospace_usec(char *buf, const struct timeval *tv)
void format_iso_time(char *buf, time_t t)
int parse_iso_time_(const char *cp, time_t *t, int strict, int nospace)
static const char * WEEKDAY_NAMES[]
void format_local_iso_time_nospace(char *buf, time_t t)
int parse_rfc1123_time(const char *buf, time_t *t)
void format_local_iso_time(char *buf, time_t t)
static const int days_per_month[]
int tor_timegm(const struct tm *tm, time_t *time_out)
struct tm * tor_gmtime_r(const time_t *timep, struct tm *result)
static int n_leapdays(int year1, int year2)
int format_time_interval(char *out, size_t out_len, long interval)
int parse_iso_time_nospace(const char *cp, time_t *t)
struct tm * tor_localtime_r(const time_t *timep, struct tm *result)
struct tm * tor_localtime_r_msg(const time_t *timep, struct tm *result, char **err_out)
struct tm * tor_gmtime_r_msg(const time_t *timep, struct tm *result, char **err_out)
Macros to manage assertions, fatal and non-fatal.
#define tor_assert_nonfatal_unreached()