10
10
* For extensions from SunOS, add SUNOS_EXT.
11
11
* For stuff needed to implement the P1003.2 date command, add POSIX2_DATE.
12
12
* For VMS dates, add VMS_EXT.
13
* For a an RFC822 time format, add MAILHEADER_EXT.
14
* For ISO week years, add ISO_DATE_EXT.
13
15
* For complete POSIX semantics, add POSIX_SEMANTICS.
15
* The code for %c, %x, and %X is my best guess as to what's "appropriate".
17
* The code for %c, %x, and %X now follows the 1003.2 specification for
16
19
* This version ignores LOCALE information.
17
20
* It also doesn't worry about multi-byte characters.
60
61
#define SUNOS_EXT 1 /* stuff in SunOS strftime routine */
61
62
#define POSIX2_DATE 1 /* stuff in Posix 1003.2 date command */
62
63
#define VMS_EXT 1 /* include %v for VMS date format */
64
#define MAILHEADER_EXT 1 /* add %z for HHMM format */
65
#define ISO_DATE_EXT 1 /* %G and %g for year of ISO week */
64
67
#define POSIX_SEMANTICS 1 /* call tzset() if TZ changes */
70
#if defined(ISO_DATE_EXT)
71
#if ! defined(POSIX2_DATE)
67
76
#if defined(POSIX2_DATE)
68
77
#if ! defined(SYSV_EXT)
283
306
case 'c': /* appropriate date and time representation */
284
sprintf(tbuf, "%s %s %2d %02d:%02d:%02d %d",
285
days_a[range(0, timeptr->tm_wday, 6)],
286
months_a[range(0, timeptr->tm_mon, 11)],
287
range(1, timeptr->tm_mday, 31),
288
range(0, timeptr->tm_hour, 23),
289
range(0, timeptr->tm_min, 59),
290
range(0, timeptr->tm_sec, 61),
291
timeptr->tm_year + 1900);
307
strftime(tbuf, sizeof tbuf, "%a %b %e %H:%M:%S %Y", timeptr);
294
310
case 'd': /* day of the month, 01 - 31 */
353
369
case 'x': /* appropriate date representation */
354
sprintf(tbuf, "%s %s %2d %d",
355
days_a[range(0, timeptr->tm_wday, 6)],
356
months_a[range(0, timeptr->tm_mon, 11)],
357
range(1, timeptr->tm_mday, 31),
358
timeptr->tm_year + 1900);
370
strftime(tbuf, sizeof tbuf, "%m/%d/%y", timeptr);
361
373
case 'X': /* appropriate time representation */
362
sprintf(tbuf, "%02d:%02d:%02d",
363
range(0, timeptr->tm_hour, 23),
364
range(0, timeptr->tm_min, 59),
365
range(0, timeptr->tm_sec, 61));
374
strftime(tbuf, sizeof tbuf, "%H:%M:%S", timeptr);
368
377
case 'y': /* year without a century, 00 - 99 */
374
383
sprintf(tbuf, "%d", 1900 + timeptr->tm_year);
386
#ifdef MAILHEADER_EXT
388
* From: Chip Rosenthal <chip@chinacat.unicom.com>
389
* Date: Sun, 19 Mar 1995 00:33:29 -0600 (CST)
391
* Warning: the %z [code] is implemented by inspecting the
392
* timezone name conditional compile settings, and
393
* inferring a method to get timezone offsets. I've tried
394
* this code on a couple of machines, but I don't doubt
395
* there is some system out there that won't like it.
396
* Maybe the easiest thing to do would be to bracket this
397
* with an #ifdef that can turn it off. The %z feature
398
* would be an admittedly obscure one that most folks can
399
* live without, but it would be a great help to those of
400
* us that muck around with various message processors.
402
case 'z': /* time zone offset east of GMT e.g. -0600 */
405
* Systems with tm_name probably have tm_tzadj as
406
* secs west of GMT. Convert to mins east of GMT.
408
off = -timeptr->tm_tzadj / 60;
409
#else /* !HAVE_TM_NAME */
412
* Systems with tm_zone probably have tm_gmtoff as
413
* secs east of GMT. Convert to mins east of GMT.
415
off = timeptr->tm_gmtoff / 60;
416
#else /* !HAVE_TM_ZONE */
419
* Systems with tzname[] probably have timezone as
420
* secs west of GMT. Convert to mins east of GMT.
422
off = -(daylight ? timezone : altzone) / 60;
423
#else /* !HAVE_TZNAME */
424
off = -zone.tz_minuteswest;
425
#endif /* !HAVE_TZNAME */
426
#endif /* !HAVE_TM_ZONE */
427
#endif /* !HAVE_TM_NAME */
434
sprintf(tbuf+1, "%02d%02d", off/60, off%60);
436
#endif /* MAILHEADER_EXT */
377
438
case 'Z': /* time zone name or abbrevation */
378
439
#ifdef HAVE_TZNAME
379
i = (daylight && timeptr->tm_isdst); /* 0 or 1 */
440
i = (daylight && timeptr->tm_isdst > 0); /* 0 or 1 */
380
441
strcpy(tbuf, tzname[i]);
382
443
#ifdef HAVE_TM_ZONE
383
444
strcpy(tbuf, timeptr->tm_zone);
447
strcpy(tbuf, timeptr->tm_name);
385
449
gettimeofday(& tv, & zone);
386
450
strcpy(tbuf, timezone(zone.tz_minuteswest,
451
timeptr->tm_isdst > 0));
452
#endif /* HAVE_TM_NAME */
453
#endif /* HAVE_TM_ZONE */
454
#endif /* HAVE_TZNAME */
464
529
case 'V': /* week of year according ISO 8601 */
465
#if defined(GAWK) && defined(VMS_EXT)
468
extern void warning();
469
static int warned = 0;
471
if (! warned && do_lint) {
474
"conversion %%V added in P1003.2; for VMS style date, use %%v");
478
530
sprintf(tbuf, "%02d", iso8601wknum(timeptr));
484
536
timeptr->tm_wday);
486
538
#endif /* POSIX2_DATE */
546
* If it's December but the ISO week number is one,
547
* that week is in next year.
548
* If it's January but the ISO week number is 52 or
549
* 53, that week is in last year.
550
* Otherwise, it's this year.
552
w = iso8601wknum(timeptr);
553
if (timeptr->tm_mon == 11 && w == 1)
554
y = 1900 + timeptr->tm_year + 1;
555
else if (timeptr->tm_mon == 0 && w >= 52)
556
y = 1900 + timeptr->tm_year - 1;
558
y = 1900 + timeptr->tm_year;
561
sprintf(tbuf, "%d", y);
563
sprintf(tbuf, "%02d", y % 100);
489
568
tbuf[1] = *format;
539
618
* If the week (Monday to Sunday) containing January 1
540
619
* has four or more days in the new year, then it is week 1;
541
620
* otherwise it is the highest numbered week of the previous
542
* (52 or 53) year, and the next week is week 1.
621
* year (52 or 53), and the next week is week 1.
544
623
* ADR: This means if Jan 1 was Monday through Thursday,
545
624
* it was week 1, otherwise week 52 or 53.
695
if (timeptr->tm_mon == 11) {
697
* The last week of the year
698
* can be in week 1 of next year.
701
* This can only happen if
709
wday = timeptr->tm_wday;
710
mday = timeptr->tm_mday;
711
if ( (wday == 1 && (mday >= 29 && mday <= 31))
712
|| (wday == 2 && (mday == 30 || mday == 31))
713
|| (wday == 3 && mday == 31))
749
851
"(%%w) day of week (0..6, Sunday == 0) %w",
750
852
"(%%x) appropriate locale date representation %x",
751
853
"(%%y) last two digits of year (00..99) %y",
854
"(%%z) timezone offset east of GMT as HHMM (e.g. -0500) %z",