| 1 | /************************************************* |
| 2 | * Exim - an Internet mail transport agent * |
| 3 | *************************************************/ |
| 4 | |
| 5 | /* Copyright (c) University of Cambridge 1995 - 2014 */ |
| 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 +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 | time_t now; |
| 56 | struct tm *t; |
| 57 | |
| 58 | if (type == tod_epoch_l) |
| 59 | { |
| 60 | struct timeval tv; |
| 61 | gettimeofday(&tv, NULL); |
| 62 | /* Unix epoch/usec format */ |
| 63 | (void) sprintf(CS timebuf, TIME_T_FMT "%06ld", tv.tv_sec, (long) tv.tv_usec ); |
| 64 | return timebuf; |
| 65 | } |
| 66 | |
| 67 | now = time(NULL); |
| 68 | |
| 69 | /* Vary log type according to timezone requirement */ |
| 70 | |
| 71 | if (type == tod_log) type = log_timezone? tod_log_zone : tod_log_bare; |
| 72 | |
| 73 | /* Styles that don't need local time */ |
| 74 | |
| 75 | else if (type == tod_epoch) |
| 76 | { |
| 77 | (void) sprintf(CS timebuf, TIME_T_FMT, now); /* Unix epoch format */ |
| 78 | return timebuf; /* NB the above will be wrong if time_t is FP */ |
| 79 | } |
| 80 | |
| 81 | else if (type == tod_zulu) |
| 82 | { |
| 83 | t = gmtime(&now); |
| 84 | (void) sprintf(CS timebuf, "%04d%02d%02d%02d%02d%02dZ", |
| 85 | 1900 + t->tm_year, 1 + t->tm_mon, t->tm_mday, t->tm_hour, t->tm_min, |
| 86 | t->tm_sec); |
| 87 | return timebuf; |
| 88 | } |
| 89 | |
| 90 | /* Convert to local time or UTC */ |
| 91 | |
| 92 | t = timestamps_utc? gmtime(&now) : localtime(&now); |
| 93 | |
| 94 | switch(type) |
| 95 | { |
| 96 | case tod_log_bare: /* Format used in logging without timezone */ |
| 97 | (void) sprintf(CS timebuf, "%04d-%02d-%02d %02d:%02d:%02d", |
| 98 | 1900 + t->tm_year, 1 + t->tm_mon, t->tm_mday, |
| 99 | t->tm_hour, t->tm_min, t->tm_sec); |
| 100 | break; |
| 101 | |
| 102 | /* Format used as suffix of log file name when 'log_datestamp' is active. For |
| 103 | testing purposes, it changes the file every second. */ |
| 104 | |
| 105 | #ifdef TESTING_LOG_DATESTAMP |
| 106 | case tod_log_datestamp_daily: |
| 107 | case tod_log_datestamp_monthly: |
| 108 | (void) sprintf(CS timebuf, "%04d%02d%02d%02d%02d", |
| 109 | 1900 + t->tm_year, 1 + t->tm_mon, t->tm_mday, t->tm_hour, t->tm_min); |
| 110 | break; |
| 111 | |
| 112 | #else |
| 113 | case tod_log_datestamp_daily: |
| 114 | (void) sprintf(CS timebuf, "%04d%02d%02d", |
| 115 | 1900 + t->tm_year, 1 + t->tm_mon, t->tm_mday); |
| 116 | break; |
| 117 | |
| 118 | case tod_log_datestamp_monthly: |
| 119 | (void) sprintf(CS timebuf, "%04d%02d", |
| 120 | 1900 + t->tm_year, 1 + t->tm_mon); |
| 121 | break; |
| 122 | #endif |
| 123 | |
| 124 | /* Format used in BSD inbox separator lines. Sort-of documented in RFC 976 |
| 125 | ("UUCP Mail Interchange Format Standard") but only by example, not by |
| 126 | explicit definition. The examples show no timezone offsets, and some MUAs |
| 127 | appear to be sensitive to this, so Exim has been changed to remove the |
| 128 | timezone offsets that originally appeared. */ |
| 129 | |
| 130 | case tod_bsdin: |
| 131 | { |
| 132 | int len = Ustrftime(timebuf, sizeof(timebuf), "%a %b %d %H:%M:%S", t); |
| 133 | Ustrftime(timebuf + len, sizeof(timebuf) - len, " %Y", t); |
| 134 | } |
| 135 | break; |
| 136 | |
| 137 | /* Other types require the GMT offset to be calculated, or just set up in the |
| 138 | case of UTC timestamping. We need to take a copy of the local time first. */ |
| 139 | |
| 140 | default: |
| 141 | { |
| 142 | int diff_hour, diff_min; |
| 143 | struct tm local; |
| 144 | memcpy(&local, t, sizeof(struct tm)); |
| 145 | |
| 146 | if (timestamps_utc) |
| 147 | { |
| 148 | diff_hour = diff_min = 0; |
| 149 | } |
| 150 | else |
| 151 | { |
| 152 | struct tm *gmt = gmtime(&now); |
| 153 | diff_min = 60*(local.tm_hour - gmt->tm_hour) + local.tm_min - gmt->tm_min; |
| 154 | if (local.tm_year != gmt->tm_year) |
| 155 | diff_min += (local.tm_year > gmt->tm_year)? 1440 : -1440; |
| 156 | else if (local.tm_yday != gmt->tm_yday) |
| 157 | diff_min += (local.tm_yday > gmt->tm_yday)? 1440 : -1440; |
| 158 | diff_hour = diff_min/60; |
| 159 | diff_min = abs(diff_min - diff_hour*60); |
| 160 | } |
| 161 | |
| 162 | switch(type) |
| 163 | { |
| 164 | case tod_log_zone: /* Format used in logging with timezone */ |
| 165 | (void) sprintf(CS timebuf, "%04d-%02d-%02d %02d:%02d:%02d %+03d%02d", |
| 166 | 1900 + local.tm_year, 1 + local.tm_mon, local.tm_mday, |
| 167 | local.tm_hour, local.tm_min, local.tm_sec, |
| 168 | diff_hour, diff_min); |
| 169 | break; |
| 170 | |
| 171 | case tod_zone: /* Just the timezone offset */ |
| 172 | (void) sprintf(CS timebuf, "%+03d%02d", diff_hour, diff_min); |
| 173 | break; |
| 174 | |
| 175 | /* tod_mbx: format used in MBX mailboxes - subtly different to tod_full */ |
| 176 | |
| 177 | #ifdef SUPPORT_MBX |
| 178 | case tod_mbx: |
| 179 | { |
| 180 | int len; |
| 181 | (void) sprintf(CS timebuf, "%02d-", local.tm_mday); |
| 182 | len = Ustrlen(timebuf); |
| 183 | len += Ustrftime(timebuf + len, sizeof(timebuf) - len, "%b-%Y %H:%M:%S", |
| 184 | &local); |
| 185 | (void) sprintf(CS timebuf + len, " %+03d%02d", diff_hour, diff_min); |
| 186 | } |
| 187 | break; |
| 188 | #endif |
| 189 | |
| 190 | /* tod_full: format used in Received: headers (use as default just in case |
| 191 | called with a junk type value) */ |
| 192 | |
| 193 | default: |
| 194 | { |
| 195 | int len = Ustrftime(timebuf, sizeof(timebuf), "%a, ", &local); |
| 196 | (void) sprintf(CS timebuf + len, "%02d ", local.tm_mday); |
| 197 | len += Ustrlen(timebuf + len); |
| 198 | len += Ustrftime(timebuf + len, sizeof(timebuf) - len, "%b %Y %H:%M:%S", |
| 199 | &local); |
| 200 | (void) sprintf(CS timebuf + len, " %+03d%02d", diff_hour, diff_min); |
| 201 | } |
| 202 | break; |
| 203 | } |
| 204 | } |
| 205 | break; |
| 206 | } |
| 207 | |
| 208 | return timebuf; |
| 209 | } |
| 210 | |
| 211 | /* End of tod.c */ |