home *** CD-ROM | disk | FTP | other *** search
Text File | 1993-05-01 | 29.1 KB | 1,183 lines |
- Newsgroups: comp.sources.unix
- From: arnold@skeeve.atl.ga.us (Arnold Robbins)
- Subject: v26i208: strftime-3.0 - still more strftime(3) and date(1), Part01/01
- Sender: unix-sources-moderator@efficacy.home.vix.com
- Approved: WhoAmI@efficacy.home.vix.com
-
- Submitted-By: arnold@skeeve.atl.ga.us (Arnold Robbins)
- Posting-Number: Volume 26, Issue 208
- Archive-Name: strftime-3.0/part01
-
- Due to the impending release of 4.4 BSD, I had to do some work on gawk. As
- part of that I updated my strftime+date package. Please post this ASAP,
- instead of 10 months from now... (:-)
-
- Thanks,
-
- arnold@skeeve.atl.ga.us (Arnold D. Robbins)
-
- [ imagine the nerve of some people :-)... --vix ]
-
- #! /bin/sh
- # This is a shell archive. Remove anything before this line, then unpack
- # it by saving it into a file and typing "sh file". To overwrite existing
- # files, type "sh file -c". You can also feed this as standard input via
- # unshar, or by typing "sh <file", e.g.. If this archive is complete, you
- # will see the following message at the end:
- # "End of archive 1 (of 1)."
- # Contents: MANIFEST Makefile README date.1 date.c strftime.3
- # strftime.c
- # Wrapped by vixie@efficacy.home.vix.com on Sun May 2 22:39:05 1993
- PATH=/bin:/usr/bin:/usr/ucb ; export PATH
- if test -f 'MANIFEST' -a "${1}" != "-c" ; then
- echo shar: Will not clobber existing file \"'MANIFEST'\"
- else
- echo shar: Extracting \"'MANIFEST'\" \(331 characters\)
- sed "s/^X//" >'MANIFEST' <<'END_OF_FILE'
- X File Name Archive # Description
- X-----------------------------------------------------------
- X MANIFEST 1 This shipping list
- X Makefile 1
- X README 1
- X date.1 1
- X date.c 1
- X strftime.3 1
- X strftime.c 1
- END_OF_FILE
- if test 331 -ne `wc -c <'MANIFEST'`; then
- echo shar: \"'MANIFEST'\" unpacked with wrong size!
- fi
- # end of 'MANIFEST'
- fi
- if test -f 'Makefile' -a "${1}" != "-c" ; then
- echo shar: Will not clobber existing file \"'Makefile'\"
- else
- echo shar: Extracting \"'Makefile'\" \(195 characters\)
- sed "s/^X//" >'Makefile' <<'END_OF_FILE'
- X# Makefile for PD date and strftime
- X
- XSRCS= date.c strftime.c
- XOBJS= date.o strftime.o
- XDOCS= date.1 strftime.3
- X
- Xdate: $(OBJS)
- X $(CC) $(CFLAGS) $(OBJS) -o $@
- X
- Xdate.o: date.c
- X
- Xstrftime.o: strftime.c
- END_OF_FILE
- if test 195 -ne `wc -c <'Makefile'`; then
- echo shar: \"'Makefile'\" unpacked with wrong size!
- fi
- # end of 'Makefile'
- fi
- if test -f 'README' -a "${1}" != "-c" ; then
- echo shar: Will not clobber existing file \"'README'\"
- else
- echo shar: Extracting \"'README'\" \(447 characters\)
- sed "s/^X//" >'README' <<'END_OF_FILE'
- XSun May 2 22:08:49 EDT 1993
- X
- XThis package implements the Posix 1003.2 date command, as a wrapper around
- Xan extended version of the ANSI strftime(3) library routine.
- XEverything in it is public domain.
- X
- XArnold Robbins -- The Basement Computer | Laundry increases
- XInternet: arnold@skeeve.ATL.GA.US | exponentially in the
- XUUCP: { gatech, emory }!skeeve!arnold | number of children.
- XBitnet: Forget it. Get on a real network. | -- Miriam Robbins
- END_OF_FILE
- if test 447 -ne `wc -c <'README'`; then
- echo shar: \"'README'\" unpacked with wrong size!
- fi
- # end of 'README'
- fi
- if test -f 'date.1' -a "${1}" != "-c" ; then
- echo shar: Will not clobber existing file \"'date.1'\"
- else
- echo shar: Extracting \"'date.1'\" \(1342 characters\)
- sed "s/^X//" >'date.1' <<'END_OF_FILE'
- X.TH DATE 1
- X.SH NAME
- Xdate \- write the date and time
- X.SH SYNOPSIS
- X.B date
- X[
- X.B \-u
- X] [
- X.RI + format
- X]
- X.SH DESCRIPTION
- X.I Date
- Xwrites the current date and time to the standard output.
- XIt is intended to be compliant with draft 11 of the Posix
- X1003.2 Command Language and Utilities standard.
- XTherefore, it is purposely
- X.I not
- Xusable by the super-user for setting the system time.
- X.LP
- X.I Date
- Xaccepts one option:
- X.TP
- X.B \-u
- XPerform operations as if the
- X.B TZ
- Xenvironment variable was set to
- X.BR GMT0 .
- X.LP
- XIf an argument to
- X.I date
- Xis given that begins with a ``+'',
- Xthen the output is controlled by the contents of the rest of
- Xthe string. Normal text is output unmodified, while field descriptors
- Xin the format string are substituted for.
- X.LP
- XThe
- X.I date
- Xprogram is essentially a wrapper around
- X.IR strftime (3);
- Xsee there for a description of the available formatting options.
- X.LP
- XIf no format string is given, or if it does not begin with a ``+'',
- Xthen the default format of \fB"%a %b %e %H:%M:%S %Z %Y"\fR will
- Xbe used. This produces the traditional style of output, such as
- X``Sun Mar 17 10:32:47 EST 1991''.
- X.SH SEE ALSO
- Xtime(2), strftime(3), localtime(3)
- X.SH BUGS
- XThis version only works for the POSIX locale.
- X.SH AUTHOR
- X.nf
- XArnold Robbins
- X.sp
- XINTERNET: arnold@skeeve.atl.ga.us
- XUUCP: emory!skeeve!arnold
- XPhone: +1 404 248 9324
- X.fi
- END_OF_FILE
- if test 1342 -ne `wc -c <'date.1'`; then
- echo shar: \"'date.1'\" unpacked with wrong size!
- fi
- # end of 'date.1'
- fi
- if test -f 'date.c' -a "${1}" != "-c" ; then
- echo shar: Will not clobber existing file \"'date.c'\"
- else
- echo shar: Extracting \"'date.c'\" \(1044 characters\)
- sed "s/^X//" >'date.c' <<'END_OF_FILE'
- X/*
- X * date.c
- X *
- X * Public domain implementation of Posix 1003.2 Draft 11
- X * date command. Lets strftime() do the dirty work.
- X */
- X
- X#include <stdio.h>
- X#include <sys/types.h>
- X#include <time.h>
- X
- Xextern char *malloc();
- Xextern size_t strftime();
- Xextern int getopt();
- Xextern int optind;
- X
- Xint
- Xmain(argc, argv)
- Xint argc;
- Xchar **argv;
- X{
- X time_t clock;
- X struct tm *now;
- X int c, size, ret;
- X char *defhow = "%a %b %e %H:%M:%S %Z %Y";
- X char *howto = defhow;
- X char *buf;
- X
- X while ((c = getopt(argc, argv, "u")) != -1)
- X switch (c) {
- X case 'u':
- X putenv("TZ=GMT0");
- X break;
- X default:
- X fprintf(stderr, "usage: %s [-u] [+format_str]\n",
- X argv[0]);
- X exit(1);
- X }
- X
- X time(& clock);
- X now = localtime(& clock);
- X
- X if (optind < argc && argv[optind][0] == '+')
- X howto = & argv[optind][1];
- X
- X size = strlen(howto) * 10;
- X if ((buf = malloc(size)) == NULL) {
- X perror("not enough memory");
- X exit(1);
- X }
- X
- X ret = strftime(buf, size, howto, now);
- X if (ret != 0)
- X printf("%s\n", buf);
- X else {
- X fprintf(stderr, "conversion failed\n");
- X exit(1);
- X }
- X
- X exit(0);
- X}
- END_OF_FILE
- if test 1044 -ne `wc -c <'date.c'`; then
- echo shar: \"'date.c'\" unpacked with wrong size!
- fi
- # end of 'date.c'
- fi
- if test -f 'strftime.3' -a "${1}" != "-c" ; then
- echo shar: Will not clobber existing file \"'strftime.3'\"
- else
- echo shar: Extracting \"'strftime.3'\" \(7705 characters\)
- sed "s/^X//" >'strftime.3' <<'END_OF_FILE'
- X.TH STRFTIME 3
- X.SH NAME
- Xstrftime \- generate formatted time information
- X.SH SYNOPSIS
- X.ft B
- X.nf
- X#include <sys/types.h>
- X#include <time.h>
- X.sp
- Xsize_t strftime(char *s, size_t maxsize, const char *format,
- X const struct tm *timeptr);
- X.SH DESCRIPTION
- XThe following description is transcribed verbatim from the December 7, 1988
- Xdraft standard for ANSI C.
- XThis draft is essentially identical in technical content
- Xto the final version of the standard.
- X.LP
- XThe
- X.B strftime
- Xfunction places characters into the array pointed to by
- X.B s
- Xas controlled by the string pointed to by
- X.BR format .
- XThe format shall be a multibyte character sequence, beginning and ending in
- Xits initial shift state.
- XThe
- X.B format
- Xstring consists of zero or more conversion specifiers and ordinary
- Xmultibyte characters. A conversion specifier consists of a
- X.B %
- Xcharacter followed by a character that determines the behavior of the
- Xconversion specifier.
- XAll ordinary multibyte characters (including the terminating null
- Xcharacter) are copied unchanged into the array.
- XIf copying takes place between objects that overlap the behavior is undefined.
- XNo more than
- X.B maxsize
- Xcharacters are placed into the array.
- XEach conversion specifier is replaced by appropriate characters as described
- Xin the following list.
- XThe appropriate characters are determined by the
- X.B LC_TIME
- Xcategory of the current locale and by the values contained in the
- Xstructure pointed to by
- X.BR timeptr .
- X.TP
- X.B %a
- Xis replaced by the locale's abbreviated weekday name.
- X.TP
- X.B %A
- Xis replaced by the locale's full weekday name.
- X.TP
- X.B %b
- Xis replaced by the locale's abbreviated month name.
- X.TP
- X.B %B
- Xis replaced by the locale's full month name.
- X.TP
- X.B %c
- Xis replaced by the locale's appropriate date and time representation.
- X.TP
- X.B %d
- Xis replaced by the day of the month as a decimal number
- X.RB ( 01 - 31 ).
- X.TP
- X.B %H
- Xis replaced by the hour (24-hour clock) as a decimal number
- X.RB ( 00 - 23 ).
- X.TP
- X.B %I
- Xis replaced by the hour (12-hour clock) as a decimal number
- X.RB ( 01 - 12 ).
- X.TP
- X.B %j
- Xis replaced by the day of the year as a decimal number
- X.RB ( 001 - 366 ).
- X.TP
- X.B %m
- Xis replaced by the month as a decimal number
- X.RB ( 01 - 12 ).
- X.TP
- X.B %M
- Xis replaced by the minute as a decimal number
- X.RB ( 00 - 59 ).
- X.TP
- X.B %p
- Xis replaced by the locale's equivalent of the AM/PM designations associated
- Xwith a 12-hour clock.
- X.TP
- X.B %S
- Xis replaced by the second as a decimal number
- X.RB ( 00 - 61 ).
- X.TP
- X.B %U
- Xis replaced by the week number of the year (the first Sunday as the first
- Xday of week 1) as a decimal number
- X.RB ( 00 - 53 ).
- X.TP
- X.B %w
- Xis replaced by the weekday as a decimal number
- X.RB [ "0 " (Sunday)- 6 ].
- X.TP
- X.B %W
- Xis replaced by the week number of the year (the first Monday as the first
- Xday of week 1) as a decimal number
- X.RB ( 00 - 53 ).
- X.TP
- X.B %x
- Xis replaced by the locale's appropriate date representation.
- X.TP
- X.B %X
- Xis replaced by the locale's appropriate time representation.
- X.TP
- X.B %y
- Xis replaced by the year without century as a decimal number
- X.RB ( 00 - 99 ).
- X.TP
- X.B %Y
- Xis replaced by the year with century as a decimal number.
- X.TP
- X.B %Z
- Xis replaced by the time zone name or abbreviation, or by no characters if
- Xno time zone is determinable.
- X.TP
- X.B %%
- Xis replaced by
- X.BR % .
- X.LP
- XIf a conversion specifier is not one of the above, the behavior is
- Xundefined.
- X.SH RETURNS
- XIf the total number of resulting characters including the terminating null
- Xcharacter is not more than
- X.BR maxsize ,
- Xthe
- X.B strftime
- Xfunction returns the number of characters placed into the array pointed to
- Xby
- X.B s
- Xnot including the terminating null character.
- XOtherwise, zero is returned and the contents of the array are indeterminate.
- X.SH NON-ANSI EXTENSIONS
- XIf
- X.B SYSV_EXT
- Xis defined when the routine is compiled, then the following additional
- Xconversions will be available.
- XThese are borrowed from the System V
- X.IR cftime (3)
- Xand
- X.IR ascftime (3)
- Xroutines.
- X.TP
- X.B %D
- Xis equivalent to specifying
- X.BR %m/%d/%y .
- X.TP
- X.B %e
- Xis replaced by the day of the month,
- Xpadded with a blank if it is only one digit.
- X.TP
- X.B %h
- Xis equivalent to
- X.BR %b ,
- Xabove.
- X.TP
- X.B %n
- Xis replaced with a newline character (\s-1ASCII LF\s+1).
- X.TP
- X.B %r
- Xis equivalent to specifying
- X.BR "%I:%M:%S %p" .
- X.TP
- X.B %R
- Xis equivalent to specifying
- X.BR %H:%M .
- X.TP
- X.B %T
- Xis equivalent to specifying
- X.BR %H:%M:%S .
- X.TP
- X.B %t
- Xis replaced with a \s-1TAB\s+1 character.
- X.PP
- XIf
- X.B SUNOS_EXT
- Xis defined when the routine is compiled, then the following additional
- Xconversions will be available.
- XThese are borrowed from the SunOS version of
- X.IR strftime .
- X.TP
- X.B %k
- Xis replaced by the hour (24-hour clock) as a decimal number
- X.RB ( 0 - 23 ).
- XSingle digit numbers are padded with a blank.
- X.TP
- X.B %l
- Xis replaced by the hour (12-hour clock) as a decimal number
- X.RB ( 1 - 12 ).
- XSingle digit numbers are padded with a blank.
- X.SH POSIX 1003.2 EXTENSIONS
- XIf
- X.B POSIX2_DATE
- Xis defined, then all of the conversions available with
- X.B SYSV_EXT
- Xand
- X.B SUNOS_EXT
- Xare available, as well as the
- Xfollowing additional conversions:
- X.TP
- X.B %C
- XThe century, as a number between 00 and 99.
- X.TP
- X.B %u
- Xis replaced by the weekday as a decimal number
- X.RB [ "1 " (Monday)- 7 ].
- X.TP
- X.B %V
- Xis replaced by the week number of the year (the first Monday as the first
- Xday of week 1) as a decimal number
- X.RB ( 01 - 53 ).
- XThe method for determining the week number is as specified by ISO 8601
- X(to wit: if the week containing January 1 has four or more days in the
- Xnew year, then it is week 1, otherwise it is week 53 of the previous year
- Xand the next week is week 1).
- X.LP
- XThe text of the POSIX standard for the
- X.I date
- Xutility describes
- X.B %U
- Xand
- X.B %W
- Xthis way:
- X.TP
- X.B %U
- Xis replaced by the week number of the year (the first Sunday as the first
- Xday of week 1) as a decimal number
- X.RB ( 00 - 53 ).
- XAll days in a new year preceding the first Sunday are considered to be
- Xin week 0.
- X.TP
- X.B %W
- Xis replaced by the week number of the year (the first Monday as the first
- Xday of week 1) as a decimal number
- X.RB ( 00 - 53 ).
- XAll days in a new year preceding the first Sunday are considered to be
- Xin week 0.
- X(Note: this last statement is quoted verbatim from the POSIX standard.
- XIt probably means to say ``all days in a new year preceding the first
- X.I Monday
- Xare considered to be in week 0.'')
- X.LP
- XIn addition, the alternate representations
- X.BR %Ec ,
- X.BR %EC ,
- X.BR %Ex ,
- X.BR %Ey ,
- X.BR %EY ,
- X.BR %Od ,
- X.BR %Oe ,
- X.BR %OH ,
- X.BR %OI ,
- X.BR %Om ,
- X.BR %OM ,
- X.BR %OS ,
- X.BR %Ou ,
- X.BR %OU ,
- X.BR %OV ,
- X.BR %Ow ,
- X.BR %OW ,
- Xand
- X.B %Oy
- Xare recognized, but their normal representations are used.
- X.SH VMS EXTENSIONS
- XIf
- X.B VMS_EXT
- Xis defined, then the following additional conversion is available:
- X.TP
- X.B %v
- XThe date in VMS format (e.g. 20-JUN-1991).
- X.SH SEE ALSO
- X.IR time (2),
- X.IR ctime (3),
- X.IR localtime (3),
- X.IR tzset (3)
- X.SH BUGS
- XThis version does not handle multibyte characters or pay attention to the
- Xsetting of the
- X.B LC_TIME
- Xenvironment variable.
- X.LP
- XIt is not clear what is ``appropriate'' for the C locale; the values
- Xreturned are a best guess on the author's part.
- X.SH CAVEATS
- XThe pre-processor symbol
- X.B POSIX_SEMANTICS
- Xis automatically defined, which forces the code to call
- X.IR tzset (3)
- Xwhenever the
- X.B TZ
- Xenvironment variable has changed.
- XIf this routine will be used in an application that will not be changing
- X.BR TZ ,
- Xthen there may be some performance improvements by not defining
- X.BR POSIX_SEMANTICS .
- X.SH AUTHOR
- X.nf
- XArnold Robbins
- X.sp
- XINTERNET: arnold@skeeve.atl.ga.us
- XUUCP: emory!skeeve!arnold
- XPhone: +1 404 248 9324
- X.fi
- X.SH ACKNOWLEDGEMENTS
- XThanks to Geoff Clare <gwc@root.co.uk> for helping debug earlier
- Xversions of this routine, and for advice about POSIX semantics.
- XAdditional thanks to Arthur David Olsen <ado@elsie.nci.nih.gov>
- Xfor some code improvements.
- END_OF_FILE
- if test 7705 -ne `wc -c <'strftime.3'`; then
- echo shar: \"'strftime.3'\" unpacked with wrong size!
- fi
- # end of 'strftime.3'
- fi
- if test -f 'strftime.c' -a "${1}" != "-c" ; then
- echo shar: Will not clobber existing file \"'strftime.c'\"
- else
- echo shar: Extracting \"'strftime.c'\" \(13629 characters\)
- sed "s/^X//" >'strftime.c' <<'END_OF_FILE'
- X/*
- X * strftime.c
- X *
- X * Public-domain relatively quick-and-dirty implementation of
- X * ANSI library routine for System V Unix systems.
- X *
- X * It's written in old-style C for maximal portability.
- X * However, since I'm used to prototypes, I've included them too.
- X *
- X * If you want stuff in the System V ascftime routine, add the SYSV_EXT define.
- X * For extensions from SunOS, add SUNOS_EXT.
- X * For stuff needed to implement the P1003.2 date command, add POSIX2_DATE.
- X * For complete POSIX semantics, add POSIX_SEMANTICS.
- X *
- X * The code for %c, %x, and %X is my best guess as to what's "appropriate".
- X * This version ignores LOCALE information.
- X * It also doesn't worry about multi-byte characters.
- X * So there.
- X *
- X * This file is also shipped with GAWK (GNU Awk), gawk specific bits of
- X * code are included if GAWK is defined.
- X *
- X * Arnold Robbins
- X * January, February, March, 1991
- X * Updated March, April 1992
- X * Updated May, 1993
- X *
- X * Fixes from ado@elsie.nci.nih.gov
- X * February 1991, May 1992
- X */
- X
- X#ifndef GAWK
- X#include <stdio.h>
- X#include <ctype.h>
- X#include <string.h>
- X#include <time.h>
- X#include <sys/types.h>
- X#endif
- X
- X/* defaults: season to taste */
- X#define SYSV_EXT 1 /* stuff in System V ascftime routine */
- X#define SUNOS_EXT 1 /* stuff in SunOS strftime routine */
- X#define POSIX2_DATE 1 /* stuff in Posix 1003.2 date command */
- X#define VMS_EXT 1 /* include %v for VMS date format */
- X#ifndef GAWK
- X#define POSIX_SEMANTICS 1 /* call tzset() if TZ changes */
- X#endif
- X
- X#if defined(POSIX2_DATE)
- X#if ! defined(SYSV_EXT)
- X#define SYSV_EXT 1
- X#endif
- X#if ! defined(SUNOS_EXT)
- X#define SUNOS_EXT 1
- X#endif
- X#endif
- X
- X#if defined(POSIX2_DATE)
- X#define adddecl(stuff) stuff
- X#else
- X#define adddecl(stuff)
- X#endif
- X
- X#undef strchr /* avoid AIX weirdness */
- X
- X#ifndef __STDC__
- X#define const /**/
- Xextern void *malloc();
- Xextern void *realloc();
- Xextern void tzset();
- Xextern char *strchr();
- Xextern char *getenv();
- Xstatic int weeknumber();
- Xadddecl(static int iso8601wknum();)
- X#else
- Xextern void *malloc(unsigned count);
- Xextern void *realloc(void *ptr, unsigned count);
- Xextern void tzset(void);
- Xextern char *strchr(const char *str, int ch);
- Xextern char *getenv(const char *v);
- Xstatic int weeknumber(const struct tm *timeptr, int firstweekday);
- Xadddecl(static int iso8601wknum(const struct tm *timeptr);)
- X#endif
- X
- X#ifdef __GNUC__
- X#define inline __inline__
- X#else
- X#define inline /**/
- X#endif
- X
- X#define range(low, item, hi) max(low, min(item, hi))
- X
- X#if !defined(MSDOS) && !defined(TZNAME_MISSING)
- Xextern char *tzname[2];
- Xextern int daylight;
- X#endif
- X
- X/* min --- return minimum of two numbers */
- X
- X#ifndef __STDC__
- Xstatic inline int
- Xmin(a, b)
- Xint a, b;
- X#else
- Xstatic inline int
- Xmin(int a, int b)
- X#endif
- X{
- X return (a < b ? a : b);
- X}
- X
- X/* max --- return maximum of two numbers */
- X
- X#ifndef __STDC__
- Xstatic inline int
- Xmax(a, b)
- Xint a, b;
- X#else
- Xstatic inline int
- Xmax(int a, int b)
- X#endif
- X{
- X return (a > b ? a : b);
- X}
- X
- X/* strftime --- produce formatted time */
- X
- X#ifndef __STDC__
- Xsize_t
- Xstrftime(s, maxsize, format, timeptr)
- Xchar *s;
- Xsize_t maxsize;
- Xconst char *format;
- Xconst struct tm *timeptr;
- X#else
- Xsize_t
- Xstrftime(char *s, size_t maxsize, const char *format, const struct tm *timeptr)
- X#endif
- X{
- X char *endp = s + maxsize;
- X char *start = s;
- X char tbuf[100];
- X int i;
- X static short first = 1;
- X#ifdef POSIX_SEMANTICS
- X static char *savetz = NULL;
- X static int savetzlen = 0;
- X char *tz;
- X#endif /* POSIX_SEMANTICS */
- X
- X /* various tables, useful in North America */
- X static char *days_a[] = {
- X "Sun", "Mon", "Tue", "Wed",
- X "Thu", "Fri", "Sat",
- X };
- X static char *days_l[] = {
- X "Sunday", "Monday", "Tuesday", "Wednesday",
- X "Thursday", "Friday", "Saturday",
- X };
- X static char *months_a[] = {
- X "Jan", "Feb", "Mar", "Apr", "May", "Jun",
- X "Jul", "Aug", "Sep", "Oct", "Nov", "Dec",
- X };
- X static char *months_l[] = {
- X "January", "February", "March", "April",
- X "May", "June", "July", "August", "September",
- X "October", "November", "December",
- X };
- X static char *ampm[] = { "AM", "PM", };
- X
- X if (s == NULL || format == NULL || timeptr == NULL || maxsize == 0)
- X return 0;
- X
- X if (strchr(format, '%') == NULL && strlen(format) + 1 >= maxsize)
- X return 0;
- X
- X#ifndef POSIX_SEMANTICS
- X if (first) {
- X tzset();
- X first = 0;
- X }
- X#else /* POSIX_SEMANTICS */
- X tz = getenv("TZ");
- X if (first) {
- X if (tz != NULL) {
- X int tzlen = strlen(tz);
- X
- X savetz = (char *) malloc(tzlen + 1);
- X if (savetz != NULL) {
- X savetzlen = tzlen + 1;
- X strcpy(savetz, tz);
- X }
- X }
- X tzset();
- X first = 0;
- X }
- X /* if we have a saved TZ, and it is different, recapture and reset */
- X if (tz && savetz && (tz[0] != savetz[0] || strcmp(tz, savetz) != 0)) {
- X i = strlen(tz) + 1;
- X if (i > savetzlen) {
- X savetz = (char *) realloc(savetz, i);
- X if (savetz) {
- X savetzlen = i;
- X strcpy(savetz, tz);
- X }
- X } else
- X strcpy(savetz, tz);
- X tzset();
- X }
- X#endif /* POSIX_SEMANTICS */
- X
- X for (; *format && s < endp - 1; format++) {
- X tbuf[0] = '\0';
- X if (*format != '%') {
- X *s++ = *format;
- X continue;
- X }
- X again:
- X switch (*++format) {
- X case '\0':
- X *s++ = '%';
- X goto out;
- X
- X case '%':
- X *s++ = '%';
- X continue;
- X
- X case 'a': /* abbreviated weekday name */
- X if (timeptr->tm_wday < 0 || timeptr->tm_wday > 6)
- X strcpy(tbuf, "?");
- X else
- X strcpy(tbuf, days_a[timeptr->tm_wday]);
- X break;
- X
- X case 'A': /* full weekday name */
- X if (timeptr->tm_wday < 0 || timeptr->tm_wday > 6)
- X strcpy(tbuf, "?");
- X else
- X strcpy(tbuf, days_l[timeptr->tm_wday]);
- X break;
- X
- X#ifdef SYSV_EXT
- X case 'h': /* abbreviated month name */
- X#endif
- X case 'b': /* abbreviated month name */
- X if (timeptr->tm_mon < 0 || timeptr->tm_mon > 11)
- X strcpy(tbuf, "?");
- X else
- X strcpy(tbuf, months_a[timeptr->tm_mon]);
- X break;
- X
- X case 'B': /* full month name */
- X if (timeptr->tm_mon < 0 || timeptr->tm_mon > 11)
- X strcpy(tbuf, "?");
- X else
- X strcpy(tbuf, months_l[timeptr->tm_mon]);
- X break;
- X
- X case 'c': /* appropriate date and time representation */
- X sprintf(tbuf, "%s %s %2d %02d:%02d:%02d %d",
- X days_a[range(0, timeptr->tm_wday, 6)],
- X months_a[range(0, timeptr->tm_mon, 11)],
- X range(1, timeptr->tm_mday, 31),
- X range(0, timeptr->tm_hour, 23),
- X range(0, timeptr->tm_min, 59),
- X range(0, timeptr->tm_sec, 61),
- X timeptr->tm_year + 1900);
- X break;
- X
- X case 'd': /* day of the month, 01 - 31 */
- X i = range(1, timeptr->tm_mday, 31);
- X sprintf(tbuf, "%02d", i);
- X break;
- X
- X case 'H': /* hour, 24-hour clock, 00 - 23 */
- X i = range(0, timeptr->tm_hour, 23);
- X sprintf(tbuf, "%02d", i);
- X break;
- X
- X case 'I': /* hour, 12-hour clock, 01 - 12 */
- X i = range(0, timeptr->tm_hour, 23);
- X if (i == 0)
- X i = 12;
- X else if (i > 12)
- X i -= 12;
- X sprintf(tbuf, "%02d", i);
- X break;
- X
- X case 'j': /* day of the year, 001 - 366 */
- X sprintf(tbuf, "%03d", timeptr->tm_yday + 1);
- X break;
- X
- X case 'm': /* month, 01 - 12 */
- X i = range(0, timeptr->tm_mon, 11);
- X sprintf(tbuf, "%02d", i + 1);
- X break;
- X
- X case 'M': /* minute, 00 - 59 */
- X i = range(0, timeptr->tm_min, 59);
- X sprintf(tbuf, "%02d", i);
- X break;
- X
- X case 'p': /* am or pm based on 12-hour clock */
- X i = range(0, timeptr->tm_hour, 23);
- X if (i < 12)
- X strcpy(tbuf, ampm[0]);
- X else
- X strcpy(tbuf, ampm[1]);
- X break;
- X
- X case 'S': /* second, 00 - 61 */
- X i = range(0, timeptr->tm_sec, 61);
- X sprintf(tbuf, "%02d", i);
- X break;
- X
- X case 'U': /* week of year, Sunday is first day of week */
- X sprintf(tbuf, "%d", weeknumber(timeptr, 0));
- X break;
- X
- X case 'w': /* weekday, Sunday == 0, 0 - 6 */
- X i = range(0, timeptr->tm_wday, 6);
- X sprintf(tbuf, "%d", i);
- X break;
- X
- X case 'W': /* week of year, Monday is first day of week */
- X sprintf(tbuf, "%d", weeknumber(timeptr, 1));
- X break;
- X
- X case 'x': /* appropriate date representation */
- X sprintf(tbuf, "%s %s %2d %d",
- X days_a[range(0, timeptr->tm_wday, 6)],
- X months_a[range(0, timeptr->tm_mon, 11)],
- X range(1, timeptr->tm_mday, 31),
- X timeptr->tm_year + 1900);
- X break;
- X
- X case 'X': /* appropriate time representation */
- X sprintf(tbuf, "%02d:%02d:%02d",
- X range(0, timeptr->tm_hour, 23),
- X range(0, timeptr->tm_min, 59),
- X range(0, timeptr->tm_sec, 61));
- X break;
- X
- X case 'y': /* year without a century, 00 - 99 */
- X i = timeptr->tm_year % 100;
- X sprintf(tbuf, "%d", i);
- X break;
- X
- X case 'Y': /* year with century */
- X sprintf(tbuf, "%d", 1900 + timeptr->tm_year);
- X break;
- X
- X case 'Z': /* time zone name or abbrevation */
- X i = 0;
- X if (
- X#ifndef TZNAME_MISSING
- X daylight &&
- X#endif
- X timeptr->tm_isdst)
- X i = 1;
- X#ifdef TZNAME_MISSING
- X strcpy(tbuf, timeptr->tm_zone);
- X#else
- X strcpy(tbuf, tzname[i]);
- X#endif
- X break;
- X
- X#ifdef SYSV_EXT
- X case 'n': /* same as \n */
- X tbuf[0] = '\n';
- X tbuf[1] = '\0';
- X break;
- X
- X case 't': /* same as \t */
- X tbuf[0] = '\t';
- X tbuf[1] = '\0';
- X break;
- X
- X case 'D': /* date as %m/%d/%y */
- X strftime(tbuf, sizeof tbuf, "%m/%d/%y", timeptr);
- X break;
- X
- X case 'e': /* day of month, blank padded */
- X sprintf(tbuf, "%2d", range(1, timeptr->tm_mday, 31));
- X break;
- X
- X case 'r': /* time as %I:%M:%S %p */
- X strftime(tbuf, sizeof tbuf, "%I:%M:%S %p", timeptr);
- X break;
- X
- X case 'R': /* time as %H:%M */
- X strftime(tbuf, sizeof tbuf, "%H:%M", timeptr);
- X break;
- X
- X case 'T': /* time as %H:%M:%S */
- X strftime(tbuf, sizeof tbuf, "%H:%M:%S", timeptr);
- X break;
- X#endif
- X
- X#ifdef SUNOS_EXT
- X case 'k': /* hour, 24-hour clock, blank pad */
- X sprintf(tbuf, "%2d", range(0, timeptr->tm_hour, 23));
- X break;
- X
- X case 'l': /* hour, 12-hour clock, 1 - 12, blank pad */
- X i = range(0, timeptr->tm_hour, 23);
- X if (i == 0)
- X i = 12;
- X else if (i > 12)
- X i -= 12;
- X sprintf(tbuf, "%2d", i);
- X break;
- X#endif
- X
- X
- X#ifdef VMS_EXT
- X case 'v': /* date as dd-bbb-YYYY */
- X sprintf(tbuf, "%2d-%3.3s-%4d",
- X range(1, timeptr->tm_mday, 31),
- X months_a[range(0, timeptr->tm_mon, 11)],
- X timeptr->tm_year + 1900);
- X for (i = 3; i < 6; i++)
- X if (islower(tbuf[i]))
- X tbuf[i] = toupper(tbuf[i]);
- X break;
- X#endif
- X
- X
- X#ifdef POSIX2_DATE
- X case 'C':
- X sprintf(tbuf, "%02d", (timeptr->tm_year + 1900) / 100);
- X break;
- X
- X
- X case 'E':
- X case 'O':
- X /* POSIX locale extensions, ignored for now */
- X goto again;
- X
- X case 'V': /* week of year according ISO 8601 */
- X#if defined(GAWK) && defined(VMS_EXT)
- X {
- X extern int do_lint;
- X extern void warning();
- X static int warned = 0;
- X
- X if (! warned && do_lint) {
- X warned = 1;
- X warning(
- X "conversion %%V added in P1003.2/11.3; for VMS style date, use %%v");
- X }
- X }
- X#endif
- X sprintf(tbuf, "%d", iso8601wknum(timeptr));
- X break;
- X
- X case 'u':
- X /* ISO 8601: Weekday as a decimal number [1 (Monday) - 7] */
- X sprintf(tbuf, "%d", timeptr->tm_wday == 0 ? 7 :
- X timeptr->tm_wday);
- X break;
- X#endif /* POSIX2_DATE */
- X default:
- X tbuf[0] = '%';
- X tbuf[1] = *format;
- X tbuf[2] = '\0';
- X break;
- X }
- X i = strlen(tbuf);
- X if (i)
- X if (s + i < endp - 1) {
- X strcpy(s, tbuf);
- X s += i;
- X } else
- X return 0;
- X }
- Xout:
- X if (s < endp && *format == '\0') {
- X *s = '\0';
- X return (s - start);
- X } else
- X return 0;
- X}
- X
- X#ifdef POSIX2_DATE
- X/* iso8601wknum --- compute week number according to ISO 8601 */
- X
- X#ifndef __STDC__
- Xstatic int
- Xiso8601wknum(timeptr)
- Xconst struct tm *timeptr;
- X#else
- Xstatic int
- Xiso8601wknum(const struct tm *timeptr)
- X#endif
- X{
- X /*
- X * From 1003.2 D11.3:
- X * If the week (Monday to Sunday) containing January 1
- X * has four or more days in the new year, then it is week 1;
- X * otherwise it is week 53 of the previous year, and the
- X * next week is week 1.
- X *
- X * ADR: This means if Jan 1 was Monday through Thursday,
- X * it was week 1, otherwise week 53.
- X */
- X
- X int simple_wknum, jan1day, diff, ret;
- X
- X /* get week number, Monday as first day of the week */
- X simple_wknum = weeknumber(timeptr, 1) + 1;
- X
- X /*
- X * With thanks and tip of the hatlo to ado@elsie.nci.nih.gov
- X *
- X * What day of the week does January 1 fall on?
- X * We know that
- X * (timeptr->tm_yday - jan1.tm_yday) MOD 7 ==
- X * (timeptr->tm_wday - jan1.tm_wday) MOD 7
- X * and that
- X * jan1.tm_yday == 1
- X * and that
- X * timeptr->tm_wday MOD 7 == timeptr->tm_wday
- X * from which it follows that. . .
- X */
- X jan1day = (timeptr->tm_yday - 1) % 7 - timeptr->tm_wday;
- X if (jan1day < 0)
- X jan1day += 7;
- X
- X /*
- X * If Jan 1 was a Monday through Thursday, it was in
- X * week 1. Otherwise it was last year's week 53, which is
- X * this year's week 0.
- X */
- X if (jan1day >= 1 && jan1day <= 4)
- X diff = 0;
- X else
- X diff = 1;
- X ret = simple_wknum - diff;
- X if (ret == 0) /* we're in the first week of the year */
- X ret = 53;
- X return ret;
- X}
- X#endif
- X
- X/* weeknumber --- figure how many weeks into the year */
- X
- X/* With thanks and tip of the hatlo to ado@elsie.nci.nih.gov */
- X
- X#ifndef __STDC__
- Xstatic int
- Xweeknumber(timeptr, firstweekday)
- Xconst struct tm *timeptr;
- Xint firstweekday;
- X#else
- Xstatic int
- Xweeknumber(const struct tm *timeptr, int firstweekday)
- X#endif
- X{
- X if (firstweekday == 0)
- X return (timeptr->tm_yday + 7 - timeptr->tm_wday) / 7;
- X else
- X return (timeptr->tm_yday + 7 -
- X (timeptr->tm_wday ? (timeptr->tm_wday - 1) : 6)) / 7;
- X}
- X
- X#if 0
- X/* ADR --- I'm loathe to mess with ado's code ... */
- X
- XDate: Wed, 24 Apr 91 20:54:08 MDT
- XFrom: Michal Jaegermann <audfax!emory!vm.ucs.UAlberta.CA!NTOMCZAK>
- XTo: arnold@audiofax.com
- X
- XHi Arnold,
- Xin a process of fixing of strftime() in libraries on Atari ST I grabbed
- Xsome pieces of code from your own strftime. When doing that it came
- Xto mind that your weeknumber() function compiles a little bit nicer
- Xin the following form:
- X/*
- X * firstweekday is 0 if starting in Sunday, non-zero if in Monday
- X */
- X{
- X return (timeptr->tm_yday - timeptr->tm_wday +
- X (firstweekday ? (timeptr->tm_wday ? 8 : 1) : 7)) / 7;
- X}
- XHow nicer it depends on a compiler, of course, but always a tiny bit.
- X
- X Cheers,
- X Michal
- X ntomczak@vm.ucs.ualberta.ca
- X#endif
- END_OF_FILE
- if test 13629 -ne `wc -c <'strftime.c'`; then
- echo shar: \"'strftime.c'\" unpacked with wrong size!
- fi
- # end of 'strftime.c'
- fi
- echo shar: End of archive 1 \(of 1\).
- cp /dev/null ark1isdone
- MISSING=""
- for I in 1 ; do
- if test ! -f ark${I}isdone ; then
- MISSING="${MISSING} ${I}"
- fi
- done
- if test "${MISSING}" = "" ; then
- echo You have the archive.
- rm -f ark[1-9]isdone
- else
- echo You still need to unpack the following archives:
- echo " " ${MISSING}
- fi
- ## End of shell archive.
- exit 0
-