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 4453 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:1633
#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:4651
static char format
Definition: pg_basebackup.c:80
#define SET_VARSIZE(PTR, len)
Definition: postgres.h:328

Definition at line 5078 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:1633
static FormatNode * NUM_cache(int len, NUMDesc *Num, text *pars_str, bool *shouldFree)
Definition: formatting.c:4029
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 5065 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 2031 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 3896 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 2111 of file formatting.c.

Referenced by DCH_from_char().

2112 {
2113  /*
2114  * Adjust all dates toward 2020; this is effectively what happens when we
2115  * assume '70' is 1970 and '69' is 2069.
2116  */
2117  /* Force 0-69 into the 2000's */
2118  if (year < 70)
2119  return year + 2000;
2120  /* Force 70-99 into the 1900's */
2121  else if (year < 100)
2122  return year + 1900;
2123  /* Force 100-519 into the 2000's */
2124  else if (year < 520)
2125  return year + 2000;
2126  /* Force 520-999 into the 1000's */
2127  else if (year < 1000)
2128  return year + 1000;
2129  else
2130  return year;
2131 }
char* asc_initcap ( const char *  buff,
size_t  nbytes 
)

Definition at line 1962 of file formatting.c.

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

Referenced by str_initcap().

1963 {
1964  char *result;
1965  char *p;
1966  int wasalnum = false;
1967 
1968  if (!buff)
1969  return NULL;
1970 
1971  result = pnstrdup(buff, nbytes);
1972 
1973  for (p = result; *p; p++)
1974  {
1975  char c;
1976 
1977  if (wasalnum)
1978  *p = c = pg_ascii_tolower((unsigned char) *p);
1979  else
1980  *p = c = pg_ascii_toupper((unsigned char) *p);
1981  /* we don't trust isalnum() here */
1982  wasalnum = ((c >= 'A' && c <= 'Z') ||
1983  (c >= 'a' && c <= 'z') ||
1984  (c >= '0' && c <= '9'));
1985  }
1986 
1987  return result;
1988 }
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:1633
char * c
#define NULL
Definition: c.h:229
char* asc_tolower ( const char *  buff,
size_t  nbytes 
)

Definition at line 1916 of file formatting.c.

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

Referenced by asc_tolower_z(), and str_tolower().

1917 {
1918  char *result;
1919  char *p;
1920 
1921  if (!buff)
1922  return NULL;
1923 
1924  result = pnstrdup(buff, nbytes);
1925 
1926  for (p = result; *p; p++)
1927  *p = pg_ascii_tolower((unsigned char) *p);
1928 
1929  return result;
1930 }
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:1633
#define NULL
Definition: c.h:229
static char* asc_tolower_z ( const char *  buff)
static

Definition at line 2011 of file formatting.c.

References asc_tolower().

Referenced by DCH_to_char(), and NUM_processor().

2012 {
2013  return asc_tolower(buff, strlen(buff));
2014 }
char * asc_tolower(const char *buff, size_t nbytes)
Definition: formatting.c:1916
char* asc_toupper ( const char *  buff,
size_t  nbytes 
)

Definition at line 1939 of file formatting.c.

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

Referenced by asc_toupper_z(), and str_toupper().

1940 {
1941  char *result;
1942  char *p;
1943 
1944  if (!buff)
1945  return NULL;
1946 
1947  result = pnstrdup(buff, nbytes);
1948 
1949  for (p = result; *p; p++)
1950  *p = pg_ascii_toupper((unsigned char) *p);
1951 
1952  return result;
1953 }
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:1633
#define NULL
Definition: c.h:229
static char* asc_toupper_z ( const char *  buff)
static

Definition at line 2017 of file formatting.c.

References asc_toupper().

Referenced by DCH_to_char().

2018 {
2019  return asc_toupper(buff, strlen(buff));
2020 }
char * asc_toupper(const char *buff, size_t nbytes)
Definition: formatting.c:1939
static text* datetime_to_char_body ( TmToChar tmtc,
text fmt,
bool  is_interval,
Oid  collid 
)
static

Definition at line 3373 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().

3374 {
3375  FormatNode *format;
3376  char *fmt_str,
3377  *result;
3378  bool incache;
3379  int fmt_len;
3380  text *res;
3381 
3382  /*
3383  * Convert fmt to C string
3384  */
3385  fmt_str = text_to_cstring(fmt);
3386  fmt_len = strlen(fmt_str);
3387 
3388  /*
3389  * Allocate workspace for result as C string
3390  */
3391  result = palloc((fmt_len * DCH_MAX_ITEM_SIZ) + 1);
3392  *result = '\0';
3393 
3394  if (fmt_len > DCH_CACHE_SIZE)
3395  {
3396  /*
3397  * Allocate new memory if format picture is bigger than static cache
3398  * and do not use cache (call parser always)
3399  */
3400  incache = FALSE;
3401 
3402  format = (FormatNode *) palloc((fmt_len + 1) * sizeof(FormatNode));
3403 
3404  parse_format(format, fmt_str, DCH_keywords,
3406  }
3407  else
3408  {
3409  /*
3410  * Use cache buffers
3411  */
3412  DCHCacheEntry *ent = DCH_cache_fetch(fmt_str);
3413 
3414  incache = TRUE;
3415  format = ent->format;
3416  }
3417 
3418  /* The real work is here */
3419  DCH_to_char(format, is_interval, tmtc, result, collid);
3420 
3421  if (!incache)
3422  pfree(format);
3423 
3424  pfree(fmt_str);
3425 
3426  /* convert C-string result to TEXT format */
3427  res = cstring_to_text(result);
3428 
3429  pfree(result);
3430  return res;
3431 }
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:1633
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:2417
FormatNode format[DCH_CACHE_SIZE+1]
Definition: formatting.c:379
static DCHCacheEntry * DCH_cache_fetch(const char *str)
Definition: formatting.c:3346
#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 3346 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().

3347 {
3348  DCHCacheEntry *ent;
3349 
3350  if ((ent = DCH_cache_search(str)) == NULL)
3351  {
3352  /*
3353  * Not in the cache, must run parser and save a new format-picture to
3354  * the cache. Do not mark the cache entry valid until parsing
3355  * succeeds.
3356  */
3357  ent = DCH_cache_getnew(str);
3358 
3359  parse_format(ent->format, str, DCH_keywords,
3361 
3362  ent->valid = true;
3363  }
3364  return ent;
3365 }
static DCHCacheEntry * DCH_cache_getnew(const char *str)
Definition: formatting.c:3256
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:3318
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 3256 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().

3257 {
3258  DCHCacheEntry *ent;
3259 
3260  /* counter overflow check - paranoia? */
3261  if (DCHCounter >= (INT_MAX - DCH_CACHE_ENTRIES))
3262  {
3263  DCHCounter = 0;
3264 
3265  for (ent = DCHCache; ent < (DCHCache + DCH_CACHE_ENTRIES); ent++)
3266  ent->age = (++DCHCounter);
3267  }
3268 
3269  /*
3270  * If cache is full, remove oldest entry (or recycle first not-valid one)
3271  */
3273  {
3274  DCHCacheEntry *old = DCHCache + 0;
3275 
3276 #ifdef DEBUG_TO_FROM_CHAR
3277  elog(DEBUG_elog_output, "cache is full (%d)", n_DCHCache);
3278 #endif
3279  if (old->valid)
3280  {
3281  for (ent = DCHCache + 1; ent < (DCHCache + DCH_CACHE_ENTRIES); ent++)
3282  {
3283  if (!ent->valid)
3284  {
3285  old = ent;
3286  break;
3287  }
3288  if (ent->age < old->age)
3289  old = ent;
3290  }
3291  }
3292 #ifdef DEBUG_TO_FROM_CHAR
3293  elog(DEBUG_elog_output, "OLD: '%s' AGE: %d", old->str, old->age);
3294 #endif
3295  old->valid = false;
3296  StrNCpy(old->str, str, DCH_CACHE_SIZE + 1);
3297  old->age = (++DCHCounter);
3298  /* caller is expected to fill format, then set valid */
3299  return old;
3300  }
3301  else
3302  {
3303 #ifdef DEBUG_TO_FROM_CHAR
3304  elog(DEBUG_elog_output, "NEW (%d)", n_DCHCache);
3305 #endif
3306  ent = DCHCache + n_DCHCache;
3307  ent->valid = false;
3308  StrNCpy(ent->str, str, DCH_CACHE_SIZE + 1);
3309  ent->age = (++DCHCounter);
3310  /* caller is expected to fill format, then set valid */
3311  ++n_DCHCache;
3312  return ent;
3313  }
3314 }
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 3318 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().

3319 {
3320  int i;
3321  DCHCacheEntry *ent;
3322 
3323  /* counter overflow check - paranoia? */
3324  if (DCHCounter >= (INT_MAX - DCH_CACHE_ENTRIES))
3325  {
3326  DCHCounter = 0;
3327 
3328  for (ent = DCHCache; ent < (DCHCache + DCH_CACHE_ENTRIES); ent++)
3329  ent->age = (++DCHCounter);
3330  }
3331 
3332  for (i = 0, ent = DCHCache; i < n_DCHCache; i++, ent++)
3333  {
3334  if (ent->valid && strcmp(ent->str, str) == 0)
3335  {
3336  ent->age = (++DCHCounter);
3337  return ent;
3338  }
3339  }
3340 
3341  return NULL;
3342 }
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 2984 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().

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

2418 {
2419  FormatNode *n;
2420  char *s;
2421  struct pg_tm *tm = &in->tm;
2422  int i;
2423 
2424  /* cache localized days and months */
2426 
2427  s = out;
2428  for (n = node; n->type != NODE_TYPE_END; n++)
2429  {
2430  if (n->type != NODE_TYPE_ACTION)
2431  {
2432  *s = n->character;
2433  s++;
2434  continue;
2435  }
2436 
2437  switch (n->key->id)
2438  {
2439  case DCH_A_M:
2440  case DCH_P_M:
2441  strcpy(s, (tm->tm_hour % HOURS_PER_DAY >= HOURS_PER_DAY / 2)
2442  ? P_M_STR : A_M_STR);
2443  s += strlen(s);
2444  break;
2445  case DCH_AM:
2446  case DCH_PM:
2447  strcpy(s, (tm->tm_hour % HOURS_PER_DAY >= HOURS_PER_DAY / 2)
2448  ? PM_STR : AM_STR);
2449  s += strlen(s);
2450  break;
2451  case DCH_a_m:
2452  case DCH_p_m:
2453  strcpy(s, (tm->tm_hour % HOURS_PER_DAY >= HOURS_PER_DAY / 2)
2454  ? p_m_STR : a_m_STR);
2455  s += strlen(s);
2456  break;
2457  case DCH_am:
2458  case DCH_pm:
2459  strcpy(s, (tm->tm_hour % HOURS_PER_DAY >= HOURS_PER_DAY / 2)
2460  ? pm_STR : am_STR);
2461  s += strlen(s);
2462  break;
2463  case DCH_HH:
2464  case DCH_HH12:
2465 
2466  /*
2467  * display time as shown on a 12-hour clock, even for
2468  * intervals
2469  */
2470  sprintf(s, "%0*d", S_FM(n->suffix) ? 0 : (tm->tm_hour >= 0) ? 2 : 3,
2471  tm->tm_hour % (HOURS_PER_DAY / 2) == 0 ? HOURS_PER_DAY / 2 :
2472  tm->tm_hour % (HOURS_PER_DAY / 2));
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_HH24:
2478  sprintf(s, "%0*d", S_FM(n->suffix) ? 0 : (tm->tm_hour >= 0) ? 2 : 3,
2479  tm->tm_hour);
2480  if (S_THth(n->suffix))
2481  str_numth(s, s, S_TH_TYPE(n->suffix));
2482  s += strlen(s);
2483  break;
2484  case DCH_MI:
2485  sprintf(s, "%0*d", S_FM(n->suffix) ? 0 : (tm->tm_min >= 0) ? 2 : 3,
2486  tm->tm_min);
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_SS:
2492  sprintf(s, "%0*d", S_FM(n->suffix) ? 0 : (tm->tm_sec >= 0) ? 2 : 3,
2493  tm->tm_sec);
2494  if (S_THth(n->suffix))
2495  str_numth(s, s, S_TH_TYPE(n->suffix));
2496  s += strlen(s);
2497  break;
2498  case DCH_MS: /* millisecond */
2499  sprintf(s, "%03d", (int) (in->fsec / INT64CONST(1000)));
2500  if (S_THth(n->suffix))
2501  str_numth(s, s, S_TH_TYPE(n->suffix));
2502  s += strlen(s);
2503  break;
2504  case DCH_US: /* microsecond */
2505  sprintf(s, "%06d", (int) in->fsec);
2506  if (S_THth(n->suffix))
2507  str_numth(s, s, S_TH_TYPE(n->suffix));
2508  s += strlen(s);
2509  break;
2510  case DCH_SSSS:
2511  sprintf(s, "%d", tm->tm_hour * SECS_PER_HOUR +
2512  tm->tm_min * SECS_PER_MINUTE +
2513  tm->tm_sec);
2514  if (S_THth(n->suffix))
2515  str_numth(s, s, S_TH_TYPE(n->suffix));
2516  s += strlen(s);
2517  break;
2518  case DCH_tz:
2520  if (tmtcTzn(in))
2521  {
2522  /* We assume here that timezone names aren't localized */
2523  char *p = asc_tolower_z(tmtcTzn(in));
2524 
2525  strcpy(s, p);
2526  pfree(p);
2527  s += strlen(s);
2528  }
2529  break;
2530  case DCH_TZ:
2532  if (tmtcTzn(in))
2533  {
2534  strcpy(s, tmtcTzn(in));
2535  s += strlen(s);
2536  }
2537  break;
2538  case DCH_OF:
2540  sprintf(s, "%c%0*d",
2541  (tm->tm_gmtoff >= 0) ? '+' : '-',
2542  S_FM(n->suffix) ? 0 : 2,
2543  abs((int) tm->tm_gmtoff) / SECS_PER_HOUR);
2544  s += strlen(s);
2545  if (abs((int) tm->tm_gmtoff) % SECS_PER_HOUR != 0)
2546  {
2547  sprintf(s, ":%02d",
2548  (abs((int) tm->tm_gmtoff) % SECS_PER_HOUR) / SECS_PER_MINUTE);
2549  s += strlen(s);
2550  }
2551  break;
2552  case DCH_A_D:
2553  case DCH_B_C:
2555  strcpy(s, (tm->tm_year <= 0 ? B_C_STR : A_D_STR));
2556  s += strlen(s);
2557  break;
2558  case DCH_AD:
2559  case DCH_BC:
2561  strcpy(s, (tm->tm_year <= 0 ? BC_STR : AD_STR));
2562  s += strlen(s);
2563  break;
2564  case DCH_a_d:
2565  case DCH_b_c:
2567  strcpy(s, (tm->tm_year <= 0 ? b_c_STR : a_d_STR));
2568  s += strlen(s);
2569  break;
2570  case DCH_ad:
2571  case DCH_bc:
2573  strcpy(s, (tm->tm_year <= 0 ? bc_STR : ad_STR));
2574  s += strlen(s);
2575  break;
2576  case DCH_MONTH:
2578  if (!tm->tm_mon)
2579  break;
2580  if (S_TM(n->suffix))
2581  {
2582  char *str = str_toupper_z(localized_full_months[tm->tm_mon - 1], collid);
2583 
2584  if (strlen(str) <= (n->key->len + TM_SUFFIX_LEN) * DCH_MAX_ITEM_SIZ)
2585  strcpy(s, str);
2586  else
2587  ereport(ERROR,
2588  (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
2589  errmsg("localized string format value too long")));
2590  }
2591  else
2592  sprintf(s, "%*s", S_FM(n->suffix) ? 0 : -9,
2593  asc_toupper_z(months_full[tm->tm_mon - 1]));
2594  s += strlen(s);
2595  break;
2596  case DCH_Month:
2598  if (!tm->tm_mon)
2599  break;
2600  if (S_TM(n->suffix))
2601  {
2602  char *str = str_initcap_z(localized_full_months[tm->tm_mon - 1], collid);
2603 
2604  if (strlen(str) <= (n->key->len + TM_SUFFIX_LEN) * DCH_MAX_ITEM_SIZ)
2605  strcpy(s, str);
2606  else
2607  ereport(ERROR,
2608  (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
2609  errmsg("localized string format value too long")));
2610  }
2611  else
2612  sprintf(s, "%*s", S_FM(n->suffix) ? 0 : -9,
2613  months_full[tm->tm_mon - 1]);
2614  s += strlen(s);
2615  break;
2616  case DCH_month:
2618  if (!tm->tm_mon)
2619  break;
2620  if (S_TM(n->suffix))
2621  {
2622  char *str = str_tolower_z(localized_full_months[tm->tm_mon - 1], collid);
2623 
2624  if (strlen(str) <= (n->key->len + TM_SUFFIX_LEN) * DCH_MAX_ITEM_SIZ)
2625  strcpy(s, str);
2626  else
2627  ereport(ERROR,
2628  (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
2629  errmsg("localized string format value too long")));
2630  }
2631  else
2632  sprintf(s, "%*s", S_FM(n->suffix) ? 0 : -9,
2633  asc_tolower_z(months_full[tm->tm_mon - 1]));
2634  s += strlen(s);
2635  break;
2636  case DCH_MON:
2638  if (!tm->tm_mon)
2639  break;
2640  if (S_TM(n->suffix))
2641  {
2642  char *str = str_toupper_z(localized_abbrev_months[tm->tm_mon - 1], collid);
2643 
2644  if (strlen(str) <= (n->key->len + TM_SUFFIX_LEN) * DCH_MAX_ITEM_SIZ)
2645  strcpy(s, str);
2646  else
2647  ereport(ERROR,
2648  (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
2649  errmsg("localized string format value too long")));
2650  }
2651  else
2652  strcpy(s, asc_toupper_z(months[tm->tm_mon - 1]));
2653  s += strlen(s);
2654  break;
2655  case DCH_Mon:
2657  if (!tm->tm_mon)
2658  break;
2659  if (S_TM(n->suffix))
2660  {
2661  char *str = str_initcap_z(localized_abbrev_months[tm->tm_mon - 1], collid);
2662 
2663  if (strlen(str) <= (n->key->len + TM_SUFFIX_LEN) * DCH_MAX_ITEM_SIZ)
2664  strcpy(s, str);
2665  else
2666  ereport(ERROR,
2667  (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
2668  errmsg("localized string format value too long")));
2669  }
2670  else
2671  strcpy(s, months[tm->tm_mon - 1]);
2672  s += strlen(s);
2673  break;
2674  case DCH_mon:
2676  if (!tm->tm_mon)
2677  break;
2678  if (S_TM(n->suffix))
2679  {
2680  char *str = str_tolower_z(localized_abbrev_months[tm->tm_mon - 1], collid);
2681 
2682  if (strlen(str) <= (n->key->len + TM_SUFFIX_LEN) * DCH_MAX_ITEM_SIZ)
2683  strcpy(s, str);
2684  else
2685  ereport(ERROR,
2686  (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
2687  errmsg("localized string format value too long")));
2688  }
2689  else
2690  strcpy(s, asc_tolower_z(months[tm->tm_mon - 1]));
2691  s += strlen(s);
2692  break;
2693  case DCH_MM:
2694  sprintf(s, "%0*d", S_FM(n->suffix) ? 0 : (tm->tm_mon >= 0) ? 2 : 3,
2695  tm->tm_mon);
2696  if (S_THth(n->suffix))
2697  str_numth(s, s, S_TH_TYPE(n->suffix));
2698  s += strlen(s);
2699  break;
2700  case DCH_DAY:
2702  if (S_TM(n->suffix))
2703  {
2704  char *str = str_toupper_z(localized_full_days[tm->tm_wday], collid);
2705 
2706  if (strlen(str) <= (n->key->len + TM_SUFFIX_LEN) * DCH_MAX_ITEM_SIZ)
2707  strcpy(s, str);
2708  else
2709  ereport(ERROR,
2710  (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
2711  errmsg("localized string format value too long")));
2712  }
2713  else
2714  sprintf(s, "%*s", S_FM(n->suffix) ? 0 : -9,
2715  asc_toupper_z(days[tm->tm_wday]));
2716  s += strlen(s);
2717  break;
2718  case DCH_Day:
2720  if (S_TM(n->suffix))
2721  {
2722  char *str = str_initcap_z(localized_full_days[tm->tm_wday], collid);
2723 
2724  if (strlen(str) <= (n->key->len + TM_SUFFIX_LEN) * DCH_MAX_ITEM_SIZ)
2725  strcpy(s, str);
2726  else
2727  ereport(ERROR,
2728  (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
2729  errmsg("localized string format value too long")));
2730  }
2731  else
2732  sprintf(s, "%*s", S_FM(n->suffix) ? 0 : -9,
2733  days[tm->tm_wday]);
2734  s += strlen(s);
2735  break;
2736  case DCH_day:
2738  if (S_TM(n->suffix))
2739  {
2740  char *str = str_tolower_z(localized_full_days[tm->tm_wday], collid);
2741 
2742  if (strlen(str) <= (n->key->len + TM_SUFFIX_LEN) * DCH_MAX_ITEM_SIZ)
2743  strcpy(s, str);
2744  else
2745  ereport(ERROR,
2746  (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
2747  errmsg("localized string format value too long")));
2748  }
2749  else
2750  sprintf(s, "%*s", S_FM(n->suffix) ? 0 : -9,
2751  asc_tolower_z(days[tm->tm_wday]));
2752  s += strlen(s);
2753  break;
2754  case DCH_DY:
2756  if (S_TM(n->suffix))
2757  {
2758  char *str = str_toupper_z(localized_abbrev_days[tm->tm_wday], collid);
2759 
2760  if (strlen(str) <= (n->key->len + TM_SUFFIX_LEN) * DCH_MAX_ITEM_SIZ)
2761  strcpy(s, str);
2762  else
2763  ereport(ERROR,
2764  (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
2765  errmsg("localized string format value too long")));
2766  }
2767  else
2768  strcpy(s, asc_toupper_z(days_short[tm->tm_wday]));
2769  s += strlen(s);
2770  break;
2771  case DCH_Dy:
2773  if (S_TM(n->suffix))
2774  {
2775  char *str = str_initcap_z(localized_abbrev_days[tm->tm_wday], collid);
2776 
2777  if (strlen(str) <= (n->key->len + TM_SUFFIX_LEN) * DCH_MAX_ITEM_SIZ)
2778  strcpy(s, str);
2779  else
2780  ereport(ERROR,
2781  (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
2782  errmsg("localized string format value too long")));
2783  }
2784  else
2785  strcpy(s, days_short[tm->tm_wday]);
2786  s += strlen(s);
2787  break;
2788  case DCH_dy:
2790  if (S_TM(n->suffix))
2791  {
2792  char *str = str_tolower_z(localized_abbrev_days[tm->tm_wday], collid);
2793 
2794  if (strlen(str) <= (n->key->len + TM_SUFFIX_LEN) * DCH_MAX_ITEM_SIZ)
2795  strcpy(s, str);
2796  else
2797  ereport(ERROR,
2798  (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
2799  errmsg("localized string format value too long")));
2800  }
2801  else
2802  strcpy(s, asc_tolower_z(days_short[tm->tm_wday]));
2803  s += strlen(s);
2804  break;
2805  case DCH_DDD:
2806  case DCH_IDDD:
2807  sprintf(s, "%0*d", S_FM(n->suffix) ? 0 : 3,
2808  (n->key->id == DCH_DDD) ?
2809  tm->tm_yday :
2810  date2isoyearday(tm->tm_year, tm->tm_mon, tm->tm_mday));
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_DD:
2816  sprintf(s, "%0*d", S_FM(n->suffix) ? 0 : 2, tm->tm_mday);
2817  if (S_THth(n->suffix))
2818  str_numth(s, s, S_TH_TYPE(n->suffix));
2819  s += strlen(s);
2820  break;
2821  case DCH_D:
2823  sprintf(s, "%d", tm->tm_wday + 1);
2824  if (S_THth(n->suffix))
2825  str_numth(s, s, S_TH_TYPE(n->suffix));
2826  s += strlen(s);
2827  break;
2828  case DCH_ID:
2830  sprintf(s, "%d", (tm->tm_wday == 0) ? 7 : tm->tm_wday);
2831  if (S_THth(n->suffix))
2832  str_numth(s, s, S_TH_TYPE(n->suffix));
2833  s += strlen(s);
2834  break;
2835  case DCH_WW:
2836  sprintf(s, "%0*d", S_FM(n->suffix) ? 0 : 2,
2837  (tm->tm_yday - 1) / 7 + 1);
2838  if (S_THth(n->suffix))
2839  str_numth(s, s, S_TH_TYPE(n->suffix));
2840  s += strlen(s);
2841  break;
2842  case DCH_IW:
2843  sprintf(s, "%0*d", S_FM(n->suffix) ? 0 : 2,
2844  date2isoweek(tm->tm_year, tm->tm_mon, tm->tm_mday));
2845  if (S_THth(n->suffix))
2846  str_numth(s, s, S_TH_TYPE(n->suffix));
2847  s += strlen(s);
2848  break;
2849  case DCH_Q:
2850  if (!tm->tm_mon)
2851  break;
2852  sprintf(s, "%d", (tm->tm_mon - 1) / 3 + 1);
2853  if (S_THth(n->suffix))
2854  str_numth(s, s, S_TH_TYPE(n->suffix));
2855  s += strlen(s);
2856  break;
2857  case DCH_CC:
2858  if (is_interval) /* straight calculation */
2859  i = tm->tm_year / 100;
2860  else
2861  {
2862  if (tm->tm_year > 0)
2863  /* Century 20 == 1901 - 2000 */
2864  i = (tm->tm_year - 1) / 100 + 1;
2865  else
2866  /* Century 6BC == 600BC - 501BC */
2867  i = tm->tm_year / 100 - 1;
2868  }
2869  if (i <= 99 && i >= -99)
2870  sprintf(s, "%0*d", S_FM(n->suffix) ? 0 : (i >= 0) ? 2 : 3, i);
2871  else
2872  sprintf(s, "%d", i);
2873  if (S_THth(n->suffix))
2874  str_numth(s, s, S_TH_TYPE(n->suffix));
2875  s += strlen(s);
2876  break;
2877  case DCH_Y_YYY:
2878  i = ADJUST_YEAR(tm->tm_year, is_interval) / 1000;
2879  sprintf(s, "%d,%03d", i,
2880  ADJUST_YEAR(tm->tm_year, is_interval) - (i * 1000));
2881  if (S_THth(n->suffix))
2882  str_numth(s, s, S_TH_TYPE(n->suffix));
2883  s += strlen(s);
2884  break;
2885  case DCH_YYYY:
2886  case DCH_IYYY:
2887  sprintf(s, "%0*d",
2888  S_FM(n->suffix) ? 0 :
2889  (ADJUST_YEAR(tm->tm_year, is_interval) >= 0) ? 4 : 5,
2890  (n->key->id == DCH_YYYY ?
2891  ADJUST_YEAR(tm->tm_year, is_interval) :
2893  tm->tm_mon,
2894  tm->tm_mday),
2895  is_interval)));
2896  if (S_THth(n->suffix))
2897  str_numth(s, s, S_TH_TYPE(n->suffix));
2898  s += strlen(s);
2899  break;
2900  case DCH_YYY:
2901  case DCH_IYY:
2902  sprintf(s, "%0*d",
2903  S_FM(n->suffix) ? 0 :
2904  (ADJUST_YEAR(tm->tm_year, is_interval) >= 0) ? 3 : 4,
2905  (n->key->id == DCH_YYY ?
2906  ADJUST_YEAR(tm->tm_year, is_interval) :
2908  tm->tm_mon,
2909  tm->tm_mday),
2910  is_interval)) % 1000);
2911  if (S_THth(n->suffix))
2912  str_numth(s, s, S_TH_TYPE(n->suffix));
2913  s += strlen(s);
2914  break;
2915  case DCH_YY:
2916  case DCH_IY:
2917  sprintf(s, "%0*d",
2918  S_FM(n->suffix) ? 0 :
2919  (ADJUST_YEAR(tm->tm_year, is_interval) >= 0) ? 2 : 3,
2920  (n->key->id == DCH_YY ?
2921  ADJUST_YEAR(tm->tm_year, is_interval) :
2923  tm->tm_mon,
2924  tm->tm_mday),
2925  is_interval)) % 100);
2926  if (S_THth(n->suffix))
2927  str_numth(s, s, S_TH_TYPE(n->suffix));
2928  s += strlen(s);
2929  break;
2930  case DCH_Y:
2931  case DCH_I:
2932  sprintf(s, "%1d",
2933  (n->key->id == DCH_Y ?
2934  ADJUST_YEAR(tm->tm_year, is_interval) :
2936  tm->tm_mon,
2937  tm->tm_mday),
2938  is_interval)) % 10);
2939  if (S_THth(n->suffix))
2940  str_numth(s, s, S_TH_TYPE(n->suffix));
2941  s += strlen(s);
2942  break;
2943  case DCH_RM:
2944  if (!tm->tm_mon)
2945  break;
2946  sprintf(s, "%*s", S_FM(n->suffix) ? 0 : -4,
2948  s += strlen(s);
2949  break;
2950  case DCH_rm:
2951  if (!tm->tm_mon)
2952  break;
2953  sprintf(s, "%*s", S_FM(n->suffix) ? 0 : -4,
2955  s += strlen(s);
2956  break;
2957  case DCH_W:
2958  sprintf(s, "%d", (tm->tm_mday - 1) / 7 + 1);
2959  if (S_THth(n->suffix))
2960  str_numth(s, s, S_TH_TYPE(n->suffix));
2961  s += strlen(s);
2962  break;
2963  case DCH_J:
2964  sprintf(s, "%d", date2j(tm->tm_year, tm->tm_mon, tm->tm_mday));
2965  if (S_THth(n->suffix))
2966  str_numth(s, s, S_TH_TYPE(n->suffix));
2967  s += strlen(s);
2968  break;
2969  }
2970  }
2971 
2972  *s = '\0';
2973 }
#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:2005
fsec_t fsec
Definition: formatting.c:462
static char * asc_tolower_z(const char *buff)
Definition: formatting.c:2011
#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:1993
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:111
#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:2017
#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:1999
#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 3614 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().

3616 {
3617  FormatNode *format;
3618  TmFromChar tmfc;
3619  int fmt_len;
3620  char *date_str;
3621  int fmask;
3622 
3623  date_str = text_to_cstring(date_txt);
3624 
3625  ZERO_tmfc(&tmfc);
3626  ZERO_tm(tm);
3627  *fsec = 0;
3628  fmask = 0; /* bit mask for ValidateDate() */
3629 
3630  fmt_len = VARSIZE_ANY_EXHDR(fmt);
3631 
3632  if (fmt_len)
3633  {
3634  char *fmt_str;
3635  bool incache;
3636 
3637  fmt_str = text_to_cstring(fmt);
3638 
3639  if (fmt_len > DCH_CACHE_SIZE)
3640  {
3641  /*
3642  * Allocate new memory if format picture is bigger than static
3643  * cache and do not use cache (call parser always)
3644  */
3645  incache = FALSE;
3646 
3647  format = (FormatNode *) palloc((fmt_len + 1) * sizeof(FormatNode));
3648 
3649  parse_format(format, fmt_str, DCH_keywords,
3651  }
3652  else
3653  {
3654  /*
3655  * Use cache buffers
3656  */
3657  DCHCacheEntry *ent = DCH_cache_fetch(fmt_str);
3658 
3659  incache = TRUE;
3660  format = ent->format;
3661  }
3662 
3663 #ifdef DEBUG_TO_FROM_CHAR
3664  /* dump_node(format, fmt_len); */
3665  /* dump_index(DCH_keywords, DCH_index); */
3666 #endif
3667 
3668  DCH_from_char(format, date_str, &tmfc);
3669 
3670  pfree(fmt_str);
3671  if (!incache)
3672  pfree(format);
3673  }
3674 
3675  DEBUG_TMFC(&tmfc);
3676 
3677  /*
3678  * Convert to_date/to_timestamp input fields to standard 'tm'
3679  */
3680  if (tmfc.ssss)
3681  {
3682  int x = tmfc.ssss;
3683 
3684  tm->tm_hour = x / SECS_PER_HOUR;
3685  x %= SECS_PER_HOUR;
3686  tm->tm_min = x / SECS_PER_MINUTE;
3687  x %= SECS_PER_MINUTE;
3688  tm->tm_sec = x;
3689  }
3690 
3691  if (tmfc.ss)
3692  tm->tm_sec = tmfc.ss;
3693  if (tmfc.mi)
3694  tm->tm_min = tmfc.mi;
3695  if (tmfc.hh)
3696  tm->tm_hour = tmfc.hh;
3697 
3698  if (tmfc.clock == CLOCK_12_HOUR)
3699  {
3700  if (tm->tm_hour < 1 || tm->tm_hour > HOURS_PER_DAY / 2)
3701  ereport(ERROR,
3702  (errcode(ERRCODE_INVALID_DATETIME_FORMAT),
3703  errmsg("hour \"%d\" is invalid for the 12-hour clock",
3704  tm->tm_hour),
3705  errhint("Use the 24-hour clock, or give an hour between 1 and 12.")));
3706 
3707  if (tmfc.pm && tm->tm_hour < HOURS_PER_DAY / 2)
3708  tm->tm_hour += HOURS_PER_DAY / 2;
3709  else if (!tmfc.pm && tm->tm_hour == HOURS_PER_DAY / 2)
3710  tm->tm_hour = 0;
3711  }
3712 
3713  if (tmfc.year)
3714  {
3715  /*
3716  * If CC and YY (or Y) are provided, use YY as 2 low-order digits for
3717  * the year in the given century. Keep in mind that the 21st century
3718  * AD runs from 2001-2100, not 2000-2099; 6th century BC runs from
3719  * 600BC to 501BC.
3720  */
3721  if (tmfc.cc && tmfc.yysz <= 2)
3722  {
3723  if (tmfc.bc)
3724  tmfc.cc = -tmfc.cc;
3725  tm->tm_year = tmfc.year % 100;
3726  if (tm->tm_year)
3727  {
3728  if (tmfc.cc >= 0)
3729  tm->tm_year += (tmfc.cc - 1) * 100;
3730  else
3731  tm->tm_year = (tmfc.cc + 1) * 100 - tm->tm_year + 1;
3732  }
3733  else
3734  {
3735  /* find century year for dates ending in "00" */
3736  tm->tm_year = tmfc.cc * 100 + ((tmfc.cc >= 0) ? 0 : 1);
3737  }
3738  }
3739  else
3740  {
3741  /* If a 4-digit year is provided, we use that and ignore CC. */
3742  tm->tm_year = tmfc.year;
3743  if (tmfc.bc && tm->tm_year > 0)
3744  tm->tm_year = -(tm->tm_year - 1);
3745  }
3746  fmask |= DTK_M(YEAR);
3747  }
3748  else if (tmfc.cc)
3749  {
3750  /* use first year of century */
3751  if (tmfc.bc)
3752  tmfc.cc = -tmfc.cc;
3753  if (tmfc.cc >= 0)
3754  /* +1 because 21st century started in 2001 */
3755  tm->tm_year = (tmfc.cc - 1) * 100 + 1;
3756  else
3757  /* +1 because year == 599 is 600 BC */
3758  tm->tm_year = tmfc.cc * 100 + 1;
3759  fmask |= DTK_M(YEAR);
3760  }
3761 
3762  if (tmfc.j)
3763  {
3764  j2date(tmfc.j, &tm->tm_year, &tm->tm_mon, &tm->tm_mday);
3765  fmask |= DTK_DATE_M;
3766  }
3767 
3768  if (tmfc.ww)
3769  {
3770  if (tmfc.mode == FROM_CHAR_DATE_ISOWEEK)
3771  {
3772  /*
3773  * If tmfc.d is not set, then the date is left at the beginning of
3774  * the ISO week (Monday).
3775  */
3776  if (tmfc.d)
3777  isoweekdate2date(tmfc.ww, tmfc.d, &tm->tm_year, &tm->tm_mon, &tm->tm_mday);
3778  else
3779  isoweek2date(tmfc.ww, &tm->tm_year, &tm->tm_mon, &tm->tm_mday);
3780  fmask |= DTK_DATE_M;
3781  }
3782  else
3783  tmfc.ddd = (tmfc.ww - 1) * 7 + 1;
3784  }
3785 
3786  if (tmfc.w)
3787  tmfc.dd = (tmfc.w - 1) * 7 + 1;
3788  if (tmfc.dd)
3789  {
3790  tm->tm_mday = tmfc.dd;
3791  fmask |= DTK_M(DAY);
3792  }
3793  if (tmfc.mm)
3794  {
3795  tm->tm_mon = tmfc.mm;
3796  fmask |= DTK_M(MONTH);
3797  }
3798 
3799  if (tmfc.ddd && (tm->tm_mon <= 1 || tm->tm_mday <= 1))
3800  {
3801  /*
3802  * The month and day field have not been set, so we use the
3803  * day-of-year field to populate them. Depending on the date mode,
3804  * this field may be interpreted as a Gregorian day-of-year, or an ISO
3805  * week date day-of-year.
3806  */
3807 
3808  if (!tm->tm_year && !tmfc.bc)
3809  ereport(ERROR,
3810  (errcode(ERRCODE_INVALID_DATETIME_FORMAT),
3811  errmsg("cannot calculate day of year without year information")));
3812 
3813  if (tmfc.mode == FROM_CHAR_DATE_ISOWEEK)
3814  {
3815  int j0; /* zeroth day of the ISO year, in Julian */
3816 
3817  j0 = isoweek2j(tm->tm_year, 1) - 1;
3818 
3819  j2date(j0 + tmfc.ddd, &tm->tm_year, &tm->tm_mon, &tm->tm_mday);
3820  fmask |= DTK_DATE_M;
3821  }
3822  else
3823  {
3824  const int *y;
3825  int i;
3826 
3827  static const int ysum[2][13] = {
3828  {0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365},
3829  {0, 31, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335, 366}};
3830 
3831  y = ysum[isleap(tm->tm_year)];
3832 
3833  for (i = 1; i <= MONTHS_PER_YEAR; i++)
3834  {
3835  if (tmfc.ddd <= y[i])
3836  break;
3837  }
3838  if (tm->tm_mon <= 1)
3839  tm->tm_mon = i;
3840 
3841  if (tm->tm_mday <= 1)
3842  tm->tm_mday = tmfc.ddd - y[i - 1];
3843 
3844  fmask |= DTK_M(MONTH) | DTK_M(DAY);
3845  }
3846  }
3847 
3848  if (tmfc.ms)
3849  *fsec += tmfc.ms * 1000;
3850  if (tmfc.us)
3851  *fsec += tmfc.us;
3852 
3853  /* Range-check date fields according to bit mask computed above */
3854  if (fmask != 0)
3855  {
3856  /* We already dealt with AD/BC, so pass isjulian = true */
3857  int dterr = ValidateDate(fmask, true, false, false, tm);
3858 
3859  if (dterr != 0)
3860  {
3861  /*
3862  * Force the error to be DTERR_FIELD_OVERFLOW even if ValidateDate
3863  * said DTERR_MD_FIELD_OVERFLOW, because we don't want to print an
3864  * irrelevant hint about datestyle.
3865  */
3866  DateTimeParseError(DTERR_FIELD_OVERFLOW, date_str, "timestamp");
3867  }
3868  }
3869 
3870  /* Range-check time fields too */
3871  if (tm->tm_hour < 0 || tm->tm_hour >= HOURS_PER_DAY ||
3872  tm->tm_min < 0 || tm->tm_min >= MINS_PER_HOUR ||
3873  tm->tm_sec < 0 || tm->tm_sec >= SECS_PER_MINUTE ||
3874  *fsec < INT64CONST(0) || *fsec >= USECS_PER_SEC)
3875  DateTimeParseError(DTERR_FIELD_OVERFLOW, date_str, "timestamp");
3876 
3877  DEBUG_TM(tm);
3878 
3879  pfree(date_str);
3880 }
#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:2984
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:3346
#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 3889 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().

3890 {
3891  memset(str, c, max);
3892  *(str + max) = '\0';
3893  return str;
3894 }
char * c
Datum float4_to_char ( PG_FUNCTION_ARGS  )

Definition at line 5491 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.

5492 {
5494  text *fmt = PG_GETARG_TEXT_PP(1);
5495  NUMDesc Num;
5496  FormatNode *format;
5497  text *result;
5498  bool shouldFree;
5499  int out_pre_spaces = 0,
5500  sign = 0;
5501  char *numstr,
5502  *orgnum,
5503  *p;
5504 
5506 
5507  if (IS_ROMAN(&Num))
5508  numstr = orgnum = int_to_roman((int) rint(value));
5509  else if (IS_EEEE(&Num))
5510  {
5511  numstr = orgnum = (char *) palloc(MAXDOUBLEWIDTH + 1);
5512  if (isnan(value) || is_infinite(value))
5513  {
5514  /*
5515  * Allow 6 characters for the leading sign, the decimal point,
5516  * "e", the exponent's sign and two exponent digits.
5517  */
5518  numstr = (char *) palloc(Num.pre + Num.post + 7);
5519  fill_str(numstr, '#', Num.pre + Num.post + 6);
5520  *numstr = ' ';
5521  *(numstr + Num.pre + 1) = '.';
5522  }
5523  else
5524  {
5525  snprintf(orgnum, MAXDOUBLEWIDTH + 1, "%+.*e", Num.post, value);
5526 
5527  /*
5528  * Swap a leading positive sign for a space.
5529  */
5530  if (*orgnum == '+')
5531  *orgnum = ' ';
5532 
5533  numstr = orgnum;
5534  }
5535  }
5536  else
5537  {
5538  float4 val = value;
5539  int numstr_pre_len;
5540 
5541  if (IS_MULTI(&Num))
5542  {
5543  float multi = pow((double) 10, (double) Num.multi);
5544 
5545  val = value * multi;
5546  Num.pre += Num.multi;
5547  }
5548 
5549  orgnum = (char *) palloc(MAXFLOATWIDTH + 1);
5550  snprintf(orgnum, MAXFLOATWIDTH + 1, "%.0f", fabs(val));
5551  numstr_pre_len = strlen(orgnum);
5552 
5553  /* adjust post digits to fit max float digits */
5554  if (numstr_pre_len >= FLT_DIG)
5555  Num.post = 0;
5556  else if (numstr_pre_len + Num.post > FLT_DIG)
5557  Num.post = FLT_DIG - numstr_pre_len;
5558  snprintf(orgnum, MAXFLOATWIDTH + 1, "%.*f", Num.post, val);
5559 
5560  if (*orgnum == '-')
5561  { /* < 0 */
5562  sign = '-';
5563  numstr = orgnum + 1;
5564  }
5565  else
5566  {
5567  sign = '+';
5568  numstr = orgnum;
5569  }
5570 
5571  if ((p = strchr(numstr, '.')))
5572  numstr_pre_len = p - numstr;
5573  else
5574  numstr_pre_len = strlen(numstr);
5575 
5576  /* needs padding? */
5577  if (numstr_pre_len < Num.pre)
5578  out_pre_spaces = Num.pre - numstr_pre_len;
5579  /* overflowed prefix digit format? */
5580  else if (numstr_pre_len > Num.pre)
5581  {
5582  numstr = (char *) palloc(Num.pre + Num.post + 2);
5583  fill_str(numstr, '#', Num.pre + Num.post + 1);
5584  *(numstr + Num.pre) = '.';
5585  }
5586  }
5587 
5589  PG_RETURN_TEXT_P(result);
5590 }
#define MAXDOUBLEWIDTH
Definition: formatting.c:125
#define IS_EEEE(_f)
Definition: formatting.c:357
return result
Definition: formatting.c:1633
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:5078
#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:5065
#define IS_MULTI(_f)
Definition: formatting.c:356
static struct @121 value
char sign
Definition: informix.c:693
static char * fill_str(char *str, int c, int max)
Definition: formatting.c:3889
static char * int_to_roman(int number)
Definition: formatting.c:4087
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
long val
Definition: informix.c:689
Datum float8_to_char ( PG_FUNCTION_ARGS  )

Definition at line 5597 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.

5598 {
5600  text *fmt = PG_GETARG_TEXT_PP(1);
5601  NUMDesc Num;
5602  FormatNode *format;
5603  text *result;
5604  bool shouldFree;
5605  int out_pre_spaces = 0,
5606  sign = 0;
5607  char *numstr,
5608  *orgnum,
5609  *p;
5610 
5612 
5613  if (IS_ROMAN(&Num))
5614  numstr = orgnum = int_to_roman((int) rint(value));
5615  else if (IS_EEEE(&Num))
5616  {
5617  numstr = orgnum = (char *) palloc(MAXDOUBLEWIDTH + 1);
5618  if (isnan(value) || is_infinite(value))
5619  {
5620  /*
5621  * Allow 6 characters for the leading sign, the decimal point,
5622  * "e", the exponent's sign and two exponent digits.
5623  */
5624  numstr = (char *) palloc(Num.pre + Num.post + 7);
5625  fill_str(numstr, '#', Num.pre + Num.post + 6);
5626  *numstr = ' ';
5627  *(numstr + Num.pre + 1) = '.';
5628  }
5629  else
5630  {
5631  snprintf(orgnum, MAXDOUBLEWIDTH + 1, "%+.*e", Num.post, value);
5632 
5633  /*
5634  * Swap a leading positive sign for a space.
5635  */
5636  if (*orgnum == '+')
5637  *orgnum = ' ';
5638 
5639  numstr = orgnum;
5640  }
5641  }
5642  else
5643  {
5644  float8 val = value;
5645  int numstr_pre_len;
5646 
5647  if (IS_MULTI(&Num))
5648  {
5649  double multi = pow((double) 10, (double) Num.multi);
5650 
5651  val = value * multi;
5652  Num.pre += Num.multi;
5653  }
5654  orgnum = (char *) palloc(MAXDOUBLEWIDTH + 1);
5655  numstr_pre_len = snprintf(orgnum, MAXDOUBLEWIDTH + 1, "%.0f", fabs(val));
5656 
5657  /* adjust post digits to fit max double digits */
5658  if (numstr_pre_len >= DBL_DIG)
5659  Num.post = 0;
5660  else if (numstr_pre_len + Num.post > DBL_DIG)
5661  Num.post = DBL_DIG - numstr_pre_len;
5662  snprintf(orgnum, MAXDOUBLEWIDTH + 1, "%.*f", Num.post, val);
5663 
5664  if (*orgnum == '-')
5665  { /* < 0 */
5666  sign = '-';
5667  numstr = orgnum + 1;
5668  }
5669  else
5670  {
5671  sign = '+';
5672  numstr = orgnum;
5673  }
5674 
5675  if ((p = strchr(numstr, '.')))
5676  numstr_pre_len = p - numstr;
5677  else
5678  numstr_pre_len = strlen(numstr);
5679 
5680  /* needs padding? */
5681  if (numstr_pre_len < Num.pre)
5682  out_pre_spaces = Num.pre - numstr_pre_len;
5683  /* overflowed prefix digit format? */
5684  else if (numstr_pre_len > Num.pre)
5685  {
5686  numstr = (char *) palloc(Num.pre + Num.post + 2);
5687  fill_str(numstr, '#', Num.pre + Num.post + 1);
5688  *(numstr + Num.pre) = '.';
5689  }
5690  }
5691 
5693  PG_RETURN_TEXT_P(result);
5694 }
#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:1633
int snprintf(char *str, size_t count, const char *fmt,...) pg_attribute_printf(3
#define NUM_TOCHAR_finish
Definition: formatting.c:5078
#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:5065
double float8
Definition: c.h:381
#define IS_MULTI(_f)
Definition: formatting.c:356
static struct @121 value
char sign
Definition: informix.c:693
static char * fill_str(char *str, int c, int max)
Definition: formatting.c:3889
static char * int_to_roman(int number)
Definition: formatting.c:4087
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
long val
Definition: informix.c:689
static int from_char_parse_int ( int *  dest,
char **  src,
FormatNode node 
)
static

Definition at line 2299 of file formatting.c.

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

Referenced by DCH_from_char().

2300 {
2301  return from_char_parse_int_len(dest, src, node->key->len, node);
2302 }
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:2209
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 2209 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().

2210 {
2211  long result;
2212  char copy[DCH_MAX_ITEM_SIZ + 1];
2213  char *init = *src;
2214  int used;
2215 
2216  /*
2217  * Skip any whitespace before parsing the integer.
2218  */
2219  *src += strspace_len(*src);
2220 
2221  Assert(len <= DCH_MAX_ITEM_SIZ);
2222  used = (int) strlcpy(copy, *src, len + 1);
2223 
2224  if (S_FM(node->suffix) || is_next_separator(node))
2225  {
2226  /*
2227  * This node is in Fill Mode, or the next node is known to be a
2228  * non-digit value, so we just slurp as many characters as we can get.
2229  */
2230  errno = 0;
2231  result = strtol(init, src, 10);
2232  }
2233  else
2234  {
2235  /*
2236  * We need to pull exactly the number of characters given in 'len' out
2237  * of the string, and convert those.
2238  */
2239  char *last;
2240 
2241  if (used < len)
2242  ereport(ERROR,
2243  (errcode(ERRCODE_INVALID_DATETIME_FORMAT),
2244  errmsg("source string too short for \"%s\" formatting field",
2245  node->key->name),
2246  errdetail("Field requires %d characters, but only %d "
2247  "remain.",
2248  len, used),
2249  errhint("If your source string is not fixed-width, try "
2250  "using the \"FM\" modifier.")));
2251 
2252  errno = 0;
2253  result = strtol(copy, &last, 10);
2254  used = last - copy;
2255 
2256  if (used > 0 && used < len)
2257  ereport(ERROR,
2258  (errcode(ERRCODE_INVALID_DATETIME_FORMAT),
2259  errmsg("invalid value \"%s\" for \"%s\"",
2260  copy, node->key->name),
2261  errdetail("Field requires %d characters, but only %d "
2262  "could be parsed.", len, used),
2263  errhint("If your source string is not fixed-width, try "
2264  "using the \"FM\" modifier.")));
2265 
2266  *src += used;
2267  }
2268 
2269  if (*src == init)
2270  ereport(ERROR,
2271  (errcode(ERRCODE_INVALID_DATETIME_FORMAT),
2272  errmsg("invalid value \"%s\" for \"%s\"",
2273  copy, node->key->name),
2274  errdetail("Value must be an integer.")));
2275 
2276  if (errno == ERANGE || result < INT_MIN || result > INT_MAX)
2277  ereport(ERROR,
2278  (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
2279  errmsg("value for \"%s\" in source string is out of range",
2280  node->key->name),
2281  errdetail("Value must be in the range %d to %d.",
2282  INT_MIN, INT_MAX)));
2283 
2284  if (dest != NULL)
2285  from_char_set_int(dest, (int) result, node);
2286  return *src - init;
2287 }
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:2176
int errcode(int sqlerrcode)
Definition: elog.c:575
return result
Definition: formatting.c:1633
#define ERROR
Definition: elog.h:43
static bool is_next_separator(FormatNode *n)
Definition: formatting.c:2079
#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:2135
#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 2387 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().

2389 {
2390  int len;
2391 
2392  *dest = seq_search(*src, array, type, max, &len);
2393  if (len <= 0)
2394  {
2395  char copy[DCH_MAX_ITEM_SIZ + 1];
2396 
2397  Assert(max <= DCH_MAX_ITEM_SIZ);
2398  strlcpy(copy, *src, max + 1);
2399 
2400  ereport(ERROR,
2401  (errcode(ERRCODE_INVALID_DATETIME_FORMAT),
2402  errmsg("invalid value \"%s\" for \"%s\"",
2403  copy, node->key->name),
2404  errdetail("The given value did not match any of the allowed "
2405  "values for this field.")));
2406  }
2407  *src += len;
2408  return len;
2409 }
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:2309
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 2176 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().

2177 {
2178  if (*dest != 0 && *dest != value)
2179  ereport(ERROR,
2180  (errcode(ERRCODE_INVALID_DATETIME_FORMAT),
2181  errmsg("conflicting values for \"%s\" field in formatting string",
2182  node->key->name),
2183  errdetail("This value contradicts a previous setting for "
2184  "the same field type.")));
2185  *dest = value;
2186 }
const KeyWord * key
Definition: formatting.c:168
int errcode(int sqlerrcode)
Definition: elog.c:575
#define ERROR
Definition: elog.h:43
static struct @121 value
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 void from_char_set_mode ( TmFromChar tmfc,
const FromCharDateMode  mode 
)
static

Definition at line 2154 of file formatting.c.

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

Referenced by DCH_from_char().

2155 {
2156  if (mode != FROM_CHAR_DATE_NONE)
2157  {
2158  if (tmfc->mode == FROM_CHAR_DATE_NONE)
2159  tmfc->mode = mode;
2160  else if (tmfc->mode != mode)
2161  ereport(ERROR,
2162  (errcode(ERRCODE_INVALID_DATETIME_FORMAT),
2163  errmsg("invalid combination of date conventions"),
2164  errhint("Do not mix Gregorian and ISO week date "
2165  "conventions in a formatting template.")));
2166  }
2167 }
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 4218 of file formatting.c.

References elog, NULL, and result.

Referenced by NUM_processor().

4219 {
4220  char *result,
4221  *p = strchr(num, '.');
4222 
4223 #ifdef DEBUG_TO_FROM_CHAR
4224  elog(DEBUG_elog_output, "get_last_relevant_decnum()");
4225 #endif
4226 
4227  if (!p)
4228  return NULL;
4229 
4230  result = p;
4231 
4232  while (*(++p))
4233  {
4234  if (*p != '0')
4235  result = p;
4236  }
4237 
4238  return result;
4239 }
return result
Definition: formatting.c:1633
#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:89
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 5286 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.

5287 {
5289  text *fmt = PG_GETARG_TEXT_PP(1);
5290  NUMDesc Num;
5291  FormatNode *format;
5292  text *result;
5293  bool shouldFree;
5294  int out_pre_spaces = 0,
5295  sign = 0;
5296  char *numstr,
5297  *orgnum;
5298 
5300 
5301  /*
5302  * On DateType depend part (int32)
5303  */
5304  if (IS_ROMAN(&Num))
5305  numstr = orgnum = int_to_roman(value);
5306  else if (IS_EEEE(&Num))
5307  {
5308  /* we can do it easily because float8 won't lose any precision */
5309  float8 val = (float8) value;
5310 
5311  orgnum = (char *) palloc(MAXDOUBLEWIDTH + 1);
5312  snprintf(orgnum, MAXDOUBLEWIDTH + 1, "%+.*e", Num.post, val);
5313 
5314  /*
5315  * Swap a leading positive sign for a space.
5316  */
5317  if (*orgnum == '+')
5318  *orgnum = ' ';
5319 
5320  numstr = orgnum;
5321  }
5322  else
5323  {
5324  int numstr_pre_len;
5325 
5326  if (IS_MULTI(&Num))
5327  {
5329  Int32GetDatum(value * ((int32) pow((double) 10, (double) Num.multi)))));
5330  Num.pre += Num.multi;
5331  }
5332  else
5333  {
5335  Int32GetDatum(value)));
5336  }
5337 
5338  if (*orgnum == '-')
5339  {
5340  sign = '-';
5341  orgnum++;
5342  }
5343  else
5344  sign = '+';
5345 
5346  numstr_pre_len = strlen(orgnum);
5347 
5348  /* post-decimal digits? Pad out with zeros. */
5349  if (Num.post)
5350  {
5351  numstr = (char *) palloc(numstr_pre_len + Num.post + 2);
5352  strcpy(numstr, orgnum);
5353  *(numstr + numstr_pre_len) = '.';
5354  memset(numstr + numstr_pre_len + 1, '0', Num.post);
5355  *(numstr + numstr_pre_len + Num.post + 1) = '\0';
5356  }
5357  else
5358  numstr = orgnum;
5359 
5360  /* needs padding? */
5361  if (numstr_pre_len < Num.pre)
5362  out_pre_spaces = Num.pre - numstr_pre_len;
5363  /* overflowed prefix digit format? */
5364  else if (numstr_pre_len > Num.pre)
5365  {
5366  numstr = (char *) palloc(Num.pre + Num.post + 2);
5367  fill_str(numstr, '#', Num.pre + Num.post + 1);
5368  *(numstr + Num.pre) = '.';
5369  }
5370  }
5371 
5373  PG_RETURN_TEXT_P(result);
5374 }
#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:1633
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:5078
#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:5065
double float8
Definition: c.h:381
#define DatumGetCString(X)
Definition: postgres.h:572
#define IS_MULTI(_f)
Definition: formatting.c:356
static struct @121 value
char sign
Definition: informix.c:693
static char * fill_str(char *str, int c, int max)
Definition: formatting.c:3889
static char * int_to_roman(int number)
Definition: formatting.c:4087
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
long val
Definition: informix.c:689
Datum int8_to_char ( PG_FUNCTION_ARGS  )

Definition at line 5381 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.

5382 {
5383  int64 value = PG_GETARG_INT64(0);
5384  text *fmt = PG_GETARG_TEXT_PP(1);
5385  NUMDesc Num;
5386  FormatNode *format;
5387  text *result;
5388  bool shouldFree;
5389  int out_pre_spaces = 0,
5390  sign = 0;
5391  char *numstr,
5392  *orgnum;
5393 
5395 
5396  /*
5397  * On DateType depend part (int32)
5398  */
5399  if (IS_ROMAN(&Num))
5400  {
5401  /* Currently don't support int8 conversion to roman... */
5402  numstr = orgnum = int_to_roman(DatumGetInt32(
5404  }
5405  else if (IS_EEEE(&Num))
5406  {
5407  /* to avoid loss of precision, must go via numeric not float8 */
5408  Numeric val;
5409 
5411  Int64GetDatum(value)));
5412  orgnum = numeric_out_sci(val, Num.post);
5413 
5414  /*
5415  * numeric_out_sci() does not emit a sign for positive numbers. We
5416  * need to add a space in this case so that positive and negative
5417  * numbers are aligned. We don't have to worry about NaN here.
5418  */
5419  if (*orgnum != '-')
5420  {
5421  numstr = (char *) palloc(strlen(orgnum) + 2);
5422  *numstr = ' ';
5423  strcpy(numstr + 1, orgnum);
5424  }
5425  else
5426  {
5427  numstr = orgnum;
5428  }
5429  }
5430  else
5431  {
5432  int numstr_pre_len;
5433 
5434  if (IS_MULTI(&Num))
5435  {
5436  double multi = pow((double) 10, (double) Num.multi);
5437 
5439  Int64GetDatum(value),
5441  Float8GetDatum(multi))));
5442  Num.pre += Num.multi;
5443  }
5444 
5446  Int64GetDatum(value)));
5447 
5448  if (*orgnum == '-')
5449  {
5450  sign = '-';
5451  orgnum++;
5452  }
5453  else
5454  sign = '+';
5455 
5456  numstr_pre_len = strlen(orgnum);
5457 
5458  /* post-decimal digits? Pad out with zeros. */
5459  if (Num.post)
5460  {
5461  numstr = (char *) palloc(numstr_pre_len + Num.post + 2);
5462  strcpy(numstr, orgnum);
5463  *(numstr + numstr_pre_len) = '.';
5464  memset(numstr + numstr_pre_len + 1, '0', Num.post);
5465  *(numstr + numstr_pre_len + Num.post + 1) = '\0';
5466  }
5467  else
5468  numstr = orgnum;
5469 
5470  /* needs padding? */
5471  if (numstr_pre_len < Num.pre)
5472  out_pre_spaces = Num.pre - numstr_pre_len;
5473  /* overflowed prefix digit format? */
5474  else if (numstr_pre_len > Num.pre)
5475  {
5476  numstr = (char *) palloc(Num.pre + Num.post + 2);
5477  fill_str(numstr, '#', Num.pre + Num.post + 1);
5478  *(numstr + Num.pre) = '.';
5479  }
5480  }
5481 
5483  PG_RETURN_TEXT_P(result);
5484 }
#define DatumGetInt32(X)
Definition: postgres.h:478
#define IS_EEEE(_f)
Definition: formatting.c:357
return result
Definition: formatting.c:1633
#define DirectFunctionCall1(func, arg1)
Definition: fmgr.h:584
#define NUM_TOCHAR_finish
Definition: formatting.c:5078
#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:5065
#define DatumGetCString(X)
Definition: postgres.h:572
#define IS_MULTI(_f)
Definition: formatting.c:356
static struct @121 value
#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:3889
Datum int8mul(PG_FUNCTION_ARGS)
Definition: int8.c:555
static char * int_to_roman(int number)
Definition: formatting.c:4087
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
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 4087 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().

4088 {
4089  int len = 0,
4090  num = 0;
4091  char *p = NULL,
4092  *result,
4093  numstr[5];
4094 
4095  result = (char *) palloc(16);
4096  *result = '\0';
4097 
4098  if (number > 3999 || number < 1)
4099  {
4100  fill_str(result, '#', 15);
4101  return result;
4102  }
4103  len = snprintf(numstr, sizeof(numstr), "%d", number);
4104 
4105  for (p = numstr; *p != '\0'; p++, --len)
4106  {
4107  num = *p - 49; /* 48 ascii + 1 */
4108  if (num < 0)
4109  continue;
4110 
4111  if (len > 3)
4112  {
4113  while (num-- != -1)
4114  strcat(result, "M");
4115  }
4116  else
4117  {
4118  if (len == 3)
4119  strcat(result, rm100[num]);
4120  else if (len == 2)
4121  strcat(result, rm10[num]);
4122  else if (len == 1)
4123  strcat(result, rm1[num]);
4124  }
4125  }
4126  return result;
4127 }
return result
Definition: formatting.c:1633
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:3889
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 3510 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.

3511 {
3512  Interval *it = PG_GETARG_INTERVAL_P(0);
3513  text *fmt = PG_GETARG_TEXT_PP(1),
3514  *res;
3515  TmToChar tmtc;
3516  struct pg_tm *tm;
3517 
3518  if (VARSIZE_ANY_EXHDR(fmt) <= 0)
3519  PG_RETURN_NULL();
3520 
3521  ZERO_tmtc(&tmtc);
3522  tm = tmtcTm(&tmtc);
3523 
3524  if (interval2tm(*it, tm, &tmtcFsec(&tmtc)) != 0)
3525  PG_RETURN_NULL();
3526 
3527  /* wday is meaningless, yday approximates the total span in days */
3528  tm->tm_yday = (tm->tm_year * MONTHS_PER_YEAR + tm->tm_mon) * DAYS_PER_MONTH + tm->tm_mday;
3529 
3530  if (!(res = datetime_to_char_body(&tmtc, fmt, true, PG_GET_COLLATION())))
3531  PG_RETURN_NULL();
3532 
3533  PG_RETURN_TEXT_P(res);
3534 }
#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:3373
#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:111
#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 2079 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().

2080 {
2081  if (n->type == NODE_TYPE_END)
2082  return FALSE;
2083 
2084  if (n->type == NODE_TYPE_ACTION && S_THth(n->suffix))
2085  return TRUE;
2086 
2087  /*
2088  * Next node
2089  */
2090  n++;
2091 
2092  /* end of format string is treated like a non-digit separator */
2093  if (n->type == NODE_TYPE_END)
2094  return TRUE;
2095 
2096  if (n->type == NODE_TYPE_ACTION)
2097  {
2098  if (n->key->is_digit)
2099  return FALSE;
2100 
2101  return TRUE;
2102  }
2103  else if (isdigit((unsigned char) n->character))
2104  return FALSE;
2105 
2106  return TRUE; /* some non-digit input (separator) */
2107 }
#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 4029 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().

4030 {
4031  FormatNode *format = NULL;
4032  char *str;
4033 
4034  str = text_to_cstring(pars_str);
4035 
4036  if (len > NUM_CACHE_SIZE)
4037  {
4038  /*
4039  * Allocate new memory if format picture is bigger than static cache
4040  * and do not use cache (call parser always)
4041  */
4042  format = (FormatNode *) palloc((len + 1) * sizeof(FormatNode));
4043 
4044  *shouldFree = true;
4045 
4046  zeroize_NUM(Num);
4047 
4048  parse_format(format, str, NUM_keywords,
4049  NULL, NUM_index, NUM_TYPE, Num);
4050  }
4051  else
4052  {
4053  /*
4054  * Use cache buffers
4055  */
4056  NUMCacheEntry *ent = NUM_cache_fetch(str);
4057 
4058  *shouldFree = false;
4059 
4060  format = ent->format;
4061 
4062  /*
4063  * Copy cache to used struct
4064  */
4065  Num->flag = ent->Num.flag;
4066  Num->lsign = ent->Num.lsign;
4067  Num->pre = ent->Num.pre;
4068  Num->post = ent->Num.post;
4069  Num->pre_lsign_num = ent->Num.pre_lsign_num;
4070  Num->need_locale = ent->Num.need_locale;
4071  Num->multi = ent->Num.multi;
4072  Num->zero_start = ent->Num.zero_start;
4073  Num->zero_end = ent->Num.zero_end;
4074  }
4075 
4076 #ifdef DEBUG_TO_FROM_CHAR
4077  /* dump_node(format, len); */
4078  dump_index(NUM_keywords, NUM_index);
4079 #endif
4080 
4081  pfree(str);
4082  return format;
4083 }
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:4001
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:3896
static NUMCacheEntry * NUM_cache_fetch ( const char *  str)
static

Definition at line 4001 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().

4002 {
4003  NUMCacheEntry *ent;
4004 
4005  if ((ent = NUM_cache_search(str)) == NULL)
4006  {
4007  /*
4008  * Not in the cache, must run parser and save a new format-picture to
4009  * the cache. Do not mark the cache entry valid until parsing
4010  * succeeds.
4011  */
4012  ent = NUM_cache_getnew(str);
4013 
4014  zeroize_NUM(&ent->Num);
4015 
4016  parse_format(ent->format, str, NUM_keywords,
4017  NULL, NUM_index, NUM_TYPE, &ent->Num);
4018 
4019  ent->valid = true;
4020  }
4021  return ent;
4022 }
static NUMCacheEntry * NUM_cache_getnew(const char *str)
Definition: formatting.c:3911
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:3973
#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:3896
static NUMCacheEntry * NUM_cache_getnew ( const char *  str)
static