Annotation of OpenXM_contrib/gnuplot/strftime.c, Revision 1.1
1.1 ! maekawa 1: #ifndef lint
! 2: static char *RCSid="$Id: strftime.c,v 1.6 1998/04/14 00:16:24 drd Exp $";
! 3: #endif
! 4:
! 5: /* GNUPLOT - strftime.c */
! 6:
! 7: /*[
! 8: * Copyright 1986 - 1993, 1998 Thomas Williams, Colin Kelley
! 9: *
! 10: * Permission to use, copy, and distribute this software and its
! 11: * documentation for any purpose with or without fee is hereby granted,
! 12: * provided that the above copyright notice appear in all copies and
! 13: * that both that copyright notice and this permission notice appear
! 14: * in supporting documentation.
! 15: *
! 16: * Permission to modify the software is granted, but not the right to
! 17: * distribute the complete modified source code. Modifications are to
! 18: * be distributed as patches to the released version. Permission to
! 19: * distribute binaries produced by compiling modified sources is granted,
! 20: * provided you
! 21: * 1. distribute the corresponding source modifications from the
! 22: * released version in the form of a patch file along with the binaries,
! 23: * 2. add special version identification to distinguish your version
! 24: * in addition to the base release version number,
! 25: * 3. provide your name and address as the primary contact for the
! 26: * support of your modified version, and
! 27: * 4. retain our contact information in regard to use of the base
! 28: * software.
! 29: * Permission to distribute the released version of the source code along
! 30: * with corresponding source modifications in the form of a patch file is
! 31: * granted with same provisions 2 through 4 for binary distributions.
! 32: *
! 33: * This software is provided "as is" without express or implied warranty
! 34: * to the extent permitted by applicable law.
! 35: ]*/
! 36:
! 37: /*
! 38: * Implementation of strftime for systems missing this (e.g. vaxctrl)
! 39: *
! 40: * This code was written based on the NeXT strftime man-page, sample output of
! 41: * the function and an ANSI-C quickreference chart. This code does not use
! 42: * parts of any existing strftime implementation.
! 43: *
! 44: * Apparently not all format chars are implemented, but this was all I had in
! 45: * my documentation.
! 46: *
! 47: * (written by Alexander Lehmann)
! 48: */
! 49:
! 50: #define NOTIMEZONE
! 51:
! 52: #include "plot.h" /* for MAX_LINE_LEN */
! 53: #include "setshow.h" /* for days/months */
! 54:
! 55: #ifdef TEST_STRFTIME /* test case; link with stdfn */
! 56: #define strftime _strftime
! 57:
! 58: #include "stdfn.h" /* for safe_strncpy */
! 59:
! 60: #include "national.h" /* language info for the following, */
! 61: /* extracted from set.c */
! 62:
! 63: char full_month_names[12][32] = { FMON01, FMON02, FMON03, FMON04, FMON05,
! 64: FMON06, FMON07, FMON08, FMON09, FMON10, FMON11, FMON12};
! 65: char abbrev_month_names[12][8] = { AMON01, AMON02, AMON03, AMON04, AMON05,
! 66: AMON06, AMON07, AMON08, AMON09, AMON10, AMON11, AMON12};
! 67:
! 68: char full_day_names[7][32] = { FDAY0, FDAY1, FDAY2, FDAY3, FDAY4, FDAY5, FDAY6 };
! 69: char abbrev_day_names[7][8] = { ADAY0, ADAY1, ADAY2, ADAY3, ADAY4, ADAY5, ADAY6 };
! 70:
! 71: #endif /* TEST_STRFTIME */
! 72:
! 73:
! 74: static void fill(from, pto, pmaxsize)
! 75: char *from;
! 76: char **pto;
! 77: size_t *pmaxsize;
! 78: {
! 79: safe_strncpy(*pto, from, *pmaxsize);
! 80: if(*pmaxsize<strlen(from)) {
! 81: (*pto) += *pmaxsize;
! 82: *pmaxsize = 0;
! 83: } else {
! 84: (*pto) += strlen(from);
! 85: (*pmaxsize) -= strlen(from);
! 86: }
! 87: }
! 88:
! 89: static void number(num, pad, pto, pmaxsize)
! 90: int num;
! 91: int pad;
! 92: char **pto;
! 93: size_t *pmaxsize;
! 94: {
! 95: char str[100];
! 96:
! 97: sprintf(str, "%0*d", pad, num);
! 98: fill(str, pto, pmaxsize);
! 99: }
! 100:
! 101: size_t strftime(s, max, format, tp)
! 102: char *s;
! 103: size_t max;
! 104: const char *format;
! 105: const struct tm *tp;
! 106: {
! 107: char *start = s;
! 108: size_t maxsize = max;
! 109:
! 110: if(max>0) {
! 111: while(*format && max>0) {
! 112: if(*format != '%') {
! 113: *s++ = *format++;
! 114: max--;
! 115: } else {
! 116: format++;
! 117: switch(*format++) {
! 118: case 'a': /* abbreviated weekday name */
! 119: if(tp->tm_wday >= 0 && tp->tm_wday <= 6)
! 120: fill(abbrev_day_names[tp->tm_wday], &s, &max);
! 121: break;
! 122: case 'A': /* full name of the weekday */
! 123: if(tp->tm_wday >= 0 && tp->tm_wday <= 6)
! 124: fill(full_day_names[tp->tm_wday], &s, &max);
! 125: break;
! 126: case 'b': /* abbreviated month name */
! 127: if(tp->tm_mon >= 0 && tp->tm_mon <= 11)
! 128: fill(abbrev_month_names[tp->tm_mon], &s, &max);
! 129: break;
! 130: case 'B': /* full name of month */
! 131: if(tp->tm_mon >= 0 && tp->tm_mon <= 11)
! 132: fill(full_month_names[tp->tm_mon], &s, &max);
! 133: break;
! 134: case 'c': /* locale's date and time reprensentation */
! 135: strftime(s, max, "%a %b %X %Y", tp);
! 136: max -= strlen(s);
! 137: s += strlen(s);
! 138: break;
! 139: case 'd': /* day of the month (01-31) */
! 140: number(tp->tm_mday, 2, &s, &max);
! 141: break;
! 142: case 'H': /* hour of the day (00-23) */
! 143: number(tp->tm_hour, 2, &s, &max);
! 144: break;
! 145: case 'I': /* hour of the day (01-12) */
! 146: number((tp->tm_hour+11)%12+1, 2, &s, &max);
! 147: break;
! 148: case 'j': /* day of the year (001-366) */
! 149: number(tp->tm_yday+1, 3, &s, &max);
! 150: break;
! 151: case 'm': /* month of the year (01-12) */
! 152: number(tp->tm_mon+1, 2,&s, &max);
! 153: break;
! 154: case 'M': /* minute (00-59) */
! 155: number(tp->tm_min, 2, &s, &max);
! 156: break;
! 157: case 'p': /* locale's version of AM or PM */
! 158: fill(tp->tm_hour >= 6 ? "PM" : "AM", &s, &max);
! 159: break;
! 160: case 'S': /* seconds (00-59) */
! 161: number(tp->tm_sec, 2, &s, &max);
! 162: break;
! 163: case 'U': /* week number of the year (00-53) with Sunday as the first day of the week */
! 164: number((tp->tm_yday-(tp->tm_yday-tp->tm_wday+7)%7+7)/7, 1, &s, &max);
! 165: break;
! 166: case 'w': /* weekday (Sunday = 0 to Saturday = 6) */
! 167: number(tp->tm_wday, 1, &s, &max);
! 168: break;
! 169: case 'W': /* week number of the year (00-53) with Monday as the first day of the week */
! 170: number((tp->tm_yday-(tp->tm_yday-tp->tm_wday+8)%7+7)/7, 2, &s, &max);
! 171: break;
! 172: case 'x': /* locale's date representation */
! 173: strftime(s, max, "%a %b %d %Y", tp);
! 174: max -= strlen(s);
! 175: s += strlen(s);
! 176: break;
! 177: case 'X': /* locale's time representation */
! 178: #ifndef NOTIMEZONE
! 179: strftime(s, max, "%H:%M:%S %Z", tp);
! 180: #else
! 181: strftime(s, max, "%H:%M:%S", tp);
! 182: #endif
! 183: max -= strlen(s);
! 184: s += strlen(s);
! 185: break;
! 186: case 'y': /* two-digit year representation (00-99) */
! 187: number(tp->tm_year%100, 2, &s, &max);
! 188: break;
! 189: case 'Y': /* four-digit year representation */
! 190: number(tp->tm_year+1900, 2, &s, &max);
! 191: break;
! 192: #ifndef NOTIMEZONE
! 193: case 'Z': /* time zone name */
! 194: fill(tp->tm_zone, &s, &max);
! 195: break;
! 196: #endif
! 197: case '%': /* percent sign */
! 198: default:
! 199: *s++ = *(format-1);
! 200: max--;
! 201: break;
! 202: }
! 203: }
! 204: }
! 205: if(s-start<maxsize) {
! 206: *s++ = '\0';
! 207: } else {
! 208: *(s-1) = '\0';
! 209: }
! 210: }
! 211:
! 212: return s-start;
! 213: }
! 214:
! 215: #ifdef TEST_STRFTIME
! 216:
! 217: #undef strftime
! 218: #define test(s) \
! 219: printf("%s -> ",s ); \
! 220: _strftime(str, 100, s, ts); \
! 221: printf("%s - ", str); \
! 222: strftime(str, 100, s, ts); \
! 223: printf("%s\n", str)
! 224:
! 225: int main()
! 226: {
! 227: char str[100];
! 228: struct tm *ts;
! 229: time_t t;
! 230: int i;
! 231:
! 232: t = time(NULL);
! 233:
! 234: ts = localtime(&t);
! 235:
! 236: test("%c");
! 237: test("test%%test");
! 238: test("%a %b %d %X %Y");
! 239: test("%x %X");
! 240: test("%A %B %U");
! 241: test("%I:%M %p %j %w");
! 242:
! 243: t -= 245*24*60*60;
! 244:
! 245: for(i = 0;i<366;i++) {
! 246: ts = localtime(&t);
! 247: printf("%03d: ", i);
! 248: test("%a %d %m %W");
! 249: t += 24*60*60;
! 250: }
! 251:
! 252: return 0;
! 253: }
! 254:
! 255: #endif
! 256:
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>