home *** CD-ROM | disk | FTP | other *** search
- Newsgroups: comp.sources.unix
- From: arnold@skeeve.ATL.GA.US (Arnold Robbins)
- Subject: v26i085: strftime - convert date and time to string (includes "date"), Part01/01
- Sender: unix-sources-moderator@pa.dec.com
- Approved: vixie@pa.dec.com
-
- Submitted-By: arnold@skeeve.ATL.GA.US (Arnold Robbins)
- Posting-Number: Volume 26, Issue 85
- Archive-Name: strftime/part01
-
- At the urging of Geoff Clare, I have made strftime posix compliant. The new
- code can be ifdef'ed out if you know your application will never change the
- TZ environment variable.
-
- I have also added range checking to everything, as a bit of bullet-proofing.
- Compiling this code with gcc gives smaller faster versions than plain old
- cc, but who's suprised by that? :-)
-
- This can supersede the recent posting.
-
- Arnold Robbins -- The Basement Computer | Laundry increases
- Internet: arnold@skeeve.ATL.GA.US | exponentially in the
- UUCP: { gatech, emory }!skeeve!arnold | number of children.
- Bitnet: Forget it. Get on a real network. | -- Miriam Robbins
-
- #! /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 shell archive."
- # Contents: README Makefile date.1 date.c strftime.3 strftime.c
- # Wrapped by vixie@cognition.pa.dec.com on Sun Jan 17 20:36:57 1993
- PATH=/bin:/usr/bin:/usr/ucb ; export PATH
- if test -f 'README' -a "${1}" != "-c" ; then
- echo shar: Will not clobber existing file \"'README'\"
- else
- echo shar: Extracting \"'README'\" \(457 characters\)
- sed "s/^X//" >'README' <<'END_OF_FILE'
- Mon Mar 23 12:00:31 EST 1992
- X
- This package implements the Posix 1003.2 Draft 11 date command, as a
- wrapper around an extended version of the ANSI strftime(3) library
- routine. Everything in it is public domain.
- X
- Arnold Robbins -- The Basement Computer | Laundry increases
- Internet: arnold@skeeve.ATL.GA.US | exponentially in the
- UUCP: { gatech, emory }!skeeve!arnold | number of children.
- Bitnet: Forget it. Get on a real network. | -- Miriam Robbins
- END_OF_FILE
- if test 457 -ne `wc -c <'README'`; then
- echo shar: \"'README'\" unpacked with wrong size!
- fi
- # end of 'README'
- 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
- SRCS= date.c strftime.c
- OBJS= date.o strftime.o
- DOCS= date.1 strftime.3
- X
- date: $(OBJS)
- X $(CC) $(CFLAGS) $(OBJS) -o $@
- X
- date.o: date.c
- X
- strftime.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 '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
- date \- 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
- writes the current date and time to the standard output.
- It is intended to be compliant with draft 11 of the Posix
- X1003.2 Command Language and Utilities standard.
- Therefore, it is purposely
- X.I not
- usable by the super-user for setting the system time.
- X.LP
- X.I Date
- accepts one option:
- X.TP
- X.B \-u
- Perform operations as if the
- X.B TZ
- environment variable was set to
- X.BR GMT0 .
- X.LP
- If an argument to
- X.I date
- is given that begins with a ``+'',
- then the output is controlled by the contents of the rest of
- the string. Normal text is output unmodified, while field descriptors
- in the format string are substituted for.
- X.LP
- The
- X.I date
- program is essentially a wrapper around
- X.IR strftime (3);
- see there for a description of the available formatting options.
- X.LP
- If no format string is given, or if it does not begin with a ``+'',
- then the default format of \fB"%a %b %e %H:%M:%S %Z %Y"\fR will
- be used. This produces the traditional style of output, such as
- X``Sun Mar 17 10:32:47 EST 1991''.
- X.SH SEE ALSO
- time(2), strftime(3), localtime(3)
- X.SH BUGS
- This version only works for the POSIX locale.
- X.SH AUTHOR
- X.nf
- Arnold Robbins
- X.sp
- INTERNET: arnold@skeeve.atl.ga.us
- UUCP: emory!skeeve!arnold
- Phone: +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
- extern char *malloc();
- extern size_t strftime();
- extern int getopt();
- extern int optind;
- X
- int
- main(argc, argv)
- int argc;
- char **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'\" \(6033 characters\)
- sed "s/^X//" >'strftime.3' <<'END_OF_FILE'
- X.TH STRFTIME 3
- X.SH NAME
- strftime \- generate formatted time information
- X.SH SYNOPSIS
- X.ft B
- X.nf
- X#include <sys/types.h>
- X#include <time.h>
- X.sp
- size_t strftime(char *s, size_t maxsize, const char *format,
- X const struct tm *timeptr);
- X.SH DESCRIPTION
- The following description is transcribed verbatim from the December 7, 1988
- draft standard for ANSI C.
- This draft is essentially identical in technical content
- to the final version of the standard.
- X.LP
- The
- X.B strftime
- function places characters into the array pointed to by
- X.B s
- as controlled by the string pointed to by
- X.BR format .
- The format shall be a multibyte character sequence, beginning and ending in
- its initial shift state.
- The
- X.B format
- string consists of zero or more conversion specifiers and ordinary
- multibyte characters. A conversion specifier consists of a
- X.B %
- character followed by a character that determines the behavior of the
- conversion specifier.
- All ordinary multibyte characters (including the terminating null
- character) are copied unchanged into the array.
- If copying takes place between objects that overlap the behavior is undefined.
- No more than
- X.B maxsize
- characters are placed into the array.
- XEach conversion specifier is replaced by appropriate characters as described
- in the following list.
- The appropriate characters are determined by the
- X.B LC_TIME
- category of the current locale and by the values contained in the
- structure pointed to by
- X.BR timeptr .
- X.TP
- X.B %a
- is replaced by the locale's abbreviated weekday name.
- X.TP
- X.B %A
- is replaced by the locale's full weekday name.
- X.TP
- X.B %b
- is replaced by the locale's abbreviated month name.
- X.TP
- X.B %B
- is replaced by the locale's full month name.
- X.TP
- X.B %c
- is replaced by the locale's appropriate date and time representation.
- X.TP
- X.B %d
- is replaced by the day of the month as a decimal number
- X.RB ( 01 - 31 ).
- X.TP
- X.B %H
- is replaced by the hour (24-hour clock) as a decimal number
- X.RB ( 00 - 23 ).
- X.TP
- X.B %I
- is replaced by the hour (12-hour clock) as a decimal number
- X.RB ( 01 - 12 ).
- X.TP
- X.B %j
- is replaced by the day of the year as a decimal number
- X.RB ( 001 - 366 ).
- X.TP
- X.B %m
- is replaced by the month as a decimal number
- X.RB ( 01 - 12 ).
- X.TP
- X.B %M
- is replaced by the minute as a decimal number
- X.RB ( 00 - 59 ).
- X.TP
- X.B %p
- is replaced by the locale's equivalent of the AM/PM designations associated
- with a 12-hour clock.
- X.TP
- X.B %S
- is replaced by the second as a decimal number
- X.RB ( 00 - 61 ).
- X.TP
- X.B %U
- is replaced by the week number of the year (the first Sunday as the first
- day of week 1) as a decimal number
- X.RB ( 00 - 53 ).
- X.TP
- X.B %w
- is replaced by the weekday as a decimal number
- X.RB [ "0 " (Sunday)- 6 ].
- X.TP
- X.B %W
- is replaced by the week number of the year (the first Monday as the first
- day of week 1) as a decimal number
- X.RB ( 00 - 53 ).
- X.TP
- X.B %x
- is replaced by the locale's appropriate date representation.
- X.TP
- X.B %X
- is replaced by the locale's appropriate time representation.
- X.TP
- X.B %y
- is replaced by the year without century as a decimal number
- X.RB ( 00 - 99 ).
- X.TP
- X.B %Y
- is replaced by the year with century as a decimal number.
- X.TP
- X.B %Z
- is replaced by the time zone name or abbreviation, or by no characters if
- no time zone is determinable.
- X.TP
- X.B %%
- is replaced by
- X.BR % .
- X.LP
- If a conversion specifier is not one of the above, the behavior is
- undefined.
- X.SH RETURNS
- If the total number of resulting characters including the terminating null
- character is not more than
- X.BR maxsize ,
- the
- X.B strftime
- function returns the number of characters placed into the array pointed to
- by
- X.B s
- not including the terminating null character.
- Otherwise, zero is returned and the contents of the array are indeterminate.
- X.SH NON-ANSI EXTENSIONS
- If
- X.B SYSV_EXT
- is defined when the routine is compiled, then the following additional
- conversions will be available.
- These are borrowed from the System V
- X.IR cftime (3)
- and
- X.IR ascftime (3)
- routines.
- X.TP
- X.B %D
- is equivalent to specifying
- X.BR %m/%d/%y .
- X.TP
- X.B %e
- is replaced by the day of the month,
- padded with a blank if it is only one digit.
- X.TP
- X.B %h
- is equivalent to
- X.BR %b ,
- above.
- X.TP
- X.B %n
- is replaced with a newline character (\s-1ASCII LF\s+1).
- X.TP
- X.B %r
- is equivalent to specifying
- X.BR "%I:%M:%S %p" .
- X.TP
- X.B %R
- is equivalent to specifying
- X.BR %H:%M .
- X.TP
- X.B %T
- is equivalent to specifying
- X.BR %H:%M:%S .
- X.TP
- X.B %t
- is replaced with a \s-1TAB\s+1 character.
- X.SH POSIX 1003.2 EXTENSIONS
- If
- X.B POSIX2_DATE
- is defined, then all of the conversions available with
- X.B SYSV_EXT
- are available, as well as the
- following additional conversions:
- X.TP
- X.B %C
- The century, as a number between 00 and 99.
- X.LP
- In additon, 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 %Ow ,
- X.BR %OW ,
- and
- X.B %Oy
- are recognized, but their normal representations are used.
- X.SH VMS EXTENSIONS
- If
- X.B VMS_EXT
- is defined, then the following additional conversion is available:
- X.TP
- X.B %V
- The date in VMS format (e.g. 20-JUN-1991).
- X.SH SEE ALSO
- time(2), ctime(3), localtime(3)
- X.SH BUGS
- This version does not handle multibyte characters or pay attention to the
- setting of the
- X.B LC_TIME
- environment variable.
- X.LP
- It is not clear what is ``appropriate'' for the C locale; the values
- returned are a best guess on the author's part.
- X.SH CAVEATS
- This implementation calls
- X.IR tzset (3)
- exactly once. If the
- X.B TZ
- environment variable is changed after
- X.B strftime
- has been called, then
- X.IR tzset (3)
- must be called again, explicitly, in order for the
- correct timezone information to be available.
- This behavior is not POSIX-conformant. To force POSIX semantics,
- define
- X.B POSIX_SEMANTICS
- when compiling.
- X.SH AUTHOR
- X.nf
- Arnold Robbins
- X.sp
- INTERNET: arnold@skeeve.atl.ga.us
- UUCP: emory!skeeve!arnold
- Phone: +1 404 248 9324
- X.fi
- X.SH ACKNOWLEDGEMENTS
- Thanks to Geoff Clare <gwc@root.co.uk> for helping debug earlier
- versions of this routine, and for advice about POSIX semantics.
- Additional thanks to Arthur David Olsen <ado@elsie.nci.nih.gov>
- for some code improvements.
- END_OF_FILE
- if test 6033 -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'\" \(10430 characters\)
- sed "s/^X//" >'strftime.c' <<'END_OF_FILE'
- X/*
- X * strftime.c
- X *
- X * Public-domain relatively quick-and-dirty implemenation 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 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 * Arnold Robbins
- X * January, February, March, 1991
- X * Updated March 1992
- X *
- X * Fixes from ado@elsie.nci.nih.gov
- X * February 1991
- X */
- X
- X#include <stdio.h>
- X#include <ctype.h>
- X#include <string.h>
- X#include <time.h>
- X#include <sys/types.h>
- X
- X#ifndef __STDC__
- X#define const /**/
- X
- extern void *malloc();
- extern void *realloc();
- extern void tzset();
- extern char *strchr();
- extern char *getenv();
- static int weeknumber();
- X#else
- extern void *malloc(unsigned count);
- extern void *realloc(void *ptr, unsigned count);
- extern void tzset(void);
- extern char *strchr(const char *str, int ch);
- extern char *getenv(const char *v);
- static int weeknumber(const struct tm *timeptr, int firstweekday);
- 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
- extern char *tzname[2];
- extern int daylight;
- X
- X#define SYSV_EXT 1 /* stuff in System V ascftime 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#define POSIX_SEMANTICS 1 /* call tzset() if TZ changes */
- X
- X#if defined(POSIX2_DATE) && ! defined(SYSV_EXT)
- X#define SYSV_EXT 1
- X#endif
- X
- X/* min --- return minimum of two numbers */
- X
- X#ifndef __STDC__
- static inline int
- min(a, b)
- int a, b;
- X#else
- static inline int
- min(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__
- static inline int
- max(a, b)
- int a, b;
- X#else
- static inline int
- max(int a, int b)
- X#endif
- X{
- X return (a > b ? a : b);
- X}
- X
- X/* strftime --- produce formatted time */
- X
- X#ifndef __STDC__
- size_t
- strftime(s, maxsize, format, timeptr)
- char *s;
- size_t maxsize;
- const char *format;
- const struct tm *timeptr;
- X#else
- size_t
- strftime(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 static char *savetz = NULL, *oldtz = NULL;
- X static int savetzlen = 0;
- X char *tz;
- X int tzlen;
- 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 tzlen = strlen(tz);
- X if (first) {
- X if (tz != NULL) {
- 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 (daylight && timeptr->tm_isdst)
- X i = 1;
- X strcpy(tbuf, tzname[i]);
- 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
- 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#endif
- 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 }
- out:
- X if (s < endp && *format == '\0') {
- X *s = '\0';
- X return (s - start);
- X } else
- X return 0;
- X}
- 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__
- static int
- weeknumber(timeptr, firstweekday)
- const struct tm *timeptr;
- int firstweekday;
- X#else
- static int
- weeknumber(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
- Date: Wed, 24 Apr 91 20:54:08 MDT
- XFrom: Michal Jaegermann <audfax!emory!vm.ucs.UAlberta.CA!NTOMCZAK>
- To: arnold@audiofax.com
- X
- Hi Arnold,
- in a process of fixing of strftime() in libraries on Atari ST I grabbed
- some pieces of code from your own strftime. When doing that it came
- to mind that your weeknumber() function compiles a little bit nicer
- in 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}
- How 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 10430 -ne `wc -c <'strftime.c'`; then
- echo shar: \"'strftime.c'\" unpacked with wrong size!
- fi
- # end of 'strftime.c'
- fi
- echo shar: End of shell archive.
- exit 0
-