Tidying and compiler-silencing
[exim.git] / src / src / tod.c
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, "%04u%02u%02u%02u%02u%02uZ",
76       1900 + (uint)t->tm_year, 1 + (uint)t->tm_mon, (uint)t->tm_mday, (uint)t->tm_hour, (uint)t->tm_min,
77       (uint)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 = f.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, "%04u-%02u-%02u %02u:%02u:%02u.%03u",
95         1900 + (uint)t->tm_year, 1 + (uint)t->tm_mon, (uint)t->tm_mday,
96         (uint)t->tm_hour, (uint)t->tm_min, (uint)t->tm_sec,
97         (uint)(now.tv_usec/1000));
98     else
99 #endif
100       sprintf(CS timebuf, "%04u-%02u-%02u %02u:%02u:%02u",
101         1900 + (uint)t->tm_year, 1 + (uint)t->tm_mon, (uint)t->tm_mday,
102         (uint)t->tm_hour, (uint)t->tm_min, (uint)t->tm_sec);
103
104     break;
105
106     /* Format used as suffix of log file name when 'log_datestamp' is active. For
107     testing purposes, it changes the file every second. */
108
109 #ifdef TESTING_LOG_DATESTAMP
110   case tod_log_datestamp_daily:
111   case tod_log_datestamp_monthly:
112     sprintf(CS timebuf, "%04u%02u%02u%02u%02u",
113       1900 + (uint)t->tm_year, 1 + (uint)t->tm_mon, (uint)t->tm_mday,
114       (uint)t->tm_hour, (uint)t->tm_min);
115     break;
116
117 #else
118   case tod_log_datestamp_daily:
119     sprintf(CS timebuf, "%04u%02u%02u",
120       1900 + (uint)t->tm_year, 1 + (uint)t->tm_mon, (uint)t->tm_mday);
121     break;
122
123   case tod_log_datestamp_monthly:
124 #ifndef COMPILE_UTILITY
125     sprintf(CS timebuf, "%04u%02u",
126       1900 + (uint)t->tm_year, 1 + (uint)t->tm_mon);
127 #endif
128     break;
129 #endif
130
131     /* Format used in BSD inbox separator lines. Sort-of documented in RFC 976
132     ("UUCP Mail Interchange Format Standard") but only by example, not by
133     explicit definition. The examples show no timezone offsets, and some MUAs
134     appear to be sensitive to this, so Exim has been changed to remove the
135     timezone offsets that originally appeared. */
136
137   case tod_bsdin:
138       {
139       int len = Ustrftime(timebuf, sizeof(timebuf), "%a %b %d %H:%M:%S", t);
140       Ustrftime(timebuf + len, sizeof(timebuf) - len, " %Y", t);
141       }
142     break;
143
144     /* Other types require the GMT offset to be calculated, or just set up in the
145     case of UTC timestamping. We need to take a copy of the local time first. */
146
147   default:
148       {
149       int diff_hour, diff_min;
150       struct tm local;
151       memcpy(&local, t, sizeof(struct tm));
152
153       if (f.timestamps_utc)
154         diff_hour = diff_min = 0;
155       else
156         {
157         struct tm * gmt = gmtime(&now.tv_sec);
158
159         diff_min = 60*(local.tm_hour - gmt->tm_hour) + local.tm_min - gmt->tm_min;
160         if (local.tm_year != gmt->tm_year)
161           diff_min += (local.tm_year > gmt->tm_year)? 1440 : -1440;
162         else if (local.tm_yday != gmt->tm_yday)
163           diff_min += (local.tm_yday > gmt->tm_yday)? 1440 : -1440;
164         diff_hour = diff_min/60;
165         diff_min  = abs(diff_min - diff_hour*60);
166         }
167
168       switch(type)
169         {
170         case tod_log_zone:          /* Format used in logging with timezone */
171 #ifndef COMPILE_UTILITY
172           if (LOGGING(millisec))
173             (void) sprintf(CS timebuf,
174               "%04u-%02u-%02u %02u:%02u:%02u.%03u %+03d%02d",
175               1900 + (uint)local.tm_year, 1 + (uint)local.tm_mon, (uint)local.tm_mday,
176               (uint)local.tm_hour, (uint)local.tm_min, (uint)local.tm_sec, (uint)(now.tv_usec/1000),
177               diff_hour, diff_min);
178           else
179 #endif
180             (void) sprintf(CS timebuf,
181               "%04u-%02u-%02u %02u:%02u:%02u %+03d%02d",
182               1900 + (uint)local.tm_year, 1 + (uint)local.tm_mon, (uint)local.tm_mday,
183               (uint)local.tm_hour, (uint)local.tm_min, (uint)local.tm_sec,
184               diff_hour, diff_min);
185           break;
186
187         case tod_zone:              /* Just the timezone offset */
188           (void) sprintf(CS timebuf, "%+03d%02d", diff_hour, diff_min);
189           break;
190
191         /* tod_mbx: format used in MBX mailboxes - subtly different to tod_full */
192
193           #ifdef SUPPORT_MBX
194         case tod_mbx:
195             {
196             int len;
197             (void) sprintf(CS timebuf, "%02u-", (uint)local.tm_mday);
198             len = Ustrlen(timebuf);
199             len += Ustrftime(timebuf + len, sizeof(timebuf) - len, "%b-%Y %H:%M:%S",
200               &local);
201             (void) sprintf(CS timebuf + len, " %+03d%02d", diff_hour, diff_min);
202             }
203           break;
204           #endif
205
206         /* tod_full: format used in Received: headers (use as default just in case
207         called with a junk type value) */
208
209         default:
210             {
211             int len = Ustrftime(timebuf, sizeof(timebuf), "%a, ", &local);
212             (void) sprintf(CS timebuf + len, "%02u ", (uint)local.tm_mday);
213             len += Ustrlen(timebuf + len);
214             len += Ustrftime(timebuf + len, sizeof(timebuf) - len, "%b %Y %H:%M:%S",
215               &local);
216             (void) sprintf(CS timebuf + len, " %+03d%02d", diff_hour, diff_min);
217             }
218           break;
219         }
220       }
221     break;
222   }
223
224 return timebuf;
225 }
226
227 /* End of tod.c */