time_t timegm( struct tm *t ) { time_t tl, tb; struct tm *tg; if ((tl = my_mktime( t )) == -1) return tl; tg = gmtime( &tl ); tg->tm_isdst = 0; if ((tb = my_mktime( tg )) == -1) return tb; return tl - (tb - tl); }
void datestamp(char *buf, int bufsize) { time_t now; int offset; time(&now); offset = my_mktime(localtime(&now)) - now; offset /= 60; date_string(now, offset, buf, bufsize); }
/* Gr. strptime is crap for this; it doesn't have a way to require RFC2822 (i.e. English) day/month names, and it doesn't work correctly with %z. */ int parse_date(const char *date, char *result, int maxlen) { struct tm tm; int offset, tm_gmt; time_t then; memset(&tm, 0, sizeof(tm)); tm.tm_year = -1; tm.tm_mon = -1; tm.tm_mday = -1; tm.tm_isdst = -1; offset = -1; tm_gmt = 0; for (;;) { int match = 0; unsigned char c = *date; /* Stop at end of string or newline */ if (!c || c == '\n') break; if (isalpha(c)) match = match_alpha(date, &tm, &offset); else if (isdigit(c)) match = match_digit(date, &tm, &offset, &tm_gmt); else if ((c == '-' || c == '+') && isdigit(date[1])) match = match_tz(date, &offset); if (!match) { /* BAD CRAP */ match = 1; } date += match; } /* mktime uses local timezone */ then = my_mktime(&tm); if (offset == -1) offset = (then - mktime(&tm)) / 60; if (then == -1) return -1; if (!tm_gmt) then -= offset * 60; return date_string(then, offset, result, maxlen); }
static int is_date(int year, int month, int day, struct tm *now_tm, time_t now, struct tm *tm) { if (month > 0 && month < 13 && day > 0 && day < 32) { struct tm check = *tm; struct tm *r = (now_tm ? &check : tm); time_t specified; r->tm_mon = month - 1; r->tm_mday = day; if (year == -1) { if (!now_tm) return 1; r->tm_year = now_tm->tm_year; } else if (year >= 1970 && year < 2100) r->tm_year = year - 1900; else if (year > 70 && year < 100) r->tm_year = year; else if (year < 38) r->tm_year = year + 100; else return 0; if (!now_tm) return 1; specified = my_mktime(r); /* Be it commit time or author time, it does not make * sense to specify timestamp way into the future. Make * sure it is not later than ten days from now... */ if (now + 10*24*3600 < specified) return 0; tm->tm_mon = r->tm_mon; tm->tm_mday = r->tm_mday; if (year != -1) tm->tm_year = r->tm_year; return 1; } return 0; }
/* * What value of "tz" was in effect back then at "time" in the * local timezone? */ static int local_tzoffset(unsigned long time) { time_t t, t_local; struct tm tm; int offset, eastwest; t = time; localtime_r(&t, &tm); t_local = my_mktime(&tm); if (t_local < t) { eastwest = -1; offset = t - t_local; } else { eastwest = 1; offset = t_local - t; } offset /= 60; /* in minutes */ offset = (offset % 60) + ((offset / 60) * 100); return offset * eastwest; }
int32 get_bin_time_second(unsigned char bintime[]) { return(my_mktime(bintime[5],bintime[4],bintime[3],bintime[2],bintime[1],bintime[0])); }