int f_unix_time(ARG0) { time_t rtx, vtx; struct tm timeinfo; int year, month, day, hour, minute, second; if (mode >= 0) { reftime(sec, &year, &month, &day, &hour, &minute, &second); timeinfo.tm_year = year - 1900; timeinfo.tm_mon = month- 1; timeinfo.tm_mday = day; timeinfo.tm_hour = hour; timeinfo.tm_min = minute; timeinfo.tm_sec = second; rtx=my_timegm(&timeinfo); if (verftime(sec, &(timeinfo.tm_year), &(timeinfo.tm_mon), &(timeinfo.tm_mday), &(timeinfo.tm_hour), &(timeinfo.tm_min), &(timeinfo.tm_sec)) == 0) { timeinfo.tm_year -= 1900; timeinfo.tm_mon -= 1; vtx=my_timegm(&timeinfo); } else vtx=-1; sprintf(inv_out,"unix_rt=%d:unix_vt=%d",(int) rtx, (int) vtx); } return 0; }
//expect time_str as a friendly string format time_t get_time_from_str_as_gmt(char* time_str) { struct tm val_time_tm; time_t val_time_t; strptime(time_str, "%FT%T", &val_time_tm); val_time_tm.tm_isdst = -1; val_time_t = my_timegm(&val_time_tm); return val_time_t; }
static long timestamp_to_long(const char *buf) { struct tm tm; const char *b=NULL; if(!(b=strchr(buf, ' '))) return 0; memset(&tm, 0, sizeof(struct tm)); if(!strptime(b, " %Y-%m-%d %H:%M:%S", &tm)) return 0; return (long)my_timegm(&tm); }
time_t ParseXML::ParseAtomDate(const std::string& val) { struct my_tm tm; #if __STDC_WANT_SECURE_LIB__ if(sscanf_s #else if(sscanf #endif (val.c_str(), "%04d-%02d-%02dT%02d:%02d:%02d", &tm.tm_year, &tm.tm_mon, &tm.tm_mday, &tm.tm_hour, &tm.tm_min, &tm.tm_sec) != 6) throw std::runtime_error("Invalid Atom date format"); tm.tm_year -= 1900; tm.tm_mon -= 1; return my_timegm(&tm); }
/* reads the Date header field of msg return value: -1: error 0: Date header field does not exist 1: success dateHF must point to an array with at least MAX_TIME bytes */ static int getDate(char * dateHF, time_t * dateHFValue, struct sip_msg * msg) { struct hdr_field * date = NULL; struct tm tmDate; if(!dateHF || !dateHFValue || !msg) { LM_ERR("dateHF, dateHFValue or msg not set\n"); return -1; } date = get_header_by_static_name(msg, "Date"); if (!date) { return 0; } if(date->body.len >= MAX_TIME) { LM_ERR("Date header field to long\n"); return -1; } /* date->body.len < MAX_TIME */ strncpy(dateHF, date->body.s, date->body.len); dateHF[date->body.len] = '\0'; if(!strptime(dateHF, DATE_FORMAT, &tmDate)) { LM_ERR("error while parsing Date header field\n"); return -1; } /* covert struct tm to time_t */ *dateHFValue = my_timegm(&tmDate); if(*dateHFValue == -1) { LM_ERR("error while converting timestamp\n"); return -1; } return 1; }
/** * Converts ASN1 time string (in a char *) to time_t * (Use ASN1_STRING_data() to convert ASN1_GENERALIZEDTIME to char * if * necessary) */ time_t verify_asn1TimeToTimeT(const char *asn1time) { char zone; struct tm time_tm; size_t len; /* Make sure to start with an empty struct */ memset(&time_tm,0,sizeof(struct tm)); len = strlen(asn1time); if ((len != 13) && (len != 15)) return 0; /* dont understand */ if ((len == 13) && ((sscanf(asn1time, "%02d%02d%02d%02d%02d%02d%c", &(time_tm.tm_year), &(time_tm.tm_mon), &(time_tm.tm_mday), &(time_tm.tm_hour), &(time_tm.tm_min), &(time_tm.tm_sec), &zone) != 7) || (zone != 'Z'))) return 0; /* dont understand */ if ((len == 15) && ((sscanf(asn1time, "20%02d%02d%02d%02d%02d%02d%c", &(time_tm.tm_year), &(time_tm.tm_mon), &(time_tm.tm_mday), &(time_tm.tm_hour), &(time_tm.tm_min), &(time_tm.tm_sec), &zone) != 7) || (zone != 'Z'))) return 0; /* dont understand */ /* time format fixups */ if (time_tm.tm_year < 90) time_tm.tm_year += 100; --(time_tm.tm_mon); return my_timegm(&time_tm); }
int Curl_parsedate(const char *date, time_t *output) { time_t t = 0; int wdaynum=-1; /* day of the week number, 0-6 (mon-sun) */ int monnum=-1; /* month of the year number, 0-11 */ int mdaynum=-1; /* day of month, 1 - 31 */ int hournum=-1; int minnum=-1; int secnum=-1; int yearnum=-1; int tzoff=-1; struct my_tm tm; enum assume dignext = DATE_MDAY; const char *indate = date; /* save the original pointer */ int part = 0; /* max 6 parts */ while(*date && (part < 6)) { bool found=FALSE; skip(&date); if(ISALPHA(*date)) { /* a name coming up */ char buf[32]=""; size_t len; sscanf(date, "%31[ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz]", buf); len = strlen(buf); if(wdaynum == -1) { wdaynum = checkday(buf, len); if(wdaynum != -1) found = TRUE; } if(!found && (monnum == -1)) { monnum = checkmonth(buf); if(monnum != -1) found = TRUE; } if(!found && (tzoff == -1)) { /* this just must be a time zone string */ tzoff = checktz(buf); if(tzoff != -1) found = TRUE; } if(!found) return PARSEDATE_FAIL; /* bad string */ date += len; } else if(ISDIGIT(*date)) { /* a digit */ int val; char *end; if((secnum == -1) && (3 == sscanf(date, "%02d:%02d:%02d", &hournum, &minnum, &secnum))) { /* time stamp! */ date += 8; } else if((secnum == -1) && (2 == sscanf(date, "%02d:%02d", &hournum, &minnum))) { /* time stamp without seconds */ date += 5; secnum = 0; } else { val = curlx_sltosi(strtol(date, &end, 10)); if((tzoff == -1) && ((end - date) == 4) && (val <= 1400) && (indate< date) && ((date[-1] == '+' || date[-1] == '-'))) { /* four digits and a value less than or equal to 1400 (to take into account all sorts of funny time zone diffs) and it is preceeded with a plus or minus. This is a time zone indication. 1400 is picked since +1300 is frequently used and +1400 is mentioned as an edge number in the document "ISO C 200X Proposal: Timezone Functions" at http://david.tribble.com/text/c0xtimezone.html If anyone has a more authoritative source for the exact maximum time zone offsets, please speak up! */ found = TRUE; tzoff = (val/100 * 60 + val%100)*60; /* the + and - prefix indicates the local time compared to GMT, this we need ther reversed math to get what we want */ tzoff = date[-1]=='+'?-tzoff:tzoff; } if(((end - date) == 8) && (yearnum == -1) && (monnum == -1) && (mdaynum == -1)) { /* 8 digits, no year, month or day yet. This is YYYYMMDD */ found = TRUE; yearnum = val/10000; monnum = (val%10000)/100-1; /* month is 0 - 11 */ mdaynum = val%100; } if(!found && (dignext == DATE_MDAY) && (mdaynum == -1)) { if((val > 0) && (val<32)) { mdaynum = val; found = TRUE; } dignext = DATE_YEAR; } if(!found && (dignext == DATE_YEAR) && (yearnum == -1)) { yearnum = val; found = TRUE; if(yearnum < 1900) { if(yearnum > 70) yearnum += 1900; else yearnum += 2000; } if(mdaynum == -1) dignext = DATE_MDAY; } if(!found) return PARSEDATE_FAIL; date = end; } } part++; } if(-1 == secnum) secnum = minnum = hournum = 0; /* no time, make it zero */ if((-1 == mdaynum) || (-1 == monnum) || (-1 == yearnum)) /* lacks vital info, fail */ return PARSEDATE_FAIL; #if SIZEOF_TIME_T < 5 /* 32 bit time_t can only hold dates to the beginning of 2038 */ if(yearnum > 2037) { *output = 0x7fffffff; return PARSEDATE_LATER; } #endif if(yearnum < 1970) { *output = 0; return PARSEDATE_SOONER; } tm.tm_sec = secnum; tm.tm_min = minnum; tm.tm_hour = hournum; tm.tm_mday = mdaynum; tm.tm_mon = monnum; tm.tm_year = yearnum - 1900; /* my_timegm() returns a time_t. time_t is often 32 bits, even on many architectures that feature 64 bit 'long'. Some systems have 64 bit time_t and deal with years beyond 2038. However, even on some of the systems with 64 bit time_t mktime() returns -1 for dates beyond 03:14:07 UTC, January 19, 2038. (Such as AIX 5100-06) */ t = my_timegm(&tm); /* time zone adjust (cast t to int to compare to negative one) */ if(-1 != (int)t) { /* Add the time zone diff between local time zone and GMT. */ long delta = (long)(tzoff!=-1?tzoff:0); if((delta>0) && (t + delta < t)) return -1; /* time_t overflow */ t += delta; } *output = t; return PARSEDATE_OK; }
// ---- main ---- settings_t* settings_read_from_file(const char* file) { settings_t* rv = settings_init(); FILE* f; int line_counter= 0; int err; if (f=fopen(file, "r")) { char* buff = malloc(sizeof(char)*1025); char* val; while (!feof(f)) { // fscanf(f, "%s", buff); read_line(f, buff); line_counter++; if (buff[0]=='#' || strlen(buff)<3) continue; val = strchr(buff,'='); if (!val) { $msg("Error in file \"%s\" at line %d", file, line_counter); $msg("line:%s", buff); exit(1); } val[0] ='\0'; val++; if (err = settings_set_variable(buff, val, rv)) { $msg("Error in file \"%s\" at line %d", file, line_counter); $msg("Can't convert variable:%s", val); exit(1); } } free(buff); fclose(f); } if (rv->start<86401) { time_t now = time(0); struct tm* tm = gmtime(&now); tm->tm_hour = 0; tm->tm_min = 0; tm->tm_sec = 0; tm->tm_isdst = 0; rv->start += my_timegm(tm); } if (rv->stop<86401) { time_t now = time(0); struct tm* tm = gmtime(&now); tm->tm_hour = 0; tm->tm_min = 0; tm->tm_sec = 0; tm->tm_isdst = 0; rv->stop += my_timegm(tm); } while (rv->stop<rv->start) rv->stop+=86400; // search orb file if (rv->use_orb_file && !rv->orb_file) { DIR *dp; struct dirent *dirp; if((dp=opendir(".")) == 0) { printf("can't open current dir\n"); exit(1); } int idx = 0; int tmp_idx; while ((dirp = readdir(dp))!=0) { if (strlen(dirp->d_name)>3) { if (toupper(dirp->d_name[0]) == 'O' && toupper(dirp->d_name[1]) == 'R' && toupper(dirp->d_name[2]) == 'B' && dirp->d_name[3] >= '0' && dirp->d_name[3] <= '9') { tmp_idx = atoi(&(dirp->d_name[3])); if (!rv->orb_file || tmp_idx > idx) { sfree(rv->orb_file); rv->orb_file = strdup(dirp->d_name); idx = tmp_idx; } } } } closedir(dp); } // check orb file exist if (rv->use_orb_file) { if (rv->orb_file) { if (access(rv->orb_file, F_OK) == -1) { rv->use_orb_file = 0; $msg("Orb file \"%s\" does not exist", rv->orb_file); } } else { $msg("%s", "Can't find orb file on current directory"); rv->use_orb_file = 0; } } return rv; }
static int parsedate(const char *date, time_t *output) { time_t t = 0; int wdaynum = -1; /* day of the week number, 0-6 (mon-sun) */ int monnum = -1; /* month of the year number, 0-11 */ int mdaynum = -1; /* day of month, 1 - 31 */ int hournum = -1; int minnum = -1; int secnum = -1; int yearnum = -1; int tzoff = -1; struct my_tm tm; enum assume dignext = DATE_MDAY; const char *indate = date; /* save the original pointer */ int part = 0; /* max 6 parts */ while(*date && (part < 6)) { bool found = FALSE; skip(&date); if(ISALPHA(*date)) { /* a name coming up */ char buf[32]=""; size_t len; if(sscanf(date, "%31[ABCDEFGHIJKLMNOPQRSTUVWXYZ" "abcdefghijklmnopqrstuvwxyz]", buf)) len = strlen(buf); else len = 0; if(wdaynum == -1) { wdaynum = checkday(buf, len); if(wdaynum != -1) found = TRUE; } if(!found && (monnum == -1)) { monnum = checkmonth(buf); if(monnum != -1) found = TRUE; } if(!found && (tzoff == -1)) { /* this just must be a time zone string */ tzoff = checktz(buf); if(tzoff != -1) found = TRUE; } if(!found) return PARSEDATE_FAIL; /* bad string */ date += len; } else if(ISDIGIT(*date)) { /* a digit */ int val; char *end; int len = 0; if((secnum == -1) && (3 == sscanf(date, "%02d:%02d:%02d%n", &hournum, &minnum, &secnum, &len))) { /* time stamp! */ date += len; } else if((secnum == -1) && (2 == sscanf(date, "%02d:%02d%n", &hournum, &minnum, &len))) { /* time stamp without seconds */ date += len; secnum = 0; } else { long lval; int error; int old_errno; old_errno = errno; errno = 0; lval = strtol(date, &end, 10); error = errno; if(errno != old_errno) errno = old_errno; if(error) return PARSEDATE_FAIL; #if LONG_MAX != INT_MAX if((lval > (long)INT_MAX) || (lval < (long)INT_MIN)) return PARSEDATE_FAIL; #endif val = curlx_sltosi(lval); if((tzoff == -1) && ((end - date) == 4) && (val <= 1400) && (indate< date) && ((date[-1] == '+' || date[-1] == '-'))) { /* four digits and a value less than or equal to 1400 (to take into account all sorts of funny time zone diffs) and it is preceded with a plus or minus. This is a time zone indication. 1400 is picked since +1300 is frequently used and +1400 is mentioned as an edge number in the document "ISO C 200X Proposal: Timezone Functions" at http://david.tribble.com/text/c0xtimezone.html If anyone has a more authoritative source for the exact maximum time zone offsets, please speak up! */ found = TRUE; tzoff = (val/100 * 60 + val%100)*60; /* the + and - prefix indicates the local time compared to GMT, this we need their reversed math to get what we want */ tzoff = date[-1]=='+'?-tzoff:tzoff; } if(((end - date) == 8) && (yearnum == -1) && (monnum == -1) && (mdaynum == -1)) { /* 8 digits, no year, month or day yet. This is YYYYMMDD */ found = TRUE; yearnum = val/10000; monnum = (val%10000)/100-1; /* month is 0 - 11 */ mdaynum = val%100; } if(!found && (dignext == DATE_MDAY) && (mdaynum == -1)) { if((val > 0) && (val<32)) { mdaynum = val; found = TRUE; } dignext = DATE_YEAR; } if(!found && (dignext == DATE_YEAR) && (yearnum == -1)) { yearnum = val; found = TRUE; if(yearnum < 100) { if(yearnum > 70) yearnum += 1900; else yearnum += 2000; } if(mdaynum == -1) dignext = DATE_MDAY; } if(!found) return PARSEDATE_FAIL; date = end; } } part++; } if(-1 == secnum) secnum = minnum = hournum = 0; /* no time, make it zero */ if((-1 == mdaynum) || (-1 == monnum) || (-1 == yearnum)) /* lacks vital info, fail */ return PARSEDATE_FAIL; #ifdef HAVE_TIME_T_UNSIGNED if(yearnum < 1970) { /* only positive numbers cannot return earlier */ *output = TIME_T_MIN; return PARSEDATE_SOONER; } #endif #if (SIZEOF_TIME_T < 5) #ifdef HAVE_TIME_T_UNSIGNED /* an unsigned 32 bit time_t can only hold dates to 2106 */ if(yearnum > 2105) { *output = TIME_T_MAX; return PARSEDATE_LATER; } #else /* a signed 32 bit time_t can only hold dates to the beginning of 2038 */ if(yearnum > 2037) { *output = TIME_T_MAX; return PARSEDATE_LATER; } if(yearnum < 1903) { *output = TIME_T_MIN; return PARSEDATE_SOONER; } #endif #else /* The Gregorian calendar was introduced 1582 */ if(yearnum < 1583) return PARSEDATE_FAIL; #endif if((mdaynum > 31) || (monnum > 11) || (hournum > 23) || (minnum > 59) || (secnum > 60)) return PARSEDATE_FAIL; /* clearly an illegal date */ tm.tm_sec = secnum; tm.tm_min = minnum; tm.tm_hour = hournum; tm.tm_mday = mdaynum; tm.tm_mon = monnum; tm.tm_year = yearnum; /* my_timegm() returns a time_t. time_t is often 32 bits, sometimes even on architectures that feature 64 bit 'long' but ultimately time_t is the correct data type to use. */ my_timegm(&tm, &t); /* Add the time zone diff between local time zone and GMT. */ if(tzoff == -1) tzoff = 0; if((tzoff > 0) && (t > TIME_T_MAX - tzoff)) { *output = TIME_T_MAX; return PARSEDATE_LATER; /* time_t overflow */ } t += tzoff; *output = t; return PARSEDATE_OK; }