/* this one will parse dates of the form: * month|day|hour|minute|sec* (2 chars each) * and year is given. Returns a time_t date. */ time_t _gnutls_x509_time2gtime (const char *ttime, int year) { char xx[3]; struct fake_tm etime; time_t ret; if (strlen (ttime) < 8) { gnutls_assert (); return (time_t) - 1; } etime.tm_year = year; /* In order to work with 32 bit * time_t. */ if (sizeof (time_t) <= 4 && etime.tm_year >= 2038) return (time_t) 2145914603; /* 2037-12-31 23:23:23 */ xx[2] = 0; /* get the month */ memcpy (xx, ttime, 2); /* month */ etime.tm_mon = atoi (xx) - 1; ttime += 2; /* get the day */ memcpy (xx, ttime, 2); /* day */ etime.tm_mday = atoi (xx); ttime += 2; /* get the hour */ memcpy (xx, ttime, 2); /* hour */ etime.tm_hour = atoi (xx); ttime += 2; /* get the minutes */ memcpy (xx, ttime, 2); /* minutes */ etime.tm_min = atoi (xx); ttime += 2; if (strlen (ttime) >= 2) { memcpy (xx, ttime, 2); etime.tm_sec = atoi (xx); ttime += 2; } else etime.tm_sec = 0; ret = mktime_utc (&etime); return ret; }
static time_t decode_internaldate (const char *in) { const char *inptr = in; int hour, min, sec, n; struct tm tm; time_t date; char *buf; memset ((void *) &tm, 0, sizeof (struct tm)); tm.tm_mday = strtoul (inptr, &buf, 10); if (buf == inptr || *buf != '-') return (time_t) -1; inptr = buf + 1; if (inptr[3] != '-') return (time_t) -1; for (n = 0; n < 12; n++) { if (!g_ascii_strncasecmp (inptr, tm_months[n], 3)) break; } if (n >= 12) return (time_t) -1; tm.tm_mon = n; inptr += 4; n = strtoul (inptr, &buf, 10); if (buf == inptr || *buf != ' ') return (time_t) -1; tm.tm_year = n - 1900; inptr = buf + 1; if (!decode_time (&inptr, &hour, &min, &sec)) return (time_t) -1; tm.tm_hour = hour; tm.tm_min = min; tm.tm_sec = sec; n = strtol (inptr, NULL, 10); date = mktime_utc (&tm); /* date is now GMT of the time we want, but not offset by the timezone ... */ /* this should convert the time to the GMT equiv time */ date -= ((n / 100) * 60 * 60) + (n % 100) * 60; return date; }
void convert_date(time_t * t, const char * dte) { static regex_t date_re; static int init_re; #define MAX_MATCH 16 size_t nmatch = MAX_MATCH; regmatch_t match[MAX_MATCH]; if (!init_re) { if (regcomp(&date_re, "([0-9]{4})[-/]([0-9]{2})[-/]([0-9]{2}) ([0-9]{2}):([0-9]{2}):([0-9]{2})( [-+][0-9]{4})?", REG_EXTENDED)) if (regcomp(&date_re, "([0-9]{4})[-/]([0-9]{2})[-/]([0-9]{2}) ([0-9]{2}):([0-9]{2}):([0-9]{2})", REG_EXTENDED)) { fprintf(stderr, "FATAL: date regex compilation error\n"); exit(1); } init_re = 1; } if (regexec(&date_re, dte, nmatch, match, 0) == 0) { regmatch_t * pm = match; struct tm tm = {0}; char tzbuf[32]; int offseth = 0, offsetm = 0; /* first regmatch_t is match location of entire re */ pm++; tm.tm_year = get_int_substr(dte, pm++); tm.tm_mon = get_int_substr(dte, pm++); tm.tm_mday = get_int_substr(dte, pm++); tm.tm_hour = get_int_substr(dte, pm++); tm.tm_min = get_int_substr(dte, pm++); tm.tm_sec = get_int_substr(dte, pm++); offseth = -get_int_substr(dte, pm++); offsetm= offseth%100; if(offsetm<0) offsetm*=-1; offseth/=100; snprintf(tzbuf, sizeof(tzbuf), "UTC%+d:%d", offseth, offsetm); tm.tm_year -= 1900; tm.tm_mon--; *t = mktime_utc(&tm, tzbuf); } else { *t = atoi(dte); } }
time_t predict_from_julian(predict_julian_date_t date) { double seconds_since = date*NUM_SECONDS_IN_DAY; time_t ret_time = get_julian_start_day(); //add number of seconds since julian start day to the julian start day, get current time_t struct tm timeinfo; gmtime_r(&ret_time, &timeinfo); timeinfo.tm_sec += seconds_since; ret_time = mktime_utc(&timeinfo); return ret_time; }
/** * Helper function for getting the Julian day start date (1979-12-31 00:00 UTC) as time_t. * * \return Internally defined Julian start date (fixed) **/ time_t get_julian_start_day() { struct tm start_time; start_time.tm_sec = 0; start_time.tm_min = 0; start_time.tm_hour = 0; start_time.tm_mday = 31; start_time.tm_mon = 11; start_time.tm_year = 1979-1900; start_time.tm_isdst = 0; return mktime_utc(&start_time); }
bool SDLAppSettings::parseDateTime(const std::string& datetime, time_t& timestamp) { int timezone_offset = 0; Regex timestamp_regex("^(\\d{4})-(\\d{2})-(\\d{2})(?: (\\d{1,2}):(\\d{2})(?::(\\d{2}))?)?(?: ([+-])(\\d{1,2}))?$"); std::vector<std::string> results; if(!timestamp_regex.match(datetime, &results) || results.size() < 3) return false; struct tm timeinfo; memset(&timeinfo, 0, sizeof(timeinfo)); timeinfo.tm_isdst = -1; timeinfo.tm_year = atoi(results[0].c_str()) - 1900; timeinfo.tm_mon = atoi(results[1].c_str()) - 1; timeinfo.tm_mday = atoi(results[2].c_str()); // optional: hours, minutes and seconds if(results.size() >= 5) { timeinfo.tm_hour = atoi(results[3].c_str()); timeinfo.tm_min = atoi(results[4].c_str()); if(results.size() >= 6) { timeinfo.tm_sec = atoi(results[5].c_str()); } } // optional: timezone (optional) if(results.size() >= 8) { int tz_hour = atoi(results[7].c_str()); int tz_min = 0; if(results.size() >= 9) { tz_min = atoi(results[8].c_str()); } int tz_offset = tz_hour * 3600 + tz_min * 60; if(results[6] == "-") { tz_offset = -tz_offset; } timestamp = mktime_utc(&timeinfo); timestamp -= tz_offset; } else { timestamp = mktime(&timeinfo); } return true; }
static int parse_created_on_time(time_t *tp, const char *d) { int year; int month; int day; int hour; int min; int sec; if(sscanf(d, "%d-%d-%dT%d:%d:%dZ", &year, &month, &day, &hour, &min, &sec) != 6) return -1; return mktime_utc(tp, year, month-1, day, hour, min, sec); }
/** * g_time_val_from_iso8601: * @iso_date: an ISO 8601 encoded date string * @time_: a #GTimeVal * * Converts a string containing an ISO 8601 encoded date and time * to a #GTimeVal and puts it into @time_. * * Return value: %TRUE if the conversion was successful. * * Since: 2.12 */ gboolean g_time_val_from_iso8601 (const gchar *iso_date, GTimeVal *time_) { struct tm tm; long val; g_return_val_if_fail (iso_date != NULL, FALSE); g_return_val_if_fail (time_ != NULL, FALSE); /* Ensure that the first character is a digit, * the first digit of the date, otherwise we don't * have an ISO 8601 date */ while (g_ascii_isspace (*iso_date)) iso_date++; if (*iso_date == '\0') return FALSE; if (!g_ascii_isdigit (*iso_date) && *iso_date != '-' && *iso_date != '+') return FALSE; val = strtoul (iso_date, (char **)&iso_date, 10); if (*iso_date == '-') { /* YYYY-MM-DD */ tm.tm_year = val - 1900; iso_date++; tm.tm_mon = strtoul (iso_date, (char **)&iso_date, 10) - 1; if (*iso_date++ != '-') return FALSE; tm.tm_mday = strtoul (iso_date, (char **)&iso_date, 10); } else { /* YYYYMMDD */ tm.tm_mday = val % 100; tm.tm_mon = (val % 10000) / 100 - 1; tm.tm_year = val / 10000 - 1900; } if (*iso_date++ != 'T') return FALSE; val = strtoul (iso_date, (char **)&iso_date, 10); if (*iso_date == ':') { /* hh:mm:ss */ tm.tm_hour = val; iso_date++; tm.tm_min = strtoul (iso_date, (char **)&iso_date, 10); if (*iso_date++ != ':') return FALSE; tm.tm_sec = strtoul (iso_date, (char **)&iso_date, 10); } else { /* hhmmss */ tm.tm_sec = val % 100; tm.tm_min = (val % 10000) / 100; tm.tm_hour = val / 10000; } time_->tv_sec = mktime_utc (&tm); time_->tv_usec = 0; if (*iso_date == ',' || *iso_date == '.') { glong mul = 100000; while (g_ascii_isdigit (*++iso_date)) { time_->tv_usec += (*iso_date - '0') * mul; mul /= 10; } } if (*iso_date == '+' || *iso_date == '-') { gint sign = (*iso_date == '+') ? -1 : 1; val = strtoul (iso_date + 1, (char **)&iso_date, 10); if (*iso_date == ':') val = 60 * val + strtoul (iso_date + 1, (char **)&iso_date, 10); else val = 60 * (val / 100) + (val % 100); time_->tv_sec += (time_t) (60 * val * sign); } else if (*iso_date++ != 'Z') return FALSE; while (g_ascii_isspace (*iso_date)) iso_date++; return *iso_date == '\0'; }