66 #if defined(TM_IN_SYS_TIME) 67 #include <sys/types.h> 78 #define POSIX_SEMANTICS 1 87 extern void tzset(
void);
88 static int weeknumber(
const struct tm *timeptr,
int firstweekday);
93 #define inline __inline__ 99 #define range(low, item, hi) max(low, min(item, hi)) 102 #if !defined(OS2) && !defined(MSDOS) && !defined(__CYGWIN__) && defined(HAVE_TZNAME) 103 extern char *tzname[2];
105 #if defined(SOLARIS) || defined(mips) || defined (M_UNIX) 106 extern long int timezone, altzone;
108 # if defined (HPUX) || defined(__hpux) 109 extern long int timezone;
111 # if !defined(__CYGWIN__) 112 extern int timezone, altzone;
125 return (a < b ? a : b);
135 return (a > b ? a : b);
147 if (year >= -9999 && year <= 9999) {
148 sprintf(buf,
"%0*d", (
int) fw, year);
160 extra = year / 10000;
162 sprintf(buf,
"%c_%04d_%d", sign, extra, year);
169 strftime(
char *s,
size_t maxsize,
const char *format,
const struct tm *timeptr)
171 char *endp = s + maxsize;
177 static short first = 1;
178 #ifdef POSIX_SEMANTICS 179 static char *savetz =
NULL;
180 static int savetzlen = 0;
187 extern char *timezone();
189 struct timezone zone;
201 static const char *days_a[] = {
202 "Sun",
"Mon",
"Tue",
"Wed",
205 static const char *days_l[] = {
206 "Sunday",
"Monday",
"Tuesday",
"Wednesday",
207 "Thursday",
"Friday",
"Saturday",
209 static const char *months_a[] = {
210 "Jan",
"Feb",
"Mar",
"Apr",
"May",
"Jun",
211 "Jul",
"Aug",
"Sep",
"Oct",
"Nov",
"Dec",
213 static const char *months_l[] = {
214 "January",
"February",
"March",
"April",
215 "May",
"June",
"July",
"August",
"September",
216 "October",
"November",
"December",
218 static const char *ampm[] = {
"AM",
"PM", };
220 if (s ==
NULL || format ==
NULL || timeptr ==
NULL || maxsize == 0)
224 if (
strchr(format,
'%') ==
NULL && strlen(format) + 1 >= maxsize)
227 #ifndef POSIX_SEMANTICS 240 int tzlen = strlen(tz);
242 savetz = (
char *)
malloc(tzlen + 1);
243 if (savetz !=
NULL) {
244 savetzlen = tzlen + 1;
252 if (tz && savetz && (tz[0] != savetz[0] || strcmp(tz, savetz) != 0)) {
255 savetz = (
char *)
realloc(savetz, i);
266 for (; *format && s < endp - 1; format++) {
268 if (*format !=
'%') {
300 for (; isdigit(*format); format++) {
301 fw = fw * 10 + (*format -
'0');
317 if (timeptr->tm_wday < 0 || timeptr->tm_wday > 6)
320 strcpy(tbuf, days_a[timeptr->tm_wday]);
324 if (timeptr->tm_wday < 0 || timeptr->tm_wday > 6)
327 strcpy(tbuf, days_l[timeptr->tm_wday]);
332 if (timeptr->tm_mon < 0 || timeptr->tm_mon > 11)
335 strcpy(tbuf, months_a[timeptr->tm_mon]);
339 if (timeptr->tm_mon < 0 || timeptr->tm_mon > 11)
342 strcpy(tbuf, months_l[timeptr->tm_mon]);
353 strftime(tbuf,
sizeof tbuf,
"%A %B %d %T %Y", timeptr);
358 if (pad !=
'\0' && fw > 0) {
359 size_t min_fw = (flag ? 3 : 2);
361 fw =
max(fw, min_fw);
365 (timeptr->tm_year + 1900
L) / 100);
369 sprintf(tbuf,
"%02ld", (timeptr->tm_year + 1900
L) / 100);
373 i =
range(1, timeptr->tm_mday, 31);
374 sprintf(tbuf,
"%02d", i);
378 strftime(tbuf,
sizeof tbuf,
"%m/%d/%y", timeptr);
382 sprintf(tbuf,
"%2d",
range(1, timeptr->tm_mday, 31));
397 strftime(m_d,
sizeof m_d,
"-%m-%d", timeptr);
400 if (pad !=
'\0' && fw > 0) {
401 fw =
max(fw, min_fw);
411 strftime(tbuf,
sizeof tbuf,
"%Y-%m-%d", timeptr);
428 if (timeptr->tm_mon == 11 && w == 1)
429 y = 1900
L + timeptr->tm_year + 1;
430 else if (timeptr->tm_mon == 0 && w >= 52)
431 y = 1900
L + timeptr->tm_year - 1;
433 y = 1900
L + timeptr->tm_year;
435 if (*format ==
'G') {
437 if (pad !=
'\0' && fw > 0) {
440 fw =
max(fw, min_fw);
447 sprintf(tbuf,
"%ld", y);
450 sprintf(tbuf,
"%02ld", y % 100);
457 i =
range(0, timeptr->tm_hour, 23);
458 sprintf(tbuf,
"%02d", i);
462 i =
range(0, timeptr->tm_hour, 23);
467 sprintf(tbuf,
"%02d", i);
471 sprintf(tbuf,
"%03d", timeptr->tm_yday + 1);
475 i =
range(0, timeptr->tm_mon, 11);
476 sprintf(tbuf,
"%02d", i + 1);
480 i =
range(0, timeptr->tm_min, 59);
481 sprintf(tbuf,
"%02d", i);
494 i =
range(0, timeptr->tm_hour, 23);
502 strftime(tbuf,
sizeof tbuf,
"%I:%M:%S %p", timeptr);
506 strftime(tbuf,
sizeof tbuf,
"%H:%M", timeptr);
509 #if defined(HAVE_MKTIME) 512 struct tm non_const_timeptr;
514 non_const_timeptr = *timeptr;
515 sprintf(tbuf,
"%ld",
mktime(& non_const_timeptr));
521 i =
range(0, timeptr->tm_sec, 60);
522 sprintf(tbuf,
"%02d", i);
532 strftime(tbuf,
sizeof tbuf,
"%H:%M:%S", timeptr);
537 sprintf(tbuf,
"%d", timeptr->tm_wday == 0 ? 7 :
542 sprintf(tbuf,
"%02d",
weeknumber(timeptr, 0));
550 i =
range(0, timeptr->tm_wday, 6);
551 sprintf(tbuf,
"%d", i);
555 sprintf(tbuf,
"%02d",
weeknumber(timeptr, 1));
559 strftime(tbuf,
sizeof tbuf,
"%A %B %d %Y", timeptr);
568 i = timeptr->tm_year % 100;
569 sprintf(tbuf,
"%02d", i);
574 if (pad !=
'\0' && fw > 0) {
577 fw =
max(fw, min_fw);
581 1900
L + timeptr->tm_year);
584 sprintf(tbuf,
"%ld", 1900
L + timeptr->tm_year);
603 if (timeptr->tm_isdst < 0)
610 off = -timeptr->tm_tzadj / 60;
617 off = timeptr->tm_gmtoff / 60;
624 # if defined(__hpux) || defined (HPUX) || defined(__CYGWIN__) 625 off = -timezone / 60;
628 off = -(daylight ? altzone : timezone) / 60;
631 gettimeofday(& tv, & zone);
632 off = -zone.tz_minuteswest;
642 sprintf(tbuf+1,
"%02ld%02ld", off/60, off%60);
647 i = (daylight && timeptr->tm_isdst > 0);
651 strcpy(tbuf, timeptr->tm_zone);
654 strcpy(tbuf, timeptr->tm_name);
656 gettimeofday(& tv, & zone);
657 strcpy(tbuf, timezone(zone.tz_minuteswest,
658 timeptr->tm_isdst > 0));
666 sprintf(tbuf,
"%2d",
range(0, timeptr->tm_hour, 23));
670 i =
range(0, timeptr->tm_hour, 23);
675 sprintf(tbuf,
"%2d", i);
691 sprintf(tbuf,
"%2d-%3.3s-%4ld",
692 range(1, timeptr->tm_mday, 31),
693 months_a[
range(0, timeptr->tm_mon, 11)],
694 timeptr->tm_year + 1900
L);
695 for (i = 3; i < 6; i++)
696 if (islower(tbuf[i]))
697 tbuf[
i] = toupper(tbuf[i]);
709 if (s + i < endp - 1) {
717 if (s < endp && *format ==
'\0') {
729 return ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0);
752 int weeknum, jan1day, diff;
770 jan1day = timeptr->tm_wday - (timeptr->tm_yday % 7);
799 #ifdef USE_BROKEN_XPG4 808 dec31ly.tm_mday = 31;
809 dec31ly.tm_wday = (jan1day == 0) ? 6 : jan1day - 1;
810 dec31ly.tm_yday = 364 +
isleap(dec31ly.tm_year + 1900
L);
817 if (timeptr->tm_mon == 11) {
831 wday = timeptr->tm_wday;
832 mday = timeptr->tm_mday;
833 if ( (wday == 1 && (mday >= 29 && mday <= 31))
834 || (wday == 2 && (mday == 30 || mday == 31))
835 || (wday == 3 && mday == 31))
849 int wday = timeptr->tm_wday;
852 if (firstweekday == 1) {
858 ret = ((timeptr->tm_yday + 7 - wday) / 7);
867 Date: Wed, 24 Apr 91 20:54:08 MDT
868 From: Michal Jaegermann <audfax!emory!vm.ucs.UAlberta.CA!NTOMCZAK>
869 To: arnold@audiofax.com
872 in a
process of fixing of
strftime() in libraries on Atari ST I grabbed
873 some pieces of code from your own
strftime. When doing that it came
874 to mind that your
weeknumber()
function compiles a little bit nicer
875 in the following form:
880 return (timeptr->tm_yday - timeptr->tm_wday +
881 (firstweekday ? (timeptr->tm_wday ? 8 : 1) : 7)) / 7;
883 How nicer it depends on a compiler, of course, but always a tiny bit.
887 ntomczak@vm.ucs.ualberta.ca
922 #include <sys/time.h> 931 static char *
array[] =
933 "(%%A) full weekday name, var length (Sunday..Saturday) %A",
934 "(%%B) full month name, var length (January..December) %B",
936 "(%%D) date (%%m/%%d/%%y) %D",
937 "(%%E) Locale extensions (ignored) %E",
938 "(%%F) full month name, var length (January..December) %F",
939 "(%%H) hour (24-hour clock, 00..23) %H",
940 "(%%I) hour (12-hour clock, 01..12) %I",
941 "(%%M) minute (00..59) %M",
942 "(%%N) Emporer/Era Name %N",
943 "(%%O) Locale extensions (ignored) %O",
944 "(%%R) time, 24-hour (%%H:%%M) %R",
945 "(%%S) second (00..60) %S",
946 "(%%T) time, 24-hour (%%H:%%M:%%S) %T",
947 "(%%U) week of year, Sunday as first day of week (00..53) %U",
948 "(%%V) week of year according to ISO 8601 %V",
949 "(%%W) week of year, Monday as first day of week (00..53) %W",
950 "(%%X) appropriate locale time representation (%H:%M:%S) %X",
951 "(%%Y) year with century (1970...) %Y",
952 "(%%Z) timezone (EDT), or blank if timezone not determinable %Z",
953 "(%%a) locale's abbreviated weekday name (Sun..Sat) %a",
954 "(%%b) locale's abbreviated month name (Jan..Dec) %b",
955 "(%%c) full date (Sat Nov 4 12:02:33 1989)%n%t%t%t %c",
956 "(%%d) day of the month (01..31) %d",
957 "(%%e) day of the month, blank-padded ( 1..31) %e",
958 "(%%h) should be same as (%%b) %h",
959 "(%%j) day of the year (001..366) %j",
960 "(%%k) hour, 24-hour clock, blank pad ( 0..23) %k",
961 "(%%l) hour, 12-hour clock, blank pad ( 0..12) %l",
962 "(%%m) month (01..12) %m",
963 "(%%o) Emporer/Era Year %o",
964 "(%%p) locale's AM or PM based on 12-hour clock %p",
965 "(%%r) time, 12-hour (same as %%I:%%M:%%S %%p) %r",
966 "(%%u) ISO 8601: Weekday as decimal number [1 (Monday) - 7] %u",
967 "(%%v) VMS date (dd-bbb-YYYY) %v",
968 "(%%w) day of week (0..6, Sunday == 0) %w",
969 "(%%x) appropriate locale date representation %x",
970 "(%%y) last two digits of year (00..99) %y",
971 "(%%z) timezone offset east of GMT as HHMM (e.g. -0500) %z",
985 char string[MAXTIME];
996 clock = time((
long *) 0);
997 tm = localtime(&clock);
999 for (k = 0; next =
array[k]; k++) {
1000 length =
strftime(
string, MAXTIME, next, tm);
1001 printf(
"%s\n",
string);
#define range(low, item, hi)
static int min(int a, int b)
static nls_uint32 nls_uint32 i
static void iso_8601_2000_year(char *buf, int year, size_t fw)
time_t mktime(struct tm *tp)
size_t strftime(char *s, size_t maxsize, const char *format, const struct tm *timeptr)
static int max(int a, int b)
int main(int argc, char **argv)
static int isleap(long year)
static int weeknumber(const struct tm *timeptr, int firstweekday)
static int iso8601wknum(const struct tm *timeptr)
char * get_string_value(char *var_name) const