OpenCores
URL https://opencores.org/ocsvn/or1k/or1k/trunk

Subversion Repositories or1k

[/] [or1k/] [trunk/] [insight/] [expect/] [exp_strf.c] - Diff between revs 578 and 1765

Go to most recent revision | Only display areas with differences | Details | Blame | View Log

Rev 578 Rev 1765
/* exp_strp.c - functions for exp_timestamp */
/* exp_strp.c - functions for exp_timestamp */
/*
/*
 * strftime.c
 * strftime.c
 *
 *
 * Public-domain implementation of ANSI C library routine.
 * Public-domain implementation of ANSI C library routine.
 *
 *
 * It's written in old-style C for maximal portability.
 * It's written in old-style C for maximal portability.
 * However, since I'm used to prototypes, I've included them too.
 * However, since I'm used to prototypes, I've included them too.
 *
 *
 * If you want stuff in the System V ascftime routine, add the SYSV_EXT define.
 * If you want stuff in the System V ascftime routine, add the SYSV_EXT define.
 * For extensions from SunOS, add SUNOS_EXT.
 * For extensions from SunOS, add SUNOS_EXT.
 * For stuff needed to implement the P1003.2 date command, add POSIX2_DATE.
 * For stuff needed to implement the P1003.2 date command, add POSIX2_DATE.
 * For VMS dates, add VMS_EXT.
 * For VMS dates, add VMS_EXT.
 * For complete POSIX semantics, add POSIX_SEMANTICS.
 * For complete POSIX semantics, add POSIX_SEMANTICS.
 *
 *
 * The code for %c, %x, and %X now follows the 1003.2 specification for
 * The code for %c, %x, and %X now follows the 1003.2 specification for
 * the POSIX locale.
 * the POSIX locale.
 * This version ignores LOCALE information.
 * This version ignores LOCALE information.
 * It also doesn't worry about multi-byte characters.
 * It also doesn't worry about multi-byte characters.
 * So there.
 * So there.
 *
 *
 * This file is also shipped with GAWK (GNU Awk), gawk specific bits of
 * This file is also shipped with GAWK (GNU Awk), gawk specific bits of
 * code are included if GAWK is defined.
 * code are included if GAWK is defined.
 *
 *
 * Arnold Robbins <arnold@skeeve.atl.ga.us>
 * Arnold Robbins <arnold@skeeve.atl.ga.us>
 * January, February, March, 1991
 * January, February, March, 1991
 * Updated March, April 1992
 * Updated March, April 1992
 * Updated April, 1993
 * Updated April, 1993
 * Updated February, 1994
 * Updated February, 1994
 * Updated May, 1994
 * Updated May, 1994
 * Updated January 1995
 * Updated January 1995
 * Updated September 1995
 * Updated September 1995
 *
 *
 * Fixes from ado@elsie.nci.nih.gov
 * Fixes from ado@elsie.nci.nih.gov
 * February 1991, May 1992
 * February 1991, May 1992
 * Fixes from Tor Lillqvist tml@tik.vtt.fi
 * Fixes from Tor Lillqvist tml@tik.vtt.fi
 * May, 1993
 * May, 1993
 * Further fixes from ado@elsie.nci.nih.gov
 * Further fixes from ado@elsie.nci.nih.gov
 * February 1994
 * February 1994
 * %z code from chip@chinacat.unicom.com
 * %z code from chip@chinacat.unicom.com
 * Applied September 1995
 * Applied September 1995
 *
 *
 *
 *
 * Modified by Don Libes for Expect, 10/93 and 12/95.
 * Modified by Don Libes for Expect, 10/93 and 12/95.
 * Forced POSIX semantics.
 * Forced POSIX semantics.
 * Replaced inline/min/max stuff with a single range function.
 * Replaced inline/min/max stuff with a single range function.
 * Removed tzset stuff.
 * Removed tzset stuff.
 * Commented out tzname stuff.
 * Commented out tzname stuff.
 *
 *
 * According to Arnold, the current version of this code can ftp'd from
 * According to Arnold, the current version of this code can ftp'd from
 * ftp.mathcs.emory.edu:/pub/arnold/strftime.shar.gz
 * ftp.mathcs.emory.edu:/pub/arnold/strftime.shar.gz
 *
 *
 */
 */
 
 
#include "expect_cf.h"
#include "expect_cf.h"
#include "tcl.h"
#include "tcl.h"
 
 
#include <stdio.h>
#include <stdio.h>
#include <ctype.h>
#include <ctype.h>
#include "string.h"
#include "string.h"
 
 
/* according to Karl Vogel, time.h is insufficient on Pyramid */
/* according to Karl Vogel, time.h is insufficient on Pyramid */
/* following is recommended by autoconf */
/* following is recommended by autoconf */
 
 
#ifdef TIME_WITH_SYS_TIME
#ifdef TIME_WITH_SYS_TIME
# include <sys/time.h>
# include <sys/time.h>
# include <time.h>
# include <time.h>
#else
#else
# ifdef HAVE_SYS_TIME_H
# ifdef HAVE_SYS_TIME_H
#  include <sys/time.h>
#  include <sys/time.h>
# else
# else
#  include <time.h>
#  include <time.h>
# endif
# endif
#endif
#endif
 
 
 
 
 
 
#include <sys/types.h>
#include <sys/types.h>
 
 
#define SYSV_EXT        1       /* stuff in System V ascftime routine */
#define SYSV_EXT        1       /* stuff in System V ascftime routine */
#define POSIX2_DATE     1       /* stuff in Posix 1003.2 date command */
#define POSIX2_DATE     1       /* stuff in Posix 1003.2 date command */
 
 
#if defined(POSIX2_DATE) && ! defined(SYSV_EXT)
#if defined(POSIX2_DATE) && ! defined(SYSV_EXT)
#define SYSV_EXT        1
#define SYSV_EXT        1
#endif
#endif
 
 
#if defined(POSIX2_DATE)
#if defined(POSIX2_DATE)
#define adddecl(stuff)  stuff
#define adddecl(stuff)  stuff
#else
#else
#define adddecl(stuff)
#define adddecl(stuff)
#endif
#endif
 
 
#ifndef __STDC__
#ifndef __STDC__
#define const
#define const
 
 
extern char *getenv();
extern char *getenv();
static int weeknumber();
static int weeknumber();
adddecl(static int iso8601wknum();)
adddecl(static int iso8601wknum();)
#else
#else
 
 
#ifndef strchr
#ifndef strchr
extern char *strchr(const char *str, int ch);
extern char *strchr(const char *str, int ch);
#endif
#endif
extern char *getenv(const char *v);
extern char *getenv(const char *v);
static int weeknumber(const struct tm *timeptr, int firstweekday);
static int weeknumber(const struct tm *timeptr, int firstweekday);
adddecl(static int iso8601wknum(const struct tm *timeptr);)
adddecl(static int iso8601wknum(const struct tm *timeptr);)
#endif
#endif
 
 
/* attempt to use strftime to compute timezone, else fallback to */
/* attempt to use strftime to compute timezone, else fallback to */
/* less portable ways */
/* less portable ways */
#if !defined(HAVE_STRFTIME)
#if !defined(HAVE_STRFTIME)
# if defined(HAVE_SV_TIMEZONE)
# if defined(HAVE_SV_TIMEZONE)
extern char *tzname[2];
extern char *tzname[2];
extern int daylight;
extern int daylight;
# else
# else
#  if defined(HAVE_TIMEZONE)
#  if defined(HAVE_TIMEZONE)
 
 
char           *
char           *
zone_name (tp)
zone_name (tp)
struct tm      *tp;
struct tm      *tp;
{
{
        char           *timezone ();
        char           *timezone ();
        struct timeval  tv;
        struct timeval  tv;
        struct timezone tz;
        struct timezone tz;
 
 
        gettimeofday (&tv, &tz);
        gettimeofday (&tv, &tz);
 
 
        return timezone (tz.tz_minuteswest, tp->tm_isdst);
        return timezone (tz.tz_minuteswest, tp->tm_isdst);
}
}
 
 
#  endif /* HAVE_TIMEZONE */
#  endif /* HAVE_TIMEZONE */
# endif /* HAVE_SV_TIMEZONE */
# endif /* HAVE_SV_TIMEZONE */
#endif /* HAVE_STRFTIME */
#endif /* HAVE_STRFTIME */
 
 
static int
static int
range(low,item,hi)
range(low,item,hi)
int low, item, hi;
int low, item, hi;
{
{
        if (item < low) return low;
        if (item < low) return low;
        if (item > hi) return hi;
        if (item > hi) return hi;
        return item;
        return item;
}
}
 
 
/* strftime --- produce formatted time */
/* strftime --- produce formatted time */
 
 
void
void
/*size_t*/
/*size_t*/
#ifndef __STDC__
#ifndef __STDC__
exp_strftime(/*s,*/ format, timeptr, dstring)
exp_strftime(/*s,*/ format, timeptr, dstring)
/*char *s;*/
/*char *s;*/
char *format;
char *format;
const struct tm *timeptr;
const struct tm *timeptr;
Tcl_DString *dstring;
Tcl_DString *dstring;
#else
#else
/*exp_strftime(char *s, size_t maxsize, const char *format, const struct tm *timeptr)*/
/*exp_strftime(char *s, size_t maxsize, const char *format, const struct tm *timeptr)*/
exp_strftime(char *format, const struct tm *timeptr,Tcl_DString *dstring)
exp_strftime(char *format, const struct tm *timeptr,Tcl_DString *dstring)
#endif
#endif
{
{
        int copied;     /* used to suppress copying when called recursively */
        int copied;     /* used to suppress copying when called recursively */
 
 
#if 0
#if 0
        char *endp = s + maxsize;
        char *endp = s + maxsize;
        char *start = s;
        char *start = s;
#endif
#endif
        char *percentptr;
        char *percentptr;
 
 
        char tbuf[100];
        char tbuf[100];
        int i;
        int i;
 
 
        /* various tables, useful in North America */
        /* various tables, useful in North America */
        static char *days_a[] = {
        static char *days_a[] = {
                "Sun", "Mon", "Tue", "Wed",
                "Sun", "Mon", "Tue", "Wed",
                "Thu", "Fri", "Sat",
                "Thu", "Fri", "Sat",
        };
        };
        static char *days_l[] = {
        static char *days_l[] = {
                "Sunday", "Monday", "Tuesday", "Wednesday",
                "Sunday", "Monday", "Tuesday", "Wednesday",
                "Thursday", "Friday", "Saturday",
                "Thursday", "Friday", "Saturday",
        };
        };
        static char *months_a[] = {
        static char *months_a[] = {
                "Jan", "Feb", "Mar", "Apr", "May", "Jun",
                "Jan", "Feb", "Mar", "Apr", "May", "Jun",
                "Jul", "Aug", "Sep", "Oct", "Nov", "Dec",
                "Jul", "Aug", "Sep", "Oct", "Nov", "Dec",
        };
        };
        static char *months_l[] = {
        static char *months_l[] = {
                "January", "February", "March", "April",
                "January", "February", "March", "April",
                "May", "June", "July", "August", "September",
                "May", "June", "July", "August", "September",
                "October", "November", "December",
                "October", "November", "December",
        };
        };
        static char *ampm[] = { "AM", "PM", };
        static char *ampm[] = { "AM", "PM", };
 
 
/*      for (; *format && s < endp - 1; format++) {*/
/*      for (; *format && s < endp - 1; format++) {*/
        for (; *format ; format++) {
        for (; *format ; format++) {
                tbuf[0] = '\0';
                tbuf[0] = '\0';
                copied = 0;              /* has not been copied yet */
                copied = 0;              /* has not been copied yet */
                percentptr = strchr(format,'%');
                percentptr = strchr(format,'%');
                if (percentptr == 0) {
                if (percentptr == 0) {
                        Tcl_DStringAppend(dstring,format,-1);
                        Tcl_DStringAppend(dstring,format,-1);
                        goto out;
                        goto out;
                } else if (percentptr != format) {
                } else if (percentptr != format) {
                        Tcl_DStringAppend(dstring,format,percentptr - format);
                        Tcl_DStringAppend(dstring,format,percentptr - format);
                        format = percentptr;
                        format = percentptr;
                }
                }
#if 0
#if 0
                if (*format != '%') {
                if (*format != '%') {
                        *s++ = *format;
                        *s++ = *format;
                        continue;
                        continue;
                }
                }
#endif
#endif
        again:
        again:
                switch (*++format) {
                switch (*++format) {
                case '\0':
                case '\0':
                        Tcl_DStringAppend(dstring,"%",1);
                        Tcl_DStringAppend(dstring,"%",1);
#if 0
#if 0
                        *s++ = '%';
                        *s++ = '%';
#endif
#endif
                        goto out;
                        goto out;
 
 
                case '%':
                case '%':
                        Tcl_DStringAppend(dstring,"%",1);
                        Tcl_DStringAppend(dstring,"%",1);
                        copied = 1;
                        copied = 1;
                        break;
                        break;
#if 0
#if 0
                        *s++ = '%';
                        *s++ = '%';
                        continue;
                        continue;
#endif
#endif
 
 
                case 'a':       /* abbreviated weekday name */
                case 'a':       /* abbreviated weekday name */
                        if (timeptr->tm_wday < 0 || timeptr->tm_wday > 6)
                        if (timeptr->tm_wday < 0 || timeptr->tm_wday > 6)
                                strcpy(tbuf, "?");
                                strcpy(tbuf, "?");
                        else
                        else
                                strcpy(tbuf, days_a[timeptr->tm_wday]);
                                strcpy(tbuf, days_a[timeptr->tm_wday]);
                        break;
                        break;
 
 
                case 'A':       /* full weekday name */
                case 'A':       /* full weekday name */
                        if (timeptr->tm_wday < 0 || timeptr->tm_wday > 6)
                        if (timeptr->tm_wday < 0 || timeptr->tm_wday > 6)
                                strcpy(tbuf, "?");
                                strcpy(tbuf, "?");
                        else
                        else
                                strcpy(tbuf, days_l[timeptr->tm_wday]);
                                strcpy(tbuf, days_l[timeptr->tm_wday]);
                        break;
                        break;
 
 
#ifdef SYSV_EXT
#ifdef SYSV_EXT
                case 'h':       /* abbreviated month name */
                case 'h':       /* abbreviated month name */
#endif
#endif
                case 'b':       /* abbreviated month name */
                case 'b':       /* abbreviated month name */
                        if (timeptr->tm_mon < 0 || timeptr->tm_mon > 11)
                        if (timeptr->tm_mon < 0 || timeptr->tm_mon > 11)
                                strcpy(tbuf, "?");
                                strcpy(tbuf, "?");
                        else
                        else
                                strcpy(tbuf, months_a[timeptr->tm_mon]);
                                strcpy(tbuf, months_a[timeptr->tm_mon]);
                        break;
                        break;
 
 
                case 'B':       /* full month name */
                case 'B':       /* full month name */
                        if (timeptr->tm_mon < 0 || timeptr->tm_mon > 11)
                        if (timeptr->tm_mon < 0 || timeptr->tm_mon > 11)
                                strcpy(tbuf, "?");
                                strcpy(tbuf, "?");
                        else
                        else
                                strcpy(tbuf, months_l[timeptr->tm_mon]);
                                strcpy(tbuf, months_l[timeptr->tm_mon]);
                        break;
                        break;
 
 
                case 'c':       /* appropriate date and time representation */
                case 'c':       /* appropriate date and time representation */
                        sprintf(tbuf, "%s %s %2d %02d:%02d:%02d %d",
                        sprintf(tbuf, "%s %s %2d %02d:%02d:%02d %d",
                                days_a[range(0, timeptr->tm_wday, 6)],
                                days_a[range(0, timeptr->tm_wday, 6)],
                                months_a[range(0, timeptr->tm_mon, 11)],
                                months_a[range(0, timeptr->tm_mon, 11)],
                                range(1, timeptr->tm_mday, 31),
                                range(1, timeptr->tm_mday, 31),
                                range(0, timeptr->tm_hour, 23),
                                range(0, timeptr->tm_hour, 23),
                                range(0, timeptr->tm_min, 59),
                                range(0, timeptr->tm_min, 59),
                                range(0, timeptr->tm_sec, 61),
                                range(0, timeptr->tm_sec, 61),
                                timeptr->tm_year + 1900);
                                timeptr->tm_year + 1900);
                        break;
                        break;
 
 
                case 'd':       /* day of the month, 01 - 31 */
                case 'd':       /* day of the month, 01 - 31 */
                        i = range(1, timeptr->tm_mday, 31);
                        i = range(1, timeptr->tm_mday, 31);
                        sprintf(tbuf, "%02d", i);
                        sprintf(tbuf, "%02d", i);
                        break;
                        break;
 
 
                case 'H':       /* hour, 24-hour clock, 00 - 23 */
                case 'H':       /* hour, 24-hour clock, 00 - 23 */
                        i = range(0, timeptr->tm_hour, 23);
                        i = range(0, timeptr->tm_hour, 23);
                        sprintf(tbuf, "%02d", i);
                        sprintf(tbuf, "%02d", i);
                        break;
                        break;
 
 
                case 'I':       /* hour, 12-hour clock, 01 - 12 */
                case 'I':       /* hour, 12-hour clock, 01 - 12 */
                        i = range(0, timeptr->tm_hour, 23);
                        i = range(0, timeptr->tm_hour, 23);
                        if (i == 0)
                        if (i == 0)
                                i = 12;
                                i = 12;
                        else if (i > 12)
                        else if (i > 12)
                                i -= 12;
                                i -= 12;
                        sprintf(tbuf, "%02d", i);
                        sprintf(tbuf, "%02d", i);
                        break;
                        break;
 
 
                case 'j':       /* day of the year, 001 - 366 */
                case 'j':       /* day of the year, 001 - 366 */
                        sprintf(tbuf, "%03d", timeptr->tm_yday + 1);
                        sprintf(tbuf, "%03d", timeptr->tm_yday + 1);
                        break;
                        break;
 
 
                case 'm':       /* month, 01 - 12 */
                case 'm':       /* month, 01 - 12 */
                        i = range(0, timeptr->tm_mon, 11);
                        i = range(0, timeptr->tm_mon, 11);
                        sprintf(tbuf, "%02d", i + 1);
                        sprintf(tbuf, "%02d", i + 1);
                        break;
                        break;
 
 
                case 'M':       /* minute, 00 - 59 */
                case 'M':       /* minute, 00 - 59 */
                        i = range(0, timeptr->tm_min, 59);
                        i = range(0, timeptr->tm_min, 59);
                        sprintf(tbuf, "%02d", i);
                        sprintf(tbuf, "%02d", i);
                        break;
                        break;
 
 
                case 'p':       /* am or pm based on 12-hour clock */
                case 'p':       /* am or pm based on 12-hour clock */
                        i = range(0, timeptr->tm_hour, 23);
                        i = range(0, timeptr->tm_hour, 23);
                        if (i < 12)
                        if (i < 12)
                                strcpy(tbuf, ampm[0]);
                                strcpy(tbuf, ampm[0]);
                        else
                        else
                                strcpy(tbuf, ampm[1]);
                                strcpy(tbuf, ampm[1]);
                        break;
                        break;
 
 
                case 'S':       /* second, 00 - 61 */
                case 'S':       /* second, 00 - 61 */
                        i = range(0, timeptr->tm_sec, 61);
                        i = range(0, timeptr->tm_sec, 61);
                        sprintf(tbuf, "%02d", i);
                        sprintf(tbuf, "%02d", i);
                        break;
                        break;
 
 
                case 'U':       /* week of year, Sunday is first day of week */
                case 'U':       /* week of year, Sunday is first day of week */
                        sprintf(tbuf, "%02d", weeknumber(timeptr, 0));
                        sprintf(tbuf, "%02d", weeknumber(timeptr, 0));
                        break;
                        break;
 
 
                case 'w':       /* weekday, Sunday == 0, 0 - 6 */
                case 'w':       /* weekday, Sunday == 0, 0 - 6 */
                        i = range(0, timeptr->tm_wday, 6);
                        i = range(0, timeptr->tm_wday, 6);
                        sprintf(tbuf, "%d", i);
                        sprintf(tbuf, "%d", i);
                        break;
                        break;
 
 
                case 'W':       /* week of year, Monday is first day of week */
                case 'W':       /* week of year, Monday is first day of week */
                        sprintf(tbuf, "%02d", weeknumber(timeptr, 1));
                        sprintf(tbuf, "%02d", weeknumber(timeptr, 1));
                        break;
                        break;
 
 
                case 'x':       /* appropriate date representation */
                case 'x':       /* appropriate date representation */
                        sprintf(tbuf, "%s %s %2d %d",
                        sprintf(tbuf, "%s %s %2d %d",
                                days_a[range(0, timeptr->tm_wday, 6)],
                                days_a[range(0, timeptr->tm_wday, 6)],
                                months_a[range(0, timeptr->tm_mon, 11)],
                                months_a[range(0, timeptr->tm_mon, 11)],
                                range(1, timeptr->tm_mday, 31),
                                range(1, timeptr->tm_mday, 31),
                                timeptr->tm_year + 1900);
                                timeptr->tm_year + 1900);
                        break;
                        break;
 
 
                case 'X':       /* appropriate time representation */
                case 'X':       /* appropriate time representation */
                        sprintf(tbuf, "%02d:%02d:%02d",
                        sprintf(tbuf, "%02d:%02d:%02d",
                                range(0, timeptr->tm_hour, 23),
                                range(0, timeptr->tm_hour, 23),
                                range(0, timeptr->tm_min, 59),
                                range(0, timeptr->tm_min, 59),
                                range(0, timeptr->tm_sec, 61));
                                range(0, timeptr->tm_sec, 61));
                        break;
                        break;
 
 
                case 'y':       /* year without a century, 00 - 99 */
                case 'y':       /* year without a century, 00 - 99 */
                        i = timeptr->tm_year % 100;
                        i = timeptr->tm_year % 100;
                        sprintf(tbuf, "%02d", i);
                        sprintf(tbuf, "%02d", i);
                        break;
                        break;
 
 
                case 'Y':       /* year with century */
                case 'Y':       /* year with century */
                        sprintf(tbuf, "%d", 1900 + timeptr->tm_year);
                        sprintf(tbuf, "%d", 1900 + timeptr->tm_year);
                        break;
                        break;
 
 
                case 'Z':       /* time zone name or abbrevation */
                case 'Z':       /* time zone name or abbrevation */
#if defined(HAVE_STRFTIME)
#if defined(HAVE_STRFTIME)
                        strftime(tbuf,sizeof tbuf,"%Z",timeptr);
                        strftime(tbuf,sizeof tbuf,"%Z",timeptr);
#else
#else
# if defined(HAVE_SV_TIMEZONE)
# if defined(HAVE_SV_TIMEZONE)
                        i = 0;
                        i = 0;
                        if (daylight && timeptr->tm_isdst)
                        if (daylight && timeptr->tm_isdst)
                                i = 1;
                                i = 1;
                        strcpy(tbuf, tzname[i]);
                        strcpy(tbuf, tzname[i]);
# else
# else
                        strcpy(tbuf, zone_name (timeptr));
                        strcpy(tbuf, zone_name (timeptr));
#  if defined(HAVE_TIMEZONE)
#  if defined(HAVE_TIMEZONE)
#  endif /* HAVE_TIMEZONE */
#  endif /* HAVE_TIMEZONE */
                        /* no timezone available */
                        /* no timezone available */
                        /* feel free to add others here */
                        /* feel free to add others here */
# endif /* HAVE_SV_TIMEZONE */
# endif /* HAVE_SV_TIMEZONE */
#endif /* HAVE STRFTIME */
#endif /* HAVE STRFTIME */
                        break;
                        break;
 
 
#ifdef SYSV_EXT
#ifdef SYSV_EXT
                case 'n':       /* same as \n */
                case 'n':       /* same as \n */
                        tbuf[0] = '\n';
                        tbuf[0] = '\n';
                        tbuf[1] = '\0';
                        tbuf[1] = '\0';
                        break;
                        break;
 
 
                case 't':       /* same as \t */
                case 't':       /* same as \t */
                        tbuf[0] = '\t';
                        tbuf[0] = '\t';
                        tbuf[1] = '\0';
                        tbuf[1] = '\0';
                        break;
                        break;
 
 
                case 'D':       /* date as %m/%d/%y */
                case 'D':       /* date as %m/%d/%y */
                        exp_strftime("%m/%d/%y", timeptr, dstring);
                        exp_strftime("%m/%d/%y", timeptr, dstring);
                        copied = 1;
                        copied = 1;
/*                      exp_strftime(tbuf, sizeof tbuf, "%m/%d/%y", timeptr);*/
/*                      exp_strftime(tbuf, sizeof tbuf, "%m/%d/%y", timeptr);*/
                        break;
                        break;
 
 
                case 'e':       /* day of month, blank padded */
                case 'e':       /* day of month, blank padded */
                        sprintf(tbuf, "%2d", range(1, timeptr->tm_mday, 31));
                        sprintf(tbuf, "%2d", range(1, timeptr->tm_mday, 31));
                        break;
                        break;
 
 
                case 'r':       /* time as %I:%M:%S %p */
                case 'r':       /* time as %I:%M:%S %p */
                        exp_strftime("%I:%M:%S %p", timeptr, dstring);
                        exp_strftime("%I:%M:%S %p", timeptr, dstring);
                        copied = 1;
                        copied = 1;
/*                      exp_strftime(tbuf, sizeof tbuf, "%I:%M:%S %p", timeptr);*/
/*                      exp_strftime(tbuf, sizeof tbuf, "%I:%M:%S %p", timeptr);*/
                        break;
                        break;
 
 
                case 'R':       /* time as %H:%M */
                case 'R':       /* time as %H:%M */
                        exp_strftime("%H:%M", timeptr, dstring);
                        exp_strftime("%H:%M", timeptr, dstring);
                        copied = 1;
                        copied = 1;
/*                      exp_strftime(tbuf, sizeof tbuf, "%H:%M", timeptr);*/
/*                      exp_strftime(tbuf, sizeof tbuf, "%H:%M", timeptr);*/
                        break;
                        break;
 
 
                case 'T':       /* time as %H:%M:%S */
                case 'T':       /* time as %H:%M:%S */
                        exp_strftime("%H:%M:%S", timeptr, dstring);
                        exp_strftime("%H:%M:%S", timeptr, dstring);
                        copied = 1;
                        copied = 1;
/*                      exp_strftime(tbuf, sizeof tbuf, "%H:%M:%S", timeptr);*/
/*                      exp_strftime(tbuf, sizeof tbuf, "%H:%M:%S", timeptr);*/
                        break;
                        break;
#endif
#endif
 
 
#ifdef POSIX2_DATE
#ifdef POSIX2_DATE
                case 'C':
                case 'C':
                        sprintf(tbuf, "%02d", (timeptr->tm_year + 1900) / 100);
                        sprintf(tbuf, "%02d", (timeptr->tm_year + 1900) / 100);
                        break;
                        break;
 
 
 
 
                case 'E':
                case 'E':
                case 'O':
                case 'O':
                        /* POSIX locale extensions, ignored for now */
                        /* POSIX locale extensions, ignored for now */
                        goto again;
                        goto again;
                case 'V':       /* week of year according ISO 8601 */
                case 'V':       /* week of year according ISO 8601 */
                        sprintf(tbuf, "%02d", iso8601wknum(timeptr));
                        sprintf(tbuf, "%02d", iso8601wknum(timeptr));
                        break;
                        break;
 
 
                case 'u':
                case 'u':
                /* ISO 8601: Weekday as a decimal number [1 (Monday) - 7] */
                /* ISO 8601: Weekday as a decimal number [1 (Monday) - 7] */
                        sprintf(tbuf, "%d", timeptr->tm_wday == 0 ? 7 :
                        sprintf(tbuf, "%d", timeptr->tm_wday == 0 ? 7 :
                                        timeptr->tm_wday);
                                        timeptr->tm_wday);
                        break;
                        break;
#endif  /* POSIX2_DATE */
#endif  /* POSIX2_DATE */
                default:
                default:
                        tbuf[0] = '%';
                        tbuf[0] = '%';
                        tbuf[1] = *format;
                        tbuf[1] = *format;
                        tbuf[2] = '\0';
                        tbuf[2] = '\0';
                        break;
                        break;
                }
                }
                if (!copied)
                if (!copied)
                        Tcl_DStringAppend(dstring,tbuf,-1);
                        Tcl_DStringAppend(dstring,tbuf,-1);
#if 0
#if 0
                i = strlen(tbuf);
                i = strlen(tbuf);
                if (i) {
                if (i) {
                        if (s + i < endp - 1) {
                        if (s + i < endp - 1) {
                                strcpy(s, tbuf);
                                strcpy(s, tbuf);
                                s += i;
                                s += i;
                        } else
                        } else
                                return 0;
                                return 0;
#endif
#endif
        }
        }
out:;
out:;
#if 0
#if 0
        if (s < endp && *format == '\0') {
        if (s < endp && *format == '\0') {
                *s = '\0';
                *s = '\0';
                return (s - start);
                return (s - start);
        } else
        } else
                return 0;
                return 0;
#endif
#endif
}
}
 
 
/* isleap --- is a year a leap year? */
/* isleap --- is a year a leap year? */
 
 
#ifndef __STDC__
#ifndef __STDC__
static int
static int
isleap(year)
isleap(year)
int year;
int year;
#else
#else
static int
static int
isleap(int year)
isleap(int year)
#endif
#endif
{
{
        return ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0);
        return ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0);
}
}
 
 
#ifdef POSIX2_DATE
#ifdef POSIX2_DATE
/* iso8601wknum --- compute week number according to ISO 8601 */
/* iso8601wknum --- compute week number according to ISO 8601 */
 
 
#ifndef __STDC__
#ifndef __STDC__
static int
static int
iso8601wknum(timeptr)
iso8601wknum(timeptr)
const struct tm *timeptr;
const struct tm *timeptr;
#else
#else
static int
static int
iso8601wknum(const struct tm *timeptr)
iso8601wknum(const struct tm *timeptr)
#endif
#endif
{
{
        /*
        /*
         * From 1003.2:
         * From 1003.2:
         *      If the week (Monday to Sunday) containing January 1
         *      If the week (Monday to Sunday) containing January 1
         *      has four or more days in the new year, then it is week 1;
         *      has four or more days in the new year, then it is week 1;
         *      otherwise it is the highest numbered week of the previous
         *      otherwise it is the highest numbered week of the previous
         *      (52 or 53) year, and the next week is week 1.
         *      (52 or 53) year, and the next week is week 1.
         *
         *
         * ADR: This means if Jan 1 was Monday through Thursday,
         * ADR: This means if Jan 1 was Monday through Thursday,
         *      it was week 1, otherwise week 53.
         *      it was week 1, otherwise week 53.
         *
         *
         * XPG4 erroneously included POSIX.2 rationale text in the
         * XPG4 erroneously included POSIX.2 rationale text in the
         * main body of the standard. Thus it requires week 53.
         * main body of the standard. Thus it requires week 53.
         */
         */
 
 
        int weeknum, jan1day, diff;
        int weeknum, jan1day, diff;
 
 
        /* get week number, Monday as first day of the week */
        /* get week number, Monday as first day of the week */
        weeknum = weeknumber(timeptr, 1);
        weeknum = weeknumber(timeptr, 1);
 
 
        /*
        /*
         * With thanks and tip of the hatlo to tml@tik.vtt.fi
         * With thanks and tip of the hatlo to tml@tik.vtt.fi
         *
         *
         * What day of the week does January 1 fall on?
         * What day of the week does January 1 fall on?
         * We know that
         * We know that
         *      (timeptr->tm_yday - jan1.tm_yday) MOD 7 ==
         *      (timeptr->tm_yday - jan1.tm_yday) MOD 7 ==
         *              (timeptr->tm_wday - jan1.tm_wday) MOD 7
         *              (timeptr->tm_wday - jan1.tm_wday) MOD 7
         * and that
         * and that
         *      jan1.tm_yday == 0
         *      jan1.tm_yday == 0
         * and that
         * and that
         *      timeptr->tm_wday MOD 7 == timeptr->tm_wday
         *      timeptr->tm_wday MOD 7 == timeptr->tm_wday
         * from which it follows that. . .
         * from which it follows that. . .
         */
         */
        jan1day = timeptr->tm_wday - (timeptr->tm_yday % 7);
        jan1day = timeptr->tm_wday - (timeptr->tm_yday % 7);
        if (jan1day < 0)
        if (jan1day < 0)
                jan1day += 7;
                jan1day += 7;
 
 
        /*
        /*
         * If Jan 1 was a Monday through Thursday, it was in
         * If Jan 1 was a Monday through Thursday, it was in
         * week 1.  Otherwise it was last year's highest week, which is
         * week 1.  Otherwise it was last year's highest week, which is
         * this year's week 0.
         * this year's week 0.
         *
         *
         * What does that mean?
         * What does that mean?
         * If Jan 1 was Monday, the week number is exactly right, it can
         * If Jan 1 was Monday, the week number is exactly right, it can
         *      never be 0.
         *      never be 0.
         * If it was Tuesday through Thursday, the weeknumber is one
         * If it was Tuesday through Thursday, the weeknumber is one
         *      less than it should be, so we add one.
         *      less than it should be, so we add one.
         * Otherwise, Friday, Saturday or Sunday, the week number is
         * Otherwise, Friday, Saturday or Sunday, the week number is
         * OK, but if it is 0, it needs to be 52 or 53.
         * OK, but if it is 0, it needs to be 52 or 53.
         */
         */
        switch (jan1day) {
        switch (jan1day) {
        case 1:         /* Monday */
        case 1:         /* Monday */
                break;
                break;
        case 2:         /* Tuesday */
        case 2:         /* Tuesday */
        case 3:         /* Wednesday */
        case 3:         /* Wednesday */
        case 4:         /* Thursday */
        case 4:         /* Thursday */
                weeknum++;
                weeknum++;
                break;
                break;
        case 5:         /* Friday */
        case 5:         /* Friday */
        case 6:         /* Saturday */
        case 6:         /* Saturday */
        case 0:          /* Sunday */
        case 0:          /* Sunday */
                if (weeknum == 0) {
                if (weeknum == 0) {
#ifdef USE_BROKEN_XPG4
#ifdef USE_BROKEN_XPG4
                        /* XPG4 (as of March 1994) says 53 unconditionally */
                        /* XPG4 (as of March 1994) says 53 unconditionally */
                        weeknum = 53;
                        weeknum = 53;
#else
#else
                        /* get week number of last week of last year */
                        /* get week number of last week of last year */
                        struct tm dec31ly;      /* 12/31 last year */
                        struct tm dec31ly;      /* 12/31 last year */
                        dec31ly = *timeptr;
                        dec31ly = *timeptr;
                        dec31ly.tm_year--;
                        dec31ly.tm_year--;
                        dec31ly.tm_mon = 11;
                        dec31ly.tm_mon = 11;
                        dec31ly.tm_mday = 31;
                        dec31ly.tm_mday = 31;
                        dec31ly.tm_wday = (jan1day == 0) ? 6 : jan1day - 1;
                        dec31ly.tm_wday = (jan1day == 0) ? 6 : jan1day - 1;
                        dec31ly.tm_yday = 364 + isleap(dec31ly.tm_year + 1900);
                        dec31ly.tm_yday = 364 + isleap(dec31ly.tm_year + 1900);
                        weeknum = iso8601wknum(& dec31ly);
                        weeknum = iso8601wknum(& dec31ly);
#endif
#endif
                }
                }
                break;
                break;
        }
        }
 
 
        if (timeptr->tm_mon == 11) {
        if (timeptr->tm_mon == 11) {
                /*
                /*
                 * The last week of the year
                 * The last week of the year
                 * can be in week 1 of next year.
                 * can be in week 1 of next year.
                 * Sigh.
                 * Sigh.
                 *
                 *
                 * This can only happen if
                 * This can only happen if
                 *      M   T  W
                 *      M   T  W
                 *      29  30 31
                 *      29  30 31
                 *      30  31
                 *      30  31
                 *      31
                 *      31
                 */
                 */
                int wday, mday;
                int wday, mday;
 
 
                wday = timeptr->tm_wday;
                wday = timeptr->tm_wday;
                mday = timeptr->tm_mday;
                mday = timeptr->tm_mday;
                if (   (wday == 1 && (mday >= 29 && mday <= 31))
                if (   (wday == 1 && (mday >= 29 && mday <= 31))
                    || (wday == 2 && (mday == 30 || mday == 31))
                    || (wday == 2 && (mday == 30 || mday == 31))
                    || (wday == 3 &&  mday == 31))
                    || (wday == 3 &&  mday == 31))
                        weeknum = 1;
                        weeknum = 1;
        }
        }
 
 
        return weeknum;
        return weeknum;
}
}
#endif
#endif
 
 
/* weeknumber --- figure how many weeks into the year */
/* weeknumber --- figure how many weeks into the year */
 
 
/* With thanks and tip of the hatlo to ado@elsie.nci.nih.gov */
/* With thanks and tip of the hatlo to ado@elsie.nci.nih.gov */
 
 
#ifndef __STDC__
#ifndef __STDC__
static int
static int
weeknumber(timeptr, firstweekday)
weeknumber(timeptr, firstweekday)
const struct tm *timeptr;
const struct tm *timeptr;
int firstweekday;
int firstweekday;
#else
#else
static int
static int
weeknumber(const struct tm *timeptr, int firstweekday)
weeknumber(const struct tm *timeptr, int firstweekday)
#endif
#endif
{
{
        int wday = timeptr->tm_wday;
        int wday = timeptr->tm_wday;
        int ret;
        int ret;
 
 
        if (firstweekday == 1) {
        if (firstweekday == 1) {
                if (wday == 0)   /* sunday */
                if (wday == 0)   /* sunday */
                        wday = 6;
                        wday = 6;
                else
                else
                        wday--;
                        wday--;
        }
        }
        ret = ((timeptr->tm_yday + 7 - wday) / 7);
        ret = ((timeptr->tm_yday + 7 - wday) / 7);
        if (ret < 0)
        if (ret < 0)
                ret = 0;
                ret = 0;
        return ret;
        return ret;
}
}
 
 

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.