2008-08-22 01:28:08 -04:00
|
|
|
/* -*- c-file-style: "linux" -*- */
|
|
|
|
|
2008-08-20 11:30:28 -04:00
|
|
|
/*
|
|
|
|
* strftime.c
|
|
|
|
*
|
|
|
|
* Public-domain implementation of ANSI C library routine.
|
|
|
|
*
|
|
|
|
* It's written in old-style C for maximal portability.
|
|
|
|
* 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.
|
|
|
|
* For extensions from SunOS, add SUNOS_EXT.
|
|
|
|
* For stuff needed to implement the P1003.2 date command, add POSIX2_DATE.
|
|
|
|
* For VMS dates, add VMS_EXT.
|
|
|
|
* For a an RFC822 time format, add MAILHEADER_EXT.
|
|
|
|
* For ISO week years, add ISO_DATE_EXT.
|
|
|
|
* For complete POSIX semantics, add POSIX_SEMANTICS.
|
|
|
|
*
|
|
|
|
* The code for %c, %x, and %X now follows the 1003.2 specification for
|
|
|
|
* the POSIX locale.
|
|
|
|
* This version ignores LOCALE information.
|
|
|
|
* It also doesn't worry about multi-byte characters.
|
|
|
|
* So there.
|
|
|
|
*
|
|
|
|
* This file is also shipped with GAWK (GNU Awk), gawk specific bits of
|
|
|
|
* code are included if GAWK is defined.
|
|
|
|
*
|
|
|
|
* Arnold Robbins
|
|
|
|
* January, February, March, 1991
|
|
|
|
* Updated March, April 1992
|
|
|
|
* Updated April, 1993
|
|
|
|
* Updated February, 1994
|
|
|
|
* Updated May, 1994
|
|
|
|
* Updated January, 1995
|
|
|
|
* Updated September, 1995
|
|
|
|
* Updated January, 1996
|
|
|
|
*
|
|
|
|
* Fixes from ado@elsie.nci.nih.gov
|
|
|
|
* February 1991, May 1992
|
|
|
|
* Fixes from Tor Lillqvist tml@tik.vtt.fi
|
|
|
|
* May, 1993
|
|
|
|
* Further fixes from ado@elsie.nci.nih.gov
|
|
|
|
* February 1994
|
|
|
|
* %z code from chip@chinacat.unicom.com
|
|
|
|
* Applied September 1995
|
|
|
|
* %V code fixed (again) and %G, %g added,
|
|
|
|
* January 1996
|
|
|
|
*/
|
|
|
|
|
2020-05-08 05:31:09 -04:00
|
|
|
#include "ruby/internal/config.h"
|
2008-08-20 11:30:28 -04:00
|
|
|
|
|
|
|
#ifndef GAWK
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <ctype.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <time.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#endif
|
2008-10-04 09:48:20 -04:00
|
|
|
#if defined(TM_IN_SYS_TIME) || !defined(GAWK)
|
2008-08-20 11:30:28 -04:00
|
|
|
#include <sys/types.h>
|
2021-04-29 09:12:44 -04:00
|
|
|
#ifdef HAVE_SYS_TIME_H
|
2008-08-20 11:30:28 -04:00
|
|
|
#include <sys/time.h>
|
|
|
|
#endif
|
2008-08-20 11:50:21 -04:00
|
|
|
#endif
|
2008-08-21 10:57:35 -04:00
|
|
|
#include <math.h>
|
2008-08-20 11:30:28 -04:00
|
|
|
|
2019-12-04 03:16:30 -05:00
|
|
|
#include "internal.h"
|
|
|
|
#include "internal/string.h"
|
|
|
|
#include "internal/vm.h"
|
|
|
|
#include "ruby/encoding.h"
|
|
|
|
#include "ruby/ruby.h"
|
2021-07-28 03:40:02 -04:00
|
|
|
#include "ruby/util.h"
|
2019-12-04 03:16:30 -05:00
|
|
|
#include "timev.h"
|
|
|
|
|
2008-08-20 11:30:28 -04:00
|
|
|
/* defaults: season to taste */
|
|
|
|
#define SYSV_EXT 1 /* stuff in System V ascftime routine */
|
|
|
|
#define SUNOS_EXT 1 /* stuff in SunOS strftime routine */
|
|
|
|
#define POSIX2_DATE 1 /* stuff in Posix 1003.2 date command */
|
|
|
|
#define VMS_EXT 1 /* include %v for VMS date format */
|
|
|
|
#define MAILHEADER_EXT 1 /* add %z for HHMM format */
|
|
|
|
#define ISO_DATE_EXT 1 /* %G and %g for year of ISO week */
|
|
|
|
|
|
|
|
#if defined(ISO_DATE_EXT)
|
|
|
|
#if ! defined(POSIX2_DATE)
|
|
|
|
#define POSIX2_DATE 1
|
|
|
|
#endif
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if defined(POSIX2_DATE)
|
|
|
|
#if ! defined(SYSV_EXT)
|
|
|
|
#define SYSV_EXT 1
|
|
|
|
#endif
|
|
|
|
#if ! defined(SUNOS_EXT)
|
|
|
|
#define SUNOS_EXT 1
|
|
|
|
#endif
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if defined(POSIX2_DATE)
|
|
|
|
#define adddecl(stuff) stuff
|
|
|
|
#else
|
|
|
|
#define adddecl(stuff)
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#undef strchr /* avoid AIX weirdness */
|
|
|
|
|
2008-08-20 11:50:21 -04:00
|
|
|
#if !defined __STDC__ && !defined _WIN32
|
2008-08-20 11:30:28 -04:00
|
|
|
#define const /**/
|
|
|
|
static int weeknumber();
|
|
|
|
adddecl(static int iso8601wknum();)
|
2009-04-21 10:56:59 -04:00
|
|
|
static int weeknumber_v();
|
|
|
|
adddecl(static int iso8601wknum_v();)
|
2008-08-20 11:30:28 -04:00
|
|
|
#else
|
|
|
|
static int weeknumber(const struct tm *timeptr, int firstweekday);
|
|
|
|
adddecl(static int iso8601wknum(const struct tm *timeptr);)
|
2009-04-21 10:56:59 -04:00
|
|
|
static int weeknumber_v(const struct vtm *vtm, int firstweekday);
|
|
|
|
adddecl(static int iso8601wknum_v(const struct vtm *vtm);)
|
2008-08-20 11:30:28 -04:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef STDC_HEADERS
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#else
|
|
|
|
extern void *malloc();
|
|
|
|
extern void *realloc();
|
|
|
|
extern char *getenv();
|
|
|
|
extern char *strchr();
|
|
|
|
#endif
|
|
|
|
|
2010-12-31 21:45:04 -05:00
|
|
|
#define range(low, item, hi) max((low), min((item), (hi)))
|
2008-08-20 11:30:28 -04:00
|
|
|
|
|
|
|
#undef min /* just in case */
|
|
|
|
|
|
|
|
/* min --- return minimum of two numbers */
|
|
|
|
|
|
|
|
static inline int
|
|
|
|
min(int a, int b)
|
|
|
|
{
|
|
|
|
return (a < b ? a : b);
|
|
|
|
}
|
|
|
|
|
|
|
|
#undef max /* also, just in case */
|
|
|
|
|
|
|
|
/* max --- return maximum of two numbers */
|
|
|
|
|
|
|
|
static inline int
|
|
|
|
max(int a, int b)
|
|
|
|
{
|
|
|
|
return (a > b ? a : b);
|
|
|
|
}
|
|
|
|
|
2008-08-23 04:06:59 -04:00
|
|
|
#ifdef NO_STRING_LITERAL_CONCATENATION
|
|
|
|
#error No string literal concatenation
|
|
|
|
#endif
|
|
|
|
|
2009-04-21 10:56:59 -04:00
|
|
|
#define add(x,y) (rb_funcall((x), '+', 1, (y)))
|
|
|
|
#define sub(x,y) (rb_funcall((x), '-', 1, (y)))
|
|
|
|
#define mul(x,y) (rb_funcall((x), '*', 1, (y)))
|
|
|
|
#define quo(x,y) (rb_funcall((x), rb_intern("quo"), 1, (y)))
|
|
|
|
#define div(x,y) (rb_funcall((x), rb_intern("div"), 1, (y)))
|
|
|
|
#define mod(x,y) (rb_funcall((x), '%', 1, (y)))
|
|
|
|
|
2008-08-20 11:30:28 -04:00
|
|
|
/* strftime --- produce formatted time */
|
|
|
|
|
2016-03-24 01:20:22 -04:00
|
|
|
enum {LEFT, CHCASE, LOWER, UPPER};
|
|
|
|
#define BIT_OF(n) (1U<<(n))
|
|
|
|
|
2016-03-23 07:57:01 -04:00
|
|
|
static char *
|
|
|
|
resize_buffer(VALUE ftime, char *s, const char **start, const char **endp,
|
2016-06-14 09:22:09 -04:00
|
|
|
ptrdiff_t n, size_t maxsize)
|
2016-03-23 07:57:01 -04:00
|
|
|
{
|
|
|
|
size_t len = s - *start;
|
|
|
|
size_t nlen = len + n * 2;
|
2016-06-14 09:22:09 -04:00
|
|
|
|
|
|
|
if (nlen < len || nlen > maxsize) {
|
|
|
|
return 0;
|
|
|
|
}
|
2016-03-23 07:57:01 -04:00
|
|
|
rb_str_set_len(ftime, len);
|
|
|
|
rb_str_modify_expand(ftime, nlen-len);
|
|
|
|
s = RSTRING_PTR(ftime);
|
|
|
|
*endp = s + nlen;
|
|
|
|
*start = s;
|
|
|
|
return s += len;
|
|
|
|
}
|
|
|
|
|
2016-06-14 09:22:09 -04:00
|
|
|
static void
|
|
|
|
buffer_size_check(const char *s,
|
|
|
|
const char *format_end, size_t format_len,
|
|
|
|
rb_encoding *enc)
|
|
|
|
{
|
|
|
|
if (!s) {
|
|
|
|
const char *format = format_end-format_len;
|
|
|
|
VALUE fmt = rb_enc_str_new(format, format_len, enc);
|
|
|
|
rb_syserr_fail_str(ERANGE, fmt);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-03-24 01:20:22 -04:00
|
|
|
static char *
|
|
|
|
case_conv(char *s, ptrdiff_t i, int flags)
|
|
|
|
{
|
|
|
|
switch (flags & (BIT_OF(UPPER)|BIT_OF(LOWER))) {
|
|
|
|
case BIT_OF(UPPER):
|
|
|
|
do {
|
|
|
|
if (ISLOWER(*s)) *s = TOUPPER(*s);
|
|
|
|
} while (s++, --i);
|
|
|
|
break;
|
|
|
|
case BIT_OF(LOWER):
|
|
|
|
do {
|
|
|
|
if (ISUPPER(*s)) *s = TOLOWER(*s);
|
|
|
|
} while (s++, --i);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
s += i;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
2016-03-24 04:44:03 -04:00
|
|
|
static VALUE
|
2016-03-24 11:49:43 -04:00
|
|
|
format_value(VALUE val, int base)
|
2016-03-24 04:44:03 -04:00
|
|
|
{
|
2021-09-03 07:50:12 -04:00
|
|
|
if (!RB_BIGNUM_TYPE_P(val))
|
2016-03-24 11:49:43 -04:00
|
|
|
val = rb_Integer(val);
|
|
|
|
return rb_big2str(val, base);
|
2016-03-24 04:44:03 -04:00
|
|
|
}
|
|
|
|
|
2011-08-27 05:18:18 -04:00
|
|
|
/*
|
|
|
|
* enc is the encoding of the format. It is used as the encoding of resulted
|
|
|
|
* string, but the name of the month and weekday are always US-ASCII. So it
|
|
|
|
* is only used for the timezone name on Windows.
|
|
|
|
*/
|
2016-03-23 07:57:01 -04:00
|
|
|
static VALUE
|
|
|
|
rb_strftime_with_timespec(VALUE ftime, const char *format, size_t format_len,
|
2018-10-08 00:30:07 -04:00
|
|
|
rb_encoding *enc, VALUE time, const struct vtm *vtm,
|
|
|
|
VALUE timev, struct timespec *ts, int gmt, size_t maxsize)
|
2008-08-20 11:30:28 -04:00
|
|
|
{
|
2016-03-23 07:57:01 -04:00
|
|
|
size_t len = RSTRING_LEN(ftime);
|
|
|
|
char *s = RSTRING_PTR(ftime);
|
|
|
|
const char *start = s;
|
|
|
|
const char *endp = start + rb_str_capacity(ftime);
|
|
|
|
const char *const format_end = format + format_len;
|
2008-08-22 01:28:08 -04:00
|
|
|
const char *sp, *tp;
|
2011-08-27 05:18:18 -04:00
|
|
|
#define TBUFSIZE 100
|
|
|
|
auto char tbuf[TBUFSIZE];
|
2008-08-20 11:30:28 -04:00
|
|
|
long off;
|
2010-03-24 23:08:28 -04:00
|
|
|
ptrdiff_t i;
|
|
|
|
int w;
|
2008-08-20 11:30:28 -04:00
|
|
|
long y;
|
2010-07-07 17:07:01 -04:00
|
|
|
int precision, flags, colons;
|
2008-08-23 04:06:59 -04:00
|
|
|
char padding;
|
2012-04-17 09:18:51 -04:00
|
|
|
#ifdef MAILHEADER_EXT
|
|
|
|
int sign;
|
|
|
|
#endif
|
2018-10-08 00:30:07 -04:00
|
|
|
VALUE zone = Qnil;
|
2008-08-20 11:30:28 -04:00
|
|
|
|
|
|
|
/* various tables, useful in North America */
|
2008-08-22 01:28:08 -04:00
|
|
|
static const char days_l[][10] = {
|
2008-08-20 11:30:28 -04:00
|
|
|
"Sunday", "Monday", "Tuesday", "Wednesday",
|
|
|
|
"Thursday", "Friday", "Saturday",
|
|
|
|
};
|
2008-08-22 01:28:08 -04:00
|
|
|
static const char months_l[][10] = {
|
2008-08-20 11:30:28 -04:00
|
|
|
"January", "February", "March", "April",
|
|
|
|
"May", "June", "July", "August", "September",
|
|
|
|
"October", "November", "December",
|
|
|
|
};
|
2008-08-22 01:28:08 -04:00
|
|
|
static const char ampm[][3] = { "AM", "PM", };
|
2008-08-20 11:30:28 -04:00
|
|
|
|
2016-03-23 07:57:01 -04:00
|
|
|
if (format == NULL || format_len == 0 || vtm == NULL) {
|
2020-06-17 01:49:16 -04:00
|
|
|
goto err;
|
2008-08-20 11:30:28 -04:00
|
|
|
}
|
|
|
|
|
2016-03-23 07:57:01 -04:00
|
|
|
if (enc &&
|
|
|
|
(enc == rb_usascii_encoding() ||
|
|
|
|
enc == rb_ascii8bit_encoding() ||
|
|
|
|
enc == rb_locale_encoding())) {
|
|
|
|
enc = NULL;
|
2011-08-27 05:18:18 -04:00
|
|
|
}
|
|
|
|
|
2016-03-23 07:57:01 -04:00
|
|
|
s += len;
|
|
|
|
for (; format < format_end; format++) {
|
2008-08-23 04:06:59 -04:00
|
|
|
#define FLAG_FOUND() do { \
|
2012-05-29 04:28:09 -04:00
|
|
|
if (precision > 0) \
|
2008-08-23 04:06:59 -04:00
|
|
|
goto unknown; \
|
|
|
|
} while (0)
|
2016-03-23 07:57:01 -04:00
|
|
|
#define NEEDS(n) do { \
|
2016-06-14 09:22:09 -04:00
|
|
|
if (s >= endp || (n) >= endp - s - 1) { \
|
|
|
|
s = resize_buffer(ftime, s, &start, &endp, (n), maxsize); \
|
|
|
|
buffer_size_check(s, format_end, format_len, enc); \
|
|
|
|
} \
|
2016-03-23 07:57:01 -04:00
|
|
|
} while (0)
|
2008-11-24 09:01:47 -05:00
|
|
|
#define FILL_PADDING(i) do { \
|
2010-12-31 21:45:04 -05:00
|
|
|
if (!(flags & BIT_OF(LEFT)) && precision > (i)) { \
|
2008-11-24 09:01:47 -05:00
|
|
|
NEEDS(precision); \
|
2010-12-31 21:45:04 -05:00
|
|
|
memset(s, padding ? padding : ' ', precision - (i)); \
|
|
|
|
s += precision - (i); \
|
2008-11-24 09:01:47 -05:00
|
|
|
} \
|
|
|
|
else { \
|
|
|
|
NEEDS(i); \
|
|
|
|
} \
|
|
|
|
} while (0);
|
2016-03-24 04:44:03 -04:00
|
|
|
#define FMT_PADDING(fmt, def_pad) \
|
|
|
|
(&"%*"fmt"\0""%0*"fmt[\
|
|
|
|
(padding == '0' || (!padding && (def_pad) == '0')) ? \
|
|
|
|
rb_strlen_lit("%*"fmt)+1 : 0])
|
|
|
|
#define FMT_PRECISION(def_prec) \
|
2016-03-24 11:01:39 -04:00
|
|
|
((flags & BIT_OF(LEFT)) ? (1) : \
|
|
|
|
(precision <= 0) ? (def_prec) : (precision))
|
2008-08-23 04:06:59 -04:00
|
|
|
#define FMT(def_pad, def_prec, fmt, val) \
|
2008-08-22 01:28:08 -04:00
|
|
|
do { \
|
2016-03-24 11:01:39 -04:00
|
|
|
precision = FMT_PRECISION(def_prec); \
|
2016-03-23 07:57:01 -04:00
|
|
|
len = s - start; \
|
|
|
|
NEEDS(precision); \
|
|
|
|
rb_str_set_len(ftime, len); \
|
2016-03-24 04:44:03 -04:00
|
|
|
rb_str_catf(ftime, FMT_PADDING(fmt, def_pad), \
|
2016-03-23 07:57:01 -04:00
|
|
|
precision, (val)); \
|
|
|
|
RSTRING_GETMEM(ftime, s, len); \
|
|
|
|
endp = (start = s) + rb_str_capacity(ftime); \
|
|
|
|
s += len; \
|
2008-08-22 01:28:08 -04:00
|
|
|
} while (0)
|
2008-11-26 09:30:25 -05:00
|
|
|
#define STRFTIME(fmt) \
|
2008-08-22 01:28:08 -04:00
|
|
|
do { \
|
2016-03-23 07:57:01 -04:00
|
|
|
len = s - start; \
|
|
|
|
rb_str_set_len(ftime, len); \
|
2016-06-14 09:22:09 -04:00
|
|
|
if (!rb_strftime_with_timespec(ftime, (fmt), rb_strlen_lit(fmt), \
|
2018-10-08 00:30:07 -04:00
|
|
|
enc, time, vtm, timev, ts, gmt, maxsize)) \
|
2016-03-23 07:57:01 -04:00
|
|
|
return 0; \
|
|
|
|
s = RSTRING_PTR(ftime); \
|
|
|
|
i = RSTRING_LEN(ftime) - len; \
|
|
|
|
endp = (start = s) + rb_str_capacity(ftime); \
|
|
|
|
s += len; \
|
2016-03-24 01:20:22 -04:00
|
|
|
if (i > 0) case_conv(s, i, flags); \
|
2008-11-25 10:28:22 -05:00
|
|
|
if (precision > i) {\
|
2020-04-07 12:13:03 -04:00
|
|
|
s += i; \
|
2011-05-30 23:59:36 -04:00
|
|
|
NEEDS(precision); \
|
2020-04-07 12:13:03 -04:00
|
|
|
s -= i; \
|
2008-11-25 10:28:22 -05:00
|
|
|
memmove(s + precision - i, s, i);\
|
|
|
|
memset(s, padding ? padding : ' ', precision - i); \
|
|
|
|
s += precision; \
|
2016-03-23 07:57:01 -04:00
|
|
|
} \
|
2008-11-25 10:28:22 -05:00
|
|
|
else s += i; \
|
2008-08-22 01:28:08 -04:00
|
|
|
} while (0)
|
2009-04-21 10:56:59 -04:00
|
|
|
#define FMTV(def_pad, def_prec, fmt, val) \
|
|
|
|
do { \
|
|
|
|
VALUE tmp = (val); \
|
|
|
|
if (FIXNUM_P(tmp)) { \
|
|
|
|
FMT((def_pad), (def_prec), "l"fmt, FIX2LONG(tmp)); \
|
|
|
|
} \
|
|
|
|
else { \
|
2016-03-24 11:49:43 -04:00
|
|
|
const int base = ((fmt[0] == 'x') ? 16 : \
|
|
|
|
(fmt[0] == 'o') ? 8 : \
|
|
|
|
10); \
|
2016-03-24 11:01:39 -04:00
|
|
|
precision = FMT_PRECISION(def_prec); \
|
2016-03-24 11:49:43 -04:00
|
|
|
if (!padding) padding = (def_pad); \
|
|
|
|
tmp = format_value(tmp, base); \
|
|
|
|
i = RSTRING_LEN(tmp); \
|
|
|
|
FILL_PADDING(i); \
|
2016-03-24 11:12:56 -04:00
|
|
|
rb_str_set_len(ftime, s-start); \
|
2016-03-24 04:44:03 -04:00
|
|
|
rb_str_append(ftime, tmp); \
|
|
|
|
RSTRING_GETMEM(ftime, s, len); \
|
|
|
|
endp = (start = s) + rb_str_capacity(ftime); \
|
|
|
|
s += len; \
|
2009-04-21 10:56:59 -04:00
|
|
|
} \
|
|
|
|
} while (0)
|
2008-08-22 01:28:08 -04:00
|
|
|
|
2016-03-23 07:57:01 -04:00
|
|
|
tp = memchr(format, '%', format_end - format);
|
|
|
|
if (!tp) tp = format_end;
|
|
|
|
NEEDS(tp - format);
|
|
|
|
memcpy(s, format, tp - format);
|
|
|
|
s += tp - format;
|
|
|
|
format = tp;
|
|
|
|
if (format == format_end) break;
|
|
|
|
|
2008-08-22 01:28:08 -04:00
|
|
|
tp = tbuf;
|
|
|
|
sp = format;
|
2008-08-21 10:57:35 -04:00
|
|
|
precision = -1;
|
2008-08-22 01:28:08 -04:00
|
|
|
flags = 0;
|
2008-08-23 04:06:59 -04:00
|
|
|
padding = 0;
|
2010-07-07 17:07:01 -04:00
|
|
|
colons = 0;
|
2008-08-20 11:30:28 -04:00
|
|
|
again:
|
2016-03-23 07:57:01 -04:00
|
|
|
if (++format >= format_end) goto unknown;
|
|
|
|
switch (*format) {
|
2008-08-20 11:30:28 -04:00
|
|
|
case '%':
|
2008-11-27 07:43:05 -05:00
|
|
|
FILL_PADDING(1);
|
2008-08-20 11:30:28 -04:00
|
|
|
*s++ = '%';
|
|
|
|
continue;
|
|
|
|
|
|
|
|
case 'a': /* abbreviated weekday name */
|
2008-11-24 09:29:37 -05:00
|
|
|
if (flags & BIT_OF(CHCASE)) {
|
|
|
|
flags &= ~(BIT_OF(LOWER)|BIT_OF(CHCASE));
|
|
|
|
flags |= BIT_OF(UPPER);
|
|
|
|
}
|
2021-04-29 21:09:32 -04:00
|
|
|
if (vtm->wday > 6)
|
2008-08-23 04:06:59 -04:00
|
|
|
i = 1, tp = "?";
|
2008-08-20 11:30:28 -04:00
|
|
|
else
|
2009-04-21 10:56:59 -04:00
|
|
|
i = 3, tp = days_l[vtm->wday];
|
2008-08-20 11:30:28 -04:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 'A': /* full weekday name */
|
2008-11-24 09:29:37 -05:00
|
|
|
if (flags & BIT_OF(CHCASE)) {
|
|
|
|
flags &= ~(BIT_OF(LOWER)|BIT_OF(CHCASE));
|
|
|
|
flags |= BIT_OF(UPPER);
|
|
|
|
}
|
2021-04-29 21:09:32 -04:00
|
|
|
if (vtm->wday > 6)
|
2008-08-23 04:06:59 -04:00
|
|
|
i = 1, tp = "?";
|
2008-08-20 11:30:28 -04:00
|
|
|
else
|
2009-04-21 10:56:59 -04:00
|
|
|
i = strlen(tp = days_l[vtm->wday]);
|
2008-08-20 11:30:28 -04:00
|
|
|
break;
|
|
|
|
|
|
|
|
#ifdef SYSV_EXT
|
|
|
|
case 'h': /* abbreviated month name */
|
|
|
|
#endif
|
|
|
|
case 'b': /* abbreviated month name */
|
2008-11-24 09:29:37 -05:00
|
|
|
if (flags & BIT_OF(CHCASE)) {
|
|
|
|
flags &= ~(BIT_OF(LOWER)|BIT_OF(CHCASE));
|
|
|
|
flags |= BIT_OF(UPPER);
|
|
|
|
}
|
2009-04-21 10:56:59 -04:00
|
|
|
if (vtm->mon < 1 || vtm->mon > 12)
|
2008-08-23 04:06:59 -04:00
|
|
|
i = 1, tp = "?";
|
2008-08-20 11:30:28 -04:00
|
|
|
else
|
2009-04-21 10:56:59 -04:00
|
|
|
i = 3, tp = months_l[vtm->mon-1];
|
2008-08-20 11:30:28 -04:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 'B': /* full month name */
|
2008-11-24 09:29:37 -05:00
|
|
|
if (flags & BIT_OF(CHCASE)) {
|
|
|
|
flags &= ~(BIT_OF(LOWER)|BIT_OF(CHCASE));
|
|
|
|
flags |= BIT_OF(UPPER);
|
|
|
|
}
|
2009-04-21 10:56:59 -04:00
|
|
|
if (vtm->mon < 1 || vtm->mon > 12)
|
2008-08-23 04:06:59 -04:00
|
|
|
i = 1, tp = "?";
|
2008-08-20 11:30:28 -04:00
|
|
|
else
|
2009-04-21 10:56:59 -04:00
|
|
|
i = strlen(tp = months_l[vtm->mon-1]);
|
2008-08-20 11:30:28 -04:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 'c': /* appropriate date and time representation */
|
2008-11-26 09:30:25 -05:00
|
|
|
STRFTIME("%a %b %e %H:%M:%S %Y");
|
2008-08-22 01:28:08 -04:00
|
|
|
continue;
|
2008-08-20 11:30:28 -04:00
|
|
|
|
|
|
|
case 'd': /* day of the month, 01 - 31 */
|
2009-04-21 10:56:59 -04:00
|
|
|
i = range(1, vtm->mday, 31);
|
2010-03-24 23:08:28 -04:00
|
|
|
FMT('0', 2, "d", (int)i);
|
2008-08-22 01:28:08 -04:00
|
|
|
continue;
|
2008-08-20 11:30:28 -04:00
|
|
|
|
|
|
|
case 'H': /* hour, 24-hour clock, 00 - 23 */
|
2009-04-21 10:56:59 -04:00
|
|
|
i = range(0, vtm->hour, 23);
|
2010-03-24 23:08:28 -04:00
|
|
|
FMT('0', 2, "d", (int)i);
|
2008-08-22 01:28:08 -04:00
|
|
|
continue;
|
2008-08-20 11:30:28 -04:00
|
|
|
|
|
|
|
case 'I': /* hour, 12-hour clock, 01 - 12 */
|
2009-04-21 10:56:59 -04:00
|
|
|
i = range(0, vtm->hour, 23);
|
2008-08-20 11:30:28 -04:00
|
|
|
if (i == 0)
|
|
|
|
i = 12;
|
|
|
|
else if (i > 12)
|
|
|
|
i -= 12;
|
2010-03-24 23:08:28 -04:00
|
|
|
FMT('0', 2, "d", (int)i);
|
2008-08-22 01:28:08 -04:00
|
|
|
continue;
|
2008-08-20 11:30:28 -04:00
|
|
|
|
|
|
|
case 'j': /* day of the year, 001 - 366 */
|
2013-04-06 12:02:46 -04:00
|
|
|
i = range(1, vtm->yday, 366);
|
|
|
|
FMT('0', 3, "d", (int)i);
|
2008-08-22 01:28:08 -04:00
|
|
|
continue;
|
2008-08-20 11:30:28 -04:00
|
|
|
|
|
|
|
case 'm': /* month, 01 - 12 */
|
2009-04-21 10:56:59 -04:00
|
|
|
i = range(1, vtm->mon, 12);
|
2010-03-24 23:08:28 -04:00
|
|
|
FMT('0', 2, "d", (int)i);
|
2008-08-22 01:28:08 -04:00
|
|
|
continue;
|
2008-08-20 11:30:28 -04:00
|
|
|
|
|
|
|
case 'M': /* minute, 00 - 59 */
|
2009-04-21 10:56:59 -04:00
|
|
|
i = range(0, vtm->min, 59);
|
2010-03-24 23:08:28 -04:00
|
|
|
FMT('0', 2, "d", (int)i);
|
2008-08-22 01:28:08 -04:00
|
|
|
continue;
|
2008-08-20 11:30:28 -04:00
|
|
|
|
2008-08-28 13:06:20 -04:00
|
|
|
case 'p': /* AM or PM based on 12-hour clock */
|
|
|
|
case 'P': /* am or pm based on 12-hour clock */
|
|
|
|
if ((*format == 'p' && (flags & BIT_OF(CHCASE))) ||
|
2008-11-24 23:42:11 -05:00
|
|
|
(*format == 'P' && !(flags & (BIT_OF(CHCASE)|BIT_OF(UPPER))))) {
|
2008-08-22 01:28:08 -04:00
|
|
|
flags &= ~(BIT_OF(UPPER)|BIT_OF(CHCASE));
|
|
|
|
flags |= BIT_OF(LOWER);
|
|
|
|
}
|
2009-04-21 10:56:59 -04:00
|
|
|
i = range(0, vtm->hour, 23);
|
2008-08-20 11:30:28 -04:00
|
|
|
if (i < 12)
|
2008-08-22 01:28:08 -04:00
|
|
|
tp = ampm[0];
|
2008-08-20 11:30:28 -04:00
|
|
|
else
|
2008-08-22 01:28:08 -04:00
|
|
|
tp = ampm[1];
|
2008-08-23 04:06:59 -04:00
|
|
|
i = 2;
|
2008-08-20 11:30:28 -04:00
|
|
|
break;
|
|
|
|
|
2008-08-28 13:06:20 -04:00
|
|
|
case 's':
|
2010-03-31 08:34:31 -04:00
|
|
|
if (ts) {
|
|
|
|
time_t sec = ts->tv_sec;
|
|
|
|
if (~(time_t)0 <= 0)
|
|
|
|
FMT('0', 1, PRI_TIMET_PREFIX"d", sec);
|
|
|
|
else
|
|
|
|
FMT('0', 1, PRI_TIMET_PREFIX"u", sec);
|
|
|
|
}
|
|
|
|
else {
|
2009-04-21 10:56:59 -04:00
|
|
|
VALUE sec = div(timev, INT2FIX(1));
|
|
|
|
FMTV('0', 1, "d", sec);
|
|
|
|
}
|
2010-03-31 08:34:31 -04:00
|
|
|
continue;
|
2008-08-28 13:06:20 -04:00
|
|
|
|
2008-08-20 11:30:28 -04:00
|
|
|
case 'S': /* second, 00 - 60 */
|
2009-04-21 10:56:59 -04:00
|
|
|
i = range(0, vtm->sec, 60);
|
2010-03-24 23:08:28 -04:00
|
|
|
FMT('0', 2, "d", (int)i);
|
2008-08-22 01:28:08 -04:00
|
|
|
continue;
|
2008-08-20 11:30:28 -04:00
|
|
|
|
|
|
|
case 'U': /* week of year, Sunday is first day of week */
|
2009-04-21 10:56:59 -04:00
|
|
|
FMT('0', 2, "d", weeknumber_v(vtm, 0));
|
2008-08-22 01:28:08 -04:00
|
|
|
continue;
|
2008-08-20 11:30:28 -04:00
|
|
|
|
|
|
|
case 'w': /* weekday, Sunday == 0, 0 - 6 */
|
2009-04-21 10:56:59 -04:00
|
|
|
i = range(0, vtm->wday, 6);
|
2010-03-24 23:08:28 -04:00
|
|
|
FMT('0', 1, "d", (int)i);
|
2008-08-22 01:28:08 -04:00
|
|
|
continue;
|
2008-08-20 11:30:28 -04:00
|
|
|
|
|
|
|
case 'W': /* week of year, Monday is first day of week */
|
2009-04-21 10:56:59 -04:00
|
|
|
FMT('0', 2, "d", weeknumber_v(vtm, 1));
|
2008-08-22 01:28:08 -04:00
|
|
|
continue;
|
2008-08-20 11:30:28 -04:00
|
|
|
|
|
|
|
case 'x': /* appropriate date representation */
|
2008-11-26 09:30:25 -05:00
|
|
|
STRFTIME("%m/%d/%y");
|
2008-08-22 01:28:08 -04:00
|
|
|
continue;
|
2008-08-20 11:30:28 -04:00
|
|
|
|
|
|
|
case 'X': /* appropriate time representation */
|
2008-11-26 09:30:25 -05:00
|
|
|
STRFTIME("%H:%M:%S");
|
2008-08-22 01:28:08 -04:00
|
|
|
continue;
|
2008-08-20 11:30:28 -04:00
|
|
|
|
|
|
|
case 'y': /* year without a century, 00 - 99 */
|
2009-04-21 10:56:59 -04:00
|
|
|
i = NUM2INT(mod(vtm->year, INT2FIX(100)));
|
2010-03-24 23:08:28 -04:00
|
|
|
FMT('0', 2, "d", (int)i);
|
2008-08-22 01:28:08 -04:00
|
|
|
continue;
|
2008-08-20 11:30:28 -04:00
|
|
|
|
|
|
|
case 'Y': /* year with century */
|
2009-11-24 06:34:45 -05:00
|
|
|
if (FIXNUM_P(vtm->year)) {
|
2012-05-28 00:54:04 -04:00
|
|
|
long y = FIX2LONG(vtm->year);
|
|
|
|
FMT('0', 0 <= y ? 4 : 5, "ld", y);
|
2009-11-24 06:34:45 -05:00
|
|
|
}
|
|
|
|
else {
|
2012-05-28 00:54:04 -04:00
|
|
|
FMTV('0', 4, "d", vtm->year);
|
2009-11-24 06:34:45 -05:00
|
|
|
}
|
2008-08-22 01:28:08 -04:00
|
|
|
continue;
|
2008-08-20 11:30:28 -04:00
|
|
|
|
|
|
|
#ifdef MAILHEADER_EXT
|
2009-11-26 00:25:08 -05:00
|
|
|
case 'z': /* time zone offset east of GMT e.g. -0600 */
|
2012-05-29 04:28:13 -04:00
|
|
|
if (gmt) {
|
|
|
|
off = 0;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
off = NUM2LONG(rb_funcall(vtm->utc_offset, rb_intern("round"), 0));
|
|
|
|
}
|
2021-02-16 05:28:28 -05:00
|
|
|
if (off < 0 || (gmt && (flags & BIT_OF(LEFT)))) {
|
2012-05-29 04:28:13 -04:00
|
|
|
off = -off;
|
|
|
|
sign = -1;
|
2017-05-09 20:39:26 -04:00
|
|
|
}
|
|
|
|
else {
|
2012-05-29 04:28:13 -04:00
|
|
|
sign = +1;
|
|
|
|
}
|
2010-07-07 17:07:01 -04:00
|
|
|
switch (colons) {
|
2012-05-28 00:54:04 -04:00
|
|
|
case 0: /* %z -> +hhmm */
|
|
|
|
precision = precision <= 5 ? 2 : precision-3;
|
|
|
|
NEEDS(precision + 3);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 1: /* %:z -> +hh:mm */
|
|
|
|
precision = precision <= 6 ? 2 : precision-4;
|
|
|
|
NEEDS(precision + 4);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 2: /* %::z -> +hh:mm:ss */
|
|
|
|
precision = precision <= 9 ? 2 : precision-7;
|
|
|
|
NEEDS(precision + 7);
|
|
|
|
break;
|
|
|
|
|
2012-05-29 04:28:13 -04:00
|
|
|
case 3: /* %:::z -> +hh[:mm[:ss]] */
|
|
|
|
if (off % 3600 == 0) {
|
|
|
|
precision = precision <= 3 ? 2 : precision-1;
|
|
|
|
NEEDS(precision + 3);
|
|
|
|
}
|
|
|
|
else if (off % 60 == 0) {
|
|
|
|
precision = precision <= 6 ? 2 : precision-4;
|
|
|
|
NEEDS(precision + 4);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
precision = precision <= 9 ? 2 : precision-7;
|
|
|
|
NEEDS(precision + 9);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2012-05-28 00:54:04 -04:00
|
|
|
default:
|
|
|
|
format--;
|
|
|
|
goto unknown;
|
2010-07-07 17:07:01 -04:00
|
|
|
}
|
2012-04-17 09:18:51 -04:00
|
|
|
i = snprintf(s, endp - s, (padding == ' ' ? "%+*ld" : "%+.*ld"),
|
2017-03-29 05:47:42 -04:00
|
|
|
precision + (padding == ' '), sign * (off / 3600));
|
2010-07-07 17:07:01 -04:00
|
|
|
if (i < 0) goto err;
|
2012-04-19 03:33:55 -04:00
|
|
|
if (sign < 0 && off < 3600) {
|
|
|
|
*(padding == ' ' ? s + i - 2 : s) = '-';
|
|
|
|
}
|
|
|
|
s += i;
|
2010-07-07 17:07:01 -04:00
|
|
|
off = off % 3600;
|
2012-05-29 04:28:13 -04:00
|
|
|
if (colons == 3 && off == 0)
|
|
|
|
continue;
|
2010-07-07 17:07:01 -04:00
|
|
|
if (1 <= colons)
|
|
|
|
*s++ = ':';
|
2010-08-07 21:08:27 -04:00
|
|
|
i = snprintf(s, endp - s, "%02d", (int)(off / 60));
|
2008-08-23 04:06:59 -04:00
|
|
|
if (i < 0) goto err;
|
|
|
|
s += i;
|
2010-07-07 17:07:01 -04:00
|
|
|
off = off % 60;
|
2012-05-29 04:28:13 -04:00
|
|
|
if (colons == 3 && off == 0)
|
|
|
|
continue;
|
2010-07-07 17:07:01 -04:00
|
|
|
if (2 <= colons) {
|
|
|
|
*s++ = ':';
|
2010-08-07 21:08:27 -04:00
|
|
|
i = snprintf(s, endp - s, "%02d", (int)off);
|
2010-07-07 17:07:01 -04:00
|
|
|
if (i < 0) goto err;
|
|
|
|
s += i;
|
|
|
|
}
|
2008-08-22 01:28:08 -04:00
|
|
|
continue;
|
2008-08-20 11:30:28 -04:00
|
|
|
#endif /* MAILHEADER_EXT */
|
|
|
|
|
2009-11-03 12:46:28 -05:00
|
|
|
case 'Z': /* time zone name or abbreviation */
|
2008-08-22 01:28:08 -04:00
|
|
|
if (flags & BIT_OF(CHCASE)) {
|
|
|
|
flags &= ~(BIT_OF(UPPER)|BIT_OF(CHCASE));
|
|
|
|
flags |= BIT_OF(LOWER);
|
|
|
|
}
|
2008-08-20 11:30:28 -04:00
|
|
|
if (gmt) {
|
2008-08-23 04:06:59 -04:00
|
|
|
i = 3;
|
2008-08-22 01:28:08 -04:00
|
|
|
tp = "UTC";
|
|
|
|
break;
|
2008-08-20 11:30:28 -04:00
|
|
|
}
|
2018-08-08 06:00:43 -04:00
|
|
|
if (NIL_P(vtm->zone)) {
|
2011-08-27 05:18:18 -04:00
|
|
|
i = 0;
|
|
|
|
}
|
|
|
|
else {
|
2018-10-08 00:30:07 -04:00
|
|
|
if (NIL_P(zone)) {
|
|
|
|
zone = rb_time_zone_abbreviation(vtm->zone, time);
|
|
|
|
}
|
|
|
|
tp = RSTRING_PTR(zone);
|
2011-08-27 05:18:18 -04:00
|
|
|
if (enc) {
|
|
|
|
for (i = 0; i < TBUFSIZE && tp[i]; i++) {
|
|
|
|
if ((unsigned char)tp[i] > 0x7F) {
|
|
|
|
VALUE str = rb_str_conv_enc_opts(rb_str_new_cstr(tp), rb_locale_encoding(), enc, ECONV_UNDEF_REPLACE|ECONV_INVALID_REPLACE, Qnil);
|
|
|
|
i = strlcpy(tbuf, RSTRING_PTR(str), TBUFSIZE);
|
|
|
|
tp = tbuf;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
i = strlen(tp);
|
|
|
|
}
|
2008-08-20 11:30:28 -04:00
|
|
|
break;
|
|
|
|
|
|
|
|
#ifdef SYSV_EXT
|
|
|
|
case 'n': /* same as \n */
|
2008-11-24 09:01:47 -05:00
|
|
|
FILL_PADDING(1);
|
2008-08-22 01:28:08 -04:00
|
|
|
*s++ = '\n';
|
|
|
|
continue;
|
2008-08-20 11:30:28 -04:00
|
|
|
|
|
|
|
case 't': /* same as \t */
|
2008-11-24 09:01:47 -05:00
|
|
|
FILL_PADDING(1);
|
2008-08-22 01:28:08 -04:00
|
|
|
*s++ = '\t';
|
|
|
|
continue;
|
2008-08-20 11:30:28 -04:00
|
|
|
|
|
|
|
case 'D': /* date as %m/%d/%y */
|
2008-11-26 09:30:25 -05:00
|
|
|
STRFTIME("%m/%d/%y");
|
2008-08-22 01:28:08 -04:00
|
|
|
continue;
|
2008-08-20 11:30:28 -04:00
|
|
|
|
|
|
|
case 'e': /* day of month, blank padded */
|
2009-04-21 10:56:59 -04:00
|
|
|
FMT(' ', 2, "d", range(1, vtm->mday, 31));
|
2008-08-22 01:28:08 -04:00
|
|
|
continue;
|
2008-08-20 11:30:28 -04:00
|
|
|
|
|
|
|
case 'r': /* time as %I:%M:%S %p */
|
2008-11-26 09:30:25 -05:00
|
|
|
STRFTIME("%I:%M:%S %p");
|
2008-08-22 01:28:08 -04:00
|
|
|
continue;
|
2008-08-20 11:30:28 -04:00
|
|
|
|
|
|
|
case 'R': /* time as %H:%M */
|
2008-11-26 09:30:25 -05:00
|
|
|
STRFTIME("%H:%M");
|
2008-08-22 01:28:08 -04:00
|
|
|
continue;
|
2008-08-20 11:30:28 -04:00
|
|
|
|
|
|
|
case 'T': /* time as %H:%M:%S */
|
2008-11-26 09:30:25 -05:00
|
|
|
STRFTIME("%H:%M:%S");
|
2008-08-22 01:28:08 -04:00
|
|
|
continue;
|
2008-08-20 11:30:28 -04:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef SUNOS_EXT
|
|
|
|
case 'k': /* hour, 24-hour clock, blank pad */
|
2009-04-21 10:56:59 -04:00
|
|
|
i = range(0, vtm->hour, 23);
|
2010-03-24 23:08:28 -04:00
|
|
|
FMT(' ', 2, "d", (int)i);
|
2008-08-22 01:28:08 -04:00
|
|
|
continue;
|
2008-08-20 11:30:28 -04:00
|
|
|
|
|
|
|
case 'l': /* hour, 12-hour clock, 1 - 12, blank pad */
|
2009-04-21 10:56:59 -04:00
|
|
|
i = range(0, vtm->hour, 23);
|
2008-08-20 11:30:28 -04:00
|
|
|
if (i == 0)
|
|
|
|
i = 12;
|
|
|
|
else if (i > 12)
|
|
|
|
i -= 12;
|
2010-03-24 23:08:28 -04:00
|
|
|
FMT(' ', 2, "d", (int)i);
|
2008-08-22 01:28:08 -04:00
|
|
|
continue;
|
2008-08-20 11:30:28 -04:00
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
|
|
#ifdef VMS_EXT
|
|
|
|
case 'v': /* date as dd-bbb-YYYY */
|
2009-04-21 10:56:59 -04:00
|
|
|
STRFTIME("%e-%^b-%4Y");
|
2008-08-22 01:28:08 -04:00
|
|
|
continue;
|
2008-08-20 11:30:28 -04:00
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
|
|
#ifdef POSIX2_DATE
|
|
|
|
case 'C':
|
2009-04-21 10:56:59 -04:00
|
|
|
FMTV('0', 2, "d", div(vtm->year, INT2FIX(100)));
|
2008-08-22 01:28:08 -04:00
|
|
|
continue;
|
2008-08-20 11:30:28 -04:00
|
|
|
|
|
|
|
case 'E':
|
2008-08-23 04:06:59 -04:00
|
|
|
/* POSIX locale extensions, ignored for now */
|
2012-05-29 04:28:09 -04:00
|
|
|
if (!format[1] || !strchr("cCxXyY", format[1]))
|
|
|
|
goto unknown;
|
2008-08-23 04:06:59 -04:00
|
|
|
goto again;
|
2008-08-20 11:30:28 -04:00
|
|
|
case 'O':
|
|
|
|
/* POSIX locale extensions, ignored for now */
|
2012-05-29 04:28:09 -04:00
|
|
|
if (!format[1] || !strchr("deHkIlmMSuUVwWy", format[1]))
|
|
|
|
goto unknown;
|
2008-08-20 11:30:28 -04:00
|
|
|
goto again;
|
|
|
|
|
|
|
|
case 'V': /* week of year according ISO 8601 */
|
2009-04-21 10:56:59 -04:00
|
|
|
FMT('0', 2, "d", iso8601wknum_v(vtm));
|
2008-08-22 01:28:08 -04:00
|
|
|
continue;
|
2008-08-20 11:30:28 -04:00
|
|
|
|
|
|
|
case 'u':
|
|
|
|
/* ISO 8601: Weekday as a decimal number [1 (Monday) - 7] */
|
2009-04-21 10:56:59 -04:00
|
|
|
FMT('0', 1, "d", vtm->wday == 0 ? 7 : vtm->wday);
|
2008-08-22 01:28:08 -04:00
|
|
|
continue;
|
2008-08-20 11:30:28 -04:00
|
|
|
#endif /* POSIX2_DATE */
|
|
|
|
|
|
|
|
#ifdef ISO_DATE_EXT
|
|
|
|
case 'G':
|
|
|
|
case 'g':
|
|
|
|
/*
|
|
|
|
* Year of ISO week.
|
|
|
|
*
|
|
|
|
* If it's December but the ISO week number is one,
|
|
|
|
* that week is in next year.
|
|
|
|
* If it's January but the ISO week number is 52 or
|
|
|
|
* 53, that week is in last year.
|
|
|
|
* Otherwise, it's this year.
|
|
|
|
*/
|
2009-04-21 10:56:59 -04:00
|
|
|
{
|
|
|
|
VALUE yv = vtm->year;
|
|
|
|
w = iso8601wknum_v(vtm);
|
|
|
|
if (vtm->mon == 12 && w == 1)
|
|
|
|
yv = add(yv, INT2FIX(1));
|
|
|
|
else if (vtm->mon == 1 && w >= 52)
|
|
|
|
yv = sub(yv, INT2FIX(1));
|
|
|
|
|
|
|
|
if (*format == 'G') {
|
2011-01-30 02:10:55 -05:00
|
|
|
if (FIXNUM_P(yv)) {
|
2011-05-30 23:59:36 -04:00
|
|
|
const long y = FIX2LONG(yv);
|
2011-01-30 02:10:55 -05:00
|
|
|
FMT('0', 0 <= y ? 4 : 5, "ld", y);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
FMTV('0', 4, "d", yv);
|
|
|
|
}
|
2009-04-21 10:56:59 -04:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
yv = mod(yv, INT2FIX(100));
|
|
|
|
y = FIX2LONG(yv);
|
|
|
|
FMT('0', 2, "ld", y);
|
|
|
|
}
|
|
|
|
continue;
|
|
|
|
}
|
2008-08-20 11:30:28 -04:00
|
|
|
|
|
|
|
#endif /* ISO_DATE_EXT */
|
|
|
|
|
2008-08-22 01:28:08 -04:00
|
|
|
|
|
|
|
case 'L':
|
|
|
|
w = 3;
|
|
|
|
goto subsec;
|
2008-08-20 11:30:28 -04:00
|
|
|
|
2008-08-21 10:57:35 -04:00
|
|
|
case 'N':
|
|
|
|
/*
|
|
|
|
* fractional second digits. default is 9 digits
|
|
|
|
* (nanosecond).
|
|
|
|
*
|
|
|
|
* %3N millisecond (3 digits)
|
|
|
|
* %6N microsecond (6 digits)
|
|
|
|
* %9N nanosecond (9 digits)
|
|
|
|
*/
|
2008-08-22 01:28:08 -04:00
|
|
|
w = 9;
|
|
|
|
subsec:
|
2010-03-31 08:34:31 -04:00
|
|
|
if (precision <= 0) {
|
|
|
|
precision = w;
|
|
|
|
}
|
|
|
|
NEEDS(precision);
|
|
|
|
|
|
|
|
if (ts) {
|
|
|
|
long subsec = ts->tv_nsec;
|
|
|
|
if (9 < precision) {
|
|
|
|
snprintf(s, endp - s, "%09ld", subsec);
|
|
|
|
memset(s+9, '0', precision-9);
|
|
|
|
s += precision;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
int i;
|
|
|
|
for (i = 0; i < 9-precision; i++)
|
|
|
|
subsec /= 10;
|
|
|
|
snprintf(s, endp - s, "%0*ld", precision, subsec);
|
|
|
|
s += precision;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
2009-04-21 10:56:59 -04:00
|
|
|
VALUE subsec = mod(timev, INT2FIX(1));
|
|
|
|
int ww;
|
|
|
|
long n;
|
2008-08-21 10:57:35 -04:00
|
|
|
|
2009-04-21 10:56:59 -04:00
|
|
|
ww = precision;
|
|
|
|
while (9 <= ww) {
|
|
|
|
subsec = mul(subsec, INT2FIX(1000000000));
|
|
|
|
ww -= 9;
|
|
|
|
}
|
|
|
|
n = 1;
|
|
|
|
for (; 0 < ww; ww--)
|
|
|
|
n *= 10;
|
|
|
|
if (n != 1)
|
|
|
|
subsec = mul(subsec, INT2FIX(n));
|
|
|
|
subsec = div(subsec, INT2FIX(1));
|
|
|
|
|
|
|
|
if (FIXNUM_P(subsec)) {
|
2011-04-25 03:56:42 -04:00
|
|
|
(void)snprintf(s, endp - s, "%0*ld", precision, FIX2LONG(subsec));
|
2009-04-21 10:56:59 -04:00
|
|
|
s += precision;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
VALUE args[2], result;
|
|
|
|
args[0] = INT2FIX(precision);
|
|
|
|
args[1] = subsec;
|
2017-03-16 20:55:55 -04:00
|
|
|
result = rb_str_format(2, args,
|
2018-10-13 05:59:22 -04:00
|
|
|
rb_fstring_lit("%0*d"));
|
2011-04-25 03:56:42 -04:00
|
|
|
(void)strlcpy(s, StringValueCStr(result), endp-s);
|
2009-04-21 10:56:59 -04:00
|
|
|
s += precision;
|
|
|
|
}
|
2008-08-21 10:57:35 -04:00
|
|
|
}
|
2008-08-22 01:28:08 -04:00
|
|
|
continue;
|
2008-08-21 10:57:35 -04:00
|
|
|
|
|
|
|
case 'F': /* Equivalent to %Y-%m-%d */
|
2009-04-21 10:56:59 -04:00
|
|
|
STRFTIME("%Y-%m-%d");
|
2008-08-22 01:28:08 -04:00
|
|
|
continue;
|
2008-08-20 11:30:28 -04:00
|
|
|
|
2008-08-22 01:28:08 -04:00
|
|
|
case '-':
|
2008-08-23 04:06:59 -04:00
|
|
|
FLAG_FOUND();
|
2008-08-22 01:28:08 -04:00
|
|
|
flags |= BIT_OF(LEFT);
|
2008-08-23 04:06:59 -04:00
|
|
|
padding = precision = 0;
|
2008-08-22 01:28:08 -04:00
|
|
|
goto again;
|
|
|
|
|
|
|
|
case '^':
|
2008-08-23 04:06:59 -04:00
|
|
|
FLAG_FOUND();
|
2008-08-22 01:28:08 -04:00
|
|
|
flags |= BIT_OF(UPPER);
|
|
|
|
goto again;
|
|
|
|
|
|
|
|
case '#':
|
2008-08-23 04:06:59 -04:00
|
|
|
FLAG_FOUND();
|
2008-08-22 01:28:08 -04:00
|
|
|
flags |= BIT_OF(CHCASE);
|
|
|
|
goto again;
|
|
|
|
|
|
|
|
case '_':
|
2008-08-23 04:06:59 -04:00
|
|
|
FLAG_FOUND();
|
|
|
|
padding = ' ';
|
2008-08-22 01:28:08 -04:00
|
|
|
goto again;
|
|
|
|
|
2010-07-07 17:07:01 -04:00
|
|
|
case ':':
|
2016-03-23 07:57:01 -04:00
|
|
|
for (colons = 1; colons <= 3; ++colons) {
|
|
|
|
if (format+colons >= format_end) goto unknown;
|
|
|
|
if (format[colons] == 'z') break;
|
|
|
|
if (format[colons] != ':') goto unknown;
|
2012-05-29 04:28:04 -04:00
|
|
|
}
|
2016-03-23 07:57:01 -04:00
|
|
|
format += colons - 1;
|
2010-07-07 17:07:01 -04:00
|
|
|
goto again;
|
|
|
|
|
2008-08-22 01:28:08 -04:00
|
|
|
case '0':
|
2008-08-23 04:06:59 -04:00
|
|
|
padding = '0';
|
2008-08-22 01:28:08 -04:00
|
|
|
case '1': case '2': case '3': case '4':
|
|
|
|
case '5': case '6': case '7': case '8': case '9':
|
|
|
|
{
|
2016-03-23 07:57:01 -04:00
|
|
|
size_t n;
|
|
|
|
int ov;
|
|
|
|
unsigned long u = ruby_scan_digits(format, format_end-format, 10, &n, &ov);
|
|
|
|
if (ov || u > INT_MAX) goto unknown;
|
|
|
|
precision = (int)u;
|
|
|
|
format += n - 1;
|
2008-08-21 10:57:35 -04:00
|
|
|
goto again;
|
|
|
|
}
|
2008-08-22 01:28:08 -04:00
|
|
|
|
|
|
|
default:
|
2008-08-23 04:06:59 -04:00
|
|
|
unknown:
|
2008-08-22 01:28:08 -04:00
|
|
|
i = format - sp + 1;
|
2008-11-25 09:24:47 -05:00
|
|
|
tp = sp;
|
|
|
|
precision = -1;
|
|
|
|
flags = 0;
|
|
|
|
padding = 0;
|
2010-07-09 08:21:26 -04:00
|
|
|
colons = 0;
|
2008-08-23 04:06:59 -04:00
|
|
|
break;
|
2008-08-20 11:30:28 -04:00
|
|
|
}
|
|
|
|
if (i) {
|
2008-11-24 09:01:47 -05:00
|
|
|
FILL_PADDING(i);
|
2008-08-22 01:28:08 -04:00
|
|
|
memcpy(s, tp, i);
|
2016-03-24 01:20:22 -04:00
|
|
|
s = case_conv(s, i, flags);
|
2008-08-20 11:30:28 -04:00
|
|
|
}
|
|
|
|
}
|
2016-03-23 22:43:34 -04:00
|
|
|
if (format != format_end) {
|
2008-08-20 11:30:28 -04:00
|
|
|
return 0;
|
2016-03-23 07:57:01 -04:00
|
|
|
}
|
|
|
|
len = s - start;
|
|
|
|
rb_str_set_len(ftime, len);
|
|
|
|
rb_str_resize(ftime, len);
|
|
|
|
return ftime;
|
2020-06-17 01:49:16 -04:00
|
|
|
|
|
|
|
err:
|
|
|
|
return 0;
|
2008-08-20 11:30:28 -04:00
|
|
|
}
|
|
|
|
|
2016-06-14 09:22:09 -04:00
|
|
|
static size_t
|
|
|
|
strftime_size_limit(size_t format_len)
|
|
|
|
{
|
|
|
|
size_t limit = format_len * (1*1024*1024);
|
|
|
|
if (limit < format_len) limit = format_len;
|
|
|
|
else if (limit < 1024) limit = 1024;
|
|
|
|
return limit;
|
|
|
|
}
|
|
|
|
|
2016-03-23 07:57:01 -04:00
|
|
|
VALUE
|
2018-10-07 22:35:31 -04:00
|
|
|
rb_strftime(const char *format, size_t format_len, rb_encoding *enc,
|
2018-10-08 00:30:07 -04:00
|
|
|
VALUE time, const struct vtm *vtm, VALUE timev, int gmt)
|
2010-03-31 08:34:31 -04:00
|
|
|
{
|
2016-03-23 07:57:01 -04:00
|
|
|
VALUE result = rb_enc_str_new(0, 0, enc);
|
|
|
|
return rb_strftime_with_timespec(result, format, format_len, enc,
|
2018-10-08 00:30:07 -04:00
|
|
|
time, vtm, timev, NULL, gmt,
|
2016-06-14 09:22:09 -04:00
|
|
|
strftime_size_limit(format_len));
|
2010-03-31 08:34:31 -04:00
|
|
|
}
|
|
|
|
|
2016-03-23 07:57:01 -04:00
|
|
|
VALUE
|
2018-10-07 22:35:31 -04:00
|
|
|
rb_strftime_timespec(const char *format, size_t format_len, rb_encoding *enc,
|
2018-10-08 00:30:07 -04:00
|
|
|
VALUE time, const struct vtm *vtm, struct timespec *ts, int gmt)
|
2010-03-31 08:34:31 -04:00
|
|
|
{
|
2016-03-23 07:57:01 -04:00
|
|
|
VALUE result = rb_enc_str_new(0, 0, enc);
|
|
|
|
return rb_strftime_with_timespec(result, format, format_len, enc,
|
2018-10-08 00:30:07 -04:00
|
|
|
time, vtm, Qnil, ts, gmt,
|
2016-06-14 09:22:09 -04:00
|
|
|
strftime_size_limit(format_len));
|
2010-03-31 08:34:31 -04:00
|
|
|
}
|
|
|
|
|
2016-06-14 09:22:09 -04:00
|
|
|
#if 0
|
|
|
|
VALUE
|
2018-10-07 22:35:31 -04:00
|
|
|
rb_strftime_limit(const char *format, size_t format_len, rb_encoding *enc,
|
2018-10-08 00:30:07 -04:00
|
|
|
VALUE time, const struct vtm *vtm, struct timespec *ts,
|
2016-06-14 09:22:09 -04:00
|
|
|
int gmt, size_t maxsize)
|
|
|
|
{
|
|
|
|
VALUE result = rb_enc_str_new(0, 0, enc);
|
|
|
|
return rb_strftime_with_timespec(result, format, format_len, enc,
|
2018-10-08 00:30:07 -04:00
|
|
|
time, vtm, Qnil, ts, gmt, maxsize);
|
2016-06-14 09:22:09 -04:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2008-08-20 11:30:28 -04:00
|
|
|
/* isleap --- is a year a leap year? */
|
|
|
|
|
|
|
|
static int
|
|
|
|
isleap(long year)
|
|
|
|
{
|
|
|
|
return ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-04-21 10:56:59 -04:00
|
|
|
static void
|
|
|
|
vtm2tm_noyear(const struct vtm *vtm, struct tm *result)
|
|
|
|
{
|
|
|
|
struct tm tm;
|
|
|
|
|
|
|
|
/* for isleap() in iso8601wknum. +100 is -1900 (mod 400). */
|
|
|
|
tm.tm_year = FIX2INT(mod(vtm->year, INT2FIX(400))) + 100;
|
|
|
|
|
|
|
|
tm.tm_mon = vtm->mon-1;
|
|
|
|
tm.tm_mday = vtm->mday;
|
|
|
|
tm.tm_hour = vtm->hour;
|
|
|
|
tm.tm_min = vtm->min;
|
|
|
|
tm.tm_sec = vtm->sec;
|
|
|
|
tm.tm_wday = vtm->wday;
|
|
|
|
tm.tm_yday = vtm->yday-1;
|
|
|
|
tm.tm_isdst = vtm->isdst;
|
|
|
|
#if defined(HAVE_STRUCT_TM_TM_GMTOFF)
|
|
|
|
tm.tm_gmtoff = NUM2LONG(vtm->utc_offset);
|
|
|
|
#endif
|
|
|
|
#if defined(HAVE_TM_ZONE)
|
2009-04-21 20:27:33 -04:00
|
|
|
tm.tm_zone = (char *)vtm->zone;
|
2009-04-21 10:56:59 -04:00
|
|
|
#endif
|
|
|
|
*result = tm;
|
|
|
|
}
|
|
|
|
|
2008-08-20 11:30:28 -04:00
|
|
|
#ifdef POSIX2_DATE
|
|
|
|
/* iso8601wknum --- compute week number according to ISO 8601 */
|
|
|
|
|
|
|
|
static int
|
|
|
|
iso8601wknum(const struct tm *timeptr)
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
* From 1003.2:
|
|
|
|
* If the week (Monday to Sunday) containing January 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
|
|
|
|
* year (52 or 53), and the next week is week 1.
|
|
|
|
*
|
|
|
|
* ADR: This means if Jan 1 was Monday through Thursday,
|
|
|
|
* it was week 1, otherwise week 52 or 53.
|
|
|
|
*
|
|
|
|
* XPG4 erroneously included POSIX.2 rationale text in the
|
|
|
|
* main body of the standard. Thus it requires week 53.
|
|
|
|
*/
|
|
|
|
|
|
|
|
int weeknum, jan1day;
|
|
|
|
|
|
|
|
/* get week number, Monday as first day of the week */
|
|
|
|
weeknum = weeknumber(timeptr, 1);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* With thanks and tip of the hatlo to tml@tik.vtt.fi
|
|
|
|
*
|
|
|
|
* What day of the week does January 1 fall on?
|
|
|
|
* We know that
|
|
|
|
* (timeptr->tm_yday - jan1.tm_yday) MOD 7 ==
|
|
|
|
* (timeptr->tm_wday - jan1.tm_wday) MOD 7
|
|
|
|
* and that
|
|
|
|
* jan1.tm_yday == 0
|
|
|
|
* and that
|
|
|
|
* timeptr->tm_wday MOD 7 == timeptr->tm_wday
|
|
|
|
* from which it follows that. . .
|
2009-11-26 00:25:08 -05:00
|
|
|
*/
|
2008-08-20 11:30:28 -04:00
|
|
|
jan1day = timeptr->tm_wday - (timeptr->tm_yday % 7);
|
|
|
|
if (jan1day < 0)
|
|
|
|
jan1day += 7;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* If Jan 1 was a Monday through Thursday, it was in
|
|
|
|
* week 1. Otherwise it was last year's highest week, which is
|
|
|
|
* this year's week 0.
|
|
|
|
*
|
|
|
|
* What does that mean?
|
|
|
|
* If Jan 1 was Monday, the week number is exactly right, it can
|
|
|
|
* never be 0.
|
|
|
|
* If it was Tuesday through Thursday, the weeknumber is one
|
|
|
|
* less than it should be, so we add one.
|
|
|
|
* Otherwise, Friday, Saturday or Sunday, the week number is
|
|
|
|
* OK, but if it is 0, it needs to be 52 or 53.
|
|
|
|
*/
|
|
|
|
switch (jan1day) {
|
|
|
|
case 1: /* Monday */
|
|
|
|
break;
|
|
|
|
case 2: /* Tuesday */
|
|
|
|
case 3: /* Wednesday */
|
|
|
|
case 4: /* Thursday */
|
|
|
|
weeknum++;
|
|
|
|
break;
|
|
|
|
case 5: /* Friday */
|
|
|
|
case 6: /* Saturday */
|
|
|
|
case 0: /* Sunday */
|
|
|
|
if (weeknum == 0) {
|
|
|
|
#ifdef USE_BROKEN_XPG4
|
|
|
|
/* XPG4 (as of March 1994) says 53 unconditionally */
|
|
|
|
weeknum = 53;
|
|
|
|
#else
|
|
|
|
/* get week number of last week of last year */
|
|
|
|
struct tm dec31ly; /* 12/31 last year */
|
|
|
|
dec31ly = *timeptr;
|
|
|
|
dec31ly.tm_year--;
|
|
|
|
dec31ly.tm_mon = 11;
|
|
|
|
dec31ly.tm_mday = 31;
|
|
|
|
dec31ly.tm_wday = (jan1day == 0) ? 6 : jan1day - 1;
|
|
|
|
dec31ly.tm_yday = 364 + isleap(dec31ly.tm_year + 1900L);
|
|
|
|
weeknum = iso8601wknum(& dec31ly);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (timeptr->tm_mon == 11) {
|
|
|
|
/*
|
|
|
|
* The last week of the year
|
|
|
|
* can be in week 1 of next year.
|
|
|
|
* Sigh.
|
|
|
|
*
|
|
|
|
* This can only happen if
|
|
|
|
* M T W
|
|
|
|
* 29 30 31
|
|
|
|
* 30 31
|
|
|
|
* 31
|
|
|
|
*/
|
|
|
|
int wday, mday;
|
|
|
|
|
|
|
|
wday = timeptr->tm_wday;
|
|
|
|
mday = timeptr->tm_mday;
|
|
|
|
if ( (wday == 1 && (mday >= 29 && mday <= 31))
|
|
|
|
|| (wday == 2 && (mday == 30 || mday == 31))
|
|
|
|
|| (wday == 3 && mday == 31))
|
|
|
|
weeknum = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return weeknum;
|
|
|
|
}
|
2009-04-21 10:56:59 -04:00
|
|
|
|
|
|
|
static int
|
|
|
|
iso8601wknum_v(const struct vtm *vtm)
|
|
|
|
{
|
|
|
|
struct tm tm;
|
|
|
|
vtm2tm_noyear(vtm, &tm);
|
|
|
|
return iso8601wknum(&tm);
|
|
|
|
}
|
|
|
|
|
2008-08-20 11:30:28 -04:00
|
|
|
#endif
|
|
|
|
|
|
|
|
/* weeknumber --- figure how many weeks into the year */
|
|
|
|
|
|
|
|
/* With thanks and tip of the hatlo to ado@elsie.nci.nih.gov */
|
|
|
|
|
|
|
|
static int
|
|
|
|
weeknumber(const struct tm *timeptr, int firstweekday)
|
|
|
|
{
|
|
|
|
int wday = timeptr->tm_wday;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
if (firstweekday == 1) {
|
|
|
|
if (wday == 0) /* sunday */
|
|
|
|
wday = 6;
|
|
|
|
else
|
|
|
|
wday--;
|
|
|
|
}
|
|
|
|
ret = ((timeptr->tm_yday + 7 - wday) / 7);
|
|
|
|
if (ret < 0)
|
|
|
|
ret = 0;
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2009-04-21 10:56:59 -04:00
|
|
|
static int
|
|
|
|
weeknumber_v(const struct vtm *vtm, int firstweekday)
|
|
|
|
{
|
|
|
|
struct tm tm;
|
|
|
|
vtm2tm_noyear(vtm, &tm);
|
|
|
|
return weeknumber(&tm, firstweekday);
|
|
|
|
}
|
|
|
|
|
2008-08-20 11:30:28 -04:00
|
|
|
#if 0
|
|
|
|
/* ADR --- I'm loathe to mess with ado's code ... */
|
|
|
|
|
|
|
|
Date: Wed, 24 Apr 91 20:54:08 MDT
|
|
|
|
From: Michal Jaegermann <audfax!emory!vm.ucs.UAlberta.CA!NTOMCZAK>
|
|
|
|
To: arnold@audiofax.com
|
|
|
|
|
|
|
|
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:
|
|
|
|
/*
|
|
|
|
* firstweekday is 0 if starting in Sunday, non-zero if in Monday
|
|
|
|
*/
|
|
|
|
{
|
|
|
|
return (timeptr->tm_yday - timeptr->tm_wday +
|
|
|
|
(firstweekday ? (timeptr->tm_wday ? 8 : 1) : 7)) / 7;
|
|
|
|
}
|
|
|
|
How nicer it depends on a compiler, of course, but always a tiny bit.
|
|
|
|
|
|
|
|
Cheers,
|
|
|
|
Michal
|
|
|
|
ntomczak@vm.ucs.ualberta.ca
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef TEST_STRFTIME
|
|
|
|
|
|
|
|
/*
|
|
|
|
* NAME:
|
|
|
|
* tst
|
|
|
|
*
|
|
|
|
* SYNOPSIS:
|
|
|
|
* tst
|
|
|
|
*
|
|
|
|
* DESCRIPTION:
|
|
|
|
* "tst" is a test driver for the function "strftime".
|
|
|
|
*
|
|
|
|
* OPTIONS:
|
|
|
|
* None.
|
|
|
|
*
|
|
|
|
* AUTHOR:
|
|
|
|
* Karl Vogel
|
|
|
|
* Control Data Systems, Inc.
|
|
|
|
* vogelke@c-17igp.wpafb.af.mil
|
|
|
|
*
|
|
|
|
* BUGS:
|
|
|
|
* None noticed yet.
|
|
|
|
*
|
|
|
|
* COMPILE:
|
|
|
|
* cc -o tst -DTEST_STRFTIME strftime.c
|
|
|
|
*/
|
|
|
|
|
|
|
|
/* ADR: I reformatted this to my liking, and deleted some unneeded code. */
|
|
|
|
|
|
|
|
#ifndef NULL
|
|
|
|
#include <stdio.h>
|
|
|
|
#endif
|
|
|
|
#include <sys/time.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
#define MAXTIME 132
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Array of time formats.
|
|
|
|
*/
|
|
|
|
|
|
|
|
static char *array[] =
|
|
|
|
{
|
|
|
|
"(%%A) full weekday name, var length (Sunday..Saturday) %A",
|
|
|
|
"(%%B) full month name, var length (January..December) %B",
|
|
|
|
"(%%C) Century %C",
|
|
|
|
"(%%D) date (%%m/%%d/%%y) %D",
|
|
|
|
"(%%E) Locale extensions (ignored) %E",
|
|
|
|
"(%%H) hour (24-hour clock, 00..23) %H",
|
|
|
|
"(%%I) hour (12-hour clock, 01..12) %I",
|
|
|
|
"(%%M) minute (00..59) %M",
|
|
|
|
"(%%O) Locale extensions (ignored) %O",
|
|
|
|
"(%%R) time, 24-hour (%%H:%%M) %R",
|
|
|
|
"(%%S) second (00..60) %S",
|
|
|
|
"(%%T) time, 24-hour (%%H:%%M:%%S) %T",
|
|
|
|
"(%%U) week of year, Sunday as first day of week (00..53) %U",
|
|
|
|
"(%%V) week of year according to ISO 8601 %V",
|
|
|
|
"(%%W) week of year, Monday as first day of week (00..53) %W",
|
|
|
|
"(%%X) appropriate locale time representation (%H:%M:%S) %X",
|
|
|
|
"(%%Y) year with century (1970...) %Y",
|
|
|
|
"(%%Z) timezone (EDT), or blank if timezone not determinable %Z",
|
|
|
|
"(%%a) locale's abbreviated weekday name (Sun..Sat) %a",
|
|
|
|
"(%%b) locale's abbreviated month name (Jan..Dec) %b",
|
|
|
|
"(%%c) full date (Sat Nov 4 12:02:33 1989)%n%t%t%t %c",
|
|
|
|
"(%%d) day of the month (01..31) %d",
|
|
|
|
"(%%e) day of the month, blank-padded ( 1..31) %e",
|
|
|
|
"(%%h) should be same as (%%b) %h",
|
|
|
|
"(%%j) day of the year (001..366) %j",
|
|
|
|
"(%%k) hour, 24-hour clock, blank pad ( 0..23) %k",
|
2009-12-06 09:55:22 -05:00
|
|
|
"(%%l) hour, 12-hour clock, blank pad ( 1..12) %l",
|
2008-08-20 11:30:28 -04:00
|
|
|
"(%%m) month (01..12) %m",
|
|
|
|
"(%%p) locale's AM or PM based on 12-hour clock %p",
|
|
|
|
"(%%r) time, 12-hour (same as %%I:%%M:%%S %%p) %r",
|
|
|
|
"(%%u) ISO 8601: Weekday as decimal number [1 (Monday) - 7] %u",
|
|
|
|
"(%%v) VMS date (dd-bbb-YYYY) %v",
|
|
|
|
"(%%w) day of week (0..6, Sunday == 0) %w",
|
|
|
|
"(%%x) appropriate locale date representation %x",
|
|
|
|
"(%%y) last two digits of year (00..99) %y",
|
|
|
|
"(%%z) timezone offset east of GMT as HHMM (e.g. -0500) %z",
|
|
|
|
(char *) NULL
|
|
|
|
};
|
|
|
|
|
|
|
|
/* main routine. */
|
|
|
|
|
|
|
|
int
|
2012-08-15 20:46:12 -04:00
|
|
|
main(int argc, char **argv)
|
2008-08-20 11:30:28 -04:00
|
|
|
{
|
|
|
|
long time();
|
|
|
|
|
|
|
|
char *next;
|
|
|
|
char string[MAXTIME];
|
|
|
|
|
|
|
|
int k;
|
|
|
|
int length;
|
|
|
|
|
|
|
|
struct tm *tm;
|
|
|
|
|
|
|
|
long clock;
|
|
|
|
|
|
|
|
/* Call the function. */
|
|
|
|
|
|
|
|
clock = time((long *) 0);
|
|
|
|
tm = localtime(&clock);
|
|
|
|
|
|
|
|
for (k = 0; next = array[k]; k++) {
|
|
|
|
length = strftime(string, MAXTIME, next, tm);
|
|
|
|
printf("%s\n", string);
|
|
|
|
}
|
|
|
|
|
|
|
|
exit(0);
|
|
|
|
}
|
|
|
|
#endif /* TEST_STRFTIME */
|