| 286 | } |
| 287 | |
| 288 | const char *show_date(timestamp_t time, int tz, struct date_mode mode) |
| 289 | { |
| 290 | struct tm *tm; |
| 291 | struct tm tmbuf = { 0 }; |
| 292 | struct tm human_tm = { 0 }; |
| 293 | int human_tz = -1; |
| 294 | static struct strbuf timebuf = STRBUF_INIT; |
| 295 | |
| 296 | if (mode.type == DATE_UNIX) { |
| 297 | strbuf_reset(&timebuf); |
| 298 | strbuf_addf(&timebuf, "%"PRItime, time); |
| 299 | return timebuf.buf; |
| 300 | } |
| 301 | |
| 302 | if (mode.type == DATE_HUMAN) { |
| 303 | struct timeval now; |
| 304 | |
| 305 | get_time(&now); |
| 306 | |
| 307 | /* Fill in the data for "current time" in human_tz and human_tm */ |
| 308 | human_tz = local_time_tzoffset(now.tv_sec, &human_tm); |
| 309 | } |
| 310 | |
| 311 | if (mode.local) |
| 312 | tz = local_tzoffset(time); |
| 313 | |
| 314 | if (mode.type == DATE_RAW) { |
| 315 | strbuf_reset(&timebuf); |
| 316 | strbuf_addf(&timebuf, "%"PRItime" %+05d", time, tz); |
| 317 | return timebuf.buf; |
| 318 | } |
| 319 | |
| 320 | if (mode.type == DATE_RELATIVE) { |
| 321 | strbuf_reset(&timebuf); |
| 322 | show_date_relative(time, &timebuf); |
| 323 | return timebuf.buf; |
| 324 | } |
| 325 | |
| 326 | if (mode.local) |
| 327 | tm = time_to_tm_local(time, &tmbuf); |
| 328 | else |
| 329 | tm = time_to_tm(time, tz, &tmbuf); |
| 330 | if (!tm) { |
| 331 | tm = time_to_tm(0, 0, &tmbuf); |
| 332 | tz = 0; |
| 333 | } |
| 334 | |
| 335 | strbuf_reset(&timebuf); |
| 336 | if (mode.type == DATE_SHORT) |
| 337 | strbuf_addf(&timebuf, "%04d-%02d-%02d", tm->tm_year + 1900, |
| 338 | tm->tm_mon + 1, tm->tm_mday); |
| 339 | else if (mode.type == DATE_ISO8601) |
| 340 | strbuf_addf(&timebuf, "%04d-%02d-%02d %02d:%02d:%02d %+05d", |
| 341 | tm->tm_year + 1900, |
| 342 | tm->tm_mon + 1, |
| 343 | tm->tm_mday, |
| 344 | tm->tm_hour, tm->tm_min, tm->tm_sec, |
| 345 | tz); |