1 /* strftime - custom formatting of date and/or time
2 Copyright (C) 1989, 1991, 1992 Free Software Foundation, Inc.
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2, or (at your option)
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
18 /* Note: this version of strftime lacks locale support,
21 Performs `%' substitutions similar to those in printf. Except
22 where noted, substituted fields have a fixed size; numeric fields are
23 padded if necessary. Padding is with zeros by default; for fields
24 that display a single number, padding can be changed or inhibited by
25 following the `%' with one of the modifiers described below. Unknown
26 field specifiers are copied as normal characters. All other
27 characters are copied to the output without change.
29 Supports a superset of the ANSI C field specifiers.
31 Literal character fields:
36 Numeric modifiers (a nonstandard extension):
37 - do not pad the field
38 _ pad the field with spaces
47 %r time, 12-hour (hh:mm:ss [AP]M)
48 %R time, 24-hour (hh:mm)
50 %T time, 24-hour (hh:mm:ss)
51 %X locale's time representation (%H:%M:%S)
52 %Z time zone (EDT), or nothing if no time zone is determinable
55 %a locale's abbreviated weekday name (Sun..Sat)
56 %A locale's full weekday name, variable length (Sunday..Saturday)
57 %b locale's abbreviated month name (Jan..Dec)
58 %B locale's full month name, variable length (January..December)
59 %c locale's date and time (Sat Nov 04 12:02:33 EST 1989)
61 %d day of month (01..31)
62 %e day of month ( 1..31)
65 %j day of year (001..366)
67 %U week number of year with Sunday as first day of week (00..53)
69 %W week number of year with Monday as first day of week (00..53)
70 %x locale's date representation (mm/dd/yy)
71 %y last two digits of year (00..99)
74 David MacKenzie <djm@gnu.ai.mit.edu> */
76 #include <sys/types.h>
77 #if defined(TM_IN_SYS_TIME) || (!defined(HAVE_TM_ZONE) && !defined(HAVE_TZNAME))
83 #if defined(HAVE_TZNAME)
84 extern char *tzname[2];
87 /* Types of padding for numbers in date and time. */
93 static char const* const days[] =
95 "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"
98 static char const * const months[] =
100 "January", "February", "March", "April", "May", "June",
101 "July", "August", "September", "October", "November", "December"
104 /* Add character C to STRING and increment LENGTH,
105 unless LENGTH would exceed MAX. */
107 #define add_char(c) (length + 1 <= max) && (string[length++] = (c))
109 /* Add a 2 digit number to STRING, padding if specified.
110 Return the number of characters added, up to MAX. */
113 add_num2 (string, num, max, pad)
122 if (top == 0 && pad == blank)
124 else if (top != 0 || pad == zero)
125 add_char (top + '0');
126 add_char (num % 10 + '0');
130 /* Add a 3 digit number to STRING, padding if specified.
131 Return the number of characters added, up to MAX. */
134 add_num3 (string, num, max, pad)
141 int mid = (num - top * 100) / 10;
144 if (top == 0 && pad == blank)
146 else if (top != 0 || pad == zero)
147 add_char (top + '0');
148 if (mid == 0 && top == 0 && pad == blank)
150 else if (mid != 0 || top != 0 || pad == zero)
151 add_char (mid + '0');
152 add_char (num % 10 + '0');
156 /* Like strncpy except return the number of characters copied. */
159 add_str (to, from, max)
166 for (i = 0; from[i] && i <= max; ++i)
171 /* Return the week in the year of the time in TM, with the weeks
172 starting on Sundays. */
180 /* Set `dl' to the day in the year of the last day of the week previous
181 to the one containing the day specified in TM. If the day specified
182 in TM is in the first week of the year, `dl' will be negative or 0.
183 Otherwise, calculate the number of complete weeks before our week
184 (dl / 7) and add any partial week at the start of the year (dl % 7). */
185 dl = tm->tm_yday - tm->tm_wday;
186 return dl <= 0 ? 0 : dl / 7 + (dl % 7 != 0);
189 /* Return the week in the year of the time in TM, with the weeks
190 starting on Mondays. */
198 if (tm->tm_wday == 0)
201 wday = tm->tm_wday - 1;
202 dl = tm->tm_yday - wday;
203 return dl <= 0 ? 0 : dl / 7 + (dl % 7 != 0);
206 #if !defined(HAVE_TM_ZONE) && !defined(HAVE_TZNAME)
215 gettimeofday (&tv, &tz);
216 return timezone (tz.tz_minuteswest, tp->tm_isdst);
220 /* Format the time given in TM according to FORMAT, and put the
222 Return the number of characters (not including terminating null)
223 that were put into STRING, or 0 if the length would have
227 strftime (string, max, format, tm)
233 enum padding pad; /* Type of padding to apply. */
234 size_t length = 0; /* Characters put in STRING so far. */
236 for (; *format && length < max; ++format)
249 else if (*format == '_')
259 /* Literal character fields: */
278 add_num2 (&string[length], tm->tm_hour, max - length,
279 *format == 'H' ? pad : blank);
286 if (tm->tm_hour == 0)
288 else if (tm->tm_hour > 12)
289 hour12 = tm->tm_hour - 12;
291 hour12 = tm->tm_hour;
293 add_num2 (&string[length], hour12, max - length,
294 *format == 'I' ? pad : blank);
299 add_num2 (&string[length], tm->tm_min, max - length, pad);
302 if (tm->tm_hour < 12)
310 strftime (&string[length], max - length, "%I:%M:%S %p", tm);
314 strftime (&string[length], max - length, "%H:%M", tm);
318 add_num2 (&string[length], tm->tm_sec, max - length, pad);
322 strftime (&string[length], max - length, "%H:%M:%S", tm);
326 strftime (&string[length], max - length, "%H:%M:%S", tm);
330 length += add_str (&string[length], tm->tm_zone, max - length);
333 if (tm->tm_isdst && tzname[1] && *tzname[1])
334 length += add_str (&string[length], tzname[1], max - length);
336 length += add_str (&string[length], tzname[0], max - length);
338 length += add_str (&string[length], zone_name (tm), max - length);
345 add_char (days[tm->tm_wday][0]);
346 add_char (days[tm->tm_wday][1]);
347 add_char (days[tm->tm_wday][2]);
351 add_str (&string[length], days[tm->tm_wday], max - length);
355 add_char (months[tm->tm_mon][0]);
356 add_char (months[tm->tm_mon][1]);
357 add_char (months[tm->tm_mon][2]);
361 add_str (&string[length], months[tm->tm_mon], max - length);
365 strftime (&string[length], max - length,
366 "%a %b %d %H:%M:%S %Z %Y", tm);
370 add_num2 (&string[length], (tm->tm_year + 1900) / 100,
375 add_num2 (&string[length], tm->tm_mday, max - length, pad);
379 add_num2 (&string[length], tm->tm_mday, max - length, blank);
383 strftime (&string[length], max - length, "%m/%d/%y", tm);
387 add_num3 (&string[length], tm->tm_yday + 1, max - length, pad);
391 add_num2 (&string[length], tm->tm_mon + 1, max - length, pad);
395 add_num2 (&string[length], sun_week (tm), max - length, pad);
398 add_char (tm->tm_wday + '0');
402 add_num2 (&string[length], mon_week (tm), max - length, pad);
406 strftime (&string[length], max - length, "%m/%d/%y", tm);
410 add_num2 (&string[length], tm->tm_year % 100,
414 add_char ((tm->tm_year + 1900) / 1000 + '0');
416 add_num3 (&string[length],
417 (1900 + tm->tm_year) % 1000, max - length, zero);