SWITCH_DECLARE(switch_status_t) switch_time_exp_tz_name(const char *tz, switch_time_exp_t *tm, switch_time_t thetime) { struct tm xtm = { 0 }; const char *tz_name = tz; const char *tzdef; time_t timep; if (!thetime) { thetime = switch_micro_time_now(); } timep = (thetime) / (int64_t) (1000000); if (!zstr(tz_name)) { tzdef = switch_lookup_timezone(tz_name); } else { /* We set the default timezone to GMT. */ tz_name = "GMT"; tzdef = "GMT"; } if (tzdef) { /* The lookup of the zone may fail. */ tztime(&timep, tzdef, &xtm); tm2switchtime(&xtm, tm); return SWITCH_STATUS_SUCCESS; } return SWITCH_STATUS_FALSE; }
int main(int argc, char *argv[]) { char buf[BUFSIZ], c; char *fmt = "%c"; struct tm *now = NULL; struct tm *(*tztime)(const time_t *) = localtime; const char *tz = "local"; time_t t; t = time(NULL); while((c = getopt(argc, argv, "d:u")) != -1) switch(c) { case 'd': t = estrtol(optarg, 0); break; case 'u': tztime = gmtime; tz = "gm"; break; default: exit(EXIT_FAILURE); } if(optind < argc && argv[optind][0] == '+') fmt = &argv[optind][1]; if(!(now = tztime(&t))) eprintf("%stime failed\n", tz); strftime(buf, sizeof buf, fmt, now); puts(buf); return EXIT_SUCCESS; }
SWITCH_DECLARE(switch_status_t) switch_strftime_tz(const char *tz, const char *format, char *date, size_t len, switch_time_t thetime) { time_t timep; const char *tz_name = tz; const char *tzdef; switch_size_t retsize; struct tm tm = { 0 }; switch_time_exp_t stm; if (!thetime) { thetime = switch_micro_time_now(); } timep = (thetime) / (int64_t) (1000000); if (!zstr(tz_name)) { tzdef = switch_lookup_timezone(tz_name); } else { /* We set the default timezone to GMT. */ tz_name = "GMT"; tzdef = "GMT"; } if (tzdef) { /* The lookup of the zone may fail. */ tztime(&timep, tzdef, &tm); tm2switchtime(&tm, &stm); switch_strftime_nocheck(date, &retsize, len, zstr(format) ? "%Y-%m-%d %T" : format, &stm); if (!zstr_buf(date)) { return SWITCH_STATUS_SUCCESS; } } return SWITCH_STATUS_FALSE; }