PostgreSQL Source Code  git master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros
formatting.c File Reference
#include "postgres.h"
#include <ctype.h>
#include <unistd.h>
#include <math.h>
#include <float.h>
#include <limits.h>
#include "catalog/pg_collation.h"
#include "mb/pg_wchar.h"
#include "utils/builtins.h"
#include "utils/date.h"
#include "utils/datetime.h"
#include "utils/formatting.h"
#include "utils/int8.h"
#include "utils/numeric.h"
#include "utils/pg_locale.h"
Include dependency graph for formatting.c:

Go to the source code of this file.

Data Structures

struct  KeySuffix
 
struct  KeyWord
 
struct  FormatNode
 
struct  NUMDesc
 
struct  DCHCacheEntry
 
struct  NUMCacheEntry
 
struct  TmFromChar
 
struct  TmToChar
 
struct  NUMProc
 

Macros

#define DCH_TYPE   1 /* DATE-TIME version */
 
#define NUM_TYPE   2 /* NUMBER version */
 
#define KeyWord_INDEX_SIZE   ('~' - ' ')
 
#define KeyWord_INDEX_FILTER(_c)   ((_c) <= ' ' || (_c) >= '~' ? 0 : 1)
 
#define DCH_MAX_ITEM_SIZ   12 /* max localized day name */
 
#define NUM_MAX_ITEM_SIZ   8 /* roman number (RN has 15 chars) */
 
#define MAXFLOATWIDTH   60
 
#define MAXDOUBLEWIDTH   500
 
#define NODE_TYPE_END   1
 
#define NODE_TYPE_ACTION   2
 
#define NODE_TYPE_CHAR   3
 
#define SUFFTYPE_PREFIX   1
 
#define SUFFTYPE_POSTFIX   2
 
#define CLOCK_24_HOUR   0
 
#define CLOCK_12_HOUR   1
 
#define ADJUST_YEAR(year, is_interval)   ((is_interval) ? (year) : ((year) <= 0 ? -((year) - 1) : (year)))
 
#define A_D_STR   "A.D."
 
#define a_d_STR   "a.d."
 
#define AD_STR   "AD"
 
#define ad_STR   "ad"
 
#define B_C_STR   "B.C."
 
#define b_c_STR   "b.c."
 
#define BC_STR   "BC"
 
#define bc_STR   "bc"
 
#define A_M_STR   "A.M."
 
#define a_m_STR   "a.m."
 
#define AM_STR   "AM"
 
#define am_STR   "am"
 
#define P_M_STR   "P.M."
 
#define p_m_STR   "p.m."
 
#define PM_STR   "PM"
 
#define pm_STR   "pm"
 
#define ONE_UPPER   1 /* Name */
 
#define ALL_UPPER   2 /* NAME */
 
#define ALL_LOWER   3 /* name */
 
#define FULL_SIZ   0
 
#define MAX_MONTH_LEN   9
 
#define MAX_MON_LEN   3
 
#define MAX_DAY_LEN   9
 
#define MAX_DY_LEN   3
 
#define MAX_RM_LEN   4
 
#define TH_UPPER   1
 
#define TH_LOWER   2
 
#define NUM_F_DECIMAL   (1 << 1)
 
#define NUM_F_LDECIMAL   (1 << 2)
 
#define NUM_F_ZERO   (1 << 3)
 
#define NUM_F_BLANK   (1 << 4)
 
#define NUM_F_FILLMODE   (1 << 5)
 
#define NUM_F_LSIGN   (1 << 6)
 
#define NUM_F_BRACKET   (1 << 7)
 
#define NUM_F_MINUS   (1 << 8)
 
#define NUM_F_PLUS   (1 << 9)
 
#define NUM_F_ROMAN   (1 << 10)
 
#define NUM_F_MULTI   (1 << 11)
 
#define NUM_F_PLUS_POST   (1 << 12)
 
#define NUM_F_MINUS_POST   (1 << 13)
 
#define NUM_F_EEEE   (1 << 14)
 
#define NUM_LSIGN_PRE   (-1)
 
#define NUM_LSIGN_POST   1
 
#define NUM_LSIGN_NONE   0
 
#define IS_DECIMAL(_f)   ((_f)->flag & NUM_F_DECIMAL)
 
#define IS_LDECIMAL(_f)   ((_f)->flag & NUM_F_LDECIMAL)
 
#define IS_ZERO(_f)   ((_f)->flag & NUM_F_ZERO)
 
#define IS_BLANK(_f)   ((_f)->flag & NUM_F_BLANK)
 
#define IS_FILLMODE(_f)   ((_f)->flag & NUM_F_FILLMODE)
 
#define IS_BRACKET(_f)   ((_f)->flag & NUM_F_BRACKET)
 
#define IS_MINUS(_f)   ((_f)->flag & NUM_F_MINUS)
 
#define IS_LSIGN(_f)   ((_f)->flag & NUM_F_LSIGN)
 
#define IS_PLUS(_f)   ((_f)->flag & NUM_F_PLUS)
 
#define IS_ROMAN(_f)   ((_f)->flag & NUM_F_ROMAN)
 
#define IS_MULTI(_f)   ((_f)->flag & NUM_F_MULTI)
 
#define IS_EEEE(_f)   ((_f)->flag & NUM_F_EEEE)
 
#define NUM_CACHE_SIZE   64
 
#define NUM_CACHE_ENTRIES   20
 
#define DCH_CACHE_SIZE   128
 
#define DCH_CACHE_ENTRIES   20
 
#define ZERO_tmfc(_X)   memset(_X, 0, sizeof(TmFromChar))
 
#define DEBUG_TMFC(_X)
 
#define DEBUG_TM(_X)
 
#define tmtcTm(_X)   (&(_X)->tm)
 
#define tmtcTzn(_X)   ((_X)->tzn)
 
#define tmtcFsec(_X)   ((_X)->fsec)
 
#define ZERO_tm(_X)
 
#define ZERO_tmtc(_X)
 
#define INVALID_FOR_INTERVAL
 
#define DCH_S_FM   0x01
 
#define DCH_S_TH   0x02
 
#define DCH_S_th   0x04
 
#define DCH_S_SP   0x08
 
#define DCH_S_TM   0x10
 
#define S_THth(_s)   ((((_s) & DCH_S_TH) || ((_s) & DCH_S_th)) ? 1 : 0)
 
#define S_TH(_s)   (((_s) & DCH_S_TH) ? 1 : 0)
 
#define S_th(_s)   (((_s) & DCH_S_th) ? 1 : 0)
 
#define S_TH_TYPE(_s)   (((_s) & DCH_S_TH) ? TH_UPPER : TH_LOWER)
 
#define S_FM(_s)   (((_s) & DCH_S_FM) ? 1 : 0)
 
#define S_SP(_s)   (((_s) & DCH_S_SP) ? 1 : 0)
 
#define S_TM(_s)   (((_s) & DCH_S_TM) ? 1 : 0)
 
#define TM_SUFFIX_LEN   2
 
#define SKIP_THth(ptr, _suf)
 
#define zeroize_NUM(_n)
 
#define OVERLOAD_TEST   (Np->inout_p >= Np->inout + input_len)
 
#define AMOUNT_TEST(_s)   (input_len-(Np->inout_p-Np->inout) >= _s)
 
#define IS_PREDEC_SPACE(_n)
 
#define NUM_TOCHAR_prepare
 
#define NUM_TOCHAR_finish
 

Typedefs

typedef struct FormatNode FormatNode
 
typedef struct TmToChar TmToChar
 
typedef struct NUMProc NUMProc
 

Enumerations

enum  FromCharDateMode { FROM_CHAR_DATE_NONE = 0, FROM_CHAR_DATE_GREGORIAN, FROM_CHAR_DATE_ISOWEEK }
 
enum  DCH_poz {
  DCH_A_D, DCH_A_M, DCH_AD, DCH_AM,
  DCH_B_C, DCH_BC, DCH_CC, DCH_DAY,
  DCH_DDD, DCH_DD, DCH_DY, DCH_Day,
  DCH_Dy, DCH_D, DCH_FX, DCH_HH24,
  DCH_HH12, DCH_HH, DCH_IDDD, DCH_ID,
  DCH_IW, DCH_IYYY, DCH_IYY, DCH_IY,
  DCH_I, DCH_J, DCH_MI, DCH_MM,
  DCH_MONTH, DCH_MON, DCH_MS, DCH_Month,
  DCH_Mon, DCH_OF, DCH_P_M, DCH_PM,
  DCH_Q, DCH_RM, DCH_SSSS, DCH_SS,
  DCH_TZ, DCH_US, DCH_WW, DCH_W,
  DCH_Y_YYY, DCH_YYYY, DCH_YYY, DCH_YY,
  DCH_Y, DCH_a_d, DCH_a_m, DCH_ad,
  DCH_am, DCH_b_c, DCH_bc, DCH_cc,
  DCH_day, DCH_ddd, DCH_dd, DCH_dy,
  DCH_d, DCH_fx, DCH_hh24, DCH_hh12,
  DCH_hh, DCH_iddd, DCH_id, DCH_iw,
  DCH_iyyy, DCH_iyy, DCH_iy, DCH_i,
  DCH_j, DCH_mi, DCH_mm, DCH_month,
  DCH_mon, DCH_ms, DCH_p_m, DCH_pm,
  DCH_q, DCH_rm, DCH_ssss, DCH_ss,
  DCH_tz, DCH_us, DCH_ww, DCH_w,
  DCH_y_yyy, DCH_yyyy, DCH_yyy, DCH_yy,
  DCH_y, _DCH_last_
}
 
enum  NUM_poz {
  NUM_COMMA, NUM_DEC, NUM_0, NUM_9,
  NUM_B, NUM_C, NUM_D, NUM_E,
  NUM_FM, NUM_G, NUM_L, NUM_MI,
  NUM_PL, NUM_PR, NUM_RN, NUM_SG,
  NUM_SP, NUM_S, NUM_TH, NUM_V,
  NUM_b, NUM_c, NUM_d, NUM_e,
  NUM_fm, NUM_g, NUM_l, NUM_mi,
  NUM_pl, NUM_pr, NUM_rn, NUM_sg,
  NUM_sp, NUM_s, NUM_th, NUM_v,
  _NUM_last_
}
 

Functions

static const KeyWordindex_seq_search (const char *str, const KeyWord *kw, const int *index)
 
static const KeySuffixsuff_search (const char *str, const KeySuffix *suf, int type)
 
static void NUMDesc_prepare (NUMDesc *num, FormatNode *n)
 
static void parse_format (FormatNode *node, const char *str, const KeyWord *kw, const KeySuffix *suf, const int *index, int ver, NUMDesc *Num)
 
static void DCH_to_char (FormatNode *node, bool is_interval, TmToChar *in, char *out, Oid collid)
 
static void DCH_from_char (FormatNode *node, char *in, TmFromChar *out)
 
static const char * get_th (char *num, int type)
 
static char * str_numth (char *dest, char *num, int type)
 
static int adjust_partial_year_to_2020 (int year)
 
static int strspace_len (char *str)
 
static void from_char_set_mode (TmFromChar *tmfc, const FromCharDateMode mode)
 
static void from_char_set_int (int *dest, const int value, const FormatNode *node)
 
static int from_char_parse_int_len (int *dest, char **src, const int len, FormatNode *node)
 
static int from_char_parse_int (int *dest, char **src, FormatNode *node)
 
static int seq_search (char *name, const char *const *array, int type, int max, int *len)
 
static int from_char_seq_search (int *dest, char **src, const char *const *array, int type, int max, FormatNode *node)
 
static void do_to_timestamp (text *date_txt, text *fmt, struct pg_tm *tm, fsec_t *fsec)
 
static char * fill_str (char *str, int c, int max)
 
static FormatNodeNUM_cache (int len, NUMDesc *Num, text *pars_str, bool *shouldFree)
 
static char * int_to_roman (int number)
 
static void NUM_prepare_locale (NUMProc *Np)
 
static char * get_last_relevant_decnum (char *num)
 
static void NUM_numpart_from_char (NUMProc *Np, int id, int input_len)
 
static void NUM_numpart_to_char (NUMProc *Np, int id)
 
static char * NUM_processor (FormatNode *node, NUMDesc *Num, char *inout, char *number, int from_char_input_len, int to_char_out_pre_spaces, int sign, bool is_to_char, Oid collid)
 
static DCHCacheEntryDCH_cache_getnew (const char *str)
 
static DCHCacheEntryDCH_cache_search (const char *str)
 
static DCHCacheEntryDCH_cache_fetch (const char *str)
 
static NUMCacheEntryNUM_cache_getnew (const char *str)
 
static NUMCacheEntryNUM_cache_search (const char *str)
 
static NUMCacheEntryNUM_cache_fetch (const char *str)
 
char * str_tolower (const char *buff, size_t nbytes, Oid collid)
 
char * str_toupper (const char *buff, size_t nbytes, Oid collid)
 
char * str_initcap (const char *buff, size_t nbytes, Oid collid)
 
char * asc_tolower (const char *buff, size_t nbytes)
 
char * asc_toupper (const char *buff, size_t nbytes)
 
char * asc_initcap (const char *buff, size_t nbytes)
 
static char * str_tolower_z (const char *buff, Oid collid)
 
static char * str_toupper_z (const char *buff, Oid collid)
 
static char * str_initcap_z (const char *buff, Oid collid)
 
static char * asc_tolower_z (const char *buff)
 
static char * asc_toupper_z (const char *buff)
 
static bool is_next_separator (FormatNode *n)
 
static textdatetime_to_char_body (TmToChar *tmtc, text *fmt, bool is_interval, Oid collid)
 
Datum timestamp_to_char (PG_FUNCTION_ARGS)
 
Datum timestamptz_to_char (PG_FUNCTION_ARGS)
 
Datum interval_to_char (PG_FUNCTION_ARGS)
 
Datum to_timestamp (PG_FUNCTION_ARGS)
 
Datum to_date (PG_FUNCTION_ARGS)
 
Datum numeric_to_number (PG_FUNCTION_ARGS)
 
Datum numeric_to_char (PG_FUNCTION_ARGS)
 
Datum int4_to_char (PG_FUNCTION_ARGS)
 
Datum int8_to_char (PG_FUNCTION_ARGS)
 
Datum float4_to_char (PG_FUNCTION_ARGS)
 
Datum float8_to_char (PG_FUNCTION_ARGS)
 

Variables

static const char *const months_full []
 
static const char *const days_short []
 
static const char *const adbc_strings [] = {ad_STR, bc_STR, AD_STR, BC_STR, NULL}
 
static const char *const adbc_strings_long [] = {a_d_STR, b_c_STR, A_D_STR, B_C_STR, NULL}
 
static const char *const ampm_strings [] = {am_STR, pm_STR, AM_STR, PM_STR, NULL}
 
static const char *const ampm_strings_long [] = {a_m_STR, p_m_STR, A_M_STR, P_M_STR, NULL}
 
static const char *const rm_months_upper []
 
static const char *const rm_months_lower []
 
static const char *const rm1 [] = {"I", "II", "III", "IV", "V", "VI", "VII", "VIII", "IX", NULL}
 
static const char *const rm10 [] = {"X", "XX", "XXX", "XL", "L", "LX", "LXX", "LXXX", "XC", NULL}
 
static const char *const rm100 [] = {"C", "CC", "CCC", "CD", "D", "DC", "DCC", "DCCC", "CM", NULL}
 
static const char *const numTH [] = {"ST", "ND", "RD", "TH", NULL}
 
static const char *const numth [] = {"st", "nd", "rd", "th", NULL}
 
static DCHCacheEntry DCHCache [DCH_CACHE_ENTRIES]
 
static int n_DCHCache = 0
 
static int DCHCounter = 0
 
static NUMCacheEntry NUMCache [NUM_CACHE_ENTRIES]
 
static int n_NUMCache = 0
 
static int NUMCounter = 0
 
static const KeySuffix DCH_suff []
 
static const KeyWord DCH_keywords []
 
static const KeyWord NUM_keywords []
 
static const int DCH_index [KeyWord_INDEX_SIZE]
 
static const int NUM_index [KeyWord_INDEX_SIZE]
 
return result
 

Macro Definition Documentation

#define A_D_STR   "A.D."

Definition at line 206 of file formatting.c.

Referenced by DCH_to_char().

#define a_d_STR   "a.d."

Definition at line 207 of file formatting.c.

Referenced by DCH_to_char().

#define A_M_STR   "A.M."

Definition at line 233 of file formatting.c.

Referenced by DCH_to_char().

#define a_m_STR   "a.m."

Definition at line 234 of file formatting.c.

Referenced by DCH_to_char().

#define AD_STR   "AD"

Definition at line 208 of file formatting.c.

Referenced by DCH_to_char().

#define ad_STR   "ad"

Definition at line 209 of file formatting.c.

Referenced by DCH_to_char().

#define ADJUST_YEAR (   year,
  is_interval 
)    ((is_interval) ? (year) : ((year) <= 0 ? -((year) - 1) : (year)))

Definition at line 204 of file formatting.c.

Referenced by DCH_to_char().

#define ALL_LOWER   3 /* name */

Definition at line 289 of file formatting.c.

Referenced by DCH_from_char(), and seq_search().

#define ALL_UPPER   2 /* NAME */

Definition at line 288 of file formatting.c.

Referenced by DCH_from_char(), and seq_search().

#define AM_STR   "AM"

Definition at line 235 of file formatting.c.

Referenced by DCH_to_char().

#define am_STR   "am"

Definition at line 236 of file formatting.c.

Referenced by DCH_to_char().

#define AMOUNT_TEST (   _s)    (input_len-(Np->inout_p-Np->inout) >= _s)

Referenced by NUM_numpart_from_char().

#define B_C_STR   "B.C."

Definition at line 211 of file formatting.c.

Referenced by DCH_to_char().

#define b_c_STR   "b.c."

Definition at line 212 of file formatting.c.

Referenced by DCH_to_char().

#define BC_STR   "BC"

Definition at line 213 of file formatting.c.

Referenced by DCH_to_char().

#define bc_STR   "bc"

Definition at line 214 of file formatting.c.

Referenced by DCH_to_char().

#define CLOCK_12_HOUR   1

Definition at line 181 of file formatting.c.

Referenced by DCH_from_char(), and do_to_timestamp().

#define CLOCK_24_HOUR   0

Definition at line 180 of file formatting.c.

#define DCH_CACHE_ENTRIES   20

Definition at line 375 of file formatting.c.

Referenced by DCH_cache_getnew(), and DCH_cache_search().

#define DCH_CACHE_SIZE   128

Definition at line 374 of file formatting.c.

Referenced by datetime_to_char_body(), DCH_cache_getnew(), and do_to_timestamp().

#define DCH_MAX_ITEM_SIZ   12 /* max localized day name */
#define DCH_S_FM   0x01

Definition at line 505 of file formatting.c.

#define DCH_S_SP   0x08

Definition at line 508 of file formatting.c.

#define DCH_S_TH   0x02

Definition at line 506 of file formatting.c.

#define DCH_S_th   0x04

Definition at line 507 of file formatting.c.

#define DCH_S_TM   0x10

Definition at line 509 of file formatting.c.

#define DCH_TYPE   1 /* DATE-TIME version */
#define DEBUG_TM (   _X)

Definition at line 452 of file formatting.c.

Referenced by do_to_timestamp().

#define DEBUG_TMFC (   _X)

Definition at line 451 of file formatting.c.

Referenced by do_to_timestamp().

#define FULL_SIZ   0

Definition at line 291 of file formatting.c.

#define INVALID_FOR_INTERVAL
Value:
do { \
if (is_interval) \
(errcode(ERRCODE_INVALID_DATETIME_FORMAT), \
errmsg("invalid format specification for an interval value"), \
errhint("Intervals are not tied to specific calendar dates."))); \
} while(0)
int errhint(const char *fmt,...)
Definition: elog.c:987
int errcode(int sqlerrcode)
Definition: elog.c:575
#define ERROR
Definition: elog.h:43
#define ereport(elevel, rest)
Definition: elog.h:122
int errmsg(const char *fmt,...)
Definition: elog.c:797

Definition at line 488 of file formatting.c.

Referenced by DCH_to_char().

#define IS_BLANK (   _f)    ((_f)->flag & NUM_F_BLANK)

Definition at line 349 of file formatting.c.

Referenced by NUMDesc_prepare().

#define IS_BRACKET (   _f)    ((_f)->flag & NUM_F_BRACKET)
#define IS_DECIMAL (   _f)    ((_f)->flag & NUM_F_DECIMAL)
#define IS_EEEE (   _f)    ((_f)->flag & NUM_F_EEEE)
#define IS_FILLMODE (   _f)    ((_f)->flag & NUM_F_FILLMODE)

Definition at line 350 of file formatting.c.

Referenced by NUM_numpart_to_char(), NUM_processor(), and NUMDesc_prepare().

#define IS_LDECIMAL (   _f)    ((_f)->flag & NUM_F_LDECIMAL)

Definition at line 347 of file formatting.c.

Referenced by NUM_prepare_locale().

#define IS_LSIGN (   _f)    ((_f)->flag & NUM_F_LSIGN)
#define IS_MINUS (   _f)    ((_f)->flag & NUM_F_MINUS)

Definition at line 352 of file formatting.c.

Referenced by NUM_numpart_from_char(), NUM_processor(), and NUMDesc_prepare().

#define IS_MULTI (   _f)    ((_f)->flag & NUM_F_MULTI)
#define IS_PLUS (   _f)    ((_f)->flag & NUM_F_PLUS)

Definition at line 354 of file formatting.c.

Referenced by NUM_numpart_from_char(), NUM_processor(), and NUMDesc_prepare().

#define IS_PREDEC_SPACE (   _n)
Value:
(IS_ZERO((_n)->Num)==FALSE && \
(_n)->number == (_n)->number_p && \
*(_n)->number == '0' && \
(_n)->Num->post != 0)
#define IS_ZERO(_f)
Definition: formatting.c:348
#define FALSE
Definition: c.h:221

Definition at line 4426 of file formatting.c.

Referenced by NUM_numpart_to_char().

#define IS_ROMAN (   _f)    ((_f)->flag & NUM_F_ROMAN)
#define IS_ZERO (   _f)    ((_f)->flag & NUM_F_ZERO)

Definition at line 348 of file formatting.c.

Referenced by NUM_numpart_to_char(), NUM_processor(), and NUMDesc_prepare().

#define KeyWord_INDEX_FILTER (   _c)    ((_c) <= ' ' || (_c) >= '~' ? 0 : 1)

Definition at line 111 of file formatting.c.

Referenced by index_seq_search().

#define KeyWord_INDEX_SIZE   ('~' - ' ')

Definition at line 110 of file formatting.c.

#define MAX_DAY_LEN   9

Definition at line 295 of file formatting.c.

Referenced by DCH_from_char().

#define MAX_DY_LEN   3

Definition at line 296 of file formatting.c.

Referenced by DCH_from_char().

#define MAX_MON_LEN   3

Definition at line 294 of file formatting.c.

Referenced by DCH_from_char().

#define MAX_MONTH_LEN   9

Definition at line 293 of file formatting.c.

Referenced by DCH_from_char().

#define MAX_RM_LEN   4

Definition at line 297 of file formatting.c.

Referenced by DCH_from_char().

#define MAXDOUBLEWIDTH   500

Definition at line 125 of file formatting.c.

Referenced by float4_to_char(), float8_to_char(), and int4_to_char().

#define MAXFLOATWIDTH   60

Definition at line 124 of file formatting.c.

Referenced by float4_to_char().

#define NODE_TYPE_ACTION   2
#define NODE_TYPE_CHAR   3

Definition at line 175 of file formatting.c.

Referenced by parse_format().

#define NODE_TYPE_END   1
#define NUM_CACHE_ENTRIES   20

Definition at line 373 of file formatting.c.

Referenced by NUM_cache_getnew(), and NUM_cache_search().

#define NUM_CACHE_SIZE   64

Definition at line 372 of file formatting.c.

Referenced by NUM_cache(), and NUM_cache_getnew().

#define NUM_F_BLANK   (1 << 4)

Definition at line 326 of file formatting.c.

Referenced by NUMDesc_prepare().

#define NUM_F_BRACKET   (1 << 7)

Definition at line 329 of file formatting.c.

Referenced by NUM_processor(), and NUMDesc_prepare().

#define NUM_F_DECIMAL   (1 << 1)

Definition at line 323 of file formatting.c.

Referenced by NUMDesc_prepare().

#define NUM_F_EEEE   (1 << 14)

Definition at line 336 of file formatting.c.

Referenced by NUMDesc_prepare().

#define NUM_F_FILLMODE   (1 << 5)

Definition at line 327 of file formatting.c.

Referenced by NUM_processor(), and NUMDesc_prepare().

#define NUM_F_LDECIMAL   (1 << 2)

Definition at line 324 of file formatting.c.

Referenced by NUMDesc_prepare().

#define NUM_F_LSIGN   (1 << 6)

Definition at line 328 of file formatting.c.

Referenced by NUMDesc_prepare().

#define NUM_F_MINUS   (1 << 8)

Definition at line 330 of file formatting.c.

Referenced by NUM_processor(), and NUMDesc_prepare().

#define NUM_F_MINUS_POST   (1 << 13)

Definition at line 335 of file formatting.c.

Referenced by NUMDesc_prepare().

#define NUM_F_MULTI   (1 << 11)

Definition at line 333 of file formatting.c.

Referenced by NUMDesc_prepare().

#define NUM_F_PLUS   (1 << 9)

Definition at line 331 of file formatting.c.

Referenced by NUM_processor(), and NUMDesc_prepare().

#define NUM_F_PLUS_POST   (1 << 12)

Definition at line 334 of file formatting.c.

Referenced by NUMDesc_prepare().

#define NUM_F_ROMAN   (1 << 10)

Definition at line 332 of file formatting.c.

Referenced by NUM_processor(), and NUMDesc_prepare().

#define NUM_F_ZERO   (1 << 3)

Definition at line 325 of file formatting.c.

Referenced by NUMDesc_prepare().

#define NUM_LSIGN_NONE   0

Definition at line 340 of file formatting.c.

Referenced by NUMDesc_prepare().

#define NUM_LSIGN_POST   1

Definition at line 339 of file formatting.c.

Referenced by NUM_numpart_to_char(), NUM_processor(), and NUMDesc_prepare().

#define NUM_LSIGN_PRE   (-1)
#define NUM_MAX_ITEM_SIZ   8 /* roman number (RN has 15 chars) */

Definition at line 118 of file formatting.c.

Referenced by numeric_to_number().

#define NUM_TOCHAR_finish
Value:
do { \
int len; \
NUM_processor(format, &Num, VARDATA(result), numstr, 0, out_pre_spaces, sign, true, PG_GET_COLLATION()); \
\
if (shouldFree) \
\
/* \
* Convert null-terminated representation of result to standard text. \
* The result is usually much bigger than it needs to be, but there \
* seems little point in realloc'ing it smaller. \
*/ \
len = strlen(VARDATA(result)); \
} while (0)
#define VARDATA(PTR)
Definition: postgres.h:303
#define VARHDRSZ
Definition: c.h:445
return result
Definition: formatting.c:1618
#define PG_GET_COLLATION()
Definition: fmgr.h:163
void pfree(void *pointer)
Definition: mcxt.c:950
char sign
Definition: informix.c:693
static char * NUM_processor(FormatNode *node, NUMDesc *Num, char *inout, char *number, int from_char_input_len, int to_char_out_pre_spaces, int sign, bool is_to_char, Oid collid)
Definition: formatting.c:4624
static char format
Definition: pg_basebackup.c:80
#define SET_VARSIZE(PTR, len)
Definition: postgres.h:328

Definition at line 5051 of file formatting.c.

Referenced by float4_to_char(), float8_to_char(), int4_to_char(), int8_to_char(), and numeric_to_char().

#define NUM_TOCHAR_prepare
Value:
do { \
int len = VARSIZE_ANY_EXHDR(fmt); \
if (len <= 0 || len >= (INT_MAX-VARHDRSZ)/NUM_MAX_ITEM_SIZ) \
format = NUM_cache(len, &Num, fmt, &shouldFree); \
} while (0)
#define VARHDRSZ
Definition: c.h:445
return result
Definition: formatting.c:1618
static FormatNode * NUM_cache(int len, NUMDesc *Num, text *pars_str, bool *shouldFree)
Definition: formatting.c:4002
void * palloc0(Size size)
Definition: mcxt.c:878
#define PG_RETURN_TEXT_P(x)
Definition: fmgr.h:330
text * cstring_to_text(const char *s)
Definition: varlena.c:149
#define NUM_MAX_ITEM_SIZ
Definition: formatting.c:118
#define VARSIZE_ANY_EXHDR(PTR)
Definition: postgres.h:340
Definition: c.h:439
static char format
Definition: pg_basebackup.c:80

Definition at line 5038 of file formatting.c.

Referenced by float4_to_char(), float8_to_char(), int4_to_char(), int8_to_char(), and numeric_to_char().

#define NUM_TYPE   2 /* NUMBER version */

Definition at line 104 of file formatting.c.

Referenced by NUM_cache(), NUM_cache_fetch(), and parse_format().

#define ONE_UPPER   1 /* Name */

Definition at line 287 of file formatting.c.

Referenced by DCH_from_char(), and seq_search().

#define OVERLOAD_TEST   (Np->inout_p >= Np->inout + input_len)

Referenced by NUM_numpart_from_char().

#define P_M_STR   "P.M."

Definition at line 238 of file formatting.c.

Referenced by DCH_to_char().

#define p_m_STR   "p.m."

Definition at line 239 of file formatting.c.

Referenced by DCH_to_char().

#define PM_STR   "PM"

Definition at line 240 of file formatting.c.

Referenced by DCH_to_char().

#define pm_STR   "pm"

Definition at line 241 of file formatting.c.

Referenced by DCH_to_char().

#define S_FM (   _s)    (((_s) & DCH_S_FM) ? 1 : 0)

Definition at line 521 of file formatting.c.

Referenced by DCH_to_char(), and from_char_parse_int_len().

#define S_SP (   _s)    (((_s) & DCH_S_SP) ? 1 : 0)

Definition at line 522 of file formatting.c.

#define S_TH (   _s)    (((_s) & DCH_S_TH) ? 1 : 0)

Definition at line 516 of file formatting.c.

#define S_th (   _s)    (((_s) & DCH_S_th) ? 1 : 0)

Definition at line 517 of file formatting.c.

#define S_TH_TYPE (   _s)    (((_s) & DCH_S_TH) ? TH_UPPER : TH_LOWER)

Definition at line 518 of file formatting.c.

Referenced by DCH_to_char().

#define S_THth (   _s)    ((((_s) & DCH_S_TH) || ((_s) & DCH_S_th)) ? 1 : 0)

Definition at line 515 of file formatting.c.

Referenced by DCH_to_char(), and is_next_separator().

#define S_TM (   _s)    (((_s) & DCH_S_TM) ? 1 : 0)

Definition at line 523 of file formatting.c.

Referenced by DCH_to_char().

#define SKIP_THth (   ptr,
  _suf 
)
Value:
do { \
if (S_THth(_suf)) \
{ \
if (*(ptr)) (ptr)++; \
if (*(ptr)) (ptr)++; \
} \
} while (0)
#define S_THth(_s)
Definition: formatting.c:515

Definition at line 2004 of file formatting.c.

Referenced by DCH_from_char().

#define SUFFTYPE_POSTFIX   2

Definition at line 178 of file formatting.c.

Referenced by parse_format().

#define SUFFTYPE_PREFIX   1

Definition at line 177 of file formatting.c.

Referenced by parse_format().

#define TH_LOWER   2

Definition at line 300 of file formatting.c.

Referenced by NUM_processor().

#define TH_UPPER   1

Definition at line 299 of file formatting.c.

Referenced by get_th(), and NUM_processor().

#define TM_SUFFIX_LEN   2

Definition at line 529 of file formatting.c.

Referenced by DCH_to_char().

#define tmtcFsec (   _X)    ((_X)->fsec)

Definition at line 468 of file formatting.c.

Referenced by interval_to_char(), timestamp_to_char(), and timestamptz_to_char().

#define tmtcTm (   _X)    (&(_X)->tm)

Definition at line 466 of file formatting.c.

Referenced by interval_to_char(), timestamp_to_char(), and timestamptz_to_char().

#define tmtcTzn (   _X)    ((_X)->tzn)

Definition at line 467 of file formatting.c.

Referenced by DCH_to_char(), and timestamptz_to_char().

#define ZERO_tm (   _X)
Value:
do { \
(_X)->tm_sec = (_X)->tm_year = (_X)->tm_min = (_X)->tm_wday = \
(_X)->tm_hour = (_X)->tm_yday = (_X)->tm_isdst = 0; \
(_X)->tm_mday = (_X)->tm_mon = 1; \
} while(0)

Definition at line 470 of file formatting.c.

Referenced by do_to_timestamp().

#define ZERO_tmfc (   _X)    memset(_X, 0, sizeof(TmFromChar))

Definition at line 432 of file formatting.c.

Referenced by do_to_timestamp().

#define ZERO_tmtc (   _X)
Value:
do { \
ZERO_tm( tmtcTm(_X) ); \
tmtcFsec(_X) = 0; \
tmtcTzn(_X) = NULL; \
} while(0)
#define tmtcFsec(_X)
Definition: formatting.c:468
#define NULL
Definition: c.h:229
#define tmtcTzn(_X)
Definition: formatting.c:467
#define tmtcTm(_X)
Definition: formatting.c:466
#define ZERO_tm(_X)
Definition: formatting.c:470

Definition at line 477 of file formatting.c.

Referenced by interval_to_char(), timestamp_to_char(), and timestamptz_to_char().

#define zeroize_NUM (   _n)
Value:
do { \
(_n)->flag = 0; \
(_n)->lsign = 0; \
(_n)->pre = 0; \
(_n)->post = 0; \
(_n)->pre_lsign_num = 0; \
(_n)->need_locale = 0; \
(_n)->multi = 0; \
(_n)->zero_start = 0; \
(_n)->zero_end = 0; \
} while(0)
char * flag(int b)
Definition: test-ctype.c:33

Definition at line 3869 of file formatting.c.

Referenced by NUM_cache(), and NUM_cache_fetch().

Typedef Documentation

Definition at line 154 of file formatting.c.

Enumeration Type Documentation

enum DCH_poz
Enumerator
DCH_A_D 
DCH_A_M 
DCH_AD 
DCH_AM 
DCH_B_C 
DCH_BC 
DCH_CC 
DCH_DAY 
DCH_DDD 
DCH_DD 
DCH_DY 
DCH_Day 
DCH_Dy 
DCH_D 
DCH_FX 
DCH_HH24 
DCH_HH12 
DCH_HH 
DCH_IDDD 
DCH_ID 
DCH_IW 
DCH_IYYY 
DCH_IYY 
DCH_IY 
DCH_I 
DCH_J 
DCH_MI 
DCH_MM 
DCH_MONTH 
DCH_MON 
DCH_MS 
DCH_Month 
DCH_Mon 
DCH_OF 
DCH_P_M 
DCH_PM 
DCH_Q 
DCH_RM 
DCH_SSSS 
DCH_SS 
DCH_TZ 
DCH_US 
DCH_WW 
DCH_W 
DCH_Y_YYY 
DCH_YYYY 
DCH_YYY 
DCH_YY 
DCH_Y 
DCH_a_d 
DCH_a_m 
DCH_ad 
DCH_am 
DCH_b_c 
DCH_bc 
DCH_cc 
DCH_day 
DCH_ddd 
DCH_dd 
DCH_dy 
DCH_d 
DCH_fx 
DCH_hh24 
DCH_hh12 
DCH_hh 
DCH_iddd 
DCH_id 
DCH_iw 
DCH_iyyy 
DCH_iyy 
DCH_iy 
DCH_i 
DCH_j 
DCH_mi 
DCH_mm 
DCH_month 
DCH_mon 
DCH_ms 
DCH_p_m 
DCH_pm 
DCH_q 
DCH_rm 
DCH_ssss 
DCH_ss 
DCH_tz 
DCH_us 
DCH_ww 
DCH_w 
DCH_y_yyy 
DCH_yyyy 
DCH_yyy 
DCH_yy 
DCH_y 
_DCH_last_ 

Definition at line 572 of file formatting.c.

573 {
574  DCH_A_D,
575  DCH_A_M,
576  DCH_AD,
577  DCH_AM,
578  DCH_B_C,
579  DCH_BC,
580  DCH_CC,
581  DCH_DAY,
582  DCH_DDD,
583  DCH_DD,
584  DCH_DY,
585  DCH_Day,
586  DCH_Dy,
587  DCH_D,
588  DCH_FX, /* global suffix */
589  DCH_HH24,
590  DCH_HH12,
591  DCH_HH,
592  DCH_IDDD,
593  DCH_ID,
594  DCH_IW,
595  DCH_IYYY,
596  DCH_IYY,
597  DCH_IY,
598  DCH_I,
599  DCH_J,
600  DCH_MI,
601  DCH_MM,
602  DCH_MONTH,
603  DCH_MON,
604  DCH_MS,
605  DCH_Month,
606  DCH_Mon,
607  DCH_OF,
608  DCH_P_M,
609  DCH_PM,
610  DCH_Q,
611  DCH_RM,
612  DCH_SSSS,
613  DCH_SS,
614  DCH_TZ,
615  DCH_US,
616  DCH_WW,
617  DCH_W,
618  DCH_Y_YYY,
619  DCH_YYYY,
620  DCH_YYY,
621  DCH_YY,
622  DCH_Y,
623  DCH_a_d,
624  DCH_a_m,
625  DCH_ad,
626  DCH_am,
627  DCH_b_c,
628  DCH_bc,
629  DCH_cc,
630  DCH_day,
631  DCH_ddd,
632  DCH_dd,
633  DCH_dy,
634  DCH_d,
635  DCH_fx,
636  DCH_hh24,
637  DCH_hh12,
638  DCH_hh,
639  DCH_iddd,
640  DCH_id,
641  DCH_iw,
642  DCH_iyyy,
643  DCH_iyy,
644  DCH_iy,
645  DCH_i,
646  DCH_j,
647  DCH_mi,
648  DCH_mm,
649  DCH_month,
650  DCH_mon,
651  DCH_ms,
652  DCH_p_m,
653  DCH_pm,
654  DCH_q,
655  DCH_rm,
656  DCH_ssss,
657  DCH_ss,
658  DCH_tz,
659  DCH_us,
660  DCH_ww,
661  DCH_w,
662  DCH_y_yyy,
663  DCH_yyyy,
664  DCH_yyy,
665  DCH_yy,
666  DCH_y,
667 
668  /* last */
669  _DCH_last_
670 } DCH_poz;
DCH_poz
Definition: formatting.c:572
Enumerator
FROM_CHAR_DATE_NONE 
FROM_CHAR_DATE_GREGORIAN 
FROM_CHAR_DATE_ISOWEEK 

Definition at line 147 of file formatting.c.

148 {
149  FROM_CHAR_DATE_NONE = 0, /* Value does not affect date mode. */
150  FROM_CHAR_DATE_GREGORIAN, /* Gregorian (day, month, year) style date */
151  FROM_CHAR_DATE_ISOWEEK /* ISO 8601 week date */
FromCharDateMode
Definition: formatting.c:147
enum NUM_poz
Enumerator
NUM_COMMA 
NUM_DEC 
NUM_0 
NUM_9 
NUM_B 
NUM_C 
NUM_D 
NUM_E 
NUM_FM 
NUM_G 
NUM_L 
NUM_MI 
NUM_PL 
NUM_PR 
NUM_RN 
NUM_SG 
NUM_SP 
NUM_S 
NUM_TH 
NUM_V 
NUM_b 
NUM_c 
NUM_d 
NUM_e 
NUM_fm 
NUM_g 
NUM_l 
NUM_mi 
NUM_pl 
NUM_pr 
NUM_rn 
NUM_sg 
NUM_sp 
NUM_s 
NUM_th 
NUM_v 
_NUM_last_ 

Definition at line 672 of file formatting.c.

673 {
674  NUM_COMMA,
675  NUM_DEC,
676  NUM_0,
677  NUM_9,
678  NUM_B,
679  NUM_C,
680  NUM_D,
681  NUM_E,
682  NUM_FM,
683  NUM_G,
684  NUM_L,
685  NUM_MI,
686  NUM_PL,
687  NUM_PR,
688  NUM_RN,
689  NUM_SG,
690  NUM_SP,
691  NUM_S,
692  NUM_TH,
693  NUM_V,
694  NUM_b,
695  NUM_c,
696  NUM_d,
697  NUM_e,
698  NUM_fm,
699  NUM_g,
700  NUM_l,
701  NUM_mi,
702  NUM_pl,
703  NUM_pr,
704  NUM_rn,
705  NUM_sg,
706  NUM_sp,
707  NUM_s,
708  NUM_th,
709  NUM_v,
710 
711  /* last */
712  _NUM_last_
713 } NUM_poz;
NUM_poz
Definition: formatting.c:672

Function Documentation

static int adjust_partial_year_to_2020 ( int  year)
static

Definition at line 2084 of file formatting.c.

Referenced by DCH_from_char().

2085 {
2086  /*
2087  * Adjust all dates toward 2020; this is effectively what happens when we
2088  * assume '70' is 1970 and '69' is 2069.
2089  */
2090  /* Force 0-69 into the 2000's */
2091  if (year < 70)
2092  return year + 2000;
2093  /* Force 70-99 into the 1900's */
2094  else if (year < 100)
2095  return year + 1900;
2096  /* Force 100-519 into the 2000's */
2097  else if (year < 520)
2098  return year + 2000;
2099  /* Force 520-999 into the 1000's */
2100  else if (year < 1000)
2101  return year + 1000;
2102  else
2103  return year;
2104 }
char* asc_initcap ( const char *  buff,
size_t  nbytes 
)

Definition at line 1935 of file formatting.c.

References NULL, pg_ascii_tolower(), pg_ascii_toupper(), pnstrdup(), and result.

Referenced by str_initcap().

1936 {
1937  char *result;
1938  char *p;
1939  int wasalnum = false;
1940 
1941  if (!buff)
1942  return NULL;
1943 
1944  result = pnstrdup(buff, nbytes);
1945 
1946  for (p = result; *p; p++)
1947  {
1948  char c;
1949 
1950  if (wasalnum)
1951  *p = c = pg_ascii_tolower((unsigned char) *p);
1952  else
1953  *p = c = pg_ascii_toupper((unsigned char) *p);
1954  /* we don't trust isalnum() here */
1955  wasalnum = ((c >= 'A' && c <= 'Z') ||
1956  (c >= 'a' && c <= 'z') ||
1957  (c >= '0' && c <= '9'));
1958  }
1959 
1960  return result;
1961 }
char * pnstrdup(const char *in, Size len)
Definition: mcxt.c:1088
unsigned char pg_ascii_tolower(unsigned char ch)
Definition: pgstrcasecmp.c:146
unsigned char pg_ascii_toupper(unsigned char ch)
Definition: pgstrcasecmp.c:135
return result
Definition: formatting.c:1618
char * c
#define NULL
Definition: c.h:229
char* asc_tolower ( const char *  buff,
size_t  nbytes 
)

Definition at line 1889 of file formatting.c.

References NULL, pg_ascii_tolower(), pnstrdup(), and result.

Referenced by asc_tolower_z(), and str_tolower().

1890 {
1891  char *result;
1892  char *p;
1893 
1894  if (!buff)
1895  return NULL;
1896 
1897  result = pnstrdup(buff, nbytes);
1898 
1899  for (p = result; *p; p++)
1900  *p = pg_ascii_tolower((unsigned char) *p);
1901 
1902  return result;
1903 }
char * pnstrdup(const char *in, Size len)
Definition: mcxt.c:1088
unsigned char pg_ascii_tolower(unsigned char ch)
Definition: pgstrcasecmp.c:146
return result
Definition: formatting.c:1618
#define NULL
Definition: c.h:229
static char* asc_tolower_z ( const char *  buff)
static

Definition at line 1984 of file formatting.c.

References asc_tolower().

Referenced by DCH_to_char(), and NUM_processor().

1985 {
1986  return asc_tolower(buff, strlen(buff));
1987 }
char * asc_tolower(const char *buff, size_t nbytes)
Definition: formatting.c:1889
char* asc_toupper ( const char *  buff,
size_t  nbytes 
)

Definition at line 1912 of file formatting.c.

References NULL, pg_ascii_toupper(), pnstrdup(), and result.

Referenced by asc_toupper_z(), and str_toupper().

1913 {
1914  char *result;
1915  char *p;
1916 
1917  if (!buff)
1918  return NULL;
1919 
1920  result = pnstrdup(buff, nbytes);
1921 
1922  for (p = result; *p; p++)
1923  *p = pg_ascii_toupper((unsigned char) *p);
1924 
1925  return result;
1926 }
char * pnstrdup(const char *in, Size len)
Definition: mcxt.c:1088
unsigned char pg_ascii_toupper(unsigned char ch)
Definition: pgstrcasecmp.c:135
return result
Definition: formatting.c:1618
#define NULL
Definition: c.h:229
static char* asc_toupper_z ( const char *  buff)
static

Definition at line 1990 of file formatting.c.

References asc_toupper().

Referenced by DCH_to_char().

1991 {
1992  return asc_toupper(buff, strlen(buff));
1993 }
char * asc_toupper(const char *buff, size_t nbytes)
Definition: formatting.c:1912
static text* datetime_to_char_body ( TmToChar tmtc,
text fmt,
bool  is_interval,
Oid  collid 
)
static

Definition at line 3346 of file formatting.c.

References cstring_to_text(), DCH_cache_fetch(), DCH_CACHE_SIZE, DCH_index, DCH_MAX_ITEM_SIZ, DCH_to_char(), DCH_TYPE, FALSE, format, DCHCacheEntry::format, NULL, palloc(), parse_format(), pfree(), result, text_to_cstring(), and TRUE.

Referenced by interval_to_char(), timestamp_to_char(), and timestamptz_to_char().

3347 {
3348  FormatNode *format;
3349  char *fmt_str,
3350  *result;
3351  bool incache;
3352  int fmt_len;
3353  text *res;
3354 
3355  /*
3356  * Convert fmt to C string
3357  */
3358  fmt_str = text_to_cstring(fmt);
3359  fmt_len = strlen(fmt_str);
3360 
3361  /*
3362  * Allocate workspace for result as C string
3363  */
3364  result = palloc((fmt_len * DCH_MAX_ITEM_SIZ) + 1);
3365  *result = '\0';
3366 
3367  if (fmt_len > DCH_CACHE_SIZE)
3368  {
3369  /*
3370  * Allocate new memory if format picture is bigger than static cache
3371  * and do not use cache (call parser always)
3372  */
3373  incache = FALSE;
3374 
3375  format = (FormatNode *) palloc((fmt_len + 1) * sizeof(FormatNode));
3376 
3377  parse_format(format, fmt_str, DCH_keywords,
3379  }
3380  else
3381  {
3382  /*
3383  * Use cache buffers
3384  */
3385  DCHCacheEntry *ent = DCH_cache_fetch(fmt_str);
3386 
3387  incache = TRUE;
3388  format = ent->format;
3389  }
3390 
3391  /* The real work is here */
3392  DCH_to_char(format, is_interval, tmtc, result, collid);
3393 
3394  if (!incache)
3395  pfree(format);
3396 
3397  pfree(fmt_str);
3398 
3399  /* convert C-string result to TEXT format */
3400  res = cstring_to_text(result);
3401 
3402  pfree(result);
3403  return res;
3404 }
struct FormatNode FormatNode
Definition: formatting.c:154
static const KeySuffix DCH_suff[]
Definition: formatting.c:531
static const int DCH_index[KeyWord_INDEX_SIZE]
Definition: formatting.c:873
return result
Definition: formatting.c:1618
void pfree(void *pointer)
Definition: mcxt.c:950
#define DCH_CACHE_SIZE
Definition: formatting.c:374
#define FALSE
Definition: c.h:221
static void parse_format(FormatNode *node, const char *str, const KeyWord *kw, const KeySuffix *suf, const int *index, int ver, NUMDesc *Num)
Definition: formatting.c:1227
static void DCH_to_char(FormatNode *node, bool is_interval, TmToChar *in, char *out, Oid collid)
Definition: formatting.c:2390
FormatNode format[DCH_CACHE_SIZE+1]
Definition: formatting.c:379
static DCHCacheEntry * DCH_cache_fetch(const char *str)
Definition: formatting.c:3319
#define DCH_TYPE
Definition: formatting.c:103
text * cstring_to_text(const char *s)
Definition: varlena.c:149
#define NULL
Definition: c.h:229
char * text_to_cstring(const text *t)
Definition: varlena.c:182
void * palloc(Size size)
Definition: mcxt.c:849
static const KeyWord DCH_keywords[]
Definition: formatting.c:719
Definition: c.h:439
#define TRUE
Definition: c.h:217
static char format
Definition: pg_basebackup.c:80
#define DCH_MAX_ITEM_SIZ
Definition: formatting.c:117
static DCHCacheEntry * DCH_cache_fetch ( const char *  str)
static

Definition at line 3319 of file formatting.c.

References DCH_cache_getnew(), DCH_cache_search(), DCH_index, DCH_TYPE, DCHCacheEntry::format, NULL, parse_format(), and DCHCacheEntry::valid.

Referenced by datetime_to_char_body(), and do_to_timestamp().

3320 {
3321  DCHCacheEntry *ent;
3322 
3323  if ((ent = DCH_cache_search(str)) == NULL)
3324  {
3325  /*
3326  * Not in the cache, must run parser and save a new format-picture to
3327  * the cache. Do not mark the cache entry valid until parsing
3328  * succeeds.
3329  */
3330  ent = DCH_cache_getnew(str);
3331 
3332  parse_format(ent->format, str, DCH_keywords,
3334 
3335  ent->valid = true;
3336  }
3337  return ent;
3338 }
static DCHCacheEntry * DCH_cache_getnew(const char *str)
Definition: formatting.c:3229
static const KeySuffix DCH_suff[]
Definition: formatting.c:531
static const int DCH_index[KeyWord_INDEX_SIZE]
Definition: formatting.c:873
static void parse_format(FormatNode *node, const char *str, const KeyWord *kw, const KeySuffix *suf, const int *index, int ver, NUMDesc *Num)
Definition: formatting.c:1227
static DCHCacheEntry * DCH_cache_search(const char *str)
Definition: formatting.c:3291
FormatNode format[DCH_CACHE_SIZE+1]
Definition: formatting.c:379
#define DCH_TYPE
Definition: formatting.c:103
#define NULL
Definition: c.h:229
static const KeyWord DCH_keywords[]
Definition: formatting.c:719
static DCHCacheEntry * DCH_cache_getnew ( const char *  str)
static

Definition at line 3229 of file formatting.c.

References DCHCacheEntry::age, DCH_CACHE_ENTRIES, DCH_CACHE_SIZE, DCHCounter, elog, n_DCHCache, DCHCacheEntry::str, StrNCpy, and DCHCacheEntry::valid.

Referenced by DCH_cache_fetch().

3230 {
3231  DCHCacheEntry *ent;
3232 
3233  /* counter overflow check - paranoia? */
3234  if (DCHCounter >= (INT_MAX - DCH_CACHE_ENTRIES))
3235  {
3236  DCHCounter = 0;
3237 
3238  for (ent = DCHCache; ent < (DCHCache + DCH_CACHE_ENTRIES); ent++)
3239  ent->age = (++DCHCounter);
3240  }
3241 
3242  /*
3243  * If cache is full, remove oldest entry (or recycle first not-valid one)
3244  */
3246  {
3247  DCHCacheEntry *old = DCHCache + 0;
3248 
3249 #ifdef DEBUG_TO_FROM_CHAR
3250  elog(DEBUG_elog_output, "cache is full (%d)", n_DCHCache);
3251 #endif
3252  if (old->valid)
3253  {
3254  for (ent = DCHCache + 1; ent < (DCHCache + DCH_CACHE_ENTRIES); ent++)
3255  {
3256  if (!ent->valid)
3257  {
3258  old = ent;
3259  break;
3260  }
3261  if (ent->age < old->age)
3262  old = ent;
3263  }
3264  }
3265 #ifdef DEBUG_TO_FROM_CHAR
3266  elog(DEBUG_elog_output, "OLD: '%s' AGE: %d", old->str, old->age);
3267 #endif
3268  old->valid = false;
3269  StrNCpy(old->str, str, DCH_CACHE_SIZE + 1);
3270  old->age = (++DCHCounter);
3271  /* caller is expected to fill format, then set valid */
3272  return old;
3273  }
3274  else
3275  {
3276 #ifdef DEBUG_TO_FROM_CHAR
3277  elog(DEBUG_elog_output, "NEW (%d)", n_DCHCache);
3278 #endif
3279  ent = DCHCache + n_DCHCache;
3280  ent->valid = false;
3281  StrNCpy(ent->str, str, DCH_CACHE_SIZE + 1);
3282  ent->age = (++DCHCounter);
3283  /* caller is expected to fill format, then set valid */
3284  ++n_DCHCache;
3285  return ent;
3286  }
3287 }
char str[DCH_CACHE_SIZE+1]
Definition: formatting.c:380
#define DCH_CACHE_SIZE
Definition: formatting.c:374
static int n_DCHCache
Definition: formatting.c:396
#define DCH_CACHE_ENTRIES
Definition: formatting.c:375
static DCHCacheEntry DCHCache[DCH_CACHE_ENTRIES]
Definition: formatting.c:395
#define StrNCpy(dst, src, len)
Definition: c.h:830
#define elog
Definition: elog.h:219
static int DCHCounter
Definition: formatting.c:397
static DCHCacheEntry * DCH_cache_search ( const char *  str)
static

Definition at line 3291 of file formatting.c.

References DCHCacheEntry::age, DCH_CACHE_ENTRIES, DCHCounter, i, n_DCHCache, NULL, DCHCacheEntry::str, and DCHCacheEntry::valid.

Referenced by DCH_cache_fetch().

3292 {
3293  int i;
3294  DCHCacheEntry *ent;
3295 
3296  /* counter overflow check - paranoia? */
3297  if (DCHCounter >= (INT_MAX - DCH_CACHE_ENTRIES))
3298  {
3299  DCHCounter = 0;
3300 
3301  for (ent = DCHCache; ent < (DCHCache + DCH_CACHE_ENTRIES); ent++)
3302  ent->age = (++DCHCounter);
3303  }
3304 
3305  for (i = 0, ent = DCHCache; i < n_DCHCache; i++, ent++)
3306  {
3307  if (ent->valid && strcmp(ent->str, str) == 0)
3308  {
3309  ent->age = (++DCHCounter);
3310  return ent;
3311  }
3312  }
3313 
3314  return NULL;
3315 }
char str[DCH_CACHE_SIZE+1]
Definition: formatting.c:380
static int n_DCHCache
Definition: formatting.c:396
#define DCH_CACHE_ENTRIES
Definition: formatting.c:375
#define NULL
Definition: c.h:229
static DCHCacheEntry DCHCache[DCH_CACHE_ENTRIES]
Definition: formatting.c:395
int i
static int DCHCounter
Definition: formatting.c:397
static void DCH_from_char ( FormatNode node,
char *  in,
TmFromChar out 
)
static

Definition at line 2957 of file formatting.c.

References adbc_strings, adbc_strings_long, adjust_partial_year_to_2020(), ALL_LOWER, ALL_UPPER, ampm_strings, ampm_strings_long, TmFromChar::bc, TmFromChar::cc, TmFromChar::clock, CLOCK_12_HOUR, TmFromChar::d, KeyWord::date_mode, days, DCH_A_D, DCH_a_d, DCH_A_M, DCH_a_m, DCH_AD, DCH_ad, DCH_AM, DCH_am, DCH_B_C, DCH_b_c, DCH_BC, DCH_bc, DCH_CC, DCH_D, DCH_DAY, DCH_Day, DCH_day, DCH_DD, DCH_DDD, DCH_DY, DCH_Dy, DCH_dy, DCH_FX, DCH_HH, DCH_HH12, DCH_HH24, DCH_I, DCH_ID, DCH_IDDD, DCH_IW, DCH_IY, DCH_IYY, DCH_IYYY, DCH_J, DCH_MI, DCH_MM, DCH_MON, DCH_Mon, DCH_mon, DCH_MONTH, DCH_Month, DCH_month, DCH_MS, DCH_OF, DCH_P_M, DCH_p_m, DCH_PM, DCH_pm, DCH_Q, DCH_RM, DCH_rm, DCH_SS, DCH_SSSS, DCH_TZ, DCH_tz, DCH_US, DCH_W, DCH_WW, DCH_Y, DCH_Y_YYY, DCH_YY, DCH_YYY, DCH_YYYY, TmFromChar::dd, TmFromChar::ddd, ereport, errcode(), errmsg(), ERROR, from_char_parse_int(), from_char_parse_int_len(), from_char_seq_search(), from_char_set_int(), from_char_set_mode(), TmFromChar::hh, KeyWord::id, TmFromChar::j, FormatNode::key, KeyWord::len, MAX_DAY_LEN, MAX_DY_LEN, MAX_MON_LEN, MAX_MONTH_LEN, MAX_RM_LEN, TmFromChar::mi, TmFromChar::mm, months, months_full, MONTHS_PER_YEAR, TmFromChar::ms, KeyWord::name, NODE_TYPE_ACTION, NODE_TYPE_END, NULL, ONE_UPPER, TmFromChar::pm, rm_months_lower, rm_months_upper, SKIP_THth, TmFromChar::ss, TmFromChar::ssss, FormatNode::suffix, FormatNode::type, TmFromChar::us, value, TmFromChar::w, TmFromChar::ww, TmFromChar::year, and TmFromChar::yysz.

Referenced by do_to_timestamp().

2958 {
2959  FormatNode *n;
2960  char *s;
2961  int len,
2962  value;
2963  bool fx_mode = false;
2964 
2965  for (n = node, s = in; n->type != NODE_TYPE_END && *s != '\0'; n++)
2966  {
2967  if (n->type != NODE_TYPE_ACTION)
2968  {
2969  /*
2970  * Separator, so consume one character from input string. Notice
2971  * we don't insist that the consumed character match the format's
2972  * character.
2973  */
2974  s++;
2975  continue;
2976  }
2977 
2978  /* Ignore spaces before fields when not in FX (fixed width) mode */
2979  if (!fx_mode && n->key->id != DCH_FX)
2980  {
2981  while (*s != '\0' && isspace((unsigned char) *s))
2982  s++;
2983  }
2984 
2985  from_char_set_mode(out, n->key->date_mode);
2986 
2987  switch (n->key->id)
2988  {
2989  case DCH_FX:
2990  fx_mode = true;
2991  break;
2992  case DCH_A_M:
2993  case DCH_P_M:
2994  case DCH_a_m:
2995  case DCH_p_m:
2997  ALL_UPPER, n->key->len, n);
2998  from_char_set_int(&out->pm, value % 2, n);
2999  out->clock = CLOCK_12_HOUR;
3000  break;
3001  case DCH_AM:
3002  case DCH_PM:
3003  case DCH_am:
3004  case DCH_pm:
3005  from_char_seq_search(&value, &s, ampm_strings,
3006  ALL_UPPER, n->key->len, n);
3007  from_char_set_int(&out->pm, value % 2, n);
3008  out->clock = CLOCK_12_HOUR;
3009  break;
3010  case DCH_HH:
3011  case DCH_HH12:
3012  from_char_parse_int_len(&out->hh, &s, 2, n);
3013  out->clock = CLOCK_12_HOUR;
3014  SKIP_THth(s, n->suffix);
3015  break;
3016  case DCH_HH24:
3017  from_char_parse_int_len(&out->hh, &s, 2, n);
3018  SKIP_THth(s, n->suffix);
3019  break;
3020  case DCH_MI:
3021  from_char_parse_int(&out->mi, &s, n);
3022  SKIP_THth(s, n->suffix);
3023  break;
3024  case DCH_SS:
3025  from_char_parse_int(&out->ss, &s, n);
3026  SKIP_THth(s, n->suffix);
3027  break;
3028  case DCH_MS: /* millisecond */
3029  len = from_char_parse_int_len(&out->ms, &s, 3, n);
3030 
3031  /*
3032  * 25 is 0.25 and 250 is 0.25 too; 025 is 0.025 and not 0.25
3033  */
3034  out->ms *= len == 1 ? 100 :
3035  len == 2 ? 10 : 1;
3036 
3037  SKIP_THth(s, n->suffix);
3038  break;
3039  case DCH_US: /* microsecond */
3040  len = from_char_parse_int_len(&out->us, &s, 6, n);
3041 
3042  out->us *= len == 1 ? 100000 :
3043  len == 2 ? 10000 :
3044  len == 3 ? 1000 :
3045  len == 4 ? 100 :
3046  len == 5 ? 10 : 1;
3047 
3048  SKIP_THth(s, n->suffix);
3049  break;
3050  case DCH_SSSS:
3051  from_char_parse_int(&out->ssss, &s, n);
3052  SKIP_THth(s, n->suffix);
3053  break;
3054  case DCH_tz:
3055  case DCH_TZ:
3056  case DCH_OF:
3057  ereport(ERROR,
3058  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
3059  errmsg("formatting field \"%s\" is only supported in to_char",
3060  n->key->name)));
3061  break;
3062  case DCH_A_D:
3063  case DCH_B_C:
3064  case DCH_a_d:
3065  case DCH_b_c:
3067  ALL_UPPER, n->key->len, n);
3068  from_char_set_int(&out->bc, value % 2, n);
3069  break;
3070  case DCH_AD:
3071  case DCH_BC:
3072  case DCH_ad:
3073  case DCH_bc:
3074  from_char_seq_search(&value, &s, adbc_strings,
3075  ALL_UPPER, n->key->len, n);
3076  from_char_set_int(&out->bc, value % 2, n);
3077  break;
3078  case DCH_MONTH:
3079  case DCH_Month:
3080  case DCH_month:
3082  MAX_MONTH_LEN, n);
3083  from_char_set_int(&out->mm, value + 1, n);
3084  break;
3085  case DCH_MON:
3086  case DCH_Mon:
3087  case DCH_mon:
3088  from_char_seq_search(&value, &s, months, ONE_UPPER,
3089  MAX_MON_LEN, n);
3090  from_char_set_int(&out->mm, value + 1, n);
3091  break;
3092  case DCH_MM:
3093  from_char_parse_int(&out->mm, &s, n);
3094  SKIP_THth(s, n->suffix);
3095  break;
3096  case DCH_DAY:
3097  case DCH_Day:
3098  case DCH_day:
3099  from_char_seq_search(&value, &s, days, ONE_UPPER,
3100  MAX_DAY_LEN, n);
3101  from_char_set_int(&out->d, value, n);
3102  out->d++;
3103  break;
3104  case DCH_DY:
3105  case DCH_Dy:
3106  case DCH_dy:
3107  from_char_seq_search(&value, &s, days, ONE_UPPER,
3108  MAX_DY_LEN, n);
3109  from_char_set_int(&out->d, value, n);
3110  out->d++;
3111  break;
3112  case DCH_DDD:
3113  from_char_parse_int(&out->ddd, &s, n);
3114  SKIP_THth(s, n->suffix);
3115  break;
3116  case DCH_IDDD:
3117  from_char_parse_int_len(&out->ddd, &s, 3, n);
3118  SKIP_THth(s, n->suffix);
3119  break;
3120  case DCH_DD:
3121  from_char_parse_int(&out->dd, &s, n);
3122  SKIP_THth(s, n->suffix);
3123  break;
3124  case DCH_D:
3125  from_char_parse_int(&out->d, &s, n);
3126  SKIP_THth(s, n->suffix);
3127  break;
3128  case DCH_ID:
3129  from_char_parse_int_len(&out->d, &s, 1, n);
3130  /* Shift numbering to match Gregorian where Sunday = 1 */
3131  if (++out->d > 7)
3132  out->d = 1;
3133  SKIP_THth(s, n->suffix);
3134  break;
3135  case DCH_WW:
3136  case DCH_IW:
3137  from_char_parse_int(&out->ww, &s, n);
3138  SKIP_THth(s, n->suffix);
3139  break;
3140  case DCH_Q:
3141 
3142  /*
3143  * We ignore 'Q' when converting to date because it is unclear
3144  * which date in the quarter to use, and some people specify
3145  * both quarter and month, so if it was honored it might
3146  * conflict with the supplied month. That is also why we don't
3147  * throw an error.
3148  *
3149  * We still parse the source string for an integer, but it
3150  * isn't stored anywhere in 'out'.
3151  */
3152  from_char_parse_int((int *) NULL, &s, n);
3153  SKIP_THth(s, n->suffix);
3154  break;
3155  case DCH_CC:
3156  from_char_parse_int(&out->cc, &s, n);
3157  SKIP_THth(s, n->suffix);
3158  break;
3159  case DCH_Y_YYY:
3160  {
3161  int matched,
3162  years,
3163  millennia,
3164  nch;
3165 
3166  matched = sscanf(s, "%d,%03d%n", &millennia, &years, &nch);
3167  if (matched < 2)
3168  ereport(ERROR,
3169  (errcode(ERRCODE_INVALID_DATETIME_FORMAT),
3170  errmsg("invalid input string for \"Y,YYY\"")));
3171  years += (millennia * 1000);
3172  from_char_set_int(&out->year, years, n);
3173  out->yysz = 4;
3174  s += nch;
3175  SKIP_THth(s, n->suffix);
3176  }
3177  break;
3178  case DCH_YYYY:
3179  case DCH_IYYY:
3180  from_char_parse_int(&out->year, &s, n);
3181  out->yysz = 4;
3182  SKIP_THth(s, n->suffix);
3183  break;
3184  case DCH_YYY:
3185  case DCH_IYY:
3186  if (from_char_parse_int(&out->year, &s, n) < 4)
3187  out->year = adjust_partial_year_to_2020(out->year);
3188  out->yysz = 3;
3189  SKIP_THth(s, n->suffix);
3190  break;
3191  case DCH_YY:
3192  case DCH_IY:
3193  if (from_char_parse_int(&out->year, &s, n) < 4)
3194  out->year = adjust_partial_year_to_2020(out->year);
3195  out->yysz = 2;
3196  SKIP_THth(s, n->suffix);
3197  break;
3198  case DCH_Y:
3199  case DCH_I:
3200  if (from_char_parse_int(&out->year, &s, n) < 4)
3201  out->year = adjust_partial_year_to_2020(out->year);
3202  out->yysz = 1;
3203  SKIP_THth(s, n->suffix);
3204  break;
3205  case DCH_RM:
3207  ALL_UPPER, MAX_RM_LEN, n);
3208  from_char_set_int(&out->mm, MONTHS_PER_YEAR - value, n);
3209  break;
3210  case DCH_rm:
3212  ALL_LOWER, MAX_RM_LEN, n);
3213  from_char_set_int(&out->mm, MONTHS_PER_YEAR - value, n);
3214  break;
3215  case DCH_W:
3216  from_char_parse_int(&out->w, &s, n);
3217  SKIP_THth(s, n->suffix);
3218  break;
3219  case DCH_J:
3220  from_char_parse_int(&out->j, &s, n);
3221  SKIP_THth(s, n->suffix);
3222  break;
3223  }
3224  }
3225 }
#define NODE_TYPE_END
Definition: formatting.c:173
const KeyWord * key
Definition: formatting.c:168
static const char *const adbc_strings_long[]
Definition: formatting.c:227
static void from_char_set_int(int *dest, const int value, const FormatNode *node)
Definition: formatting.c:2149
FromCharDateMode date_mode
Definition: formatting.c:162
#define MAX_DY_LEN
Definition: formatting.c:296
int errcode(int sqlerrcode)
Definition: elog.c:575
static const char *const rm_months_upper[]
Definition: formatting.c:262
int id
Definition: formatting.c:160
static const char *const ampm_strings_long[]
Definition: formatting.c:254
static int from_char_parse_int_len(int *dest, char **src, const int len, FormatNode *node)
Definition: formatting.c:2182
static const char *const adbc_strings[]
Definition: formatting.c:226
#define MONTHS_PER_YEAR
Definition: timestamp.h:69
#define MAX_MON_LEN
Definition: formatting.c:294
#define ONE_UPPER
Definition: formatting.c:287
const char *const months[]
Definition: datetime.c:66
#define NODE_TYPE_ACTION
Definition: formatting.c:174
#define ERROR
Definition: elog.h:43
static int from_char_seq_search(int *dest, char **src, const char *const *array, int type, int max, FormatNode *node)
Definition: formatting.c:2360
#define SKIP_THth(ptr, _suf)
Definition: formatting.c:2004
const char *const days[]
Definition: datetime.c:69
#define ereport(elevel, rest)
Definition: elog.h:122
#define MAX_RM_LEN
Definition: formatting.c:297
#define CLOCK_12_HOUR
Definition: formatting.c:181
static const char *const ampm_strings[]
Definition: formatting.c:253
const char * name
Definition: formatting.c:158
static const char *const months_full[]
Definition: formatting.c:188
int len
Definition: formatting.c:159
#define ALL_UPPER
Definition: formatting.c:288
static int adjust_partial_year_to_2020(int year)
Definition: formatting.c:2084
#define NULL
Definition: c.h:229
#define MAX_MONTH_LEN
Definition: formatting.c:293
static int from_char_parse_int(int *dest, char **src, FormatNode *node)
Definition: formatting.c:2272
int errmsg(const char *fmt,...)
Definition: elog.c:797
static void from_char_set_mode(TmFromChar *tmfc, const FromCharDateMode mode)
Definition: formatting.c:2127
#define ALL_LOWER
Definition: formatting.c:289
static struct @121 value
#define MAX_DAY_LEN
Definition: formatting.c:295
static const char *const rm_months_lower[]
Definition: formatting.c:265
static void DCH_to_char ( FormatNode node,
bool  is_interval,
TmToChar in,
char *  out,
Oid  collid 
)
static

Definition at line 2390 of file formatting.c.

References A_D_STR, a_d_STR, A_M_STR, a_m_STR, AD_STR, ad_STR, ADJUST_YEAR, AM_STR, am_STR, asc_tolower_z(), asc_toupper_z(), B_C_STR, b_c_STR, BC_STR, bc_STR, cache_locale_time(), FormatNode::character, date2isoweek(), date2isoyear(), date2isoyearday(), date2j(), days, days_short, DCH_A_D, DCH_a_d, DCH_A_M, DCH_a_m, DCH_AD, DCH_ad, DCH_AM, DCH_am, DCH_B_C, DCH_b_c, DCH_BC, DCH_bc, DCH_CC, DCH_D, DCH_DAY, DCH_Day, DCH_day, DCH_DD, DCH_DDD, DCH_DY, DCH_Dy, DCH_dy, DCH_HH, DCH_HH12, DCH_HH24, DCH_I, DCH_ID, DCH_IDDD, DCH_IW, DCH_IY, DCH_IYY, DCH_IYYY, DCH_J, DCH_MAX_ITEM_SIZ, DCH_MI, DCH_MM, DCH_MON, DCH_Mon, DCH_mon, DCH_MONTH, DCH_Month, DCH_month, DCH_MS, DCH_OF, DCH_P_M, DCH_p_m, DCH_PM, DCH_pm, DCH_Q, DCH_RM, DCH_rm, DCH_SS, DCH_SSSS, DCH_TZ, DCH_tz, DCH_US, DCH_W, DCH_WW, DCH_Y, DCH_Y_YYY, DCH_YY, DCH_YYY, DCH_YYYY, ereport, errcode(), errmsg(), ERROR, TmToChar::fsec, HOURS_PER_DAY, i, KeyWord::id, INT64CONST, INVALID_FOR_INTERVAL, FormatNode::key, KeyWord::len, localized_abbrev_days, localized_abbrev_months, localized_full_days, localized_full_months, months, months_full, MONTHS_PER_YEAR, NODE_TYPE_ACTION, NODE_TYPE_END, P_M_STR, p_m_STR, pfree(), PM_STR, pm_STR, rm_months_lower, rm_months_upper, S_FM, S_TH_TYPE, S_THth, S_TM, SECS_PER_HOUR, SECS_PER_MINUTE, str_initcap_z(), str_numth(), str_tolower_z(), str_toupper_z(), FormatNode::suffix, tm, TmToChar::tm, pg_tm::tm_gmtoff, pg_tm::tm_hour, pg_tm::tm_mday, pg_tm::tm_min, pg_tm::tm_mon, pg_tm::tm_sec, TM_SUFFIX_LEN, pg_tm::tm_wday, pg_tm::tm_yday, pg_tm::tm_year, tmtcTzn, and FormatNode::type.

Referenced by datetime_to_char_body().

2391 {
2392  FormatNode *n;
2393  char *s;
2394  struct pg_tm *tm = &in->tm;
2395  int i;
2396 
2397  /* cache localized days and months */
2399 
2400  s = out;
2401  for (n = node; n->type != NODE_TYPE_END; n++)
2402  {
2403  if (n->type != NODE_TYPE_ACTION)
2404  {
2405  *s = n->character;
2406  s++;
2407  continue;
2408  }
2409 
2410  switch (n->key->id)
2411  {
2412  case DCH_A_M:
2413  case DCH_P_M:
2414  strcpy(s, (tm->tm_hour % HOURS_PER_DAY >= HOURS_PER_DAY / 2)
2415  ? P_M_STR : A_M_STR);
2416  s += strlen(s);
2417  break;
2418  case DCH_AM:
2419  case DCH_PM:
2420  strcpy(s, (tm->tm_hour % HOURS_PER_DAY >= HOURS_PER_DAY / 2)
2421  ? PM_STR : AM_STR);
2422  s += strlen(s);
2423  break;
2424  case DCH_a_m:
2425  case DCH_p_m:
2426  strcpy(s, (tm->tm_hour % HOURS_PER_DAY >= HOURS_PER_DAY / 2)
2427  ? p_m_STR : a_m_STR);
2428  s += strlen(s);
2429  break;
2430  case DCH_am:
2431  case DCH_pm:
2432  strcpy(s, (tm->tm_hour % HOURS_PER_DAY >= HOURS_PER_DAY / 2)
2433  ? pm_STR : am_STR);
2434  s += strlen(s);
2435  break;
2436  case DCH_HH:
2437  case DCH_HH12:
2438 
2439  /*
2440  * display time as shown on a 12-hour clock, even for
2441  * intervals
2442  */
2443  sprintf(s, "%0*d", S_FM(n->suffix) ? 0 : (tm->tm_hour >= 0) ? 2 : 3,
2444  tm->tm_hour % (HOURS_PER_DAY / 2) == 0 ? HOURS_PER_DAY / 2 :
2445  tm->tm_hour % (HOURS_PER_DAY / 2));
2446  if (S_THth(n->suffix))
2447  str_numth(s, s, S_TH_TYPE(n->suffix));
2448  s += strlen(s);
2449  break;
2450  case DCH_HH24:
2451  sprintf(s, "%0*d", S_FM(n->suffix) ? 0 : (tm->tm_hour >= 0) ? 2 : 3,
2452  tm->tm_hour);
2453  if (S_THth(n->suffix))
2454  str_numth(s, s, S_TH_TYPE(n->suffix));
2455  s += strlen(s);
2456  break;
2457  case DCH_MI:
2458  sprintf(s, "%0*d", S_FM(n->suffix) ? 0 : (tm->tm_min >= 0) ? 2 : 3,
2459  tm->tm_min);
2460  if (S_THth(n->suffix))
2461  str_numth(s, s, S_TH_TYPE(n->suffix));
2462  s += strlen(s);
2463  break;
2464  case DCH_SS:
2465  sprintf(s, "%0*d", S_FM(n->suffix) ? 0 : (tm->tm_sec >= 0) ? 2 : 3,
2466  tm->tm_sec);
2467  if (S_THth(n->suffix))
2468  str_numth(s, s, S_TH_TYPE(n->suffix));
2469  s += strlen(s);
2470  break;
2471  case DCH_MS: /* millisecond */
2472  sprintf(s, "%03d", (int) (in->fsec / INT64CONST(1000)));
2473  if (S_THth(n->suffix))
2474  str_numth(s, s, S_TH_TYPE(n->suffix));
2475  s += strlen(s);
2476  break;
2477  case DCH_US: /* microsecond */
2478  sprintf(s, "%06d", (int) in->fsec);
2479  if (S_THth(n->suffix))
2480  str_numth(s, s, S_TH_TYPE(n->suffix));
2481  s += strlen(s);
2482  break;
2483  case DCH_SSSS:
2484  sprintf(s, "%d", tm->tm_hour * SECS_PER_HOUR +
2485  tm->tm_min * SECS_PER_MINUTE +
2486  tm->tm_sec);
2487  if (S_THth(n->suffix))
2488  str_numth(s, s, S_TH_TYPE(n->suffix));
2489  s += strlen(s);
2490  break;
2491  case DCH_tz:
2493  if (tmtcTzn(in))
2494  {
2495  /* We assume here that timezone names aren't localized */
2496  char *p = asc_tolower_z(tmtcTzn(in));
2497 
2498  strcpy(s, p);
2499  pfree(p);
2500  s += strlen(s);
2501  }
2502  break;
2503  case DCH_TZ:
2505  if (tmtcTzn(in))
2506  {
2507  strcpy(s, tmtcTzn(in));
2508  s += strlen(s);
2509  }
2510  break;
2511  case DCH_OF:
2513  sprintf(s, "%c%0*d",
2514  (tm->tm_gmtoff >= 0) ? '+' : '-',
2515  S_FM(n->suffix) ? 0 : 2,
2516  abs((int) tm->tm_gmtoff) / SECS_PER_HOUR);
2517  s += strlen(s);
2518  if (abs((int) tm->tm_gmtoff) % SECS_PER_HOUR != 0)
2519  {
2520  sprintf(s, ":%02d",
2521  (abs((int) tm->tm_gmtoff) % SECS_PER_HOUR) / SECS_PER_MINUTE);
2522  s += strlen(s);
2523  }
2524  break;
2525  case DCH_A_D:
2526  case DCH_B_C:
2528  strcpy(s, (tm->tm_year <= 0 ? B_C_STR : A_D_STR));
2529  s += strlen(s);
2530  break;
2531  case DCH_AD:
2532  case DCH_BC:
2534  strcpy(s, (tm->tm_year <= 0 ? BC_STR : AD_STR));
2535  s += strlen(s);
2536  break;
2537  case DCH_a_d:
2538  case DCH_b_c:
2540  strcpy(s, (tm->tm_year <= 0 ? b_c_STR : a_d_STR));
2541  s += strlen(s);
2542  break;
2543  case DCH_ad:
2544  case DCH_bc:
2546  strcpy(s, (tm->tm_year <= 0 ? bc_STR : ad_STR));
2547  s += strlen(s);
2548  break;
2549  case DCH_MONTH:
2551  if (!tm->tm_mon)
2552  break;
2553  if (S_TM(n->suffix))
2554  {
2555  char *str = str_toupper_z(localized_full_months[tm->tm_mon - 1], collid);
2556 
2557  if (strlen(str) <= (n->key->len + TM_SUFFIX_LEN) * DCH_MAX_ITEM_SIZ)
2558  strcpy(s, str);
2559  else
2560  ereport(ERROR,
2561  (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
2562  errmsg("localized string format value too long")));
2563  }
2564  else
2565  sprintf(s, "%*s", S_FM(n->suffix) ? 0 : -9,
2566  asc_toupper_z(months_full[tm->tm_mon - 1]));
2567  s += strlen(s);
2568  break;
2569  case DCH_Month:
2571  if (!tm->tm_mon)
2572  break;
2573  if (S_TM(n->suffix))
2574  {
2575  char *str = str_initcap_z(localized_full_months[tm->tm_mon - 1], collid);
2576 
2577  if (strlen(str) <= (n->key->len + TM_SUFFIX_LEN) * DCH_MAX_ITEM_SIZ)
2578  strcpy(s, str);
2579  else
2580  ereport(ERROR,
2581  (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
2582  errmsg("localized string format value too long")));
2583  }
2584  else
2585  sprintf(s, "%*s", S_FM(n->suffix) ? 0 : -9,
2586  months_full[tm->tm_mon - 1]);
2587  s += strlen(s);
2588  break;
2589  case DCH_month:
2591  if (!tm->tm_mon)
2592  break;
2593  if (S_TM(n->suffix))
2594  {
2595  char *str = str_tolower_z(localized_full_months[tm->tm_mon - 1], collid);
2596 
2597  if (strlen(str) <= (n->key->len + TM_SUFFIX_LEN) * DCH_MAX_ITEM_SIZ)
2598  strcpy(s, str);
2599  else
2600  ereport(ERROR,
2601  (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
2602  errmsg("localized string format value too long")));
2603  }
2604  else
2605  sprintf(s, "%*s", S_FM(n->suffix) ? 0 : -9,
2606  asc_tolower_z(months_full[tm->tm_mon - 1]));
2607  s += strlen(s);
2608  break;
2609  case DCH_MON:
2611  if (!tm->tm_mon)
2612  break;
2613  if (S_TM(n->suffix))
2614  {
2615  char *str = str_toupper_z(localized_abbrev_months[tm->tm_mon - 1], collid);
2616 
2617  if (strlen(str) <= (n->key->len + TM_SUFFIX_LEN) * DCH_MAX_ITEM_SIZ)
2618  strcpy(s, str);
2619  else
2620  ereport(ERROR,
2621  (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
2622  errmsg("localized string format value too long")));
2623  }
2624  else
2625  strcpy(s, asc_toupper_z(months[tm->tm_mon - 1]));
2626  s += strlen(s);
2627  break;
2628  case DCH_Mon:
2630  if (!tm->tm_mon)
2631  break;
2632  if (S_TM(n->suffix))
2633  {
2634  char *str = str_initcap_z(localized_abbrev_months[tm->tm_mon - 1], collid);
2635 
2636  if (strlen(str) <= (n->key->len + TM_SUFFIX_LEN) * DCH_MAX_ITEM_SIZ)
2637  strcpy(s, str);
2638  else
2639  ereport(ERROR,
2640  (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
2641  errmsg("localized string format value too long")));
2642  }
2643  else
2644  strcpy(s, months[tm->tm_mon - 1]);
2645  s += strlen(s);
2646  break;
2647  case DCH_mon:
2649  if (!tm->tm_mon)
2650  break;
2651  if (S_TM(n->suffix))
2652  {
2653  char *str = str_tolower_z(localized_abbrev_months[tm->tm_mon - 1], collid);
2654 
2655  if (strlen(str) <= (n->key->len + TM_SUFFIX_LEN) * DCH_MAX_ITEM_SIZ)
2656  strcpy(s, str);
2657  else
2658  ereport(ERROR,
2659  (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
2660  errmsg("localized string format value too long")));
2661  }
2662  else
2663  strcpy(s, asc_tolower_z(months[tm->tm_mon - 1]));
2664  s += strlen(s);
2665  break;
2666  case DCH_MM:
2667  sprintf(s, "%0*d", S_FM(n->suffix) ? 0 : (tm->tm_mon >= 0) ? 2 : 3,
2668  tm->tm_mon);
2669  if (S_THth(n->suffix))
2670  str_numth(s, s, S_TH_TYPE(n->suffix));
2671  s += strlen(s);
2672  break;
2673  case DCH_DAY:
2675  if (S_TM(n->suffix))
2676  {
2677  char *str = str_toupper_z(localized_full_days[tm->tm_wday], collid);
2678 
2679  if (strlen(str) <= (n->key->len + TM_SUFFIX_LEN) * DCH_MAX_ITEM_SIZ)
2680  strcpy(s, str);
2681  else
2682  ereport(ERROR,
2683  (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
2684  errmsg("localized string format value too long")));
2685  }
2686  else
2687  sprintf(s, "%*s", S_FM(n->suffix) ? 0 : -9,
2688  asc_toupper_z(days[tm->tm_wday]));
2689  s += strlen(s);
2690  break;
2691  case DCH_Day:
2693  if (S_TM(n->suffix))
2694  {
2695  char *str = str_initcap_z(localized_full_days[tm->tm_wday], collid);
2696 
2697  if (strlen(str) <= (n->key->len + TM_SUFFIX_LEN) * DCH_MAX_ITEM_SIZ)
2698  strcpy(s, str);
2699  else
2700  ereport(ERROR,
2701  (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
2702  errmsg("localized string format value too long")));
2703  }
2704  else
2705  sprintf(s, "%*s", S_FM(n->suffix) ? 0 : -9,
2706  days[tm->tm_wday]);
2707  s += strlen(s);
2708  break;
2709  case DCH_day:
2711  if (S_TM(n->suffix))
2712  {
2713  char *str = str_tolower_z(localized_full_days[tm->tm_wday], collid);
2714 
2715  if (strlen(str) <= (n->key->len + TM_SUFFIX_LEN) * DCH_MAX_ITEM_SIZ)
2716  strcpy(s, str);
2717  else
2718  ereport(ERROR,
2719  (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
2720  errmsg("localized string format value too long")));
2721  }
2722  else
2723  sprintf(s, "%*s", S_FM(n->suffix) ? 0 : -9,
2724  asc_tolower_z(days[tm->tm_wday]));
2725  s += strlen(s);
2726  break;
2727  case DCH_DY:
2729  if (S_TM(n->suffix))
2730  {
2731  char *str = str_toupper_z(localized_abbrev_days[tm->tm_wday], collid);
2732 
2733  if (strlen(str) <= (n->key->len + TM_SUFFIX_LEN) * DCH_MAX_ITEM_SIZ)
2734  strcpy(s, str);
2735  else
2736  ereport(ERROR,
2737  (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
2738  errmsg("localized string format value too long")));
2739  }
2740  else
2741  strcpy(s, asc_toupper_z(days_short[tm->tm_wday]));
2742  s += strlen(s);
2743  break;
2744  case DCH_Dy:
2746  if (S_TM(n->suffix))
2747  {
2748  char *str = str_initcap_z(localized_abbrev_days[tm->tm_wday], collid);
2749 
2750  if (strlen(str) <= (n->key->len + TM_SUFFIX_LEN) * DCH_MAX_ITEM_SIZ)
2751  strcpy(s, str);
2752  else
2753  ereport(ERROR,
2754  (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
2755  errmsg("localized string format value too long")));
2756  }
2757  else
2758  strcpy(s, days_short[tm->tm_wday]);
2759  s += strlen(s);
2760  break;
2761  case DCH_dy:
2763  if (S_TM(n->suffix))
2764  {
2765  char *str = str_tolower_z(localized_abbrev_days[tm->tm_wday], collid);
2766 
2767  if (strlen(str) <= (n->key->len + TM_SUFFIX_LEN) * DCH_MAX_ITEM_SIZ)
2768  strcpy(s, str);
2769  else
2770  ereport(ERROR,
2771  (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
2772  errmsg("localized string format value too long")));
2773  }
2774  else
2775  strcpy(s, asc_tolower_z(days_short[tm->tm_wday]));
2776  s += strlen(s);
2777  break;
2778  case DCH_DDD:
2779  case DCH_IDDD:
2780  sprintf(s, "%0*d", S_FM(n->suffix) ? 0 : 3,
2781  (n->key->id == DCH_DDD) ?
2782  tm->tm_yday :
2783  date2isoyearday(tm->tm_year, tm->tm_mon, tm->tm_mday));
2784  if (S_THth(n->suffix))
2785  str_numth(s, s, S_TH_TYPE(n->suffix));
2786  s += strlen(s);
2787  break;
2788  case DCH_DD:
2789  sprintf(s, "%0*d", S_FM(n->suffix) ? 0 : 2, tm->tm_mday);
2790  if (S_THth(n->suffix))
2791  str_numth(s, s, S_TH_TYPE(n->suffix));
2792  s += strlen(s);
2793  break;
2794  case DCH_D:
2796  sprintf(s, "%d", tm->tm_wday + 1);
2797  if (S_THth(n->suffix))
2798  str_numth(s, s, S_TH_TYPE(n->suffix));
2799  s += strlen(s);
2800  break;
2801  case DCH_ID:
2803  sprintf(s, "%d", (tm->tm_wday == 0) ? 7 : tm->tm_wday);
2804  if (S_THth(n->suffix))
2805  str_numth(s, s, S_TH_TYPE(n->suffix));
2806  s += strlen(s);
2807  break;
2808  case DCH_WW:
2809  sprintf(s, "%0*d", S_FM(n->suffix) ? 0 : 2,
2810  (tm->tm_yday - 1) / 7 + 1);
2811  if (S_THth(n->suffix))
2812  str_numth(s, s, S_TH_TYPE(n->suffix));
2813  s += strlen(s);
2814  break;
2815  case DCH_IW:
2816  sprintf(s, "%0*d", S_FM(n->suffix) ? 0 : 2,
2817  date2isoweek(tm->tm_year, tm->tm_mon, tm->tm_mday));
2818  if (S_THth(n->suffix))
2819  str_numth(s, s, S_TH_TYPE(n->suffix));
2820  s += strlen(s);
2821  break;
2822  case DCH_Q:
2823  if (!tm->tm_mon)
2824  break;
2825  sprintf(s, "%d", (tm->tm_mon - 1) / 3 + 1);
2826  if (S_THth(n->suffix))
2827  str_numth(s, s, S_TH_TYPE(n->suffix));
2828  s += strlen(s);
2829  break;
2830  case DCH_CC:
2831  if (is_interval) /* straight calculation */
2832  i = tm->tm_year / 100;
2833  else
2834  {
2835  if (tm->tm_year > 0)
2836  /* Century 20 == 1901 - 2000 */
2837  i = (tm->tm_year - 1) / 100 + 1;
2838  else
2839  /* Century 6BC == 600BC - 501BC */
2840  i = tm->tm_year / 100 - 1;
2841  }
2842  if (i <= 99 && i >= -99)
2843  sprintf(s, "%0*d", S_FM(n->suffix) ? 0 : (i >= 0) ? 2 : 3, i);
2844  else
2845  sprintf(s, "%d", i);
2846  if (S_THth(n->suffix))
2847  str_numth(s, s, S_TH_TYPE(n->suffix));
2848  s += strlen(s);
2849  break;
2850  case DCH_Y_YYY:
2851  i = ADJUST_YEAR(tm->tm_year, is_interval) / 1000;
2852  sprintf(s, "%d,%03d", i,
2853  ADJUST_YEAR(tm->tm_year, is_interval) - (i * 1000));
2854  if (S_THth(n->suffix))
2855  str_numth(s, s, S_TH_TYPE(n->suffix));
2856  s += strlen(s);
2857  break;
2858  case DCH_YYYY:
2859  case DCH_IYYY:
2860  sprintf(s, "%0*d",
2861  S_FM(n->suffix) ? 0 :
2862  (ADJUST_YEAR(tm->tm_year, is_interval) >= 0) ? 4 : 5,
2863  (n->key->id == DCH_YYYY ?
2864  ADJUST_YEAR(tm->tm_year, is_interval) :
2866  tm->tm_mon,
2867  tm->tm_mday),
2868  is_interval)));
2869  if (S_THth(n->suffix))
2870  str_numth(s, s, S_TH_TYPE(n->suffix));
2871  s += strlen(s);
2872  break;
2873  case DCH_YYY:
2874  case DCH_IYY:
2875  sprintf(s, "%0*d",
2876  S_FM(n->suffix) ? 0 :
2877  (ADJUST_YEAR(tm->tm_year, is_interval) >= 0) ? 3 : 4,
2878  (n->key->id == DCH_YYY ?
2879  ADJUST_YEAR(tm->tm_year, is_interval) :
2881  tm->tm_mon,
2882  tm->tm_mday),
2883  is_interval)) % 1000);
2884  if (S_THth(n->suffix))
2885  str_numth(s, s, S_TH_TYPE(n->suffix));
2886  s += strlen(s);
2887  break;
2888  case DCH_YY:
2889  case DCH_IY:
2890  sprintf(s, "%0*d",
2891  S_FM(n->suffix) ? 0 :
2892  (ADJUST_YEAR(tm->tm_year, is_interval) >= 0) ? 2 : 3,
2893  (n->key->id == DCH_YY ?
2894  ADJUST_YEAR(tm->tm_year, is_interval) :
2896  tm->tm_mon,
2897  tm->tm_mday),
2898  is_interval)) % 100);
2899  if (S_THth(n->suffix))
2900  str_numth(s, s, S_TH_TYPE(n->suffix));
2901  s += strlen(s);
2902  break;
2903  case DCH_Y:
2904  case DCH_I:
2905  sprintf(s, "%1d",
2906  (n->key->id == DCH_Y ?
2907  ADJUST_YEAR(tm->tm_year, is_interval) :
2909  tm->tm_mon,
2910  tm->tm_mday),
2911  is_interval)) % 10);
2912  if (S_THth(n->suffix))
2913  str_numth(s, s, S_TH_TYPE(n->suffix));
2914  s += strlen(s);
2915  break;
2916  case DCH_RM:
2917  if (!tm->tm_mon)
2918  break;
2919  sprintf(s, "%*s", S_FM(n->suffix) ? 0 : -4,
2921  s += strlen(s);
2922  break;
2923  case DCH_rm:
2924  if (!tm->tm_mon)
2925  break;
2926  sprintf(s, "%*s", S_FM(n->suffix) ? 0 : -4,
2928  s += strlen(s);
2929  break;
2930  case DCH_W:
2931  sprintf(s, "%d", (tm->tm_mday - 1) / 7 + 1);
2932  if (S_THth(n->suffix))
2933  str_numth(s, s, S_TH_TYPE(n->suffix));
2934  s += strlen(s);
2935  break;
2936  case DCH_J:
2937  sprintf(s, "%d", date2j(tm->tm_year, tm->tm_mon, tm->tm_mday));
2938  if (S_THth(n->suffix))
2939  str_numth(s, s, S_TH_TYPE(n->suffix));
2940  s += strlen(s);
2941  break;
2942  }
2943  }
2944 
2945  *s = '\0';
2946 }
#define a_m_STR
Definition: formatting.c:234
#define NODE_TYPE_END
Definition: formatting.c:173
const KeyWord * key
Definition: formatting.c:168
int tm_wday
Definition: pgtime.h:33
#define b_c_STR
Definition: formatting.c:212
#define S_THth(_s)
Definition: formatting.c:515
int tm_hour
Definition: pgtime.h:29
#define BC_STR
Definition: formatting.c:213
#define A_M_STR
Definition: formatting.c:233
static char * str_initcap_z(const char *buff, Oid collid)
Definition: formatting.c:1978
fsec_t fsec
Definition: formatting.c:462
static char * asc_tolower_z(const char *buff)
Definition: formatting.c:1984
#define AD_STR
Definition: formatting.c:208
int errcode(int sqlerrcode)
Definition: elog.c:575
struct pg_tm tm
Definition: formatting.c:461
#define p_m_STR
Definition: formatting.c:239
#define am_STR
Definition: formatting.c:236
static char * str_tolower_z(const char *buff, Oid collid)
Definition: formatting.c:1966
long int tm_gmtoff
Definition: pgtime.h:36
Definition: pgtime.h:25
char * localized_abbrev_months[12]
Definition: pg_locale.c:96
static char * str_numth(char *dest, char *num, int type)
Definition: formatting.c:1438
static const char *const rm_months_upper[]
Definition: formatting.c:262
int id
Definition: formatting.c:160
#define pm_STR
Definition: formatting.c:241
static struct pg_tm tm
Definition: localtime.c:103
#define MONTHS_PER_YEAR
Definition: timestamp.h:69
void pfree(void *pointer)
Definition: mcxt.c:950
const char *const months[]
Definition: datetime.c:66
#define NODE_TYPE_ACTION
Definition: formatting.c:174
#define ERROR
Definition: elog.h:43
int date2isoweek(int year, int mon, int mday)
Definition: timestamp.c:4073
#define S_FM(_s)
Definition: formatting.c:521
int date2isoyear(int year, int mon, int mday)
Definition: timestamp.c:4127
int tm_mday
Definition: pgtime.h:30
#define HOURS_PER_DAY
Definition: timestamp.h:78
int tm_mon
Definition: pgtime.h:31
#define SECS_PER_MINUTE
Definition: timestamp.h:88
char character
Definition: formatting.c:169
const char *const days[]
Definition: datetime.c:69
#define A_D_STR
Definition: formatting.c:206
#define a_d_STR
Definition: formatting.c:207
#define ereport(elevel, rest)
Definition: elog.h:122
#define SECS_PER_HOUR
Definition: timestamp.h:87
#define bc_STR
Definition: formatting.c:214
#define S_TH_TYPE(_s)
Definition: formatting.c:518
static const char *const months_full[]
Definition: formatting.c:188
int len
Definition: formatting.c:159
char * localized_full_days[7]
Definition: pg_locale.c:95
int date2j(int y, int m, int d)
Definition: datetime.c:292
#define ad_STR
Definition: formatting.c:209
void cache_locale_time(void)
Definition: pg_locale.c:786
#define INT64CONST(x)
Definition: c.h:310
#define P_M_STR
Definition: formatting.c:238
static char * asc_toupper_z(const char *buff)
Definition: formatting.c:1990
#define ADJUST_YEAR(year, is_interval)
Definition: formatting.c:204
#define tmtcTzn(_X)
Definition: formatting.c:467
int tm_year
Definition: pgtime.h:32
int errmsg(const char *fmt,...)
Definition: elog.c:797
static char * str_toupper_z(const char *buff, Oid collid)
Definition: formatting.c:1972
#define INVALID_FOR_INTERVAL
Definition: formatting.c:488
int date2isoyearday(int year, int mon, int mday)
Definition: timestamp.c:4184
int i
#define S_TM(_s)
Definition: formatting.c:523
int tm_yday
Definition: pgtime.h:34
#define PM_STR
Definition: formatting.c:240
#define AM_STR
Definition: formatting.c:235
#define TM_SUFFIX_LEN
Definition: formatting.c:529
int tm_sec
Definition: pgtime.h:27
char * localized_full_months[12]
Definition: pg_locale.c:97
#define DCH_MAX_ITEM_SIZ
Definition: formatting.c:117
char * localized_abbrev_days[7]
Definition: pg_locale.c:94
int tm_min
Definition: pgtime.h:28
static const char *const rm_months_lower[]
Definition: formatting.c:265
static const char *const days_short[]
Definition: formatting.c:193
#define B_C_STR
Definition: formatting.c:211
static void do_to_timestamp ( text date_txt,
text fmt,
struct pg_tm tm,
fsec_t fsec 
)
static

Definition at line 3587 of file formatting.c.

References TmFromChar::bc, TmFromChar::cc, TmFromChar::clock, CLOCK_12_HOUR, TmFromChar::d, DateTimeParseError(), DAY, DCH_cache_fetch(), DCH_CACHE_SIZE, DCH_from_char(), DCH_index, DCH_TYPE, TmFromChar::dd, TmFromChar::ddd, DEBUG_TM, DEBUG_TMFC, DTERR_FIELD_OVERFLOW, DTK_DATE_M, DTK_M, ereport, errcode(), errhint(), errmsg(), ERROR, FALSE, format, DCHCacheEntry::format, FROM_CHAR_DATE_ISOWEEK, TmFromChar::hh, HOURS_PER_DAY, i, INT64CONST, isleap, isoweek2date(), isoweek2j(), isoweekdate2date(), TmFromChar::j, j2date(), TmFromChar::mi, MINS_PER_HOUR, TmFromChar::mm, TmFromChar::mode, MONTH, MONTHS_PER_YEAR, TmFromChar::ms, NULL, palloc(), parse_format(), pfree(), TmFromChar::pm, SECS_PER_HOUR, SECS_PER_MINUTE, TmFromChar::ss, TmFromChar::ssss, text_to_cstring(), pg_tm::tm_hour, pg_tm::tm_mday, pg_tm::tm_min, pg_tm::tm_mon, pg_tm::tm_sec, pg_tm::tm_year, TRUE, TmFromChar::us, USECS_PER_SEC, ValidateDate(), VARSIZE_ANY_EXHDR, TmFromChar::w, TmFromChar::ww, YEAR, TmFromChar::year, TmFromChar::yysz, ZERO_tm, and ZERO_tmfc.

Referenced by to_date(), and to_timestamp().

3589 {
3590  FormatNode *format;
3591  TmFromChar tmfc;
3592  int fmt_len;
3593  char *date_str;
3594  int fmask;
3595 
3596  date_str = text_to_cstring(date_txt);
3597 
3598  ZERO_tmfc(&tmfc);
3599  ZERO_tm(tm);
3600  *fsec = 0;
3601  fmask = 0; /* bit mask for ValidateDate() */
3602 
3603  fmt_len = VARSIZE_ANY_EXHDR(fmt);
3604 
3605  if (fmt_len)
3606  {
3607  char *fmt_str;
3608  bool incache;
3609 
3610  fmt_str = text_to_cstring(fmt);
3611 
3612  if (fmt_len > DCH_CACHE_SIZE)
3613  {
3614  /*
3615  * Allocate new memory if format picture is bigger than static
3616  * cache and do not use cache (call parser always)
3617  */
3618  incache = FALSE;
3619 
3620  format = (FormatNode *) palloc((fmt_len + 1) * sizeof(FormatNode));
3621 
3622  parse_format(format, fmt_str, DCH_keywords,
3624  }
3625  else
3626  {
3627  /*
3628  * Use cache buffers
3629  */
3630  DCHCacheEntry *ent = DCH_cache_fetch(fmt_str);
3631 
3632  incache = TRUE;
3633  format = ent->format;
3634  }
3635 
3636 #ifdef DEBUG_TO_FROM_CHAR
3637  /* dump_node(format, fmt_len); */
3638  /* dump_index(DCH_keywords, DCH_index); */
3639 #endif
3640 
3641  DCH_from_char(format, date_str, &tmfc);
3642 
3643  pfree(fmt_str);
3644  if (!incache)
3645  pfree(format);
3646  }
3647 
3648  DEBUG_TMFC(&tmfc);
3649 
3650  /*
3651  * Convert to_date/to_timestamp input fields to standard 'tm'
3652  */
3653  if (tmfc.ssss)
3654  {
3655  int x = tmfc.ssss;
3656 
3657  tm->tm_hour = x / SECS_PER_HOUR;
3658  x %= SECS_PER_HOUR;
3659  tm->tm_min = x / SECS_PER_MINUTE;
3660  x %= SECS_PER_MINUTE;
3661  tm->tm_sec = x;
3662  }
3663 
3664  if (tmfc.ss)
3665  tm->tm_sec = tmfc.ss;
3666  if (tmfc.mi)
3667  tm->tm_min = tmfc.mi;
3668  if (tmfc.hh)
3669  tm->tm_hour = tmfc.hh;
3670 
3671  if (tmfc.clock == CLOCK_12_HOUR)
3672  {
3673  if (tm->tm_hour < 1 || tm->tm_hour > HOURS_PER_DAY / 2)
3674  ereport(ERROR,
3675  (errcode(ERRCODE_INVALID_DATETIME_FORMAT),
3676  errmsg("hour \"%d\" is invalid for the 12-hour clock",
3677  tm->tm_hour),
3678  errhint("Use the 24-hour clock, or give an hour between 1 and 12.")));
3679 
3680  if (tmfc.pm && tm->tm_hour < HOURS_PER_DAY / 2)
3681  tm->tm_hour += HOURS_PER_DAY / 2;
3682  else if (!tmfc.pm && tm->tm_hour == HOURS_PER_DAY / 2)
3683  tm->tm_hour = 0;
3684  }
3685 
3686  if (tmfc.year)
3687  {
3688  /*
3689  * If CC and YY (or Y) are provided, use YY as 2 low-order digits for
3690  * the year in the given century. Keep in mind that the 21st century
3691  * AD runs from 2001-2100, not 2000-2099; 6th century BC runs from
3692  * 600BC to 501BC.
3693  */
3694  if (tmfc.cc && tmfc.yysz <= 2)
3695  {
3696  if (tmfc.bc)
3697  tmfc.cc = -tmfc.cc;
3698  tm->tm_year = tmfc.year % 100;
3699  if (tm->tm_year)
3700  {
3701  if (tmfc.cc >= 0)
3702  tm->tm_year += (tmfc.cc - 1) * 100;
3703  else
3704  tm->tm_year = (tmfc.cc + 1) * 100 - tm->tm_year + 1;
3705  }
3706  else
3707  {
3708  /* find century year for dates ending in "00" */
3709  tm->tm_year = tmfc.cc * 100 + ((tmfc.cc >= 0) ? 0 : 1);
3710  }
3711  }
3712  else
3713  {
3714  /* If a 4-digit year is provided, we use that and ignore CC. */
3715  tm->tm_year = tmfc.year;
3716  if (tmfc.bc && tm->tm_year > 0)
3717  tm->tm_year = -(tm->tm_year - 1);
3718  }
3719  fmask |= DTK_M(YEAR);
3720  }
3721  else if (tmfc.cc)
3722  {
3723  /* use first year of century */
3724  if (tmfc.bc)
3725  tmfc.cc = -tmfc.cc;
3726  if (tmfc.cc >= 0)
3727  /* +1 because 21st century started in 2001 */
3728  tm->tm_year = (tmfc.cc - 1) * 100 + 1;
3729  else
3730  /* +1 because year == 599 is 600 BC */
3731  tm->tm_year = tmfc.cc * 100 + 1;
3732  fmask |= DTK_M(YEAR);
3733  }
3734 
3735  if (tmfc.j)
3736  {
3737  j2date(tmfc.j, &tm->tm_year, &tm->tm_mon, &tm->tm_mday);
3738  fmask |= DTK_DATE_M;
3739  }
3740 
3741  if (tmfc.ww)
3742  {
3743  if (tmfc.mode == FROM_CHAR_DATE_ISOWEEK)
3744  {
3745  /*
3746  * If tmfc.d is not set, then the date is left at the beginning of
3747  * the ISO week (Monday).
3748  */
3749  if (tmfc.d)
3750  isoweekdate2date(tmfc.ww, tmfc.d, &tm->tm_year, &tm->tm_mon, &tm->tm_mday);
3751  else
3752  isoweek2date(tmfc.ww, &tm->tm_year, &tm->tm_mon, &tm->tm_mday);
3753  fmask |= DTK_DATE_M;
3754  }
3755  else
3756  tmfc.ddd = (tmfc.ww - 1) * 7 + 1;
3757  }
3758 
3759  if (tmfc.w)
3760  tmfc.dd = (tmfc.w - 1) * 7 + 1;
3761  if (tmfc.dd)
3762  {
3763  tm->tm_mday = tmfc.dd;
3764  fmask |= DTK_M(DAY);
3765  }
3766  if (tmfc.mm)
3767  {
3768  tm->tm_mon = tmfc.mm;
3769  fmask |= DTK_M(MONTH);
3770  }
3771 
3772  if (tmfc.ddd && (tm->tm_mon <= 1 || tm->tm_mday <= 1))
3773  {
3774  /*
3775  * The month and day field have not been set, so we use the
3776  * day-of-year field to populate them. Depending on the date mode,
3777  * this field may be interpreted as a Gregorian day-of-year, or an ISO
3778  * week date day-of-year.
3779  */
3780 
3781  if (!tm->tm_year && !tmfc.bc)
3782  ereport(ERROR,
3783  (errcode(ERRCODE_INVALID_DATETIME_FORMAT),
3784  errmsg("cannot calculate day of year without year information")));
3785 
3786  if (tmfc.mode == FROM_CHAR_DATE_ISOWEEK)
3787  {
3788  int j0; /* zeroth day of the ISO year, in Julian */
3789 
3790  j0 = isoweek2j(tm->tm_year, 1) - 1;
3791 
3792  j2date(j0 + tmfc.ddd, &tm->tm_year, &tm->tm_mon, &tm->tm_mday);
3793  fmask |= DTK_DATE_M;
3794  }
3795  else
3796  {
3797  const int *y;
3798  int i;
3799 
3800  static const int ysum[2][13] = {
3801  {0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365},
3802  {0, 31, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335, 366}};
3803 
3804  y = ysum[isleap(tm->tm_year)];
3805 
3806  for (i = 1; i <= MONTHS_PER_YEAR; i++)
3807  {
3808  if (tmfc.ddd <= y[i])
3809  break;
3810  }
3811  if (tm->tm_mon <= 1)
3812  tm->tm_mon = i;
3813 
3814  if (tm->tm_mday <= 1)
3815  tm->tm_mday = tmfc.ddd - y[i - 1];
3816 
3817  fmask |= DTK_M(MONTH) | DTK_M(DAY);
3818  }
3819  }
3820 
3821  if (tmfc.ms)
3822  *fsec += tmfc.ms * 1000;
3823  if (tmfc.us)
3824  *fsec += tmfc.us;
3825 
3826  /* Range-check date fields according to bit mask computed above */
3827  if (fmask != 0)
3828  {
3829  /* We already dealt with AD/BC, so pass isjulian = true */
3830  int dterr = ValidateDate(fmask, true, false, false, tm);
3831 
3832  if (dterr != 0)
3833  {
3834  /*
3835  * Force the error to be DTERR_FIELD_OVERFLOW even if ValidateDate
3836  * said DTERR_MD_FIELD_OVERFLOW, because we don't want to print an
3837  * irrelevant hint about datestyle.
3838  */
3839  DateTimeParseError(DTERR_FIELD_OVERFLOW, date_str, "timestamp");
3840  }
3841  }
3842 
3843  /* Range-check time fields too */
3844  if (tm->tm_hour < 0 || tm->tm_hour >= HOURS_PER_DAY ||
3845  tm->tm_min < 0 || tm->tm_min >= MINS_PER_HOUR ||
3846  tm->tm_sec < 0 || tm->tm_sec >= SECS_PER_MINUTE ||
3847  *fsec < INT64CONST(0) || *fsec >= USECS_PER_SEC)
3848  DateTimeParseError(DTERR_FIELD_OVERFLOW, date_str, "timestamp");
3849 
3850  DEBUG_TM(tm);
3851 
3852  pfree(date_str);
3853 }
#define DEBUG_TM(_X)
Definition: formatting.c:452
struct FormatNode FormatNode
Definition: formatting.c:154
#define DAY
Definition: datetime.h:94
int errhint(const char *fmt,...)
Definition: elog.c:987
void DateTimeParseError(int dterr, const char *str, const char *datatype)
Definition: datetime.c:3765
#define USECS_PER_SEC
Definition: timestamp.h:94
#define YEAR
Definition: datetime.h:93
int tm_hour
Definition: pgtime.h:29
#define isleap(y)
Definition: datetime.h:273
static const KeySuffix DCH_suff[]
Definition: formatting.c:531
static const int DCH_index[KeyWord_INDEX_SIZE]
Definition: formatting.c:873
int errcode(int sqlerrcode)
Definition: elog.c:575
void isoweekdate2date(int isoweek, int wday, int *year, int *mon, int *mday)
Definition: timestamp.c:4055
static void DCH_from_char(FormatNode *node, char *in, TmFromChar *out)
Definition: formatting.c:2957
FromCharDateMode mode
Definition: formatting.c:410
#define DTK_DATE_M
Definition: datetime.h:194
#define MINS_PER_HOUR
Definition: timestamp.h:89
#define MONTHS_PER_YEAR
Definition: timestamp.h:69
void pfree(void *pointer)
Definition: mcxt.c:950
#define DCH_CACHE_SIZE
Definition: formatting.c:374
#define ERROR
Definition: elog.h:43
#define DTERR_FIELD_OVERFLOW
Definition: datetime.h:283
#define FALSE
Definition: c.h:221
static void parse_format(FormatNode *node, const char *str, const KeyWord *kw, const KeySuffix *suf, const int *index, int ver, NUMDesc *Num)
Definition: formatting.c:1227
int tm_mday
Definition: pgtime.h:30
#define HOURS_PER_DAY
Definition: timestamp.h:78
int tm_mon
Definition: pgtime.h:31
#define SECS_PER_MINUTE
Definition: timestamp.h:88
FormatNode format[DCH_CACHE_SIZE+1]
Definition: formatting.c:379
static DCHCacheEntry * DCH_cache_fetch(const char *str)
Definition: formatting.c:3319
#define ereport(elevel, rest)
Definition: elog.h:122
void j2date(int jd, int *year, int *month, int *day)
Definition: datetime.c:317
#define SECS_PER_HOUR
Definition: timestamp.h:87
#define CLOCK_12_HOUR
Definition: formatting.c:181
#define MONTH
Definition: datetime.h:92
#define DCH_TYPE
Definition: formatting.c:103
void isoweek2date(int woy, int *year, int *mon, int *mday)
Definition: timestamp.c:4042
#define ZERO_tmfc(_X)
Definition: formatting.c:432
int isoweek2j(int year, int week)
Definition: timestamp.c:4022
#define NULL
Definition: c.h:229
#define INT64CONST(x)
Definition: c.h:310
#define DEBUG_TMFC(_X)
Definition: formatting.c:451
char * text_to_cstring(const text *t)
Definition: varlena.c:182
int tm_year
Definition: pgtime.h:32
#define VARSIZE_ANY_EXHDR(PTR)
Definition: postgres.h:340
void * palloc(Size size)
Definition: mcxt.c:849
int errmsg(const char *fmt,...)
Definition: elog.c:797
static const KeyWord DCH_keywords[]
Definition: formatting.c:719
int i
#define DTK_M(t)
Definition: datetime.h:190
#define TRUE
Definition: c.h:217
static char format
Definition: pg_basebackup.c:80
int tm_sec
Definition: pgtime.h:27
#define ZERO_tm(_X)
Definition: formatting.c:470
int tm_min
Definition: pgtime.h:28
int ValidateDate(int fmask, bool isjulian, bool is2digits, bool bc, struct pg_tm *tm)
Definition: datetime.c:2479
static char * fill_str ( char *  str,
int  c,
int  max 
)
static

Definition at line 3862 of file formatting.c.

Referenced by float4_to_char(), float8_to_char(), int4_to_char(), int8_to_char(), int_to_roman(), and numeric_to_char().

3863 {
3864  memset(str, c, max);
3865  *(str + max) = '\0';
3866  return str;
3867 }
char * c
Datum float4_to_char ( PG_FUNCTION_ARGS  )

Definition at line 5464 of file formatting.c.

References fill_str(), format, int_to_roman(), IS_EEEE, is_infinite(), IS_MULTI, IS_ROMAN, MAXDOUBLEWIDTH, MAXFLOATWIDTH, NUMDesc::multi, NUM_TOCHAR_finish, NUM_TOCHAR_prepare, palloc(), PG_GETARG_FLOAT4, PG_GETARG_TEXT_PP, PG_RETURN_TEXT_P, NUMDesc::post, NUMDesc::pre, result, rint(), sign, snprintf(), val, and value.

5465 {
5467  text *fmt = PG_GETARG_TEXT_PP(1);
5468  NUMDesc Num;
5469  FormatNode *format;
5470  text *result;
5471  bool shouldFree;
5472  int out_pre_spaces = 0,
5473  sign = 0;
5474  char *numstr,
5475  *orgnum,
5476  *p;
5477 
5479 
5480  if (IS_ROMAN(&Num))
5481  numstr = orgnum = int_to_roman((int) rint(value));
5482  else if (IS_EEEE(&Num))
5483  {
5484  numstr = orgnum = (char *) palloc(MAXDOUBLEWIDTH + 1);
5485  if (isnan(value) || is_infinite(value))
5486  {
5487  /*
5488  * Allow 6 characters for the leading sign, the decimal point,
5489  * "e", the exponent's sign and two exponent digits.
5490  */
5491  numstr = (char *) palloc(Num.pre + Num.post + 7);
5492  fill_str(numstr, '#', Num.pre + Num.post + 6);
5493  *numstr = ' ';
5494  *(numstr + Num.pre + 1) = '.';
5495  }
5496  else
5497  {
5498  snprintf(orgnum, MAXDOUBLEWIDTH + 1, "%+.*e", Num.post, value);
5499 
5500  /*
5501  * Swap a leading positive sign for a space.
5502  */
5503  if (*orgnum == '+')
5504  *orgnum = ' ';
5505 
5506  numstr = orgnum;
5507  }
5508  }
5509  else
5510  {
5511  float4 val = value;
5512  int numstr_pre_len;
5513 
5514  if (IS_MULTI(&Num))
5515  {
5516  float multi = pow((double) 10, (double) Num.multi);
5517 
5518  val = value * multi;
5519  Num.pre += Num.multi;
5520  }
5521 
5522  orgnum = (char *) palloc(MAXFLOATWIDTH + 1);
5523  snprintf(orgnum, MAXFLOATWIDTH + 1, "%.0f", fabs(val));
5524  numstr_pre_len = strlen(orgnum);
5525 
5526  /* adjust post digits to fit max float digits */
5527  if (numstr_pre_len >= FLT_DIG)
5528  Num.post = 0;
5529  else if (numstr_pre_len + Num.post > FLT_DIG)
5530  Num.post = FLT_DIG - numstr_pre_len;
5531  snprintf(orgnum, MAXFLOATWIDTH + 1, "%.*f", Num.post, val);
5532 
5533  if (*orgnum == '-')
5534  { /* < 0 */
5535  sign = '-';
5536  numstr = orgnum + 1;
5537  }
5538  else
5539  {
5540  sign = '+';
5541  numstr = orgnum;
5542  }
5543 
5544  if ((p = strchr(numstr, '.')))
5545  numstr_pre_len = p - numstr;
5546  else
5547  numstr_pre_len = strlen(numstr);
5548 
5549  /* needs padding? */
5550  if (numstr_pre_len < Num.pre)
5551  out_pre_spaces = Num.pre - numstr_pre_len;
5552  /* overflowed prefix digit format? */
5553  else if (numstr_pre_len > Num.pre)
5554  {
5555  numstr = (char *) palloc(Num.pre + Num.post + 2);
5556  fill_str(numstr, '#', Num.pre + Num.post + 1);
5557  *(numstr + Num.pre) = '.';
5558  }
5559  }
5560 
5562  PG_RETURN_TEXT_P(result);
5563 }
#define MAXDOUBLEWIDTH
Definition: formatting.c:125
#define IS_EEEE(_f)
Definition: formatting.c:357
return result
Definition: formatting.c:1618
int snprintf(char *str, size_t count, const char *fmt,...) pg_attribute_printf(3
#define MAXFLOATWIDTH
Definition: formatting.c:124
#define NUM_TOCHAR_finish
Definition: formatting.c:5051
#define IS_ROMAN(_f)
Definition: formatting.c:355
#define PG_GETARG_TEXT_PP(n)
Definition: fmgr.h:273
#define NUM_TOCHAR_prepare
Definition: formatting.c:5038
#define IS_MULTI(_f)
Definition: formatting.c:356
char sign
Definition: informix.c:693
static char * fill_str(char *str, int c, int max)
Definition: formatting.c:3862
static char * int_to_roman(int number)
Definition: formatting.c:4060
int pre
Definition: formatting.c:308
double rint(double x)
Definition: rint.c:22
#define PG_GETARG_FLOAT4(n)
Definition: fmgr.h:245
float float4
Definition: c.h:380
#define PG_RETURN_TEXT_P(x)
Definition: fmgr.h:330
int post
Definition: formatting.c:308
int is_infinite(double val)
Definition: float.c:199
void * palloc(Size size)
Definition: mcxt.c:849
int multi
Definition: formatting.c:308
Definition: c.h:439
static char format
Definition: pg_basebackup.c:80
static struct @121 value
long val
Definition: informix.c:689
Datum float8_to_char ( PG_FUNCTION_ARGS  )

Definition at line 5570 of file formatting.c.

References fill_str(), format, int_to_roman(), IS_EEEE, is_infinite(), IS_MULTI, IS_ROMAN, MAXDOUBLEWIDTH, NUMDesc::multi, NUM_TOCHAR_finish, NUM_TOCHAR_prepare, palloc(), PG_GETARG_FLOAT8, PG_GETARG_TEXT_PP, PG_RETURN_TEXT_P, NUMDesc::post, NUMDesc::pre, result, rint(), sign, snprintf(), val, and value.

5571 {
5573  text *fmt = PG_GETARG_TEXT_PP(1);
5574  NUMDesc Num;
5575  FormatNode *format;
5576  text *result;
5577  bool shouldFree;
5578  int out_pre_spaces = 0,
5579  sign = 0;
5580  char *numstr,
5581  *orgnum,
5582  *p;
5583 
5585 
5586  if (IS_ROMAN(&Num))
5587  numstr = orgnum = int_to_roman((int) rint(value));
5588  else if (IS_EEEE(&Num))
5589  {
5590  numstr = orgnum = (char *) palloc(MAXDOUBLEWIDTH + 1);
5591  if (isnan(value) || is_infinite(value))
5592  {
5593  /*
5594  * Allow 6 characters for the leading sign, the decimal point,
5595  * "e", the exponent's sign and two exponent digits.
5596  */
5597  numstr = (char *) palloc(Num.pre + Num.post + 7);
5598  fill_str(numstr, '#', Num.pre + Num.post + 6);
5599  *numstr = ' ';
5600  *(numstr + Num.pre + 1) = '.';
5601  }
5602  else
5603  {
5604  snprintf(orgnum, MAXDOUBLEWIDTH + 1, "%+.*e", Num.post, value);
5605 
5606  /*
5607  * Swap a leading positive sign for a space.
5608  */
5609  if (*orgnum == '+')
5610  *orgnum = ' ';
5611 
5612  numstr = orgnum;
5613  }
5614  }
5615  else
5616  {
5617  float8 val = value;
5618  int numstr_pre_len;
5619 
5620  if (IS_MULTI(&Num))
5621  {
5622  double multi = pow((double) 10, (double) Num.multi);
5623 
5624  val = value * multi;
5625  Num.pre += Num.multi;
5626  }
5627  orgnum = (char *) palloc(MAXDOUBLEWIDTH + 1);
5628  numstr_pre_len = snprintf(orgnum, MAXDOUBLEWIDTH + 1, "%.0f", fabs(val));
5629 
5630  /* adjust post digits to fit max double digits */
5631  if (numstr_pre_len >= DBL_DIG)
5632  Num.post = 0;
5633  else if (numstr_pre_len + Num.post > DBL_DIG)
5634  Num.post = DBL_DIG - numstr_pre_len;
5635  snprintf(orgnum, MAXDOUBLEWIDTH + 1, "%.*f", Num.post, val);
5636 
5637  if (*orgnum == '-')
5638  { /* < 0 */
5639  sign = '-';
5640  numstr = orgnum + 1;
5641  }
5642  else
5643  {
5644  sign = '+';
5645  numstr = orgnum;
5646  }
5647 
5648  if ((p = strchr(numstr, '.')))
5649  numstr_pre_len = p - numstr;
5650  else
5651  numstr_pre_len = strlen(numstr);
5652 
5653  /* needs padding? */
5654  if (numstr_pre_len < Num.pre)
5655  out_pre_spaces = Num.pre - numstr_pre_len;
5656  /* overflowed prefix digit format? */
5657  else if (numstr_pre_len > Num.pre)
5658  {
5659  numstr = (char *) palloc(Num.pre + Num.post + 2);
5660  fill_str(numstr, '#', Num.pre + Num.post + 1);
5661  *(numstr + Num.pre) = '.';
5662  }
5663  }
5664 
5666  PG_RETURN_TEXT_P(result);
5667 }
#define PG_GETARG_FLOAT8(n)
Definition: fmgr.h:246
#define MAXDOUBLEWIDTH
Definition: formatting.c:125
#define IS_EEEE(_f)
Definition: formatting.c:357
return result
Definition: formatting.c:1618
int snprintf(char *str, size_t count, const char *fmt,...) pg_attribute_printf(3
#define NUM_TOCHAR_finish
Definition: formatting.c:5051
#define IS_ROMAN(_f)
Definition: formatting.c:355
#define PG_GETARG_TEXT_PP(n)
Definition: fmgr.h:273
#define NUM_TOCHAR_prepare
Definition: formatting.c:5038
double float8
Definition: c.h:381
#define IS_MULTI(_f)
Definition: formatting.c:356
char sign
Definition: informix.c:693
static char * fill_str(char *str, int c, int max)
Definition: formatting.c:3862
static char * int_to_roman(int number)
Definition: formatting.c:4060
int pre
Definition: formatting.c:308
double rint(double x)
Definition: rint.c:22
#define PG_RETURN_TEXT_P(x)
Definition: fmgr.h:330
int post
Definition: formatting.c:308
int is_infinite(double val)
Definition: float.c:199
void * palloc(Size size)
Definition: mcxt.c:849
int multi
Definition: formatting.c:308
Definition: c.h:439
static char format
Definition: pg_basebackup.c:80
static struct @121 value
long val
Definition: informix.c:689
static int from_char_parse_int ( int *  dest,
char **  src,
FormatNode node 
)
static

Definition at line 2272 of file formatting.c.

References from_char_parse_int_len(), FormatNode::key, and KeyWord::len.

Referenced by DCH_from_char().

2273 {
2274  return from_char_parse_int_len(dest, src, node->key->len, node);
2275 }
const KeyWord * key
Definition: formatting.c:168
static int from_char_parse_int_len(int *dest, char **src, const int len, FormatNode *node)
Definition: formatting.c:2182
int len
Definition: formatting.c:159
static int from_char_parse_int_len ( int *  dest,
char **  src,
const int  len,
FormatNode node 
)
static

Definition at line 2182 of file formatting.c.

References Assert, DCH_MAX_ITEM_SIZ, ereport, errcode(), errdetail(), errhint(), errmsg(), ERROR, from_char_set_int(), init(), is_next_separator(), FormatNode::key, KeyWord::name, NULL, result, S_FM, strlcpy(), strspace_len(), and FormatNode::suffix.

Referenced by DCH_from_char(), and from_char_parse_int().

2183 {
2184  long result;
2185  char copy[DCH_MAX_ITEM_SIZ + 1];
2186  char *init = *src;
2187  int used;
2188 
2189  /*
2190  * Skip any whitespace before parsing the integer.
2191  */
2192  *src += strspace_len(*src);
2193 
2194  Assert(len <= DCH_MAX_ITEM_SIZ);
2195  used = (int) strlcpy(copy, *src, len + 1);
2196 
2197  if (S_FM(node->suffix) || is_next_separator(node))
2198  {
2199  /*
2200  * This node is in Fill Mode, or the next node is known to be a
2201  * non-digit value, so we just slurp as many characters as we can get.
2202  */
2203  errno = 0;
2204  result = strtol(init, src, 10);
2205  }
2206  else
2207  {
2208  /*
2209  * We need to pull exactly the number of characters given in 'len' out
2210  * of the string, and convert those.
2211  */
2212  char *last;
2213 
2214  if (used < len)
2215  ereport(ERROR,
2216  (errcode(ERRCODE_INVALID_DATETIME_FORMAT),
2217  errmsg("source string too short for \"%s\" formatting field",
2218  node->key->name),
2219  errdetail("Field requires %d characters, but only %d "
2220  "remain.",
2221  len, used),
2222  errhint("If your source string is not fixed-width, try "
2223  "using the \"FM\" modifier.")));
2224 
2225  errno = 0;
2226  result = strtol(copy, &last, 10);
2227  used = last - copy;
2228 
2229  if (used > 0 && used < len)
2230  ereport(ERROR,
2231  (errcode(ERRCODE_INVALID_DATETIME_FORMAT),
2232  errmsg("invalid value \"%s\" for \"%s\"",
2233  copy, node->key->name),
2234  errdetail("Field requires %d characters, but only %d "
2235  "could be parsed.", len, used),
2236  errhint("If your source string is not fixed-width, try "
2237  "using the \"FM\" modifier.")));
2238 
2239  *src += used;
2240  }
2241 
2242  if (*src == init)
2243  ereport(ERROR,
2244  (errcode(ERRCODE_INVALID_DATETIME_FORMAT),
2245  errmsg("invalid value \"%s\" for \"%s\"",
2246  copy, node->key->name),
2247  errdetail("Value must be an integer.")));
2248 
2249  if (errno == ERANGE || result < INT_MIN || result > INT_MAX)
2250  ereport(ERROR,
2251  (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
2252  errmsg("value for \"%s\" in source string is out of range",
2253  node->key->name),
2254  errdetail("Value must be in the range %d to %d.",
2255  INT_MIN, INT_MAX)));
2256 
2257  if (dest != NULL)
2258  from_char_set_int(dest, (int) result, node);
2259  return *src - init;
2260 }
int errhint(const char *fmt,...)
Definition: elog.c:987
const KeyWord * key
Definition: formatting.c:168
static void from_char_set_int(int *dest, const int value, const FormatNode *node)
Definition: formatting.c:2149
int errcode(int sqlerrcode)
Definition: elog.c:575
return result
Definition: formatting.c:1618
#define ERROR
Definition: elog.h:43
static bool is_next_separator(FormatNode *n)
Definition: formatting.c:2052
#define S_FM(_s)
Definition: formatting.c:521
int errdetail(const char *fmt,...)
Definition: elog.c:873
#define ereport(elevel, rest)
Definition: elog.h:122
const char * name
Definition: formatting.c:158
size_t strlcpy(char *dst, const char *src, size_t siz)
Definition: strlcpy.c:45
static int strspace_len(char *str)
Definition: formatting.c:2108
#define NULL
Definition: c.h:229
#define Assert(condition)
Definition: c.h:675
static void init(bool is_no_vacuum)
Definition: pgbench.c:2571
int errmsg(const char *fmt,...)
Definition: elog.c:797
#define DCH_MAX_ITEM_SIZ
Definition: formatting.c:117
static int from_char_seq_search ( int *  dest,
char **  src,
const char *const *  array,
int  type,
int  max,
FormatNode node 
)
static

Definition at line 2360 of file formatting.c.

References Assert, DCH_MAX_ITEM_SIZ, ereport, errcode(), errdetail(), errmsg(), ERROR, FormatNode::key, KeyWord::name, seq_search(), and strlcpy().

Referenced by DCH_from_char().

2362 {
2363  int len;
2364 
2365  *dest = seq_search(*src, array, type, max, &len);
2366  if (len <= 0)
2367  {
2368  char copy[DCH_MAX_ITEM_SIZ + 1];
2369 
2370  Assert(max <= DCH_MAX_ITEM_SIZ);
2371  strlcpy(copy, *src, max + 1);
2372 
2373  ereport(ERROR,
2374  (errcode(ERRCODE_INVALID_DATETIME_FORMAT),
2375  errmsg("invalid value \"%s\" for \"%s\"",
2376  copy, node->key->name),
2377  errdetail("The given value did not match any of the allowed "
2378  "values for this field.")));
2379  }
2380  *src += len;
2381  return len;
2382 }
const KeyWord * key
Definition: formatting.c:168
static int seq_search(char *name, const char *const *array, int type, int max, int *len)
Definition: formatting.c:2282
int errcode(int sqlerrcode)
Definition: elog.c:575
#define ERROR
Definition: elog.h:43
int errdetail(const char *fmt,...)
Definition: elog.c:873
#define ereport(elevel, rest)
Definition: elog.h:122
const char * name
Definition: formatting.c:158
size_t strlcpy(char *dst, const char *src, size_t siz)
Definition: strlcpy.c:45
#define Assert(condition)
Definition: c.h:675
int errmsg(const char *fmt,...)
Definition: elog.c:797
#define DCH_MAX_ITEM_SIZ
Definition: formatting.c:117
static void from_char_set_int ( int *  dest,
const int  value,
const FormatNode node 
)
static

Definition at line 2149 of file formatting.c.

References ereport, errcode(), errdetail(), errmsg(), ERROR, FormatNode::key, KeyWord::name, and value.

Referenced by DCH_from_char(), and from_char_parse_int_len().

2150 {
2151  if (*dest != 0 && *dest != value)
2152  ereport(ERROR,
2153  (errcode(ERRCODE_INVALID_DATETIME_FORMAT),
2154  errmsg("conflicting values for \"%s\" field in formatting string",
2155  node->key->name),
2156  errdetail("This value contradicts a previous setting for "
2157  "the same field type.")));
2158  *dest = value;
2159 }
const KeyWord * key
Definition: formatting.c:168
int errcode(int sqlerrcode)
Definition: elog.c:575
#define ERROR
Definition: elog.h:43
int errdetail(const char *fmt,...)
Definition: elog.c:873
#define ereport(elevel, rest)
Definition: elog.h:122
const char * name
Definition: formatting.c:158
int errmsg(const char *fmt,...)
Definition: elog.c:797
static struct @121 value
static void from_char_set_mode ( TmFromChar tmfc,
const FromCharDateMode  mode 
)
static

Definition at line 2127 of file formatting.c.

References ereport, errcode(), errhint(), errmsg(), ERROR, FROM_CHAR_DATE_NONE, and TmFromChar::mode.

Referenced by DCH_from_char().

2128 {
2129  if (mode != FROM_CHAR_DATE_NONE)
2130  {
2131  if (tmfc->mode == FROM_CHAR_DATE_NONE)
2132  tmfc->mode = mode;
2133  else if (tmfc->mode != mode)
2134  ereport(ERROR,
2135  (errcode(ERRCODE_INVALID_DATETIME_FORMAT),
2136  errmsg("invalid combination of date conventions"),
2137  errhint("Do not mix Gregorian and ISO week date "
2138  "conventions in a formatting template.")));
2139  }
2140 }
int errhint(const char *fmt,...)
Definition: elog.c:987
int errcode(int sqlerrcode)
Definition: elog.c:575
FromCharDateMode mode
Definition: formatting.c:410
#define ERROR
Definition: elog.h:43
#define ereport(elevel, rest)
Definition: elog.h:122
int errmsg(const char *fmt,...)
Definition: elog.c:797
static char * get_last_relevant_decnum ( char *  num)
static

Definition at line 4191 of file formatting.c.

References elog, NULL, and result.

Referenced by NUM_processor().

4192 {
4193  char *result,
4194  *p = strchr(num, '.');
4195 
4196 #ifdef DEBUG_TO_FROM_CHAR
4197  elog(DEBUG_elog_output, "get_last_relevant_decnum()");
4198 #endif
4199 
4200  if (!p)
4201  return NULL;
4202 
4203  result = p;
4204 
4205  while (*(++p))
4206  {
4207  if (*p != '0')
4208  result = p;
4209  }
4210 
4211  return result;
4212 }
return result
Definition: formatting.c:1618
#define NULL
Definition: c.h:229
#define elog
Definition: elog.h:219
static const char * get_th ( char *  num,
int  type 
)
static

Definition at line 1392 of file formatting.c.

References ereport, errcode(), errmsg(), ERROR, numTH, numth, and TH_UPPER.

Referenced by NUM_processor(), and str_numth().

1393 {
1394  int len = strlen(num),
1395  last,
1396  seclast;
1397 
1398  last = *(num + (len - 1));
1399  if (!isdigit((unsigned char) last))
1400  ereport(ERROR,
1401  (errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
1402  errmsg("\"%s\" is not a number", num)));
1403 
1404  /*
1405  * All "teens" (<x>1[0-9]) get 'TH/th', while <x>[02-9][123] still get
1406  * 'ST/st', 'ND/nd', 'RD/rd', respectively
1407  */
1408  if ((len > 1) && ((seclast = num[len - 2]) == '1'))
1409  last = 0;
1410 
1411  switch (last)
1412  {
1413  case '1':
1414  if (type == TH_UPPER)
1415  return numTH[0];
1416  return numth[0];
1417  case '2':
1418  if (type == TH_UPPER)
1419  return numTH[1];
1420  return numth[1];
1421  case '3':
1422  if (type == TH_UPPER)
1423  return numTH[2];
1424  return numth[2];
1425  default:
1426  if (type == TH_UPPER)
1427  return numTH[3];
1428  return numth[3];
1429  }
1430 }
int errcode(int sqlerrcode)
Definition: elog.c:575
static const char *const numTH[]
Definition: formatting.c:280
#define TH_UPPER
Definition: formatting.c:299
#define ERROR
Definition: elog.h:43
#define ereport(elevel, rest)
Definition: elog.h:122
static const char *const numth[]
Definition: formatting.c:281
int errmsg(const char *fmt,...)
Definition: elog.c:797
static const KeyWord * index_seq_search ( const char *  str,
const KeyWord kw,
const int *  index 
)
static

Definition at line 1008 of file formatting.c.

References KeyWord_INDEX_FILTER, KeyWord::len, KeyWord::name, and NULL.

Referenced by parse_format().

1009 {
1010  int poz;
1011 
1012  if (!KeyWord_INDEX_FILTER(*str))
1013  return NULL;
1014 
1015  if ((poz = *(index + (*str - ' '))) > -1)
1016  {
1017  const KeyWord *k = kw + poz;
1018 
1019  do
1020  {
1021  if (strncmp(str, k->name, k->len) == 0)
1022  return k;
1023  k++;
1024  if (!k->name)
1025  return NULL;
1026  } while (*str == *k->name);
1027  }
1028  return NULL;
1029 }
Definition: type.h:90
const char * name
Definition: formatting.c:158
int len
Definition: formatting.c:159
#define KeyWord_INDEX_FILTER(_c)
Definition: formatting.c:111
#define NULL
Definition: c.h:229
Datum int4_to_char ( PG_FUNCTION_ARGS  )

Definition at line 5259 of file formatting.c.

References DatumGetCString, DirectFunctionCall1, fill_str(), format, Int32GetDatum, int4out(), int_to_roman(), IS_EEEE, IS_MULTI, IS_ROMAN, MAXDOUBLEWIDTH, NUMDesc::multi, NUM_TOCHAR_finish, NUM_TOCHAR_prepare, palloc(), PG_GETARG_INT32, PG_GETARG_TEXT_PP, PG_RETURN_TEXT_P, NUMDesc::post, NUMDesc::pre, result, sign, snprintf(), val, and value.

5260 {
5262  text *fmt = PG_GETARG_TEXT_PP(1);
5263  NUMDesc Num;
5264  FormatNode *format;
5265  text *result;
5266  bool shouldFree;
5267  int out_pre_spaces = 0,
5268  sign = 0;
5269  char *numstr,
5270  *orgnum;
5271 
5273 
5274  /*
5275  * On DateType depend part (int32)
5276  */
5277  if (IS_ROMAN(&Num))
5278  numstr = orgnum = int_to_roman(value);
5279  else if (IS_EEEE(&Num))
5280  {
5281  /* we can do it easily because float8 won't lose any precision */
5282  float8 val = (float8) value;
5283 
5284  orgnum = (char *) palloc(MAXDOUBLEWIDTH + 1);
5285  snprintf(orgnum, MAXDOUBLEWIDTH + 1, "%+.*e", Num.post, val);
5286 
5287  /*
5288  * Swap a leading positive sign for a space.
5289  */
5290  if (*orgnum == '+')
5291  *orgnum = ' ';
5292 
5293  numstr = orgnum;
5294  }
5295  else
5296  {
5297  int numstr_pre_len;
5298 
5299  if (IS_MULTI(&Num))
5300  {
5302  Int32GetDatum(value * ((int32) pow((double) 10, (double) Num.multi)))));
5303  Num.pre += Num.multi;
5304  }
5305  else
5306  {
5308  Int32GetDatum(value)));
5309  }
5310 
5311  if (*orgnum == '-')
5312  {
5313  sign = '-';
5314  orgnum++;
5315  }
5316  else
5317  sign = '+';
5318 
5319  numstr_pre_len = strlen(orgnum);
5320 
5321  /* post-decimal digits? Pad out with zeros. */
5322  if (Num.post)
5323  {
5324  numstr = (char *) palloc(numstr_pre_len + Num.post + 2);
5325  strcpy(numstr, orgnum);
5326  *(numstr + numstr_pre_len) = '.';
5327  memset(numstr + numstr_pre_len + 1, '0', Num.post);
5328  *(numstr + numstr_pre_len + Num.post + 1) = '\0';
5329  }
5330  else
5331  numstr = orgnum;
5332 
5333  /* needs padding? */
5334  if (numstr_pre_len < Num.pre)
5335  out_pre_spaces = Num.pre - numstr_pre_len;
5336  /* overflowed prefix digit format? */
5337  else if (numstr_pre_len > Num.pre)
5338  {
5339  numstr = (char *) palloc(Num.pre + Num.post + 2);
5340  fill_str(numstr, '#', Num.pre + Num.post + 1);
5341  *(numstr + Num.pre) = '.';
5342  }
5343  }
5344 
5346  PG_RETURN_TEXT_P(result);
5347 }
#define PG_GETARG_INT32(n)
Definition: fmgr.h:234
#define MAXDOUBLEWIDTH
Definition: formatting.c:125
#define IS_EEEE(_f)
Definition: formatting.c:357
return result
Definition: formatting.c:1618
int snprintf(char *str, size_t count, const char *fmt,...) pg_attribute_printf(3
#define DirectFunctionCall1(func, arg1)
Definition: fmgr.h:584
#define NUM_TOCHAR_finish
Definition: formatting.c:5051
#define IS_ROMAN(_f)
Definition: formatting.c:355
signed int int32
Definition: c.h:256
#define PG_GETARG_TEXT_PP(n)
Definition: fmgr.h:273
#define NUM_TOCHAR_prepare
Definition: formatting.c:5038
double float8
Definition: c.h:381
#define DatumGetCString(X)
Definition: postgres.h:572
#define IS_MULTI(_f)
Definition: formatting.c:356
char sign
Definition: informix.c:693
static char * fill_str(char *str, int c, int max)
Definition: formatting.c:3862
static char * int_to_roman(int number)
Definition: formatting.c:4060
int pre
Definition: formatting.c:308
Datum int4out(PG_FUNCTION_ARGS)
Definition: int.c:277
#define PG_RETURN_TEXT_P(x)
Definition: fmgr.h:330
int post
Definition: formatting.c:308
#define Int32GetDatum(X)
Definition: postgres.h:485
void * palloc(Size size)
Definition: mcxt.c:849
int multi
Definition: formatting.c:308
Definition: c.h:439
static char format
Definition: pg_basebackup.c:80
static struct @121 value
long val
Definition: informix.c:689
Datum int8_to_char ( PG_FUNCTION_ARGS  )

Definition at line 5354 of file formatting.c.

References DatumGetCString, DatumGetInt32, DatumGetInt64, DatumGetNumeric, DirectFunctionCall1, DirectFunctionCall2, dtoi8(), fill_str(), Float8GetDatum(), format, Int64GetDatum(), int84(), int8_numeric(), int8mul(), int8out(), int_to_roman(), IS_EEEE, IS_MULTI, IS_ROMAN, NUMDesc::multi, NUM_TOCHAR_finish, NUM_TOCHAR_prepare, numeric_out_sci(), palloc(), PG_GETARG_INT64, PG_GETARG_TEXT_PP, PG_RETURN_TEXT_P, NUMDesc::post, NUMDesc::pre, result, sign, val, and value.

5355 {
5356  int64 value = PG_GETARG_INT64(0);
5357  text *fmt = PG_GETARG_TEXT_PP(1);
5358  NUMDesc Num;
5359  FormatNode *format;
5360  text *result;
5361  bool shouldFree;
5362  int out_pre_spaces = 0,
5363  sign = 0;
5364  char *numstr,
5365  *orgnum;
5366 
5368 
5369  /*
5370  * On DateType depend part (int32)
5371  */
5372  if (IS_ROMAN(&Num))
5373  {
5374  /* Currently don't support int8 conversion to roman... */
5375  numstr = orgnum = int_to_roman(DatumGetInt32(
5377  }
5378  else if (IS_EEEE(&Num))
5379  {
5380  /* to avoid loss of precision, must go via numeric not float8 */
5381  Numeric val;
5382 
5384  Int64GetDatum(value)));
5385  orgnum = numeric_out_sci(val, Num.post);
5386 
5387  /*
5388  * numeric_out_sci() does not emit a sign for positive numbers. We
5389  * need to add a space in this case so that positive and negative
5390  * numbers are aligned. We don't have to worry about NaN here.
5391  */
5392  if (*orgnum != '-')
5393  {
5394  numstr = (char *) palloc(strlen(orgnum) + 2);
5395  *numstr = ' ';
5396  strcpy(numstr + 1, orgnum);
5397  }
5398  else
5399  {
5400  numstr = orgnum;
5401  }
5402  }
5403  else
5404  {
5405  int numstr_pre_len;
5406 
5407  if (IS_MULTI(&Num))
5408  {
5409  double multi = pow((double) 10, (double) Num.multi);
5410 
5412  Int64GetDatum(value),
5414  Float8GetDatum(multi))));
5415  Num.pre += Num.multi;
5416  }
5417 
5419  Int64GetDatum(value)));
5420 
5421  if (*orgnum == '-')
5422  {
5423  sign = '-';
5424  orgnum++;
5425  }
5426  else
5427  sign = '+';
5428 
5429  numstr_pre_len = strlen(orgnum);
5430 
5431  /* post-decimal digits? Pad out with zeros. */
5432  if (Num.post)
5433  {
5434  numstr = (char *) palloc(numstr_pre_len + Num.post + 2);
5435  strcpy(numstr, orgnum);
5436  *(numstr + numstr_pre_len) = '.';
5437  memset(numstr + numstr_pre_len + 1, '0', Num.post);
5438  *(numstr + numstr_pre_len + Num.post + 1) = '\0';
5439  }
5440  else
5441  numstr = orgnum;
5442 
5443  /* needs padding? */
5444  if (numstr_pre_len < Num.pre)
5445  out_pre_spaces = Num.pre - numstr_pre_len;
5446  /* overflowed prefix digit format? */
5447  else if (numstr_pre_len > Num.pre)
5448  {
5449  numstr = (char *) palloc(Num.pre + Num.post + 2);
5450  fill_str(numstr, '#', Num.pre + Num.post + 1);
5451  *(numstr + Num.pre) = '.';
5452  }
5453  }
5454 
5456  PG_RETURN_TEXT_P(result);
5457 }
#define DatumGetInt32(X)
Definition: postgres.h:478
#define IS_EEEE(_f)
Definition: formatting.c:357
return result
Definition: formatting.c:1618
#define DirectFunctionCall1(func, arg1)
Definition: fmgr.h:584
#define NUM_TOCHAR_finish
Definition: formatting.c:5051
#define IS_ROMAN(_f)
Definition: formatting.c:355
Datum int8_numeric(PG_FUNCTION_ARGS)
Definition: numeric.c:2968
Datum Float8GetDatum(float8 X)
Definition: fmgr.c:1815
#define PG_GETARG_TEXT_PP(n)
Definition: fmgr.h:273
#define NUM_TOCHAR_prepare
Definition: formatting.c:5038
#define DatumGetCString(X)
Definition: postgres.h:572
#define IS_MULTI(_f)
Definition: formatting.c:356
#define DatumGetInt64(X)
Definition: postgres.h:613
char sign
Definition: informix.c:693
static char * fill_str(char *str, int c, int max)
Definition: formatting.c:3862
Datum int8mul(PG_FUNCTION_ARGS)
Definition: int8.c:555
static char * int_to_roman(int number)
Definition: formatting.c:4060
Datum dtoi8(PG_FUNCTION_ARGS)
Definition: int8.c:1343
int pre
Definition: formatting.c:308
Datum Int64GetDatum(int64 X)
Definition: fmgr.c:1791
#define PG_RETURN_TEXT_P(x)
Definition: fmgr.h:330
int post
Definition: formatting.c:308
#define DatumGetNumeric(X)
Definition: numeric.h:49
Datum int84(PG_FUNCTION_ARGS)
Definition: int8.c:1287
void * palloc(Size size)
Definition: mcxt.c:849
int multi
Definition: formatting.c:308
Definition: c.h:439
static char format
Definition: pg_basebackup.c:80
static struct @121 value
char * numeric_out_sci(Numeric num, int scale)
Definition: numeric.c:719
#define PG_GETARG_INT64(n)
Definition: fmgr.h:247
long val
Definition: informix.c:689
Datum int8out(PG_FUNCTION_ARGS)
Definition: int8.c:158
#define DirectFunctionCall2(func, arg1, arg2)
Definition: fmgr.h:586
static char * int_to_roman ( int  number)
static

Definition at line 4060 of file formatting.c.

References fill_str(), NULL, palloc(), result, rm1, rm10, rm100, and snprintf().

Referenced by float4_to_char(), float8_to_char(), int4_to_char(), int8_to_char(), and numeric_to_char().

4061 {
4062  int len = 0,
4063  num = 0;
4064  char *p = NULL,
4065  *result,
4066  numstr[5];
4067 
4068  result = (char *) palloc(16);
4069  *result = '\0';
4070 
4071  if (number > 3999 || number < 1)
4072  {
4073  fill_str(result, '#', 15);
4074  return result;
4075  }
4076  len = snprintf(numstr, sizeof(numstr), "%d", number);
4077 
4078  for (p = numstr; *p != '\0'; p++, --len)
4079  {
4080  num = *p - 49; /* 48 ascii + 1 */
4081  if (num < 0)
4082  continue;
4083 
4084  if (len > 3)
4085  {
4086  while (num-- != -1)
4087  strcat(result, "M");
4088  }
4089  else
4090  {
4091  if (len == 3)
4092  strcat(result, rm100[num]);
4093  else if (len == 2)
4094  strcat(result, rm10[num]);
4095  else if (len == 1)
4096  strcat(result, rm1[num]);
4097  }
4098  }
4099  return result;
4100 }
return result
Definition: formatting.c:1618
int snprintf(char *str, size_t count, const char *fmt,...) pg_attribute_printf(3
static const char *const rm10[]
Definition: formatting.c:273
static const char *const rm1[]
Definition: formatting.c:272
static char * fill_str(char *str, int c, int max)
Definition: formatting.c:3862
static const char *const rm100[]
Definition: formatting.c:274
#define NULL
Definition: c.h:229
void * palloc(Size size)
Definition: mcxt.c:849
Datum interval_to_char ( PG_FUNCTION_ARGS  )

Definition at line 3483 of file formatting.c.

References datetime_to_char_body(), DAYS_PER_MONTH, interval2tm(), MONTHS_PER_YEAR, PG_GET_COLLATION, PG_GETARG_INTERVAL_P, PG_GETARG_TEXT_PP, PG_RETURN_NULL, PG_RETURN_TEXT_P, tm, pg_tm::tm_mday, pg_tm::tm_mon, pg_tm::tm_yday, pg_tm::tm_year, tmtcFsec, tmtcTm, VARSIZE_ANY_EXHDR, and ZERO_tmtc.

3484 {
3485  Interval *it = PG_GETARG_INTERVAL_P(0);
3486  text *fmt = PG_GETARG_TEXT_PP(1),
3487  *res;
3488  TmToChar tmtc;
3489  struct pg_tm *tm;
3490 
3491  if (VARSIZE_ANY_EXHDR(fmt) <= 0)
3492  PG_RETURN_NULL();
3493 
3494  ZERO_tmtc(&tmtc);
3495  tm = tmtcTm(&tmtc);
3496 
3497  if (interval2tm(*it, tm, &tmtcFsec(&tmtc)) != 0)
3498  PG_RETURN_NULL();
3499 
3500  /* wday is meaningless, yday approximates the total span in days */
3501  tm->tm_yday = (tm->tm_year * MONTHS_PER_YEAR + tm->tm_mon) * DAYS_PER_MONTH + tm->tm_mday;
3502 
3503  if (!(res = datetime_to_char_body(&tmtc, fmt, true, PG_GET_COLLATION())))
3504  PG_RETURN_NULL();
3505 
3506  PG_RETURN_TEXT_P(res);
3507 }
#define PG_GETARG_INTERVAL_P(n)
Definition: timestamp.h:37
static text * datetime_to_char_body(TmToChar *tmtc, text *fmt, bool is_interval, Oid collid)
Definition: formatting.c:3346
#define ZERO_tmtc(_X)
Definition: formatting.c:477
Definition: pgtime.h:25
#define PG_GET_COLLATION()
Definition: fmgr.h:163
#define tmtcFsec(_X)
Definition: formatting.c:468
#define PG_GETARG_TEXT_PP(n)
Definition: fmgr.h:273
static struct pg_tm tm
Definition: localtime.c:103
#define MONTHS_PER_YEAR
Definition: timestamp.h:69
int interval2tm(Interval span, struct pg_tm *tm, fsec_t *fsec)
Definition: timestamp.c:1902
int tm_mday
Definition: pgtime.h:30
int tm_mon
Definition: pgtime.h:31
#define DAYS_PER_MONTH
Definition: timestamp.h:77
#define PG_RETURN_TEXT_P(x)
Definition: fmgr.h:330
int tm_year
Definition: pgtime.h:32
#define VARSIZE_ANY_EXHDR(PTR)
Definition: postgres.h:340
int tm_yday
Definition: pgtime.h:34
Definition: c.h:439
#define tmtcTm(_X)
Definition: formatting.c:466
#define PG_RETURN_NULL()
Definition: fmgr.h:305
static bool is_next_separator ( FormatNode n)
static

Definition at line 2052 of file formatting.c.

References FormatNode::character, FALSE, KeyWord::is_digit, FormatNode::key, NODE_TYPE_ACTION, NODE_TYPE_END, S_THth, FormatNode::suffix, TRUE, and FormatNode::type.

Referenced by from_char_parse_int_len().

2053 {
2054  if (n->type == NODE_TYPE_END)
2055  return FALSE;
2056 
2057  if (n->type == NODE_TYPE_ACTION && S_THth(n->suffix))
2058  return TRUE;
2059 
2060  /*
2061  * Next node
2062  */
2063  n++;
2064 
2065  /* end of format string is treated like a non-digit separator */
2066  if (n->type == NODE_TYPE_END)
2067  return TRUE;
2068 
2069  if (n->type == NODE_TYPE_ACTION)
2070  {
2071  if (n->key->is_digit)
2072  return FALSE;
2073 
2074  return TRUE;
2075  }
2076  else if (isdigit((unsigned char) n->character))
2077  return FALSE;
2078 
2079  return TRUE; /* some non-digit input (separator) */
2080 }
#define NODE_TYPE_END
Definition: formatting.c:173
const KeyWord * key
Definition: formatting.c:168
#define S_THth(_s)
Definition: formatting.c:515
#define NODE_TYPE_ACTION
Definition: formatting.c:174
#define FALSE
Definition: c.h:221
char character
Definition: formatting.c:169
#define TRUE
Definition: c.h:217
bool is_digit
Definition: formatting.c:161
static FormatNode * NUM_cache ( int  len,
NUMDesc Num,
text pars_str,
bool shouldFree 
)
static

Definition at line 4002 of file formatting.c.

References NUMDesc::flag, format, NUMCacheEntry::format, NUMDesc::lsign, NUMDesc::multi, NUMDesc::need_locale, NULL, NUMCacheEntry::Num, NUM_cache_fetch(), NUM_CACHE_SIZE, NUM_index, NUM_TYPE, palloc(), parse_format(), pfree(), NUMDesc::post, NUMDesc::pre, NUMDesc::pre_lsign_num, text_to_cstring(), NUMDesc::zero_end, NUMDesc::zero_start, and zeroize_NUM.

Referenced by numeric_to_number().

4003 {
4004  FormatNode *format = NULL;
4005  char *str;
4006 
4007  str = text_to_cstring(pars_str);
4008 
4009  if (len > NUM_CACHE_SIZE)
4010  {
4011  /*
4012  * Allocate new memory if format picture is bigger than static cache
4013  * and do not use cache (call parser always)
4014  */
4015  format = (FormatNode *) palloc((len + 1) * sizeof(FormatNode));
4016 
4017  *shouldFree = true;
4018 
4019  zeroize_NUM(Num);
4020 
4021  parse_format(format, str, NUM_keywords,
4022  NULL, NUM_index, NUM_TYPE, Num);
4023  }
4024  else
4025  {
4026  /*
4027  * Use cache buffers
4028  */
4029  NUMCacheEntry *ent = NUM_cache_fetch(str);
4030 
4031  *shouldFree = false;
4032 
4033  format = ent->format;
4034 
4035  /*
4036  * Copy cache to used struct
4037  */
4038  Num->flag = ent->Num.flag;
4039  Num->lsign = ent->Num.lsign;
4040  Num->pre = ent->Num.pre;
4041  Num->post = ent->Num.post;
4042  Num->pre_lsign_num = ent->Num.pre_lsign_num;
4043  Num->need_locale = ent->Num.need_locale;
4044  Num->multi = ent->Num.multi;
4045  Num->zero_start = ent->Num.zero_start;
4046  Num->zero_end = ent->Num.zero_end;
4047  }
4048 
4049 #ifdef DEBUG_TO_FROM_CHAR
4050  /* dump_node(format, len); */
4051  dump_index(NUM_keywords, NUM_index);
4052 #endif
4053 
4054  pfree(str);
4055  return format;
4056 }
NUMDesc Num
Definition: formatting.c:391
int need_locale
Definition: formatting.c:308
struct FormatNode FormatNode
Definition: formatting.c:154
int flag
Definition: formatting.c:308
#define NUM_CACHE_SIZE
Definition: formatting.c:372
void pfree(void *pointer)
Definition: mcxt.c:950
static void parse_format(FormatNode *node, const char *str, const KeyWord *kw, const KeySuffix *suf, const int *index, int ver, NUMDesc *Num)
Definition: formatting.c:1227
int pre
Definition: formatting.c:308
#define NUM_TYPE
Definition: formatting.c:104
static NUMCacheEntry * NUM_cache_fetch(const char *str)
Definition: formatting.c:3974
int post
Definition: formatting.c:308
#define NULL
Definition: c.h:229
int zero_end
Definition: formatting.c:308
int pre_lsign_num
Definition: formatting.c:308
static const KeyWord NUM_keywords[]
Definition: formatting.c:825
char * text_to_cstring(const text *t)
Definition: varlena.c:182
void * palloc(Size size)
Definition: mcxt.c:849
FormatNode format[NUM_CACHE_SIZE+1]
Definition: formatting.c:387
int multi
Definition: formatting.c:308
int lsign
Definition: formatting.c:308
int zero_start
Definition: formatting.c:308
static char format
Definition: pg_basebackup.c:80
static const int NUM_index[KeyWord_INDEX_SIZE]
Definition: formatting.c:897
#define zeroize_NUM(_n)
Definition: formatting.c:3869
static NUMCacheEntry * NUM_cache_fetch ( const char *  str)
static

Definition at line 3974 of file formatting.c.

References NUMCacheEntry::format, NULL, NUMCacheEntry::Num, NUM_cache_getnew(), NUM_cache_search(), NUM_index, NUM_TYPE, parse_format(), NUMCacheEntry::valid, and zeroize_NUM.

Referenced by NUM_cache().

3975 {
3976  NUMCacheEntry *ent;
3977 
3978  if ((ent = NUM_cache_search(str)) == NULL)
3979  {
3980  /*
3981  * Not in the cache, must run parser and save a new format-picture to
3982  * the cache. Do not mark the cache entry valid until parsing
3983  * succeeds.
3984  */
3985  ent = NUM_cache_getnew(str);
3986 
3987  zeroize_NUM(&ent->Num);
3988 
3989  parse_format(ent->format, str, NUM_keywords,
3990  NULL, NUM_index, NUM_TYPE, &ent->Num);
3991 
3992  ent->valid = true;
3993  }
3994  return ent;
3995 }
static NUMCacheEntry * NUM_cache_getnew(const char *str)
Definition: formatting.c:3884
NUMDesc Num
Definition: formatting.c:391
static void parse_format(FormatNode *node, const char *str, const KeyWord *kw, const KeySuffix *suf, const int *index, int ver, NUMDesc *Num)
Definition: formatting.c:1227
static NUMCacheEntry * NUM_cache_search(const char *str)
Definition: formatting.c:3946
#define NUM_TYPE
Definition: formatting.c:104
#define NULL
Definition: c.h:229
static const KeyWord NUM_keywords[]
Definition: formatting.c:825
FormatNode format[NUM_CACHE_SIZE+1]
Definition: formatting.c:387
static const int NUM_index[KeyWord_INDEX_SIZE]
Definition: formatting.c:897
#define zeroize_NUM(_n)
Definition: formatting.c:3869
static NUMCacheEntry * NUM_cache_getnew ( const char *  str)
static