| 1 | /************************************************* |
| 2 | * Exim - an Internet mail transport agent * |
| 3 | *************************************************/ |
| 4 | |
| 5 | /* Copyright (c) University of Cambridge 1995 - 2018 */ |
| 6 | /* See the file NOTICE for conditions of use and distribution. */ |
| 7 | |
| 8 | /* A function for returning the time of day in various formats */ |
| 9 | |
| 10 | |
| 11 | #include "exim.h" |
| 12 | |
| 13 | /* #define TESTING_LOG_DATESTAMP */ |
| 14 | |
| 15 | |
| 16 | static uschar timebuf[sizeof("www, dd-mmm-yyyy hh:mm:ss.ddd +zzzz")]; |
| 17 | |
| 18 | |
| 19 | /************************************************* |
| 20 | * Return timestamp * |
| 21 | *************************************************/ |
| 22 | |
| 23 | /* The log timestamp format is dd-mmm-yy so as to be non-confusing on both |
| 24 | sides of the Atlantic. We calculate an explicit numerical offset from GMT for |
| 25 | the full datestamp and BSD inbox datestamp. Note that on some systems |
| 26 | localtime() and gmtime() re-use the same store, so we must save the local time |
| 27 | values before calling gmtime(). If timestamps_utc is set, don't use |
| 28 | localtime(); all times are then in UTC (with offset +0000). |
| 29 | |
| 30 | There are also some contortions to get the day of the month without |
| 31 | a leading zero for the full stamp, since Ustrftime() doesn't provide this |
| 32 | option. |
| 33 | |
| 34 | Argument: type of timestamp required: |
| 35 | tod_bsdin BSD inbox format |
| 36 | tod_epoch Unix epoch format |
| 37 | tod_epochl Unix epoch/usec format |
| 38 | tod_full full date and time |
| 39 | tod_log log file data line format, |
| 40 | with zone if log_timezone is TRUE |
| 41 | tod_log_bare always without zone |
| 42 | tod_log_datestamp_daily for log file names when datestamped daily |
| 43 | tod_log_datestamp_monthly for log file names when datestamped monthly |
| 44 | tod_log_zone always with zone |
| 45 | tod_mbx MBX inbox format |
| 46 | tod_zone just the timezone offset |
| 47 | tod_zulu time in 8601 zulu format |
| 48 | |
| 49 | Returns: pointer to fixed buffer containing the timestamp |
| 50 | */ |
| 51 | |
| 52 | uschar * |
| 53 | tod_stamp(int type) |
| 54 | { |
| 55 | struct timeval now; |
| 56 | struct tm * t; |
| 57 | |
| 58 | gettimeofday(&now, NULL); |
| 59 | |
| 60 | /* Styles that don't need local time */ |
| 61 | |
| 62 | switch(type) |
| 63 | { |
| 64 | case tod_epoch: |
| 65 | (void) sprintf(CS timebuf, TIME_T_FMT, now.tv_sec); /* Unix epoch format */ |
| 66 | return timebuf; /* NB the above will be wrong if time_t is FP */ |
| 67 | |
| 68 | case tod_epoch_l: |
| 69 | /* Unix epoch/usec format */ |
| 70 | (void) sprintf(CS timebuf, TIME_T_FMT "%06ld", now.tv_sec, (long) now.tv_usec ); |
| 71 | return timebuf; |
| 72 | |
| 73 | case tod_zulu: |
| 74 | t = gmtime(&now.tv_sec); |
| 75 | (void) sprintf(CS timebuf, "%04d%02d%02d%02d%02d%02dZ", |
| 76 | 1900 + t->tm_year, 1 + t->tm_mon, t->tm_mday, t->tm_hour, t->tm_min, |
| 77 | t->tm_sec); |
| 78 | return timebuf; |
| 79 | } |
| 80 | |
| 81 | /* Vary log type according to timezone requirement */ |
| 82 | |
| 83 | if (type == tod_log) type = log_timezone ? tod_log_zone : tod_log_bare; |
| 84 | |
| 85 | /* Convert to local time or UTC */ |
| 86 | |
| 87 | t = timestamps_utc ? gmtime(&now.tv_sec) : localtime(&now.tv_sec); |
| 88 | |
| 89 | switch(type) |
| 90 | { |
| 91 | case tod_log_bare: /* Format used in logging without timezone */ |
| 92 | #ifndef COMPILE_UTILITY |
| 93 | if (LOGGING(millisec)) |
| 94 | sprintf(CS timebuf, "%04d-%02d-%02d %02d:%02d:%02d.%03d", |
| 95 | 1900 + t->tm_year, 1 + t->tm_mon, t->tm_mday, |
| 96 | t->tm_hour, t->tm_min, t->tm_sec, (int)(now.tv_usec/1000)); |
| 97 | else |
| 98 | #endif |
| 99 | sprintf(CS timebuf, "%04d-%02d-%02d %02d:%02d:%02d", |
| 100 | 1900 + t->tm_year, 1 + t->tm_mon, t->tm_mday, |
| 101 | t->tm_hour, t->tm_min, t->tm_sec); |
| 102 | |
| 103 | break; |
| 104 | |
| 105 | /* Format used as suffix of log file name when 'log_datestamp' is active. For |
| 106 | testing purposes, it changes the file every second. */ |
| 107 | |
| 108 | #ifdef TESTING_LOG_DATESTAMP |
| 109 | case tod_log_datestamp_daily: |
| 110 | case tod_log_datestamp_monthly: |
| 111 | sprintf(CS timebuf, "%04d%02d%02d%02d%02d", |
| 112 | 1900 + t->tm_year, 1 + t->tm_mon, t->tm_mday, t->tm_hour, t->tm_min); |
| 113 | break; |
| 114 | |
| 115 | #else |
| 116 | case tod_log_datestamp_daily: |
| 117 | sprintf(CS timebuf, "%04d%02d%02d", |
| 118 | 1900 + t->tm_year, 1 + t->tm_mon, t->tm_mday); |
| 119 | break; |
| 120 | |
| 121 | case tod_log_datestamp_monthly: |
| 122 | #ifndef COMPILE_UTILITY |
| 123 | sprintf(CS timebuf, "%04d%02d", |
| 124 | 1900 + t->tm_year, 1 + t->tm_mon); |
| 125 | #endif |
| 126 | break; |
| 127 | #endif |
| 128 | |
| 129 | /* Format used in BSD inbox separator lines. Sort-of documented in RFC 976 |
| 130 | ("UUCP Mail Interchange Format Standard") but only by example, not by |
| 131 | explicit definition. The examples show no timezone offsets, and some MUAs |
| 132 | appear to be sensitive to this, so Exim has been changed to remove the |
| 133 | timezone offsets that originally appeared. */ |
| 134 | |
| 135 | case tod_bsdin: |
| 136 | { |
| 137 | int len = Ustrftime(timebuf, sizeof(timebuf), "%a %b %d %H:%M:%S", t); |
| 138 | Ustrftime(timebuf + len, sizeof(timebuf) - len, " %Y", t); |
| 139 | } |
| 140 | break; |
| 141 | |
| 142 | /* Other types require the GMT offset to be calculated, or just set up in the |
| 143 | case of UTC timestamping. We need to take a copy of the local time first. */ |
| 144 | |
| 145 | default: |
| 146 | { |
| 147 | int diff_hour, diff_min; |
| 148 | struct tm local; |
| 149 | memcpy(&local, t, sizeof(struct tm)); |
| 150 | |
| 151 | if (timestamps_utc) |
| 152 | diff_hour = diff_min = 0; |
| 153 | else |
| 154 | { |
| 155 | struct tm * gmt = gmtime(&now.tv_sec); |
| 156 | |
| 157 | diff_min = 60*(local.tm_hour - gmt->tm_hour) + local.tm_min - gmt->tm_min; |
| 158 | if (local.tm_year != gmt->tm_year) |
| 159 | diff_min += (local.tm_year > gmt->tm_year)? 1440 : -1440; |
| 160 | else if (local.tm_yday != gmt->tm_yday) |
| 161 | diff_min += (local.tm_yday > gmt->tm_yday)? 1440 : -1440; |
| 162 | diff_hour = diff_min/60; |
| 163 | diff_min = abs(diff_min - diff_hour*60); |
| 164 | } |
| 165 | |
| 166 | switch(type) |
| 167 | { |
| 168 | case tod_log_zone: /* Format used in logging with timezone */ |
| 169 | #ifndef COMPILE_UTILITY |
| 170 | if (LOGGING(millisec)) |
| 171 | (void) sprintf(CS timebuf, |
| 172 | "%04d-%02d-%02d %02d:%02d:%02d.%03d %+03d%02d", |
| 173 | 1900 + local.tm_year, 1 + local.tm_mon, local.tm_mday, |
| 174 | local.tm_hour, local.tm_min, local.tm_sec, (int)(now.tv_usec/1000), |
| 175 | diff_hour, diff_min); |
| 176 | else |
| 177 | #endif |
| 178 | (void) sprintf(CS timebuf, |
| 179 | "%04d-%02d-%02d %02d:%02d:%02d %+03d%02d", |
| 180 | 1900 + local.tm_year, 1 + local.tm_mon, local.tm_mday, |
| 181 | local.tm_hour, local.tm_min, local.tm_sec, |
| 182 | diff_hour, diff_min); |
| 183 | break; |
| 184 | |
| 185 | case tod_zone: /* Just the timezone offset */ |
| 186 | (void) sprintf(CS timebuf, "%+03d%02d", diff_hour, diff_min); |
| 187 | break; |
| 188 | |
| 189 | /* tod_mbx: format used in MBX mailboxes - subtly different to tod_full */ |
| 190 | |
| 191 | #ifdef SUPPORT_MBX |
| 192 | case tod_mbx: |
| 193 | { |
| 194 | int len; |
| 195 | (void) sprintf(CS timebuf, "%02d-", local.tm_mday); |
| 196 | len = Ustrlen(timebuf); |
| 197 | len += Ustrftime(timebuf + len, sizeof(timebuf) - len, "%b-%Y %H:%M:%S", |
| 198 | &local); |
| 199 | (void) sprintf(CS timebuf + len, " %+03d%02d", diff_hour, diff_min); |
| 200 | } |
| 201 | break; |
| 202 | #endif |
| 203 | |
| 204 | /* tod_full: format used in Received: headers (use as default just in case |
| 205 | called with a junk type value) */ |
| 206 | |
| 207 | default: |
| 208 | { |
| 209 | int len = Ustrftime(timebuf, sizeof(timebuf), "%a, ", &local); |
| 210 | (void) sprintf(CS timebuf + len, "%02d ", local.tm_mday); |
| 211 | len += Ustrlen(timebuf + len); |
| 212 | len += Ustrftime(timebuf + len, sizeof(timebuf) - len, "%b %Y %H:%M:%S", |
| 213 | &local); |
| 214 | (void) sprintf(CS timebuf + len, " %+03d%02d", diff_hour, diff_min); |
| 215 | } |
| 216 | break; |
| 217 | } |
| 218 | } |
| 219 | break; |
| 220 | } |
| 221 | |
| 222 | return timebuf; |
| 223 | } |
| 224 | |
| 225 | /* End of tod.c */ |