Convert the given 'second' unix timestamp, into a date-time string in the UTC time zone if 'utc' is true, or the local time zone if it is false. The returned string is of the form yyy-MM-dd HH::mm::SS. Note that for time points before the Unix epoch, 'subsecond' might have a negative value. In this case 'second' has to be adjusted.
| 58 | // Note that for time points before the Unix epoch, 'subsecond' might have a negative |
| 59 | // value. In this case 'second' has to be adjusted. |
| 60 | static string FormatSecond(time_t second, int64_t subsecond, bool utc) { |
| 61 | char buf[256]; |
| 62 | struct tm tmp; |
| 63 | auto input_time = (subsecond < 0) ? second - 1 : second; |
| 64 | |
| 65 | // gcc 4.9 does not support C++14 get_time and put_time functions, so we're |
| 66 | // stuck with strftime() for now. |
| 67 | if (utc) { |
| 68 | strftime(buf, sizeof(buf), "%F %T", gmtime_r(&input_time, &tmp)); |
| 69 | } else { |
| 70 | strftime(buf, sizeof(buf), "%F %T", localtime_r(&input_time, &tmp)); |
| 71 | } |
| 72 | return string(buf); |
| 73 | } |
| 74 | |
| 75 | // Format the sub-second part of a time point, at the precision specified by 'p'. The |
| 76 | // returned string is meant to be appended to the string returned by FormatSecond() |