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

Definition at line 5075 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:1632
static FormatNode * NUM_cache(int len, NUMDesc *Num, text *pars_str, bool *shouldFree)
Definition: formatting.c:4026
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 5062 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 2028 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 3893 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 2108 of file formatting.c.

Referenced by DCH_from_char().

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

Definition at line 1959 of file formatting.c.

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

Referenced by str_initcap().

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

Definition at line 1913 of file formatting.c.

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

Referenced by asc_tolower_z(), and str_tolower().

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

Definition at line 2008 of file formatting.c.

References asc_tolower().

Referenced by DCH_to_char(), and NUM_processor().

2009 {
2010  return asc_tolower(buff, strlen(buff));
2011 }
char * asc_tolower(const char *buff, size_t nbytes)
Definition: formatting.c:1913
char* asc_toupper ( const char *  buff,
size_t  nbytes 
)

Definition at line 1936 of file formatting.c.

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

Referenced by asc_toupper_z(), and str_toupper().

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

Definition at line 2014 of file formatting.c.

References asc_toupper().

Referenced by DCH_to_char().

2015 {
2016  return asc_toupper(buff, strlen(buff));
2017 }
char * asc_toupper(const char *buff, size_t nbytes)
Definition: formatting.c:1936
static text* datetime_to_char_body ( TmToChar tmtc,
text fmt,
bool  is_interval,
Oid  collid 
)
static

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

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

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

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

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

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

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

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

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

3887 {
3888  memset(str, c, max);
3889  *(str + max) = '\0';
3890  return str;
3891 }
char * c
Datum float4_to_char ( PG_FUNCTION_ARGS  )

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

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

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

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

Definition at line 2296 of file formatting.c.

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

Referenced by DCH_from_char().

2297 {
2298  return from_char_parse_int_len(dest, src, node->key->len, node);
2299 }
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:2206
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 2206 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().

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

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

2174 {
2175  if (*dest != 0 && *dest != value)
2176  ereport(ERROR,
2177  (errcode(ERRCODE_INVALID_DATETIME_FORMAT),
2178  errmsg("conflicting values for \"%s\" field in formatting string",
2179  node->key->name),
2180  errdetail("This value contradicts a previous setting for "
2181  "the same field type.")));
2182  *dest = value;
2183 }
const KeyWord * key
Definition: formatting.c:168
int errcode(int sqlerrcode)
Definition: elog.c:575
#define ERROR
Definition: elog.h:43
int errdetail(const char *fmt,...)
Definition: elog.c:873
#define ereport(elevel, rest)
Definition: elog.h:122
const char * name
Definition: formatting.c:158
int errmsg(const char *fmt,...)
Definition: elog.c:797
static struct @121 value
static void from_char_set_mode ( TmFromChar tmfc,
const FromCharDateMode  mode 
)
static

Definition at line 2151 of file formatting.c.

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

Referenced by DCH_from_char().

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

References elog, NULL, and result.

Referenced by NUM_processor().

4216 {
4217  char *result,
4218  *p = strchr(num, '.');
4219 
4220 #ifdef DEBUG_TO_FROM_CHAR
4221  elog(DEBUG_elog_output, "get_last_relevant_decnum()");
4222 #endif
4223 
4224  if (!p)
4225  return NULL;
4226 
4227  result = p;
4228 
4229  while (*(++p))
4230  {
4231  if (*p != '0')
4232  result = p;
4233  }
4234 
4235  return result;
4236 }
return result
Definition: formatting.c:1632
#define NULL
Definition: c.h:229
#define elog
Definition: elog.h:219
static const char * get_th ( char *  num,
int  type 
)
static

Definition at line 1392 of file formatting.c.

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

Referenced by NUM_processor(), and str_numth().

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

Definition at line 1008 of file formatting.c.

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

Referenced by parse_format().

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

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

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

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

5379 {
5380  int64 value = PG_GETARG_INT64(0);
5381  text *fmt = PG_GETARG_TEXT_PP(1);
5382  NUMDesc Num;
5383  FormatNode *format;
5384  text *result;
5385  bool shouldFree;
5386  int out_pre_spaces = 0,
5387  sign = 0;
5388  char *numstr,
5389  *orgnum;
5390 
5392 
5393  /*
5394  * On DateType depend part (int32)
5395  */
5396  if (IS_ROMAN(&Num))
5397  {
5398  /* Currently don't support int8 conversion to roman... */
5399  numstr = orgnum = int_to_roman(DatumGetInt32(
5401  }
5402  else if (IS_EEEE(&Num))
5403  {
5404  /* to avoid loss of precision, must go via numeric not float8 */
5405  Numeric val;
5406 
5408  Int64GetDatum(value)));
5409  orgnum = numeric_out_sci(val, Num.post);
5410 
5411  /*
5412  * numeric_out_sci() does not emit a sign for positive numbers. We
5413  * need to add a space in this case so that positive and negative
5414  * numbers are aligned. We don't have to worry about NaN here.
5415  */
5416  if (*orgnum != '-')
5417  {
5418  numstr = (char *) palloc(strlen(orgnum) + 2);
5419  *numstr = ' ';
5420  strcpy(numstr + 1, orgnum);
5421  }
5422  else
5423  {
5424  numstr = orgnum;
5425  }
5426  }
5427  else
5428  {
5429  int numstr_pre_len;
5430 
5431  if (IS_MULTI(&Num))
5432  {
5433  double multi = pow((double) 10, (double) Num.multi);
5434 
5436  Int64GetDatum(value),
5438  Float8GetDatum(multi))));
5439  Num.pre += Num.multi;
5440  }
5441 
5443  Int64GetDatum(value)));
5444 
5445  if (*orgnum == '-')
5446  {
5447  sign = '-';
5448  orgnum++;
5449  }
5450  else
5451  sign = '+';
5452 
5453  numstr_pre_len = strlen(orgnum);
5454 
5455  /* post-decimal digits? Pad out with zeros. */
5456  if (Num.post)
5457  {
5458  numstr = (char *) palloc(numstr_pre_len + Num.post + 2);
5459  strcpy(numstr, orgnum);
5460  *(numstr + numstr_pre_len) = '.';
5461  memset(numstr + numstr_pre_len + 1, '0', Num.post);
5462  *(numstr + numstr_pre_len + Num.post + 1) = '\0';
5463  }
5464  else
5465  numstr = orgnum;
5466 
5467  /* needs padding? */
5468  if (numstr_pre_len < Num.pre)
5469  out_pre_spaces = Num.pre - numstr_pre_len;
5470  /* overflowed prefix digit format? */
5471  else if (numstr_pre_len > Num.pre)
5472  {
5473  numstr = (char *) palloc(Num.pre + Num.post + 2);
5474  fill_str(numstr, '#', Num.pre + Num.post + 1);
5475  *(numstr + Num.pre) = '.';
5476  }
5477  }
5478 
5480  PG_RETURN_TEXT_P(result);
5481 }
#define DatumGetInt32(X)
Definition: postgres.h:478
#define IS_EEEE(_f)
Definition: formatting.c:357
return result
Definition: formatting.c:1632
#define DirectFunctionCall1(func, arg1)
Definition: fmgr.h:584
#define NUM_TOCHAR_finish
Definition: formatting.c:5075
#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:5062
#define DatumGetCString(X)
Definition: postgres.h:572
#define IS_MULTI(_f)
Definition: formatting.c:356
#define DatumGetInt64(X)
Definition: postgres.h:613
char sign
Definition: informix.c:693
static char * fill_str(char *str, int c, int max)
Definition: formatting.c:3886
Datum int8mul(PG_FUNCTION_ARGS)
Definition: int8.c:555
static char * int_to_roman(int number)
Definition: formatting.c:4084
Datum dtoi8(PG_FUNCTION_ARGS)
Definition: int8.c:1343
int pre
Definition: formatting.c:308
Datum Int64GetDatum(int64 X)
Definition: fmgr.c:1791
#define PG_RETURN_TEXT_P(x)
Definition: fmgr.h:330
int post
Definition: formatting.c:308
#define DatumGetNumeric(X)
Definition: numeric.h:49
Datum int84(PG_FUNCTION_ARGS)
Definition: int8.c:1287
void * palloc(Size size)
Definition: mcxt.c:849
int multi
Definition: formatting.c:308
Definition: c.h:439
static char format
Definition: pg_basebackup.c:80
static struct @121 value
char * numeric_out_sci(Numeric num, int scale)
Definition: numeric.c:719
#define PG_GETARG_INT64(n)
Definition: fmgr.h:247
long val
Definition: informix.c:689
Datum int8out(PG_FUNCTION_ARGS)
Definition: int8.c:158
#define DirectFunctionCall2(func, arg1, arg2)
Definition: fmgr.h:586
static char * int_to_roman ( int  number)
static

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

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

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

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

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

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

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