Ruby  2.4.2p198(2017-09-14revision59899)
Macros | Enumerations | Functions
strftime.c File Reference
#include "ruby/ruby.h"
#include "ruby/encoding.h"
#include "timev.h"
#include "internal.h"
#include <stdio.h>
#include <ctype.h>
#include <string.h>
#include <time.h>
#include <sys/types.h>
#include <errno.h>
#include <math.h>
Include dependency graph for strftime.c:

Go to the source code of this file.

Macros

#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 */
 
#define adddecl(stuff)   stuff
 
#define const
 
#define range(low, item, hi)   max((low), min((item), (hi)))
 
#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)))
 
#define BIT_OF(n)   (1U<<(n))
 
#define TBUFSIZE   100
 
#define FLAG_FOUND()
 
#define NEEDS(n)
 
#define FILL_PADDING(i)
 
#define FMT_PADDING(fmt, def_pad)
 
#define FMT_PRECISION(def_prec)
 
#define FMT(def_pad, def_prec, fmt, val)
 
#define STRFTIME(fmt)
 
#define FMTV(def_pad, def_prec, fmt, val)
 

Enumerations

enum  { LEFT, CHCASE, LOWER, UPPER }
 

Functions

static int weeknumber ()
 
 adddecl (static int iso8601wknum();)
 
static int max (int a, int b)
 
static char * resize_buffer (VALUE ftime, char *s, const char **start, const char **endp, ptrdiff_t n, size_t maxsize)
 
static void buffer_size_check (const char *s, const char *format_end, size_t format_len, rb_encoding *enc)
 
static char * case_conv (char *s, ptrdiff_t i, int flags)
 
static VALUE format_value (VALUE val, int base)
 
static VALUE rb_strftime_with_timespec (VALUE ftime, const char *format, size_t format_len, rb_encoding *enc, const struct vtm *vtm, VALUE timev, struct timespec *ts, int gmt, size_t maxsize)
 
static size_t strftime_size_limit (size_t format_len)
 
VALUE rb_strftime (const char *format, size_t format_len, rb_encoding *enc, const struct vtm *vtm, VALUE timev, int gmt)
 
VALUE rb_strftime_timespec (const char *format, size_t format_len, rb_encoding *enc, const struct vtm *vtm, struct timespec *ts, int gmt)
 
static int isleap (long year)
 
static void vtm2tm_noyear (const struct vtm *vtm, struct tm *result)
 
static int iso8601wknum (const struct tm *timeptr)
 
static int iso8601wknum_v (const struct vtm *vtm)
 
static int weeknumber (const struct tm *timeptr, int firstweekday)
 
static int weeknumber_v (const struct vtm *vtm, int firstweekday)
 

Macro Definition Documentation

◆ add

#define add (   x,
 
)    (rb_funcall((x), '+', 1, (y)))

Definition at line 151 of file strftime.c.

Referenced by rb_strftime_with_timespec().

◆ adddecl

#define adddecl (   stuff)    stuff

Definition at line 95 of file strftime.c.

◆ BIT_OF

#define BIT_OF (   n)    (1U<<(n))

Definition at line 161 of file strftime.c.

Referenced by case_conv(), and rb_strftime_with_timespec().

◆ const

#define const

Definition at line 103 of file strftime.c.

◆ div

#define div (   x,
 
)    (rb_funcall((x), rb_intern("div"), 1, (y)))

Definition at line 155 of file strftime.c.

Referenced by rb_strftime_with_timespec().

◆ FILL_PADDING

#define FILL_PADDING (   i)
Value:
do { \
if (!(flags & BIT_OF(LEFT)) && precision > (i)) { \
NEEDS(precision); \
memset(s, padding ? padding : ' ', precision - (i)); \
s += precision - (i); \
} \
else { \
NEEDS(i); \
} \
} while (0);
Definition: strftime.c:160
#define BIT_OF(n)
Definition: strftime.c:161

Referenced by rb_strftime_with_timespec().

◆ FLAG_FOUND

#define FLAG_FOUND ( )
Value:
do { \
if (precision > 0) \
goto unknown; \
} while (0)

Referenced by rb_strftime_with_timespec().

◆ FMT

#define FMT (   def_pad,
  def_prec,
  fmt,
  val 
)
Value:
do { \
precision = FMT_PRECISION(def_prec); \
len = s - start; \
NEEDS(precision); \
rb_str_set_len(ftime, len); \
rb_str_catf(ftime, FMT_PADDING(fmt, def_pad), \
precision, (val)); \
RSTRING_GETMEM(ftime, s, len); \
endp = (start = s) + rb_str_capacity(ftime); \
s += len; \
} while (0)
#define FMT_PRECISION(def_prec)
#define FMT_PADDING(fmt, def_pad)
#define val
register unsigned int len
Definition: zonetab.h:51
size_t rb_str_capacity(VALUE str)
Definition: string.c:674

Referenced by rb_strftime_with_timespec().

◆ FMT_PADDING

#define FMT_PADDING (   fmt,
  def_pad 
)
Value:
(&"%*"fmt"\0""%0*"fmt[\
(padding == '0' || (!padding && (def_pad) == '0')) ? \
rb_strlen_lit("%*"fmt)+1 : 0])
#define rb_strlen_lit(str)
Definition: intern.h:867

◆ FMT_PRECISION

#define FMT_PRECISION (   def_prec)
Value:
((flags & BIT_OF(LEFT)) ? (1) : \
(precision <= 0) ? (def_prec) : (precision))
Definition: strftime.c:160
#define BIT_OF(n)
Definition: strftime.c:161

◆ FMTV

#define FMTV (   def_pad,
  def_prec,
  fmt,
  val 
)
Value:
do { \
VALUE tmp = (val); \
if (FIXNUM_P(tmp)) { \
FMT((def_pad), (def_prec), "l"fmt, FIX2LONG(tmp)); \
} \
else { \
const int base = ((fmt[0] == 'x') ? 16 : \
(fmt[0] == 'o') ? 8 : \
10); \
precision = FMT_PRECISION(def_prec); \
if (!padding) padding = (def_pad); \
tmp = format_value(tmp, base); \
i = RSTRING_LEN(tmp); \
FILL_PADDING(i); \
rb_str_set_len(ftime, s-start); \
rb_str_append(ftime, tmp); \
RSTRING_GETMEM(ftime, s, len); \
endp = (start = s) + rb_str_capacity(ftime); \
s += len; \
} \
} while (0)
#define FMT_PRECISION(def_prec)
#define FIXNUM_P(f)
Definition: ruby.h:365
#define val
#define RSTRING_LEN(str)
Definition: ruby.h:978
register unsigned int len
Definition: zonetab.h:51
size_t rb_str_capacity(VALUE str)
Definition: string.c:674
#define FIX2LONG(x)
Definition: ruby.h:363
static VALUE format_value(VALUE val, int base)
Definition: strftime.c:215

Referenced by rb_strftime_with_timespec().

◆ ISO_DATE_EXT

#define ISO_DATE_EXT   1 /* %G and %g for year of ISO week */

Definition at line 77 of file strftime.c.

◆ MAILHEADER_EXT

#define MAILHEADER_EXT   1 /* add %z for HHMM format */

Definition at line 76 of file strftime.c.

◆ mod

#define mod (   x,
 
)    (rb_funcall((x), '%', 1, (y)))

Definition at line 156 of file strftime.c.

Referenced by rb_strftime_with_timespec(), and vtm2tm_noyear().

◆ mul

#define mul (   x,
 
)    (rb_funcall((x), '*', 1, (y)))

Definition at line 153 of file strftime.c.

Referenced by rb_strftime_with_timespec().

◆ NEEDS

#define NEEDS (   n)
Value:
do { \
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); \
} \
} while (0)
static char * resize_buffer(VALUE ftime, char *s, const char **start, const char **endp, ptrdiff_t n, size_t maxsize)
Definition: strftime.c:164

Referenced by rb_strftime_with_timespec().

◆ POSIX2_DATE

#define POSIX2_DATE   1 /* stuff in Posix 1003.2 date command */

Definition at line 74 of file strftime.c.

◆ quo

#define quo (   x,
 
)    (rb_funcall((x), rb_intern("quo"), 1, (y)))

Definition at line 154 of file strftime.c.

◆ range

#define range (   low,
  item,
  hi 
)    max((low), min((item), (hi)))

◆ STRFTIME

#define STRFTIME (   fmt)
Value:
do { \
len = s - start; \
rb_str_set_len(ftime, len); \
if (!rb_strftime_with_timespec(ftime, (fmt), rb_strlen_lit(fmt), \
enc, vtm, timev, ts, gmt, maxsize)) \
return 0; \
s = RSTRING_PTR(ftime); \
i = RSTRING_LEN(ftime) - len; \
endp = (start = s) + rb_str_capacity(ftime); \
s += len; \
if (i > 0) case_conv(s, i, flags); \
if (precision > i) {\
NEEDS(precision); \
memmove(s + precision - i, s, i);\
memset(s, padding ? padding : ' ', precision - i); \
s += precision; \
} \
else s += i; \
} while (0)
if(len<=MAX_WORD_LENGTH &&len >=MIN_WORD_LENGTH)
Definition: zonetab.h:883
static VALUE rb_strftime_with_timespec(VALUE ftime, const char *format, size_t format_len, rb_encoding *enc, const struct vtm *vtm, VALUE timev, struct timespec *ts, int gmt, size_t maxsize)
Definition: strftime.c:228
#define RSTRING_LEN(str)
Definition: ruby.h:978
#define rb_strlen_lit(str)
Definition: intern.h:867
register unsigned int len
Definition: zonetab.h:51
#define RSTRING_PTR(str)
Definition: ruby.h:982
static char * case_conv(char *s, ptrdiff_t i, int flags)
Definition: strftime.c:194
size_t rb_str_capacity(VALUE str)
Definition: string.c:674

Referenced by rb_strftime_with_timespec().

◆ sub

#define sub (   x,
 
)    (rb_funcall((x), '-', 1, (y)))

Definition at line 152 of file strftime.c.

Referenced by rb_strftime_with_timespec().

◆ SUNOS_EXT

#define SUNOS_EXT   1 /* stuff in SunOS strftime routine */

Definition at line 73 of file strftime.c.

◆ SYSV_EXT

#define SYSV_EXT   1 /* stuff in System V ascftime routine */

Definition at line 72 of file strftime.c.

◆ TBUFSIZE

#define TBUFSIZE   100

◆ VMS_EXT

#define VMS_EXT   1 /* include %v for VMS date format */

Definition at line 75 of file strftime.c.

Enumeration Type Documentation

◆ anonymous enum

anonymous enum
Enumerator
LEFT 
CHCASE 
LOWER 
UPPER 

Definition at line 160 of file strftime.c.

Function Documentation

◆ adddecl()

adddecl ( static int iso8601wknum();  )

Definition at line 105 of file strftime.c.

◆ buffer_size_check()

static void buffer_size_check ( const char *  s,
const char *  format_end,
size_t  format_len,
rb_encoding enc 
)
static

Definition at line 182 of file strftime.c.

References rb_enc_str_new(), and rb_syserr_fail_str().

◆ case_conv()

static char* case_conv ( char *  s,
ptrdiff_t  i,
int  flags 
)
static

Definition at line 194 of file strftime.c.

References BIT_OF, ISLOWER, ISUPPER, LOWER, TOLOWER, and TOUPPER.

Referenced by rb_strftime_with_timespec().

◆ format_value()

static VALUE format_value ( VALUE  val,
int  base 
)
static

Definition at line 215 of file strftime.c.

References rb_big2str(), rb_Integer(), RB_TYPE_P, and T_BIGNUM.

◆ isleap()

static int isleap ( long  year)
static

Definition at line 947 of file strftime.c.

Referenced by iso8601wknum().

◆ iso8601wknum()

static int iso8601wknum ( const struct tm *  timeptr)
static

Definition at line 982 of file strftime.c.

References isleap(), L, and weeknumber().

Referenced by iso8601wknum_v().

◆ iso8601wknum_v()

static int iso8601wknum_v ( const struct vtm *  vtm)
static

Definition at line 1089 of file strftime.c.

References iso8601wknum(), and vtm2tm_noyear().

Referenced by rb_strftime_with_timespec().

◆ max()

static int max ( int  a,
int  b 
)
inlinestatic

◆ rb_strftime()

VALUE rb_strftime ( const char *  format,
size_t  format_len,
rb_encoding enc,
const struct vtm *  vtm,
VALUE  timev,
int  gmt 
)

Definition at line 913 of file strftime.c.

References NULL, rb_enc_str_new(), rb_strftime_with_timespec(), result, and strftime_size_limit().

Referenced by rb_strftime_alloc().

◆ rb_strftime_timespec()

VALUE rb_strftime_timespec ( const char *  format,
size_t  format_len,
rb_encoding enc,
const struct vtm *  vtm,
struct timespec ts,
int  gmt 
)

Definition at line 923 of file strftime.c.

References Qnil, rb_enc_str_new(), rb_strftime_with_timespec(), result, and strftime_size_limit().

Referenced by rb_strftime_alloc().

◆ rb_strftime_with_timespec()

static VALUE rb_strftime_with_timespec ( VALUE  ftime,
const char *  format,
size_t  format_len,
rb_encoding enc,
const struct vtm *  vtm,
VALUE  timev,
struct timespec ts,
int  gmt,
size_t  maxsize 
)
static

◆ resize_buffer()

static char* resize_buffer ( VALUE  ftime,
char *  s,
const char **  start,
const char **  endp,
ptrdiff_t  n,
size_t  maxsize 
)
static

Definition at line 164 of file strftime.c.

References len, rb_str_modify_expand(), rb_str_set_len(), and RSTRING_PTR.

◆ strftime_size_limit()

static size_t strftime_size_limit ( size_t  format_len)
static

Definition at line 904 of file strftime.c.

Referenced by rb_strftime(), and rb_strftime_timespec().

◆ vtm2tm_noyear()

static void vtm2tm_noyear ( const struct vtm *  vtm,
struct tm *  result 
)
static

Definition at line 954 of file strftime.c.

References FIX2INT, INT2FIX, mod, and NUM2LONG.

Referenced by iso8601wknum_v(), and weeknumber_v().

◆ weeknumber() [1/2]

static int weeknumber ( )
static

Referenced by iso8601wknum(), and weeknumber_v().

◆ weeknumber() [2/2]

static int weeknumber ( const struct tm *  timeptr,
int  firstweekday 
)
static

Definition at line 1103 of file strftime.c.

◆ weeknumber_v()

static int weeknumber_v ( const struct vtm *  vtm,
int  firstweekday 
)
static

Definition at line 1121 of file strftime.c.

References argc, argv, I, main(), NULL, vtm2tm_noyear(), and weeknumber().

Referenced by rb_strftime_with_timespec().