PostgreSQL Source Code  git master
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 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)   (Np->inout_p <= Np->inout + (input_len - (s)))
 
#define IS_PREDEC_SPACE(_n)
 
#define NUM_TOCHAR_prepare
 
#define NUM_TOCHAR_finish
 

Typedefs

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_TZH, DCH_TZM, 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 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)
 
static void NUM_eat_non_data_chars (NUMProc *Np, int n, int input_len)
 
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]
 

Macro Definition Documentation

◆ A_D_STR

#define A_D_STR   "A.D."

Definition at line 197 of file formatting.c.

Referenced by DCH_to_char().

◆ a_d_STR

#define a_d_STR   "a.d."

Definition at line 198 of file formatting.c.

Referenced by DCH_to_char().

◆ A_M_STR

#define A_M_STR   "A.M."

Definition at line 224 of file formatting.c.

Referenced by DCH_to_char().

◆ a_m_STR

#define a_m_STR   "a.m."

Definition at line 225 of file formatting.c.

Referenced by DCH_to_char().

◆ AD_STR

#define AD_STR   "AD"

Definition at line 199 of file formatting.c.

Referenced by DCH_to_char().

◆ ad_STR

#define ad_STR   "ad"

Definition at line 200 of file formatting.c.

Referenced by DCH_to_char().

◆ ADJUST_YEAR

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

Definition at line 195 of file formatting.c.

Referenced by DCH_to_char().

◆ ALL_LOWER

#define ALL_LOWER   3 /* name */

Definition at line 280 of file formatting.c.

Referenced by DCH_from_char(), and seq_search().

◆ ALL_UPPER

#define ALL_UPPER   2 /* NAME */

Definition at line 279 of file formatting.c.

Referenced by DCH_from_char(), and seq_search().

◆ AM_STR

#define AM_STR   "AM"

Definition at line 226 of file formatting.c.

Referenced by DCH_to_char().

◆ am_STR

#define am_STR   "am"

Definition at line 227 of file formatting.c.

Referenced by DCH_to_char().

◆ AMOUNT_TEST

#define AMOUNT_TEST (   s)    (Np->inout_p <= Np->inout + (input_len - (s)))

Definition at line 4283 of file formatting.c.

Referenced by NUM_numpart_from_char(), and NUM_processor().

◆ B_C_STR

#define B_C_STR   "B.C."

Definition at line 202 of file formatting.c.

Referenced by DCH_to_char().

◆ b_c_STR

#define b_c_STR   "b.c."

Definition at line 203 of file formatting.c.

Referenced by DCH_to_char().

◆ BC_STR

#define BC_STR   "BC"

Definition at line 204 of file formatting.c.

Referenced by DCH_to_char().

◆ bc_STR

#define bc_STR   "bc"

Definition at line 205 of file formatting.c.

Referenced by DCH_to_char().

◆ CLOCK_12_HOUR

#define CLOCK_12_HOUR   1

Definition at line 172 of file formatting.c.

Referenced by DCH_from_char(), and do_to_timestamp().

◆ CLOCK_24_HOUR

#define CLOCK_24_HOUR   0

Definition at line 171 of file formatting.c.

◆ DCH_CACHE_ENTRIES

#define DCH_CACHE_ENTRIES   20

Definition at line 366 of file formatting.c.

Referenced by DCH_cache_getnew(), and DCH_cache_search().

◆ DCH_CACHE_SIZE

#define DCH_CACHE_SIZE   128

Definition at line 365 of file formatting.c.

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

◆ DCH_MAX_ITEM_SIZ

#define DCH_MAX_ITEM_SIZ   12 /* max localized day name */

◆ DCH_S_FM

#define DCH_S_FM   0x01

Definition at line 500 of file formatting.c.

◆ DCH_S_SP

#define DCH_S_SP   0x08

Definition at line 503 of file formatting.c.

◆ DCH_S_TH

#define DCH_S_TH   0x02

Definition at line 501 of file formatting.c.

◆ DCH_S_th

#define DCH_S_th   0x04

Definition at line 502 of file formatting.c.

◆ DCH_S_TM

#define DCH_S_TM   0x10

Definition at line 504 of file formatting.c.

◆ DCH_TYPE

#define DCH_TYPE   1 /* DATE-TIME version */

◆ DEBUG_TM

#define DEBUG_TM (   _X)

Definition at line 446 of file formatting.c.

Referenced by do_to_timestamp().

◆ DEBUG_TMFC

#define DEBUG_TMFC (   _X)

Definition at line 445 of file formatting.c.

Referenced by do_to_timestamp().

◆ FULL_SIZ

#define FULL_SIZ   0

Definition at line 282 of file formatting.c.

◆ INVALID_FOR_INTERVAL

#define INVALID_FOR_INTERVAL
Value:
do { \
if (is_interval) \
ereport(ERROR, \
(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
int errmsg(const char *fmt,...)
Definition: elog.c:797

Definition at line 483 of file formatting.c.

Referenced by DCH_to_char().

◆ IS_BLANK

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

Definition at line 340 of file formatting.c.

Referenced by NUMDesc_prepare().

◆ IS_BRACKET

#define IS_BRACKET (   _f)    ((_f)->flag & NUM_F_BRACKET)

◆ IS_DECIMAL

#define IS_DECIMAL (   _f)    ((_f)->flag & NUM_F_DECIMAL)

◆ IS_EEEE

#define IS_EEEE (   _f)    ((_f)->flag & NUM_F_EEEE)

◆ IS_FILLMODE

#define IS_FILLMODE (   _f)    ((_f)->flag & NUM_F_FILLMODE)

Definition at line 341 of file formatting.c.

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

◆ IS_LDECIMAL

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

Definition at line 338 of file formatting.c.

Referenced by NUM_prepare_locale().

◆ IS_LSIGN

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

◆ IS_MINUS

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

Definition at line 343 of file formatting.c.

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

◆ IS_MULTI

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

◆ IS_PLUS

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

Definition at line 345 of file formatting.c.

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

◆ IS_PREDEC_SPACE

#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:339

Definition at line 4494 of file formatting.c.

Referenced by NUM_numpart_to_char().

◆ IS_ROMAN

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

◆ IS_ZERO

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

Definition at line 339 of file formatting.c.

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

◆ KeyWord_INDEX_FILTER

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

Definition at line 111 of file formatting.c.

Referenced by index_seq_search().

◆ KeyWord_INDEX_SIZE

#define KeyWord_INDEX_SIZE   ('~' - ' ')

Definition at line 110 of file formatting.c.

◆ MAX_DAY_LEN

#define MAX_DAY_LEN   9

Definition at line 286 of file formatting.c.

Referenced by DCH_from_char().

◆ MAX_DY_LEN

#define MAX_DY_LEN   3

Definition at line 287 of file formatting.c.

Referenced by DCH_from_char().

◆ MAX_MON_LEN

#define MAX_MON_LEN   3

Definition at line 285 of file formatting.c.

Referenced by DCH_from_char().

◆ MAX_MONTH_LEN

#define MAX_MONTH_LEN   9

Definition at line 284 of file formatting.c.

Referenced by DCH_from_char().

◆ MAX_RM_LEN

#define MAX_RM_LEN   4

Definition at line 288 of file formatting.c.

Referenced by DCH_from_char().

◆ NODE_TYPE_ACTION

#define NODE_TYPE_ACTION   2

◆ NODE_TYPE_CHAR

#define NODE_TYPE_CHAR   3

Definition at line 166 of file formatting.c.

Referenced by parse_format().

◆ NODE_TYPE_END

#define NODE_TYPE_END   1

◆ NUM_CACHE_ENTRIES

#define NUM_CACHE_ENTRIES   20

Definition at line 364 of file formatting.c.

Referenced by NUM_cache_getnew(), and NUM_cache_search().

◆ NUM_CACHE_SIZE

#define NUM_CACHE_SIZE   64

Definition at line 363 of file formatting.c.

Referenced by NUM_cache(), and NUM_cache_getnew().

◆ NUM_F_BLANK

#define NUM_F_BLANK   (1 << 4)

Definition at line 317 of file formatting.c.

Referenced by NUMDesc_prepare().

◆ NUM_F_BRACKET

#define NUM_F_BRACKET   (1 << 7)

Definition at line 320 of file formatting.c.

Referenced by NUM_processor(), and NUMDesc_prepare().

◆ NUM_F_DECIMAL

#define NUM_F_DECIMAL   (1 << 1)

Definition at line 314 of file formatting.c.

Referenced by NUMDesc_prepare().

◆ NUM_F_EEEE

#define NUM_F_EEEE   (1 << 14)

Definition at line 327 of file formatting.c.

Referenced by NUMDesc_prepare().

◆ NUM_F_FILLMODE

#define NUM_F_FILLMODE   (1 << 5)

Definition at line 318 of file formatting.c.

Referenced by NUM_processor(), and NUMDesc_prepare().

◆ NUM_F_LDECIMAL

#define NUM_F_LDECIMAL   (1 << 2)

Definition at line 315 of file formatting.c.

Referenced by NUMDesc_prepare().

◆ NUM_F_LSIGN

#define NUM_F_LSIGN   (1 << 6)

Definition at line 319 of file formatting.c.

Referenced by NUMDesc_prepare().

◆ NUM_F_MINUS

#define NUM_F_MINUS   (1 << 8)

Definition at line 321 of file formatting.c.

Referenced by NUM_processor(), and NUMDesc_prepare().

◆ NUM_F_MINUS_POST

#define NUM_F_MINUS_POST   (1 << 13)

Definition at line 326 of file formatting.c.

Referenced by NUMDesc_prepare().

◆ NUM_F_MULTI

#define NUM_F_MULTI   (1 << 11)

Definition at line 324 of file formatting.c.

Referenced by NUMDesc_prepare().

◆ NUM_F_PLUS

#define NUM_F_PLUS   (1 << 9)

Definition at line 322 of file formatting.c.

Referenced by NUM_processor(), and NUMDesc_prepare().

◆ NUM_F_PLUS_POST

#define NUM_F_PLUS_POST   (1 << 12)

Definition at line 325 of file formatting.c.

Referenced by NUMDesc_prepare().

◆ NUM_F_ROMAN

#define NUM_F_ROMAN   (1 << 10)

Definition at line 323 of file formatting.c.

Referenced by NUM_processor(), and NUMDesc_prepare().

◆ NUM_F_ZERO

#define NUM_F_ZERO   (1 << 3)

Definition at line 316 of file formatting.c.

Referenced by NUMDesc_prepare().

◆ NUM_LSIGN_NONE

#define NUM_LSIGN_NONE   0

Definition at line 331 of file formatting.c.

Referenced by NUMDesc_prepare().

◆ NUM_LSIGN_POST

#define NUM_LSIGN_POST   1

Definition at line 330 of file formatting.c.

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

◆ NUM_LSIGN_PRE

#define NUM_LSIGN_PRE   (-1)

◆ NUM_MAX_ITEM_SIZ

#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().

◆ NUM_TOCHAR_finish

#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) \
pfree(format); \
\
/* \
* 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)); \
SET_VARSIZE(result, len + VARHDRSZ); \
} while (0)
#define VARDATA(PTR)
Definition: postgres.h:302
#define VARHDRSZ
Definition: c.h:522
#define PG_GET_COLLATION()
Definition: fmgr.h:168
char sign
Definition: informix.c:693
static char format
Definition: pg_basebackup.c:83
static char * NUM_processor(FormatNode *node, NUMDesc *Num, char *inout, char *number, int input_len, int to_char_out_pre_spaces, int sign, bool is_to_char, Oid collid)
Definition: formatting.c:4708

Definition at line 5202 of file formatting.c.

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

◆ NUM_TOCHAR_prepare

#define NUM_TOCHAR_prepare
Value:
do { \
int len = VARSIZE_ANY_EXHDR(fmt); \
if (len <= 0 || len >= (INT_MAX-VARHDRSZ)/NUM_MAX_ITEM_SIZ) \
PG_RETURN_TEXT_P(cstring_to_text("")); \
result = (text *) palloc0((len * NUM_MAX_ITEM_SIZ) + 1 + VARHDRSZ); \
format = NUM_cache(len, &Num, fmt, &shouldFree); \
} while (0)
#define VARHDRSZ
Definition: c.h:522
static FormatNode * NUM_cache(int len, NUMDesc *Num, text *pars_str, bool *shouldFree)
Definition: formatting.c:4065
void * palloc0(Size size)
Definition: mcxt.c:955
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:341
Definition: c.h:516

Definition at line 5189 of file formatting.c.

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

◆ NUM_TYPE

#define NUM_TYPE   2 /* NUMBER version */

Definition at line 104 of file formatting.c.

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

◆ ONE_UPPER

#define ONE_UPPER   1 /* Name */

Definition at line 278 of file formatting.c.

Referenced by DCH_from_char(), and seq_search().

◆ OVERLOAD_TEST

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

Definition at line 4282 of file formatting.c.

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

◆ P_M_STR

#define P_M_STR   "P.M."

Definition at line 229 of file formatting.c.

Referenced by DCH_to_char().

◆ p_m_STR

#define p_m_STR   "p.m."

Definition at line 230 of file formatting.c.

Referenced by DCH_to_char().

◆ PM_STR

#define PM_STR   "PM"

Definition at line 231 of file formatting.c.

Referenced by DCH_to_char().

◆ pm_STR

#define pm_STR   "pm"

Definition at line 232 of file formatting.c.

Referenced by DCH_to_char().

◆ S_FM

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

Definition at line 516 of file formatting.c.

Referenced by DCH_to_char(), and from_char_parse_int_len().

◆ S_SP

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

Definition at line 517 of file formatting.c.

◆ S_TH

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

Definition at line 511 of file formatting.c.

◆ S_th

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

Definition at line 512 of file formatting.c.

◆ S_TH_TYPE

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

Definition at line 513 of file formatting.c.

Referenced by DCH_to_char().

◆ S_THth

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

Definition at line 510 of file formatting.c.

Referenced by DCH_to_char(), and is_next_separator().

◆ S_TM

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

Definition at line 518 of file formatting.c.

Referenced by DCH_to_char().

◆ SKIP_THth

#define SKIP_THth (   ptr,
  _suf 
)
Value:
do { \
if (S_THth(_suf)) \
{ \
if (*(ptr)) (ptr) += pg_mblen(ptr); \
if (*(ptr)) (ptr) += pg_mblen(ptr); \
} \
} while (0)
#define S_THth(_s)
Definition: formatting.c:510
int pg_mblen(const char *mbstr)
Definition: mbutils.c:760

Definition at line 2015 of file formatting.c.

Referenced by DCH_from_char().

◆ SUFFTYPE_POSTFIX

#define SUFFTYPE_POSTFIX   2

Definition at line 169 of file formatting.c.

Referenced by parse_format().

◆ SUFFTYPE_PREFIX

#define SUFFTYPE_PREFIX   1

Definition at line 168 of file formatting.c.

Referenced by parse_format().

◆ TH_LOWER

#define TH_LOWER   2

Definition at line 291 of file formatting.c.

Referenced by NUM_processor().

◆ TH_UPPER

#define TH_UPPER   1

Definition at line 290 of file formatting.c.

Referenced by get_th(), and NUM_processor().

◆ TM_SUFFIX_LEN

#define TM_SUFFIX_LEN   2

Definition at line 524 of file formatting.c.

Referenced by DCH_to_char().

◆ tmtcFsec

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

Definition at line 462 of file formatting.c.

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

◆ tmtcTm

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

Definition at line 460 of file formatting.c.

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

◆ tmtcTzn

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

Definition at line 461 of file formatting.c.

Referenced by DCH_to_char(), and timestamptz_to_char().

◆ ZERO_tm

#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; \
(_X)->tm_zone = NULL; \
} while(0)

Definition at line 464 of file formatting.c.

Referenced by do_to_timestamp().

◆ ZERO_tmfc

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

Definition at line 426 of file formatting.c.

Referenced by do_to_timestamp().

◆ ZERO_tmtc

#define ZERO_tmtc (   _X)
Value:
do { \
ZERO_tm( tmtcTm(_X) ); \
tmtcFsec(_X) = 0; \
tmtcTzn(_X) = NULL; \
} while(0)
#define tmtcTm(_X)
Definition: formatting.c:460

Definition at line 472 of file formatting.c.

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

◆ zeroize_NUM

#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 3932 of file formatting.c.

Referenced by NUM_cache(), and NUM_cache_fetch().

Typedef Documentation

◆ NUMProc

◆ TmToChar

Enumeration Type Documentation

◆ DCH_poz

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_TZH 
DCH_TZM 
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 567 of file formatting.c.

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

◆ FromCharDateMode

Enumerator
FROM_CHAR_DATE_NONE 
FROM_CHAR_DATE_GREGORIAN 
FROM_CHAR_DATE_ISOWEEK 

Definition at line 140 of file formatting.c.

141 {
142  FROM_CHAR_DATE_NONE = 0, /* Value does not affect date mode. */
143  FROM_CHAR_DATE_GREGORIAN, /* Gregorian (day, month, year) style date */
144  FROM_CHAR_DATE_ISOWEEK /* ISO 8601 week date */
FromCharDateMode
Definition: formatting.c:140

◆ NUM_poz

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 669 of file formatting.c.

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

Function Documentation

◆ adjust_partial_year_to_2020()

static int adjust_partial_year_to_2020 ( int  year)
static

Definition at line 2096 of file formatting.c.

Referenced by DCH_from_char().

2097 {
2098  /*
2099  * Adjust all dates toward 2020; this is effectively what happens when we
2100  * assume '70' is 1970 and '69' is 2069.
2101  */
2102  /* Force 0-69 into the 2000's */
2103  if (year < 70)
2104  return year + 2000;
2105  /* Force 70-99 into the 1900's */
2106  else if (year < 100)
2107  return year + 1900;
2108  /* Force 100-519 into the 2000's */
2109  else if (year < 520)
2110  return year + 2000;
2111  /* Force 520-999 into the 1000's */
2112  else if (year < 1000)
2113  return year + 1000;
2114  else
2115  return year;
2116 }

◆ asc_initcap()

char* asc_initcap ( const char *  buff,
size_t  nbytes 
)

Definition at line 1946 of file formatting.c.

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

Referenced by str_initcap().

1947 {
1948  char *result;
1949  char *p;
1950  int wasalnum = false;
1951 
1952  if (!buff)
1953  return NULL;
1954 
1955  result = pnstrdup(buff, nbytes);
1956 
1957  for (p = result; *p; p++)
1958  {
1959  char c;
1960 
1961  if (wasalnum)
1962  *p = c = pg_ascii_tolower((unsigned char) *p);
1963  else
1964  *p = c = pg_ascii_toupper((unsigned char) *p);
1965  /* we don't trust isalnum() here */
1966  wasalnum = ((c >= 'A' && c <= 'Z') ||
1967  (c >= 'a' && c <= 'z') ||
1968  (c >= '0' && c <= '9'));
1969  }
1970 
1971  return result;
1972 }
char * pnstrdup(const char *in, Size len)
Definition: mcxt.c:1172
unsigned char pg_ascii_tolower(unsigned char ch)
Definition: pgstrcasecmp.c:146
unsigned char pg_ascii_toupper(unsigned char ch)
Definition: pgstrcasecmp.c:135
char * c

◆ asc_tolower()

char* asc_tolower ( const char *  buff,
size_t  nbytes 
)

Definition at line 1900 of file formatting.c.

References pg_ascii_tolower(), and pnstrdup().

Referenced by asc_tolower_z(), and str_tolower().

1901 {
1902  char *result;
1903  char *p;
1904 
1905  if (!buff)
1906  return NULL;
1907 
1908  result = pnstrdup(buff, nbytes);
1909 
1910  for (p = result; *p; p++)
1911  *p = pg_ascii_tolower((unsigned char) *p);
1912 
1913  return result;
1914 }
char * pnstrdup(const char *in, Size len)
Definition: mcxt.c:1172
unsigned char pg_ascii_tolower(unsigned char ch)
Definition: pgstrcasecmp.c:146

◆ asc_tolower_z()

static char* asc_tolower_z ( const char *  buff)
static

Definition at line 1995 of file formatting.c.

References asc_tolower().

Referenced by DCH_to_char(), and NUM_processor().

1996 {
1997  return asc_tolower(buff, strlen(buff));
1998 }
char * asc_tolower(const char *buff, size_t nbytes)
Definition: formatting.c:1900

◆ asc_toupper()

char* asc_toupper ( const char *  buff,
size_t  nbytes 
)

Definition at line 1923 of file formatting.c.

References pg_ascii_toupper(), and pnstrdup().

Referenced by asc_toupper_z(), and str_toupper().

1924 {
1925  char *result;
1926  char *p;
1927 
1928  if (!buff)
1929  return NULL;
1930 
1931  result = pnstrdup(buff, nbytes);
1932 
1933  for (p = result; *p; p++)
1934  *p = pg_ascii_toupper((unsigned char) *p);
1935 
1936  return result;
1937 }
char * pnstrdup(const char *in, Size len)
Definition: mcxt.c:1172
unsigned char pg_ascii_toupper(unsigned char ch)
Definition: pgstrcasecmp.c:135

◆ asc_toupper_z()

static char* asc_toupper_z ( const char *  buff)
static

Definition at line 2001 of file formatting.c.

References asc_toupper().

Referenced by DCH_to_char().

2002 {
2003  return asc_toupper(buff, strlen(buff));
2004 }
char * asc_toupper(const char *buff, size_t nbytes)
Definition: formatting.c:1923

◆ datetime_to_char_body()

static text* datetime_to_char_body ( TmToChar tmtc,
text fmt,
bool  is_interval,
Oid  collid 
)
static

Definition at line 3385 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, format, DCHCacheEntry::format, palloc(), parse_format(), pfree(), and text_to_cstring().

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

3386 {
3387  FormatNode *format;
3388  char *fmt_str,
3389  *result;
3390  bool incache;
3391  int fmt_len;
3392  text *res;
3393 
3394  /*
3395  * Convert fmt to C string
3396  */
3397  fmt_str = text_to_cstring(fmt);
3398  fmt_len = strlen(fmt_str);
3399 
3400  /*
3401  * Allocate workspace for result as C string
3402  */
3403  result = palloc((fmt_len * DCH_MAX_ITEM_SIZ) + 1);
3404  *result = '\0';
3405 
3406  if (fmt_len > DCH_CACHE_SIZE)
3407  {
3408  /*
3409  * Allocate new memory if format picture is bigger than static cache
3410  * and do not use cache (call parser always)
3411  */
3412  incache = false;
3413 
3414  format = (FormatNode *) palloc((fmt_len + 1) * sizeof(FormatNode));
3415 
3416  parse_format(format, fmt_str, DCH_keywords,
3417  DCH_suff, DCH_index, DCH_TYPE, NULL);
3418  }
3419  else
3420  {
3421  /*
3422  * Use cache buffers
3423  */
3424  DCHCacheEntry *ent = DCH_cache_fetch(fmt_str);
3425 
3426  incache = true;
3427  format = ent->format;
3428  }
3429 
3430  /* The real work is here */
3431  DCH_to_char(format, is_interval, tmtc, result, collid);
3432 
3433  if (!incache)
3434  pfree(format);
3435 
3436  pfree(fmt_str);
3437 
3438  /* convert C-string result to TEXT format */
3439  res = cstring_to_text(result);
3440 
3441  pfree(result);
3442  return res;
3443 }
static const KeySuffix DCH_suff[]
Definition: formatting.c:526
static const int DCH_index[KeyWord_INDEX_SIZE]
Definition: formatting.c:872
void pfree(void *pointer)
Definition: mcxt.c:1031
#define DCH_CACHE_SIZE
Definition: formatting.c:365
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:1226
static void DCH_to_char(FormatNode *node, bool is_interval, TmToChar *in, char *out, Oid collid)
Definition: formatting.c:2402
FormatNode format[DCH_CACHE_SIZE+1]
Definition: formatting.c:370
static DCHCacheEntry * DCH_cache_fetch(const char *str)
Definition: formatting.c:3358
#define DCH_TYPE
Definition: formatting.c:103
text * cstring_to_text(const char *s)
Definition: varlena.c:149
char * text_to_cstring(const text *t)
Definition: varlena.c:182
void * palloc(Size size)
Definition: mcxt.c:924
static const KeyWord DCH_keywords[]
Definition: formatting.c:716
Definition: c.h:516
static char format
Definition: pg_basebackup.c:83
#define DCH_MAX_ITEM_SIZ
Definition: formatting.c:117

◆ DCH_cache_fetch()

static DCHCacheEntry * DCH_cache_fetch ( const char *  str)
static

Definition at line 3358 of file formatting.c.

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

Referenced by datetime_to_char_body(), and do_to_timestamp().

3359 {
3360  DCHCacheEntry *ent;
3361 
3362  if ((ent = DCH_cache_search(str)) == NULL)
3363  {
3364  /*
3365  * Not in the cache, must run parser and save a new format-picture to
3366  * the cache. Do not mark the cache entry valid until parsing
3367  * succeeds.
3368  */
3369  ent = DCH_cache_getnew(str);
3370 
3372  DCH_suff, DCH_index, DCH_TYPE, NULL);
3373 
3374  ent->valid = true;
3375  }
3376  return ent;
3377 }
static DCHCacheEntry * DCH_cache_getnew(const char *str)
Definition: formatting.c:3268
static const KeySuffix DCH_suff[]
Definition: formatting.c:526
static const int DCH_index[KeyWord_INDEX_SIZE]
Definition: formatting.c:872
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:1226
static DCHCacheEntry * DCH_cache_search(const char *str)
Definition: formatting.c:3330
FormatNode format[DCH_CACHE_SIZE+1]
Definition: formatting.c:370
#define DCH_TYPE
Definition: formatting.c:103
static const KeyWord DCH_keywords[]
Definition: formatting.c:716

◆ DCH_cache_getnew()

static DCHCacheEntry * DCH_cache_getnew ( const char *  str)
static

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

3269 {
3270  DCHCacheEntry *ent;
3271 
3272  /* counter overflow check - paranoia? */
3273  if (DCHCounter >= (INT_MAX - DCH_CACHE_ENTRIES))
3274  {
3275  DCHCounter = 0;
3276 
3277  for (ent = DCHCache; ent < (DCHCache + DCH_CACHE_ENTRIES); ent++)
3278  ent->age = (++DCHCounter);
3279  }
3280 
3281  /*
3282  * If cache is full, remove oldest entry (or recycle first not-valid one)
3283  */
3285  {
3286  DCHCacheEntry *old = DCHCache + 0;
3287 
3288 #ifdef DEBUG_TO_FROM_CHAR
3289  elog(DEBUG_elog_output, "cache is full (%d)", n_DCHCache);
3290 #endif
3291  if (old->valid)
3292  {
3293  for (ent = DCHCache + 1; ent < (DCHCache + DCH_CACHE_ENTRIES); ent++)
3294  {
3295  if (!ent->valid)
3296  {
3297  old = ent;
3298  break;
3299  }
3300  if (ent->age < old->age)
3301  old = ent;
3302  }
3303  }
3304 #ifdef DEBUG_TO_FROM_CHAR
3305  elog(DEBUG_elog_output, "OLD: '%s' AGE: %d", old->str, old->age);
3306 #endif
3307  old->valid = false;
3308  StrNCpy(old->str, str, DCH_CACHE_SIZE + 1);
3309  old->age = (++DCHCounter);
3310  /* caller is expected to fill format, then set valid */
3311  return old;
3312  }
3313  else
3314  {
3315 #ifdef DEBUG_TO_FROM_CHAR
3316  elog(DEBUG_elog_output, "NEW (%d)", n_DCHCache);
3317 #endif
3318  ent = DCHCache + n_DCHCache;
3319  ent->valid = false;
3320  StrNCpy(ent->str, str, DCH_CACHE_SIZE + 1);
3321  ent->age = (++DCHCounter);
3322  /* caller is expected to fill format, then set valid */
3323  ++n_DCHCache;
3324  return ent;
3325  }
3326 }
char str[DCH_CACHE_SIZE+1]
Definition: formatting.c:371
#define DCH_CACHE_SIZE
Definition: formatting.c:365
static int n_DCHCache
Definition: formatting.c:387
#define DCH_CACHE_ENTRIES
Definition: formatting.c:366
static DCHCacheEntry DCHCache[DCH_CACHE_ENTRIES]
Definition: formatting.c:386
#define StrNCpy(dst, src, len)
Definition: c.h:881
#define elog
Definition: elog.h:219
static int DCHCounter
Definition: formatting.c:388

◆ DCH_cache_search()

static DCHCacheEntry * DCH_cache_search ( const char *  str)
static

Definition at line 3330 of file formatting.c.

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

Referenced by DCH_cache_fetch().

3331 {
3332  int i;
3333  DCHCacheEntry *ent;
3334 
3335  /* counter overflow check - paranoia? */
3336  if (DCHCounter >= (INT_MAX - DCH_CACHE_ENTRIES))
3337  {
3338  DCHCounter = 0;
3339 
3340  for (ent = DCHCache; ent < (DCHCache + DCH_CACHE_ENTRIES); ent++)
3341  ent->age = (++DCHCounter);
3342  }
3343 
3344  for (i = 0, ent = DCHCache; i < n_DCHCache; i++, ent++)
3345  {
3346  if (ent->valid && strcmp(ent->str, str) == 0)
3347  {
3348  ent->age = (++DCHCounter);
3349  return ent;
3350  }
3351  }
3352 
3353  return NULL;
3354 }
char str[DCH_CACHE_SIZE+1]
Definition: formatting.c:371
static int n_DCHCache
Definition: formatting.c:387
#define DCH_CACHE_ENTRIES
Definition: formatting.c:366
static DCHCacheEntry DCHCache[DCH_CACHE_ENTRIES]
Definition: formatting.c:386
int i
static int DCHCounter
Definition: formatting.c:388

◆ DCH_from_char()

static void DCH_from_char ( FormatNode node,
char *  in,
TmFromChar out 
)
static

Definition at line 2982 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_TZH, DCH_TZM, 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, ONE_UPPER, pg_mblen(), TmFromChar::pm, rm_months_lower, rm_months_upper, SKIP_THth, TmFromChar::ss, TmFromChar::ssss, FormatNode::suffix, FormatNode::type, TmFromChar::tzh, TmFromChar::tzm, TmFromChar::tzsign, TmFromChar::us, value, TmFromChar::w, TmFromChar::ww, TmFromChar::year, and TmFromChar::yysz.

Referenced by do_to_timestamp().

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

◆ DCH_to_char()

static void DCH_to_char ( FormatNode node,
bool  is_interval,
TmToChar in,
char *  out,
Oid  collid 
)
static

Definition at line 2402 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_TZH, DCH_TZM, 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, 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, generate_unaccent_rules::str, str_initcap_z(), str_numth(), str_tolower_z(), str_toupper_z(), FormatNode::suffix, 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().

2403 {
2404  FormatNode *n;
2405  char *s;
2406  struct pg_tm *tm = &in->tm;
2407  int i;
2408 
2409  /* cache localized days and months */
2411 
2412  s = out;
2413  for (n = node; n->type != NODE_TYPE_END; n++)
2414  {
2415  if (n->type != NODE_TYPE_ACTION)
2416  {
2417  strcpy(s, n->character);
2418  s += strlen(s);
2419  continue;
2420  }
2421 
2422  switch (n->key->id)
2423  {
2424  case DCH_A_M:
2425  case DCH_P_M:
2426  strcpy(s, (tm->tm_hour % HOURS_PER_DAY >= HOURS_PER_DAY / 2)
2427  ? P_M_STR : A_M_STR);
2428  s += strlen(s);
2429  break;
2430  case DCH_AM:
2431  case DCH_PM:
2432  strcpy(s, (tm->tm_hour % HOURS_PER_DAY >= HOURS_PER_DAY / 2)
2433  ? PM_STR : AM_STR);
2434  s += strlen(s);
2435  break;
2436  case DCH_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_HH:
2449  case DCH_HH12:
2450 
2451  /*
2452  * display time as shown on a 12-hour clock, even for
2453  * intervals
2454  */
2455  sprintf(s, "%0*d", S_FM(n->suffix) ? 0 : (tm->tm_hour >= 0) ? 2 : 3,
2456  tm->tm_hour % (HOURS_PER_DAY / 2) == 0 ? HOURS_PER_DAY / 2 :
2457  tm->tm_hour % (HOURS_PER_DAY / 2));
2458  if (S_THth(n->suffix))
2459  str_numth(s, s, S_TH_TYPE(n->suffix));
2460  s += strlen(s);
2461  break;
2462  case DCH_HH24:
2463  sprintf(s, "%0*d", S_FM(n->suffix) ? 0 : (tm->tm_hour >= 0) ? 2 : 3,
2464  tm->tm_hour);
2465  if (S_THth(n->suffix))
2466  str_numth(s, s, S_TH_TYPE(n->suffix));
2467  s += strlen(s);
2468  break;
2469  case DCH_MI:
2470  sprintf(s, "%0*d", S_FM(n->suffix) ? 0 : (tm->tm_min >= 0) ? 2 : 3,
2471  tm->tm_min);
2472  if (S_THth(n->suffix))
2473  str_numth(s, s, S_TH_TYPE(n->suffix));
2474  s += strlen(s);
2475  break;
2476  case DCH_SS:
2477  sprintf(s, "%0*d", S_FM(n->suffix) ? 0 : (tm->tm_sec >= 0) ? 2 : 3,
2478  tm->tm_sec);
2479  if (S_THth(n->suffix))
2480  str_numth(s, s, S_TH_TYPE(n->suffix));
2481  s += strlen(s);
2482  break;
2483  case DCH_MS: /* millisecond */
2484  sprintf(s, "%03d", (int) (in->fsec / INT64CONST(1000)));
2485  if (S_THth(n->suffix))
2486  str_numth(s, s, S_TH_TYPE(n->suffix));
2487  s += strlen(s);
2488  break;
2489  case DCH_US: /* microsecond */
2490  sprintf(s, "%06d", (int) in->fsec);
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_SSSS:
2496  sprintf(s, "%d", tm->tm_hour * SECS_PER_HOUR +
2497  tm->tm_min * SECS_PER_MINUTE +
2498  tm->tm_sec);
2499  if (S_THth(n->suffix))
2500  str_numth(s, s, S_TH_TYPE(n->suffix));
2501  s += strlen(s);
2502  break;
2503  case DCH_tz:
2505  if (tmtcTzn(in))
2506  {
2507  /* We assume here that timezone names aren't localized */
2508  char *p = asc_tolower_z(tmtcTzn(in));
2509 
2510  strcpy(s, p);
2511  pfree(p);
2512  s += strlen(s);
2513  }
2514  break;
2515  case DCH_TZ:
2517  if (tmtcTzn(in))
2518  {
2519  strcpy(s, tmtcTzn(in));
2520  s += strlen(s);
2521  }
2522  break;
2523  case DCH_TZH:
2525  sprintf(s, "%c%02d",
2526  (tm->tm_gmtoff >= 0) ? '+' : '-',
2527  abs((int) tm->tm_gmtoff) / SECS_PER_HOUR);
2528  s += strlen(s);
2529  break;
2530  case DCH_TZM:
2532  sprintf(s, "%02d",
2533  (abs((int) tm->tm_gmtoff) % SECS_PER_HOUR) / SECS_PER_MINUTE);
2534  s += strlen(s);
2535  break;
2536  case DCH_OF:
2538  sprintf(s, "%c%0*d",
2539  (tm->tm_gmtoff >= 0) ? '+' : '-',
2540  S_FM(n->suffix) ? 0 : 2,
2541  abs((int) tm->tm_gmtoff) / SECS_PER_HOUR);
2542  s += strlen(s);
2543  if (abs((int) tm->tm_gmtoff) % SECS_PER_HOUR != 0)
2544  {
2545  sprintf(s, ":%02d",
2546  (abs((int) tm->tm_gmtoff) % SECS_PER_HOUR) / SECS_PER_MINUTE);
2547  s += strlen(s);
2548  }
2549  break;
2550  case DCH_A_D:
2551  case DCH_B_C:
2553  strcpy(s, (tm->tm_year <= 0 ? B_C_STR : A_D_STR));
2554  s += strlen(s);
2555  break;
2556  case DCH_AD:
2557  case DCH_BC:
2559  strcpy(s, (tm->tm_year <= 0 ? BC_STR : AD_STR));
2560  s += strlen(s);
2561  break;
2562  case DCH_a_d:
2563  case DCH_b_c:
2565  strcpy(s, (tm->tm_year <= 0 ? b_c_STR : a_d_STR));
2566  s += strlen(s);
2567  break;
2568  case DCH_ad:
2569  case DCH_bc:
2571  strcpy(s, (tm->tm_year <= 0 ? bc_STR : ad_STR));
2572  s += strlen(s);
2573  break;
2574  case DCH_MONTH:
2576  if (!tm->tm_mon)
2577  break;
2578  if (S_TM(n->suffix))
2579  {
2580  char *str = str_toupper_z(localized_full_months[tm->tm_mon - 1], collid);
2581 
2582  if (strlen(str) <= (n->key->len + TM_SUFFIX_LEN) * DCH_MAX_ITEM_SIZ)
2583  strcpy(s, str);
2584  else
2585  ereport(ERROR,
2586  (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
2587  errmsg("localized string format value too long")));
2588  }
2589  else
2590  sprintf(s, "%*s", S_FM(n->suffix) ? 0 : -9,
2591  asc_toupper_z(months_full[tm->tm_mon - 1]));
2592  s += strlen(s);
2593  break;
2594  case DCH_Month:
2596  if (!tm->tm_mon)
2597  break;
2598  if (S_TM(n->suffix))
2599  {
2600  char *str = str_initcap_z(localized_full_months[tm->tm_mon - 1], collid);
2601 
2602  if (strlen(str) <= (n->key->len + TM_SUFFIX_LEN) * DCH_MAX_ITEM_SIZ)
2603  strcpy(s, str);
2604  else
2605  ereport(ERROR,
2606  (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
2607  errmsg("localized string format value too long")));
2608  }
2609  else
2610  sprintf(s, "%*s", S_FM(n->suffix) ? 0 : -9,
2611  months_full[tm->tm_mon - 1]);
2612  s += strlen(s);
2613  break;
2614  case DCH_month:
2616  if (!tm->tm_mon)
2617  break;
2618  if (S_TM(n->suffix))
2619  {
2620  char *str = str_tolower_z(localized_full_months[tm->tm_mon - 1], collid);
2621 
2622  if (strlen(str) <= (n->key->len + TM_SUFFIX_LEN) * DCH_MAX_ITEM_SIZ)
2623  strcpy(s, str);
2624  else
2625  ereport(ERROR,
2626  (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
2627  errmsg("localized string format value too long")));
2628  }
2629  else
2630  sprintf(s, "%*s", S_FM(n->suffix) ? 0 : -9,
2631  asc_tolower_z(months_full[tm->tm_mon - 1]));
2632  s += strlen(s);
2633  break;
2634  case DCH_MON:
2636  if (!tm->tm_mon)
2637  break;
2638  if (S_TM(n->suffix))
2639  {
2640  char *str = str_toupper_z(localized_abbrev_months[tm->tm_mon - 1], collid);
2641 
2642  if (strlen(str) <= (n->key->len + TM_SUFFIX_LEN) * DCH_MAX_ITEM_SIZ)
2643  strcpy(s, str);
2644  else
2645  ereport(ERROR,
2646  (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
2647  errmsg("localized string format value too long")));
2648  }
2649  else
2650  strcpy(s, asc_toupper_z(months[tm->tm_mon - 1]));
2651  s += strlen(s);
2652  break;
2653  case DCH_Mon:
2655  if (!tm->tm_mon)
2656  break;
2657  if (S_TM(n->suffix))
2658  {
2659  char *str = str_initcap_z(localized_abbrev_months[tm->tm_mon - 1], collid);
2660 
2661  if (strlen(str) <= (n->key->len + TM_SUFFIX_LEN) * DCH_MAX_ITEM_SIZ)
2662  strcpy(s, str);
2663  else
2664  ereport(ERROR,
2665  (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
2666  errmsg("localized string format value too long")));
2667  }
2668  else
2669  strcpy(s, months[tm->tm_mon - 1]);
2670  s += strlen(s);
2671  break;
2672  case DCH_mon:
2674  if (!tm->tm_mon)
2675  break;
2676  if (S_TM(n->suffix))
2677  {
2678  char *str = str_tolower_z(localized_abbrev_months[tm->tm_mon - 1], collid);
2679 
2680  if (strlen(str) <= (n->key->len + TM_SUFFIX_LEN) * DCH_MAX_ITEM_SIZ)
2681  strcpy(s, str);
2682  else
2683  ereport(ERROR,
2684  (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
2685  errmsg("localized string format value too long")));
2686  }
2687  else
2688  strcpy(s, asc_tolower_z(months[tm->tm_mon - 1]));
2689  s += strlen(s);
2690  break;
2691  case DCH_MM:
2692  sprintf(s, "%0*d", S_FM(n->suffix) ? 0 : (tm->tm_mon >= 0) ? 2 : 3,
2693  tm->tm_mon);
2694  if (S_THth(n->suffix))
2695  str_numth(s, s, S_TH_TYPE(n->suffix));
2696  s += strlen(s);
2697  break;
2698  case DCH_DAY:
2700  if (S_TM(n->suffix))
2701  {
2702  char *str = str_toupper_z(localized_full_days[tm->tm_wday], collid);
2703 
2704  if (strlen(str) <= (n->key->len + TM_SUFFIX_LEN) * DCH_MAX_ITEM_SIZ)
2705  strcpy(s, str);
2706  else
2707  ereport(ERROR,
2708  (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
2709  errmsg("localized string format value too long")));
2710  }
2711  else
2712  sprintf(s, "%*s", S_FM(n->suffix) ? 0 : -9,
2713  asc_toupper_z(days[tm->tm_wday]));
2714  s += strlen(s);
2715  break;
2716  case DCH_Day:
2718  if (S_TM(n->suffix))
2719  {
2720  char *str = str_initcap_z(localized_full_days[tm->tm_wday], collid);
2721 
2722  if (strlen(str) <= (n->key->len + TM_SUFFIX_LEN) * DCH_MAX_ITEM_SIZ)
2723  strcpy(s, str);
2724  else
2725  ereport(ERROR,
2726  (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
2727  errmsg("localized string format value too long")));
2728  }
2729  else
2730  sprintf(s, "%*s", S_FM(n->suffix) ? 0 : -9,
2731  days[tm->tm_wday]);
2732  s += strlen(s);
2733  break;
2734  case DCH_day:
2736  if (S_TM(n->suffix))
2737  {
2738  char *str = str_tolower_z(localized_full_days[tm->tm_wday], collid);
2739 
2740  if (strlen(str) <= (n->key->len + TM_SUFFIX_LEN) * DCH_MAX_ITEM_SIZ)
2741  strcpy(s, str);
2742  else
2743  ereport(ERROR,
2744  (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
2745  errmsg("localized string format value too long")));
2746  }
2747  else
2748  sprintf(s, "%*s", S_FM(n->suffix) ? 0 : -9,
2749  asc_tolower_z(days[tm->tm_wday]));
2750  s += strlen(s);
2751  break;
2752  case DCH_DY:
2754  if (S_TM(n->suffix))
2755  {
2756  char *str = str_toupper_z(localized_abbrev_days[tm->tm_wday], collid);
2757 
2758  if (strlen(str) <= (n->key->len + TM_SUFFIX_LEN) * DCH_MAX_ITEM_SIZ)
2759  strcpy(s, str);
2760  else
2761  ereport(ERROR,
2762  (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
2763  errmsg("localized string format value too long")));
2764  }
2765  else
2766  strcpy(s, asc_toupper_z(days_short[tm->tm_wday]));
2767  s += strlen(s);
2768  break;
2769  case DCH_Dy:
2771  if (S_TM(n->suffix))
2772  {
2773  char *str = str_initcap_z(localized_abbrev_days[tm->tm_wday], collid);
2774 
2775  if (strlen(str) <= (n->key->len + TM_SUFFIX_LEN) * DCH_MAX_ITEM_SIZ)
2776  strcpy(s, str);
2777  else
2778  ereport(ERROR,
2779  (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
2780  errmsg("localized string format value too long")));
2781  }
2782  else
2783  strcpy(s, days_short[tm->tm_wday]);
2784  s += strlen(s);
2785  break;
2786  case DCH_dy:
2788  if (S_TM(n->suffix))
2789  {
2790  char *str = str_tolower_z(localized_abbrev_days[tm->tm_wday], collid);
2791 
2792  if (strlen(str) <= (n->key->len + TM_SUFFIX_LEN) * DCH_MAX_ITEM_SIZ)
2793  strcpy(s, str);
2794  else
2795  ereport(ERROR,
2796  (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
2797  errmsg("localized string format value too long")));
2798  }
2799  else
2800  strcpy(s, asc_tolower_z(days_short[tm->tm_wday]));
2801  s += strlen(s);
2802  break;
2803  case DCH_DDD:
2804  case DCH_IDDD:
2805  sprintf(s, "%0*d", S_FM(n->suffix) ? 0 : 3,
2806  (n->key->id == DCH_DDD) ?
2807  tm->tm_yday :
2808  date2isoyearday(tm->tm_year, tm->tm_mon, tm->tm_mday));
2809  if (S_THth(n->suffix))
2810  str_numth(s, s, S_TH_TYPE(n->suffix));
2811  s += strlen(s);
2812  break;
2813  case DCH_DD:
2814  sprintf(s, "%0*d", S_FM(n->suffix) ? 0 : 2, tm->tm_mday);
2815  if (S_THth(n->suffix))
2816  str_numth(s, s, S_TH_TYPE(n->suffix));
2817  s += strlen(s);
2818  break;
2819  case DCH_D:
2821  sprintf(s, "%d", tm->tm_wday + 1);
2822  if (S_THth(n->suffix))
2823  str_numth(s, s, S_TH_TYPE(n->suffix));
2824  s += strlen(s);
2825  break;
2826  case DCH_ID:
2828  sprintf(s, "%d", (tm->tm_wday == 0) ? 7 : tm->tm_wday);
2829  if (S_THth(n->suffix))
2830  str_numth(s, s, S_TH_TYPE(n->suffix));
2831  s += strlen(s);
2832  break;
2833  case DCH_WW:
2834  sprintf(s, "%0*d", S_FM(n->suffix) ? 0 : 2,
2835  (tm->tm_yday - 1) / 7 + 1);
2836  if (S_THth(n->suffix))
2837  str_numth(s, s, S_TH_TYPE(n->suffix));
2838  s += strlen(s);
2839  break;
2840  case DCH_IW:
2841  sprintf(s, "%0*d", S_FM(n->suffix) ? 0 : 2,
2842  date2isoweek(tm->tm_year, tm->tm_mon, tm->tm_mday));
2843  if (S_THth(n->suffix))
2844  str_numth(s, s, S_TH_TYPE(n->suffix));
2845  s += strlen(s);
2846  break;
2847  case DCH_Q:
2848  if (!tm->tm_mon)
2849  break;
2850  sprintf(s, "%d", (tm->tm_mon - 1) / 3 + 1);
2851  if (S_THth(n->suffix))
2852  str_numth(s, s, S_TH_TYPE(n->suffix));
2853  s += strlen(s);
2854  break;
2855  case DCH_CC:
2856  if (is_interval) /* straight calculation */
2857  i = tm->tm_year / 100;
2858  else
2859  {
2860  if (tm->tm_year > 0)
2861  /* Century 20 == 1901 - 2000 */
2862  i = (tm->tm_year - 1) / 100 + 1;
2863  else
2864  /* Century 6BC == 600BC - 501BC */
2865  i = tm->tm_year / 100 - 1;
2866  }
2867  if (i <= 99 && i >= -99)
2868  sprintf(s, "%0*d", S_FM(n->suffix) ? 0 : (i >= 0) ? 2 : 3, i);
2869  else
2870  sprintf(s, "%d", i);
2871  if (S_THth(n->suffix))
2872  str_numth(s, s, S_TH_TYPE(n->suffix));
2873  s += strlen(s);
2874  break;
2875  case DCH_Y_YYY:
2876  i = ADJUST_YEAR(tm->tm_year, is_interval) / 1000;
2877  sprintf(s, "%d,%03d", i,
2878  ADJUST_YEAR(tm->tm_year, is_interval) - (i * 1000));
2879  if (S_THth(n->suffix))
2880  str_numth(s, s, S_TH_TYPE(n->suffix));
2881  s += strlen(s);
2882  break;
2883  case DCH_YYYY:
2884  case DCH_IYYY:
2885  sprintf(s, "%0*d",
2886  S_FM(n->suffix) ? 0 :
2887  (ADJUST_YEAR(tm->tm_year, is_interval) >= 0) ? 4 : 5,
2888  (n->key->id == DCH_YYYY ?
2889  ADJUST_YEAR(tm->tm_year, is_interval) :
2891  tm->tm_mon,
2892  tm->tm_mday),
2893  is_interval)));
2894  if (S_THth(n->suffix))
2895  str_numth(s, s, S_TH_TYPE(n->suffix));
2896  s += strlen(s);
2897  break;
2898  case DCH_YYY:
2899  case DCH_IYY:
2900  sprintf(s, "%0*d",
2901  S_FM(n->suffix) ? 0 :
2902  (ADJUST_YEAR(tm->tm_year, is_interval) >= 0) ? 3 : 4,
2903  (n->key->id == DCH_YYY ?
2904  ADJUST_YEAR(tm->tm_year, is_interval) :
2906  tm->tm_mon,
2907  tm->tm_mday),
2908  is_interval)) % 1000);
2909  if (S_THth(n->suffix))
2910  str_numth(s, s, S_TH_TYPE(n->suffix));
2911  s += strlen(s);
2912  break;
2913  case DCH_YY:
2914  case DCH_IY:
2915  sprintf(s, "%0*d",
2916  S_FM(n->suffix) ? 0 :
2917  (ADJUST_YEAR(tm->tm_year, is_interval) >= 0) ? 2 : 3,
2918  (n->key->id == DCH_YY ?
2919  ADJUST_YEAR(tm->tm_year, is_interval) :
2921  tm->tm_mon,
2922  tm->tm_mday),
2923  is_interval)) % 100);
2924  if (S_THth(n->suffix))
2925  str_numth(s, s, S_TH_TYPE(n->suffix));
2926  s += strlen(s);
2927  break;
2928  case DCH_Y:
2929  case DCH_I:
2930  sprintf(s, "%1d",
2931  (n->key->id == DCH_Y ?
2932  ADJUST_YEAR(tm->tm_year, is_interval) :
2934  tm->tm_mon,
2935  tm->tm_mday),
2936  is_interval)) % 10);
2937  if (S_THth(n->suffix))
2938  str_numth(s, s, S_TH_TYPE(n->suffix));
2939  s += strlen(s);
2940  break;
2941  case DCH_RM:
2942  if (!tm->tm_mon)
2943  break;
2944  sprintf(s, "%*s", S_FM(n->suffix) ? 0 : -4,
2946  s += strlen(s);
2947  break;
2948  case DCH_rm:
2949  if (!tm->tm_mon)
2950  break;
2951  sprintf(s, "%*s", S_FM(n->suffix) ? 0 : -4,
2953  s += strlen(s);
2954  break;
2955  case DCH_W:
2956  sprintf(s, "%d", (tm->tm_mday - 1) / 7 + 1);
2957  if (S_THth(n->suffix))
2958  str_numth(s, s, S_TH_TYPE(n->suffix));
2959  s += strlen(s);
2960  break;
2961  case DCH_J:
2962  sprintf(s, "%d", date2j(tm->tm_year, tm->tm_mon, tm->tm_mday));
2963  if (S_THth(n->suffix))
2964  str_numth(s, s, S_TH_TYPE(n->suffix));
2965  s += strlen(s);
2966  break;
2967  }
2968  }
2969 
2970  *s = '\0';
2971 }
#define a_m_STR
Definition: formatting.c:225
#define NODE_TYPE_END
Definition: formatting.c:164
const KeyWord * key
Definition: formatting.c:159
int tm_wday
Definition: pgtime.h:33
#define b_c_STR
Definition: formatting.c:203
#define S_THth(_s)
Definition: formatting.c:510
int tm_hour
Definition: pgtime.h:29
#define BC_STR
Definition: formatting.c:204
#define A_M_STR
Definition: formatting.c:224
static char * str_initcap_z(const char *buff, Oid collid)
Definition: formatting.c:1989
fsec_t fsec
Definition: formatting.c:456
static char * asc_tolower_z(const char *buff)
Definition: formatting.c:1995
#define AD_STR
Definition: formatting.c:199
int errcode(int sqlerrcode)
Definition: elog.c:575
struct pg_tm tm
Definition: formatting.c:455
#define p_m_STR
Definition: formatting.c:230
#define am_STR
Definition: formatting.c:227
static char * str_tolower_z(const char *buff, Oid collid)
Definition: formatting.c:1977
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:1428
static const char *const rm_months_upper[]
Definition: formatting.c:253
int id
Definition: formatting.c:151
#define pm_STR
Definition: formatting.c:232
static struct pg_tm tm
Definition: localtime.c:106
#define MONTHS_PER_YEAR
Definition: timestamp.h:69
void pfree(void *pointer)
Definition: mcxt.c:1031
const char *const months[]
Definition: datetime.c:67
#define NODE_TYPE_ACTION
Definition: formatting.c:165
#define ERROR
Definition: elog.h:43
char character[MAX_MULTIBYTE_CHAR_LEN+1]
Definition: formatting.c:160
int date2isoweek(int year, int mon, int mday)
Definition: timestamp.c:4213
#define S_FM(_s)
Definition: formatting.c:516
int date2isoyear(int year, int mon, int mday)
Definition: timestamp.c:4267
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
const char *const days[]
Definition: datetime.c:70
#define A_D_STR
Definition: formatting.c:197
#define a_d_STR
Definition: formatting.c:198
#define ereport(elevel, rest)
Definition: elog.h:122
#define SECS_PER_HOUR
Definition: timestamp.h:87
#define bc_STR
Definition: formatting.c:205
#define S_TH_TYPE(_s)
Definition: formatting.c:513
static const char *const months_full[]
Definition: formatting.c:179
int len
Definition: formatting.c:150
char * localized_full_days[7]
Definition: pg_locale.c:95
int date2j(int y, int m, int d)
Definition: datetime.c:276
#define ad_STR
Definition: formatting.c:200
void cache_locale_time(void)
Definition: pg_locale.c:786
#define P_M_STR
Definition: formatting.c:229
static char * asc_toupper_z(const char *buff)
Definition: formatting.c:2001
#define ADJUST_YEAR(year, is_interval)
Definition: formatting.c:195
#define tmtcTzn(_X)
Definition: formatting.c:461
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:1983
#define INVALID_FOR_INTERVAL
Definition: formatting.c:483
int date2isoyearday(int year, int mon, int mday)
Definition: timestamp.c:4324
int i
#define S_TM(_s)
Definition: formatting.c:518
int tm_yday
Definition: pgtime.h:34
#define PM_STR
Definition: formatting.c:231
#define AM_STR
Definition: formatting.c:226
#define TM_SUFFIX_LEN
Definition: formatting.c:524
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:256
static const char *const days_short[]
Definition: formatting.c:184
#define B_C_STR
Definition: formatting.c:202

◆ do_to_timestamp()

static void do_to_timestamp ( text date_txt,
text fmt,
struct pg_tm tm,
fsec_t fsec 
)
static

Definition at line 3635 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, DTERR_TZDISP_OVERFLOW, DTK_DATE_M, DTK_M, ereport, errcode(), errhint(), errmsg(), ERROR, format, DCHCacheEntry::format, FROM_CHAR_DATE_ISOWEEK, TmFromChar::hh, HOURS_PER_DAY, i, isleap, isoweek2date(), isoweek2j(), isoweekdate2date(), TmFromChar::j, j2date(), MAX_TZDISP_HOUR, TmFromChar::mi, MINS_PER_HOUR, TmFromChar::mm, TmFromChar::mode, MONTH, MONTHS_PER_YEAR, TmFromChar::ms, palloc(), parse_format(), pfree(), TmFromChar::pm, psprintf(), 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, pg_tm::tm_zone, TmFromChar::tzh, TmFromChar::tzm, TmFromChar::tzsign, 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().

3637 {
3638  FormatNode *format;
3639  TmFromChar tmfc;
3640  int fmt_len;
3641  char *date_str;
3642  int fmask;
3643 
3644  date_str = text_to_cstring(date_txt);
3645 
3646  ZERO_tmfc(&tmfc);
3647  ZERO_tm(tm);
3648  *fsec = 0;
3649  fmask = 0; /* bit mask for ValidateDate() */
3650 
3651  fmt_len = VARSIZE_ANY_EXHDR(fmt);
3652 
3653  if (fmt_len)
3654  {
3655  char *fmt_str;
3656  bool incache;
3657 
3658  fmt_str = text_to_cstring(fmt);
3659 
3660  if (fmt_len > DCH_CACHE_SIZE)
3661  {
3662  /*
3663  * Allocate new memory if format picture is bigger than static
3664  * cache and do not use cache (call parser always)
3665  */
3666  incache = false;
3667 
3668  format = (FormatNode *) palloc((fmt_len + 1) * sizeof(FormatNode));
3669 
3670  parse_format(format, fmt_str, DCH_keywords,
3671  DCH_suff, DCH_index, DCH_TYPE, NULL);
3672  }
3673  else
3674  {
3675  /*
3676  * Use cache buffers
3677  */
3678  DCHCacheEntry *ent = DCH_cache_fetch(fmt_str);
3679 
3680  incache = true;
3681  format = ent->format;
3682  }
3683 
3684 #ifdef DEBUG_TO_FROM_CHAR
3685  /* dump_node(format, fmt_len); */
3686  /* dump_index(DCH_keywords, DCH_index); */
3687 #endif
3688 
3689  DCH_from_char(format, date_str, &tmfc);
3690 
3691  pfree(fmt_str);
3692  if (!incache)
3693  pfree(format);
3694  }
3695 
3696  DEBUG_TMFC(&tmfc);
3697 
3698  /*
3699  * Convert to_date/to_timestamp input fields to standard 'tm'
3700  */
3701  if (tmfc.ssss)
3702  {
3703  int x = tmfc.ssss;
3704 
3705  tm->tm_hour = x / SECS_PER_HOUR;
3706  x %= SECS_PER_HOUR;
3707  tm->tm_min = x / SECS_PER_MINUTE;
3708  x %= SECS_PER_MINUTE;
3709  tm->tm_sec = x;
3710  }
3711 
3712  if (tmfc.ss)
3713  tm->tm_sec = tmfc.ss;
3714  if (tmfc.mi)
3715  tm->tm_min = tmfc.mi;
3716  if (tmfc.hh)
3717  tm->tm_hour = tmfc.hh;
3718 
3719  if (tmfc.clock == CLOCK_12_HOUR)
3720  {
3721  if (tm->tm_hour < 1 || tm->tm_hour > HOURS_PER_DAY / 2)
3722  ereport(ERROR,
3723  (errcode(ERRCODE_INVALID_DATETIME_FORMAT),
3724  errmsg("hour \"%d\" is invalid for the 12-hour clock",
3725  tm->tm_hour),
3726  errhint("Use the 24-hour clock, or give an hour between 1 and 12.")));
3727 
3728  if (tmfc.pm && tm->tm_hour < HOURS_PER_DAY / 2)
3729  tm->tm_hour += HOURS_PER_DAY / 2;
3730  else if (!tmfc.pm && tm->tm_hour == HOURS_PER_DAY / 2)
3731  tm->tm_hour = 0;
3732  }
3733 
3734  if (tmfc.year)
3735  {
3736  /*
3737  * If CC and YY (or Y) are provided, use YY as 2 low-order digits for
3738  * the year in the given century. Keep in mind that the 21st century
3739  * AD runs from 2001-2100, not 2000-2099; 6th century BC runs from
3740  * 600BC to 501BC.
3741  */
3742  if (tmfc.cc && tmfc.yysz <= 2)
3743  {
3744  if (tmfc.bc)
3745  tmfc.cc = -tmfc.cc;
3746  tm->tm_year = tmfc.year % 100;
3747  if (tm->tm_year)
3748  {
3749  if (tmfc.cc >= 0)
3750  tm->tm_year += (tmfc.cc - 1) * 100;
3751  else
3752  tm->tm_year = (tmfc.cc + 1) * 100 - tm->tm_year + 1;
3753  }
3754  else
3755  {
3756  /* find century year for dates ending in "00" */
3757  tm->tm_year = tmfc.cc * 100 + ((tmfc.cc >= 0) ? 0 : 1);
3758  }
3759  }
3760  else
3761  {
3762  /* If a 4-digit year is provided, we use that and ignore CC. */
3763  tm->tm_year = tmfc.year;
3764  if (tmfc.bc && tm->tm_year > 0)
3765  tm->tm_year = -(tm->tm_year - 1);
3766  }
3767  fmask |= DTK_M(YEAR);
3768  }
3769  else if (tmfc.cc)
3770  {
3771  /* use first year of century */
3772  if (tmfc.bc)
3773  tmfc.cc = -tmfc.cc;
3774  if (tmfc.cc >= 0)
3775  /* +1 because 21st century started in 2001 */
3776  tm->tm_year = (tmfc.cc - 1) * 100 + 1;
3777  else
3778  /* +1 because year == 599 is 600 BC */
3779  tm->tm_year = tmfc.cc * 100 + 1;
3780  fmask |= DTK_M(YEAR);
3781  }
3782 
3783  if (tmfc.j)
3784  {
3785  j2date(tmfc.j, &tm->tm_year, &tm->tm_mon, &tm->tm_mday);
3786  fmask |= DTK_DATE_M;
3787  }
3788 
3789  if (tmfc.ww)
3790  {
3791  if (tmfc.mode == FROM_CHAR_DATE_ISOWEEK)
3792  {
3793  /*
3794  * If tmfc.d is not set, then the date is left at the beginning of
3795  * the ISO week (Monday).
3796  */
3797  if (tmfc.d)
3798  isoweekdate2date(tmfc.ww, tmfc.d, &tm->tm_year, &tm->tm_mon, &tm->tm_mday);
3799  else
3800  isoweek2date(tmfc.ww, &tm->tm_year, &tm->tm_mon, &tm->tm_mday);
3801  fmask |= DTK_DATE_M;
3802  }
3803  else
3804  tmfc.ddd = (tmfc.ww - 1) * 7 + 1;
3805  }
3806 
3807  if (tmfc.w)
3808  tmfc.dd = (tmfc.w - 1) * 7 + 1;
3809  if (tmfc.dd)
3810  {
3811  tm->tm_mday = tmfc.dd;
3812  fmask |= DTK_M(DAY);
3813  }
3814  if (tmfc.mm)
3815  {
3816  tm->tm_mon = tmfc.mm;
3817  fmask |= DTK_M(MONTH);
3818  }
3819 
3820  if (tmfc.ddd && (tm->tm_mon <= 1 || tm->tm_mday <= 1))
3821  {
3822  /*
3823  * The month and day field have not been set, so we use the
3824  * day-of-year field to populate them. Depending on the date mode,
3825  * this field may be interpreted as a Gregorian day-of-year, or an ISO
3826  * week date day-of-year.
3827  */
3828 
3829  if (!tm->tm_year && !tmfc.bc)
3830  ereport(ERROR,
3831  (errcode(ERRCODE_INVALID_DATETIME_FORMAT),
3832  errmsg("cannot calculate day of year without year information")));
3833 
3834  if (tmfc.mode == FROM_CHAR_DATE_ISOWEEK)
3835  {
3836  int j0; /* zeroth day of the ISO year, in Julian */
3837 
3838  j0 = isoweek2j(tm->tm_year, 1) - 1;
3839 
3840  j2date(j0 + tmfc.ddd, &tm->tm_year, &tm->tm_mon, &tm->tm_mday);
3841  fmask |= DTK_DATE_M;
3842  }
3843  else
3844  {
3845  const int *y;
3846  int i;
3847 
3848  static const int ysum[2][13] = {
3849  {0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365},
3850  {0, 31, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335, 366}};
3851 
3852  y = ysum[isleap(tm->tm_year)];
3853 
3854  for (i = 1; i <= MONTHS_PER_YEAR; i++)
3855  {
3856  if (tmfc.ddd <= y[i])
3857  break;
3858  }
3859  if (tm->tm_mon <= 1)
3860  tm->tm_mon = i;
3861 
3862  if (tm->tm_mday <= 1)
3863  tm->tm_mday = tmfc.ddd - y[i - 1];
3864 
3865  fmask |= DTK_M(MONTH) | DTK_M(DAY);
3866  }
3867  }
3868 
3869  if (tmfc.ms)
3870  *fsec += tmfc.ms * 1000;
3871  if (tmfc.us)
3872  *fsec += tmfc.us;
3873 
3874  /* Range-check date fields according to bit mask computed above */
3875  if (fmask != 0)
3876  {
3877  /* We already dealt with AD/BC, so pass isjulian = true */
3878  int dterr = ValidateDate(fmask, true, false, false, tm);
3879 
3880  if (dterr != 0)
3881  {
3882  /*
3883  * Force the error to be DTERR_FIELD_OVERFLOW even if ValidateDate
3884  * said DTERR_MD_FIELD_OVERFLOW, because we don't want to print an
3885  * irrelevant hint about datestyle.
3886  */
3887  DateTimeParseError(DTERR_FIELD_OVERFLOW, date_str, "timestamp");
3888  }
3889  }
3890 
3891  /* Range-check time fields too */
3892  if (tm->tm_hour < 0 || tm->tm_hour >= HOURS_PER_DAY ||
3893  tm->tm_min < 0 || tm->tm_min >= MINS_PER_HOUR ||
3894  tm->tm_sec < 0 || tm->tm_sec >= SECS_PER_MINUTE ||
3895  *fsec < INT64CONST(0) || *fsec >= USECS_PER_SEC)
3896  DateTimeParseError(DTERR_FIELD_OVERFLOW, date_str, "timestamp");
3897 
3898  /* Save parsed time-zone into tm->tm_zone if it was specified */
3899  if (tmfc.tzsign)
3900  {
3901  char *tz;
3902 
3903  if (tmfc.tzh < 0 || tmfc.tzh > MAX_TZDISP_HOUR ||
3904  tmfc.tzm < 0 || tmfc.tzm >= MINS_PER_HOUR)
3905  DateTimeParseError(DTERR_TZDISP_OVERFLOW, date_str, "timestamp");
3906 
3907  tz = psprintf("%c%02d:%02d",
3908  tmfc.tzsign > 0 ? '+' : '-', tmfc.tzh, tmfc.tzm);
3909 
3910  tm->tm_zone = tz;
3911  }
3912 
3913  DEBUG_TM(tm);
3914 
3915  pfree(date_str);
3916 }
#define DEBUG_TM(_X)
Definition: formatting.c:446
#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:3754
#define USECS_PER_SEC
Definition: timestamp.h:94
#define YEAR
Definition: datetime.h:93
int tm_hour
Definition: pgtime.h:29
char * psprintf(const char *fmt,...)
Definition: psprintf.c:46
#define isleap(y)
Definition: datetime.h:273
static const KeySuffix DCH_suff[]
Definition: formatting.c:526
static const int DCH_index[KeyWord_INDEX_SIZE]
Definition: formatting.c:872
int errcode(int sqlerrcode)
Definition: elog.c:575
void isoweekdate2date(int isoweek, int wday, int *year, int *mon, int *mday)
Definition: timestamp.c:4195
static void DCH_from_char(FormatNode *node, char *in, TmFromChar *out)
Definition: formatting.c:2982
FromCharDateMode mode
Definition: formatting.c:401
#define DTK_DATE_M
Definition: datetime.h:194
#define MINS_PER_HOUR
Definition: timestamp.h:89
#define MAX_TZDISP_HOUR
Definition: timestamp.h:103
#define MONTHS_PER_YEAR
Definition: timestamp.h:69
void pfree(void *pointer)
Definition: mcxt.c:1031
#define DCH_CACHE_SIZE
Definition: formatting.c:365
#define ERROR
Definition: elog.h:43
#define DTERR_FIELD_OVERFLOW
Definition: datetime.h:283
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:1226
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:370
static DCHCacheEntry * DCH_cache_fetch(const char *str)
Definition: formatting.c:3358
const char * tm_zone
Definition: pgtime.h:37
#define ereport(elevel, rest)
Definition: elog.h:122
void j2date(int jd, int *year, int *month, int *day)
Definition: datetime.c:301
#define SECS_PER_HOUR
Definition: timestamp.h:87
#define CLOCK_12_HOUR
Definition: formatting.c:172
#define MONTH
Definition: datetime.h:92
#define DCH_TYPE
Definition: formatting.c:103
#define DTERR_TZDISP_OVERFLOW
Definition: datetime.h:286
void isoweek2date(int woy, int *year, int *mon, int *mday)
Definition: timestamp.c:4182
#define ZERO_tmfc(_X)
Definition: formatting.c:426
int isoweek2j(int year, int week)
Definition: timestamp.c:4162
#define DEBUG_TMFC(_X)
Definition: formatting.c:445
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:341
void * palloc(Size size)
Definition: mcxt.c:924
int errmsg(const char *fmt,...)
Definition: elog.c:797
static const KeyWord DCH_keywords[]
Definition: formatting.c:716
int i
#define DTK_M(t)
Definition: datetime.h:190
static char format
Definition: pg_basebackup.c:83
int tm_sec
Definition: pgtime.h:27
#define ZERO_tm(_X)
Definition: formatting.c:464
int tm_min
Definition: pgtime.h:28
int ValidateDate(int fmask, bool isjulian, bool is2digits, bool bc, struct pg_tm *tm)
Definition: datetime.c:2466

◆ fill_str()

static char * fill_str ( char *  str,
int  c,
int  max 
)
static

Definition at line 3925 of file formatting.c.

References generate_unaccent_rules::str.

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

3926 {
3927  memset(str, c, max);
3928  *(str + max) = '\0';
3929  return str;
3930 }
char * c

◆ float4_to_char()

Datum float4_to_char ( PG_FUNCTION_ARGS  )

Definition at line 5614 of file formatting.c.

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

5615 {
5617  text *fmt = PG_GETARG_TEXT_PP(1);
5618  NUMDesc Num;
5619  FormatNode *format;
5620  text *result;
5621  bool shouldFree;
5622  int out_pre_spaces = 0,
5623  sign = 0;
5624  char *numstr,
5625  *orgnum,
5626  *p;
5627 
5629 
5630  if (IS_ROMAN(&Num))
5631  numstr = orgnum = int_to_roman((int) rint(value));
5632  else if (IS_EEEE(&Num))
5633  {
5634  if (isnan(value) || is_infinite(value))
5635  {
5636  /*
5637  * Allow 6 characters for the leading sign, the decimal point,
5638  * "e", the exponent's sign and two exponent digits.
5639  */
5640  numstr = (char *) palloc(Num.pre + Num.post + 7);
5641  fill_str(numstr, '#', Num.pre + Num.post + 6);
5642  *numstr = ' ';
5643  *(numstr + Num.pre + 1) = '.';
5644  }
5645  else
5646  {
5647  numstr = orgnum = psprintf("%+.*e", Num.post, value);
5648 
5649  /*
5650  * Swap a leading positive sign for a space.
5651  */
5652  if (*orgnum == '+')
5653  *orgnum = ' ';
5654 
5655  numstr = orgnum;
5656  }
5657  }
5658  else
5659  {
5660  float4 val = value;
5661  int numstr_pre_len;
5662 
5663  if (IS_MULTI(&Num))
5664  {
5665  float multi = pow((double) 10, (double) Num.multi);
5666 
5667  val = value * multi;
5668  Num.pre += Num.multi;
5669  }
5670 
5671  orgnum = (char *) psprintf("%.0f", fabs(val));
5672  numstr_pre_len = strlen(orgnum);
5673 
5674  /* adjust post digits to fit max float digits */
5675  if (numstr_pre_len >= FLT_DIG)
5676  Num.post = 0;
5677  else if (numstr_pre_len + Num.post > FLT_DIG)
5678  Num.post = FLT_DIG - numstr_pre_len;
5679  orgnum = psprintf("%.*f", Num.post, val);
5680 
5681  if (*orgnum == '-')
5682  { /* < 0 */
5683  sign = '-';
5684  numstr = orgnum + 1;
5685  }
5686  else
5687  {
5688  sign = '+';
5689  numstr = orgnum;
5690  }
5691 
5692  if ((p = strchr(numstr, '.')))
5693  numstr_pre_len = p - numstr;
5694  else
5695  numstr_pre_len = strlen(numstr);
5696 
5697  /* needs padding? */
5698  if (numstr_pre_len < Num.pre)
5699  out_pre_spaces = Num.pre - numstr_pre_len;
5700  /* overflowed prefix digit format? */
5701  else if (numstr_pre_len > Num.pre)
5702  {
5703  numstr = (char *) palloc(Num.pre + Num.post + 2);
5704  fill_str(numstr, '#', Num.pre + Num.post + 1);
5705  *(numstr + Num.pre) = '.';
5706  }
5707  }
5708 
5710  PG_RETURN_TEXT_P(result);
5711 }
char * psprintf(const char *fmt,...)
Definition: psprintf.c:46
#define IS_EEEE(_f)
Definition: formatting.c:348
#define NUM_TOCHAR_finish
Definition: formatting.c:5202
#define IS_ROMAN(_f)
Definition: formatting.c:346
#define PG_GETARG_TEXT_PP(n)
Definition: fmgr.h:278
#define NUM_TOCHAR_prepare
Definition: formatting.c:5189
#define IS_MULTI(_f)
Definition: formatting.c:347
char sign
Definition: informix.c:693
static char * fill_str(char *str, int c, int max)
Definition: formatting.c:3925
static char * int_to_roman(int number)
Definition: formatting.c:4123
int pre
Definition: formatting.c:299
double rint(double x)
Definition: rint.c:22
#define PG_GETARG_FLOAT4(n)
Definition: fmgr.h:250
float float4
Definition: c.h:457
static struct @131 value
#define PG_RETURN_TEXT_P(x)
Definition: fmgr.h:336
int post
Definition: formatting.c:299
int is_infinite(double val)
Definition: float.c:196
void * palloc(Size size)
Definition: mcxt.c:924
int multi
Definition: formatting.c:299
Definition: c.h:516
static char format
Definition: pg_basebackup.c:83
long val
Definition: informix.c:689

◆ float8_to_char()

Datum float8_to_char ( PG_FUNCTION_ARGS  )

Definition at line 5718 of file formatting.c.

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

5719 {
5721  text *fmt = PG_GETARG_TEXT_PP(1);
5722  NUMDesc Num;
5723  FormatNode *format;
5724  text *result;
5725  bool shouldFree;
5726  int out_pre_spaces = 0,
5727  sign = 0;
5728  char *numstr,
5729  *orgnum,
5730  *p;
5731 
5733 
5734  if (IS_ROMAN(&Num))
5735  numstr = orgnum = int_to_roman((int) rint(value));
5736  else if (IS_EEEE(&Num))
5737  {
5738  if (isnan(value) || is_infinite(value))
5739  {
5740  /*
5741  * Allow 6 characters for the leading sign, the decimal point,
5742  * "e", the exponent's sign and two exponent digits.
5743  */
5744  numstr = (char *) palloc(Num.pre + Num.post + 7);
5745  fill_str(numstr, '#', Num.pre + Num.post + 6);
5746  *numstr = ' ';
5747  *(numstr + Num.pre + 1) = '.';
5748  }
5749  else
5750  {
5751  numstr = orgnum = (char *) psprintf("%+.*e", Num.post, value);
5752 
5753  /*
5754  * Swap a leading positive sign for a space.
5755  */
5756  if (*orgnum == '+')
5757  *orgnum = ' ';
5758 
5759  numstr = orgnum;
5760  }
5761  }
5762  else
5763  {
5764  float8 val = value;
5765  int numstr_pre_len;
5766 
5767  if (IS_MULTI(&Num))
5768  {
5769  double multi = pow((double) 10, (double) Num.multi);
5770 
5771  val = value * multi;
5772  Num.pre += Num.multi;
5773  }
5774  orgnum = psprintf("%.0f", fabs(val));
5775  numstr_pre_len = strlen(orgnum);
5776 
5777  /* adjust post digits to fit max double digits */
5778  if (numstr_pre_len >= DBL_DIG)
5779  Num.post = 0;
5780  else if (numstr_pre_len + Num.post > DBL_DIG)
5781  Num.post = DBL_DIG - numstr_pre_len;
5782  orgnum = psprintf("%.*f", Num.post, val);
5783 
5784  if (*orgnum == '-')
5785  { /* < 0 */
5786  sign = '-';
5787  numstr = orgnum + 1;
5788  }
5789  else
5790  {
5791  sign = '+';
5792  numstr = orgnum;
5793  }
5794 
5795  if ((p = strchr(numstr, '.')))
5796  numstr_pre_len = p - numstr;
5797  else
5798  numstr_pre_len = strlen(numstr);
5799 
5800  /* needs padding? */
5801  if (numstr_pre_len < Num.pre)
5802  out_pre_spaces = Num.pre - numstr_pre_len;
5803  /* overflowed prefix digit format? */
5804  else if (numstr_pre_len > Num.pre)
5805  {
5806  numstr = (char *) palloc(Num.pre + Num.post + 2);
5807  fill_str(numstr, '#', Num.pre + Num.post + 1);
5808  *(numstr + Num.pre) = '.';
5809  }
5810  }
5811 
5813  PG_RETURN_TEXT_P(result);
5814 }
#define PG_GETARG_FLOAT8(n)
Definition: fmgr.h:251
char * psprintf(const char *fmt,...)
Definition: psprintf.c:46
#define IS_EEEE(_f)
Definition: formatting.c:348
#define NUM_TOCHAR_finish
Definition: formatting.c:5202
#define IS_ROMAN(_f)
Definition: formatting.c:346
#define PG_GETARG_TEXT_PP(n)
Definition: fmgr.h:278
#define NUM_TOCHAR_prepare
Definition: formatting.c:5189
double float8
Definition: c.h:458
#define IS_MULTI(_f)
Definition: formatting.c:347
char sign
Definition: informix.c:693
static char * fill_str(char *str, int c, int max)
Definition: formatting.c:3925
static char * int_to_roman(int number)
Definition: formatting.c:4123
int pre
Definition: formatting.c:299
double rint(double x)
Definition: rint.c:22
static struct @131 value
#define PG_RETURN_TEXT_P(x)
Definition: fmgr.h:336
int post
Definition: formatting.c:299
int is_infinite(double val)
Definition: float.c:196
void * palloc(Size size)
Definition: mcxt.c:924
int multi
Definition: formatting.c:299
Definition: c.h:516
static char format
Definition: pg_basebackup.c:83
long val
Definition: informix.c:689

◆ from_char_parse_int()

static int from_char_parse_int ( int *  dest,
char **  src,
FormatNode node 
)
static

Definition at line 2284 of file formatting.c.

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

Referenced by DCH_from_char().

2285 {
2286  return from_char_parse_int_len(dest, src, node->key->len, node);
2287 }
const KeyWord * key
Definition: formatting.c:159
static int from_char_parse_int_len(int *dest, char **src, const int len, FormatNode *node)
Definition: formatting.c:2194
int len
Definition: formatting.c:150

◆ from_char_parse_int_len()

static int from_char_parse_int_len ( int *  dest,
char **  src,
const int  len,
FormatNode node 
)
static

Definition at line 2194 of file formatting.c.

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

Referenced by DCH_from_char(), and from_char_parse_int().

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

◆ from_char_seq_search()

static int from_char_seq_search ( int *  dest,
char **  src,
const char *const *  array,
int  type,
int  max,
FormatNode node 
)
static

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

2374 {
2375  int len;
2376 
2377  *dest = seq_search(*src, array, type, max, &len);
2378  if (len <= 0)
2379  {
2380  char copy[DCH_MAX_ITEM_SIZ + 1];
2381 
2382  Assert(max <= DCH_MAX_ITEM_SIZ);
2383  strlcpy(copy, *src, max + 1);
2384 
2385  ereport(ERROR,
2386  (errcode(ERRCODE_INVALID_DATETIME_FORMAT),
2387  errmsg("invalid value \"%s\" for \"%s\"",
2388  copy, node->key->name),
2389  errdetail("The given value did not match any of the allowed "
2390  "values for this field.")));
2391  }
2392  *src += len;
2393  return len;
2394 }
const KeyWord * key
Definition: formatting.c:159
static int seq_search(char *name, const char *const *array, int type, int max, int *len)
Definition: formatting.c:2294
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:149
size_t strlcpy(char *dst, const char *src, size_t siz)
Definition: strlcpy.c:45
#define Assert(condition)
Definition: c.h:699
int errmsg(const char *fmt,...)
Definition: elog.c:797
#define DCH_MAX_ITEM_SIZ
Definition: formatting.c:117

◆ from_char_set_int()

static void from_char_set_int ( int *  dest,
const int  value,
const FormatNode node 
)
static

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

2162 {
2163  if (*dest != 0 && *dest != value)
2164  ereport(ERROR,
2165  (errcode(ERRCODE_INVALID_DATETIME_FORMAT),
2166  errmsg("conflicting values for \"%s\" field in formatting string",
2167  node->key->name),
2168  errdetail("This value contradicts a previous setting for "
2169  "the same field type.")));
2170  *dest = value;
2171 }
const KeyWord * key
Definition: formatting.c:159
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:149
static struct @131 value
int errmsg(const char *fmt,...)
Definition: elog.c:797

◆ from_char_set_mode()

static void from_char_set_mode ( TmFromChar tmfc,
const FromCharDateMode  mode 
)
static

Definition at line 2139 of file formatting.c.

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

Referenced by DCH_from_char().

2140 {
2141  if (mode != FROM_CHAR_DATE_NONE)
2142  {
2143  if (tmfc->mode == FROM_CHAR_DATE_NONE)
2144  tmfc->mode = mode;
2145  else if (tmfc->mode != mode)
2146  ereport(ERROR,
2147  (errcode(ERRCODE_INVALID_DATETIME_FORMAT),
2148  errmsg("invalid combination of date conventions"),
2149  errhint("Do not mix Gregorian and ISO week date "
2150  "conventions in a formatting template.")));
2151  }
2152 }
int errhint(const char *fmt,...)
Definition: elog.c:987
int errcode(int sqlerrcode)
Definition: elog.c:575
FromCharDateMode mode
Definition: formatting.c:401
#define ERROR
Definition: elog.h:43
#define ereport(elevel, rest)
Definition: elog.h:122
int errmsg(const char *fmt,...)
Definition: elog.c:797

◆ get_last_relevant_decnum()

static char * get_last_relevant_decnum ( char *  num)
static

Definition at line 4254 of file formatting.c.

References elog.

Referenced by NUM_processor().

4255 {
4256  char *result,
4257  *p = strchr(num, '.');
4258 
4259 #ifdef DEBUG_TO_FROM_CHAR
4260  elog(DEBUG_elog_output, "get_last_relevant_decnum()");
4261 #endif
4262 
4263  if (!p)
4264  return NULL;
4265 
4266  result = p;
4267 
4268  while (*(++p))
4269  {
4270  if (*p != '0')
4271  result = p;
4272  }
4273 
4274  return result;
4275 }
#define elog
Definition: elog.h:219

◆ get_th()

static const char * get_th ( char *  num,
int  type 
)
static

Definition at line 1382 of file formatting.c.

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

Referenced by NUM_processor(), and str_numth().

1383 {
1384  int len = strlen(num),
1385  last,
1386  seclast;
1387 
1388  last = *(num + (len - 1));
1389  if (!isdigit((unsigned char) last))
1390  ereport(ERROR,
1391  (errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
1392  errmsg("\"%s\" is not a number", num)));
1393 
1394  /*
1395  * All "teens" (<x>1[0-9]) get 'TH/th', while <x>[02-9][123] still get
1396  * 'ST/st', 'ND/nd', 'RD/rd', respectively
1397  */
1398  if ((len > 1) && ((seclast = num[len - 2]) == '1'))
1399  last = 0;
1400 
1401  switch (last)
1402  {
1403  case '1':
1404  if (type == TH_UPPER)
1405  return numTH[0];
1406  return numth[0];
1407  case '2':
1408  if (type == TH_UPPER)
1409  return numTH[1];
1410  return numth[1];
1411  case '3':
1412  if (type == TH_UPPER)
1413  return numTH[2];
1414  return numth[2];
1415  default:
1416  if (type == TH_UPPER)
1417  return numTH[3];
1418  return numth[3];
1419  }
1420 }
int errcode(int sqlerrcode)
Definition: elog.c:575
static const char *const numTH[]
Definition: formatting.c:271
#define TH_UPPER
Definition: formatting.c:290
#define ERROR
Definition: elog.h:43
#define ereport(elevel, rest)
Definition: elog.h:122
static const char *const numth[]
Definition: formatting.c:272
int errmsg(const char *fmt,...)
Definition: elog.c:797

◆ index_seq_search()

static const KeyWord * index_seq_search ( const char *  str,
const KeyWord kw,
const int *  index 
)
static

Definition at line 1007 of file formatting.c.

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

Referenced by parse_format().

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

◆ int4_to_char()

Datum int4_to_char ( PG_FUNCTION_ARGS  )

Definition at line 5410 of file formatting.c.

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

5411 {
5413  text *fmt = PG_GETARG_TEXT_PP(1);
5414  NUMDesc Num;
5415  FormatNode *format;
5416  text *result;
5417  bool shouldFree;
5418  int out_pre_spaces = 0,
5419  sign = 0;
5420  char *numstr,
5421  *orgnum;
5422 
5424 
5425  /*
5426  * On DateType depend part (int32)
5427  */
5428  if (IS_ROMAN(&Num))
5429  numstr = orgnum = int_to_roman(value);
5430  else if (IS_EEEE(&Num))
5431  {
5432  /* we can do it easily because float8 won't lose any precision */
5433  float8 val = (float8) value;
5434 
5435  orgnum = (char *) psprintf("%+.*e", Num.post, val);
5436 
5437  /*
5438  * Swap a leading positive sign for a space.
5439  */
5440  if (*orgnum == '+')
5441  *orgnum = ' ';
5442 
5443  numstr = orgnum;
5444  }
5445  else
5446  {
5447  int numstr_pre_len;
5448 
5449  if (IS_MULTI(&Num))
5450  {
5452  Int32GetDatum(value * ((int32) pow((double) 10, (double) Num.multi)))));
5453  Num.pre += Num.multi;
5454  }
5455  else
5456  {
5458  Int32GetDatum(value)));
5459  }
5460 
5461  if (*orgnum == '-')
5462  {
5463  sign = '-';
5464  orgnum++;
5465  }
5466  else
5467  sign = '+';
5468 
5469  numstr_pre_len = strlen(orgnum);
5470 
5471  /* post-decimal digits? Pad out with zeros. */
5472  if (Num.post)
5473  {
5474  numstr = (char *) palloc(numstr_pre_len + Num.post + 2);
5475  strcpy(numstr, orgnum);
5476  *(numstr + numstr_pre_len) = '.';
5477  memset(numstr + numstr_pre_len + 1, '0', Num.post);
5478  *(numstr + numstr_pre_len + Num.post + 1) = '\0';
5479  }
5480  else
5481  numstr = orgnum;
5482 
5483  /* needs padding? */
5484  if (numstr_pre_len < Num.pre)
5485  out_pre_spaces = Num.pre - numstr_pre_len;
5486  /* overflowed prefix digit format? */
5487  else if (numstr_pre_len > Num.pre)
5488  {
5489  numstr = (char *) palloc(Num.pre + Num.post + 2);
5490  fill_str(numstr, '#', Num.pre + Num.post + 1);
5491  *(numstr + Num.pre) = '.';
5492  }
5493  }
5494 
5496  PG_RETURN_TEXT_P(result);
5497 }
#define PG_GETARG_INT32(n)
Definition: fmgr.h:239
char * psprintf(const char *fmt,...)
Definition: psprintf.c:46
#define IS_EEEE(_f)
Definition: formatting.c:348
#define DirectFunctionCall1(func, arg1)
Definition: fmgr.h:590
#define NUM_TOCHAR_finish
Definition: formatting.c:5202
#define IS_ROMAN(_f)
Definition: formatting.c:346
signed int int32
Definition: c.h:313
#define PG_GETARG_TEXT_PP(n)
Definition: fmgr.h:278
#define NUM_TOCHAR_prepare
Definition: formatting.c:5189
double float8
Definition: c.h:458
#define DatumGetCString(X)
Definition: postgres.h:551
#define IS_MULTI(_f)
Definition: formatting.c:347
char sign
Definition: informix.c:693
static char * fill_str(char *str, int c, int max)
Definition: formatting.c:3925
static char * int_to_roman(int number)
Definition: formatting.c:4123
int pre
Definition: formatting.c:299
Datum int4out(PG_FUNCTION_ARGS)
Definition: int.c:275
static struct @131 value
#define PG_RETURN_TEXT_P(x)
Definition: fmgr.h:336
int post
Definition: formatting.c:299
#define Int32GetDatum(X)
Definition: postgres.h:464
void * palloc(Size size)
Definition: mcxt.c:924
int multi
Definition: formatting.c:299
Definition: c.h:516
static char format
Definition: pg_basebackup.c:83
long val
Definition: informix.c:689

◆ int8_to_char()

Datum int8_to_char ( PG_FUNCTION_ARGS  )

Definition at line 5504 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, sign, val, and value.

5505 {
5506  int64 value = PG_GETARG_INT64(0);
5507  text *fmt = PG_GETARG_TEXT_PP(1);
5508  NUMDesc Num;
5509  FormatNode *format;
5510  text *result;
5511  bool shouldFree;
5512  int out_pre_spaces = 0,
5513  sign = 0;
5514  char *numstr,
5515  *orgnum;
5516 
5518 
5519  /*
5520  * On DateType depend part (int32)
5521  */
5522  if (IS_ROMAN(&Num))
5523  {
5524  /* Currently don't support int8 conversion to roman... */
5525  numstr = orgnum = int_to_roman(DatumGetInt32(
5527  }
5528  else if (IS_EEEE(&Num))
5529  {
5530  /* to avoid loss of precision, must go via numeric not float8 */
5531  Numeric val;
5532 
5534  Int64GetDatum(value)));
5535  orgnum = numeric_out_sci(val, Num.post);
5536 
5537  /*
5538  * numeric_out_sci() does not emit a sign for positive numbers. We
5539  * need to add a space in this case so that positive and negative
5540  * numbers are aligned. We don't have to worry about NaN here.
5541  */
5542  if (*orgnum != '-')
5543  {
5544  numstr = (char *) palloc(strlen(orgnum) + 2);
5545  *numstr = ' ';
5546  strcpy(numstr + 1, orgnum);
5547  }
5548  else
5549  {
5550  numstr = orgnum;
5551  }
5552  }
5553  else
5554  {
5555  int numstr_pre_len;
5556 
5557  if (IS_MULTI(&Num))
5558  {
5559  double multi = pow((double) 10, (double) Num.multi);
5560 
5562  Int64GetDatum(value),
5564  Float8GetDatum(multi))));
5565  Num.pre += Num.multi;
5566  }
5567 
5569  Int64GetDatum(value)));
5570 
5571  if (*orgnum == '-')
5572  {
5573  sign = '-';
5574  orgnum++;
5575  }
5576  else
5577  sign = '+';
5578 
5579  numstr_pre_len = strlen(orgnum);
5580 
5581  /* post-decimal digits? Pad out with zeros. */
5582  if (Num.post)
5583  {
5584  numstr = (char *) palloc(numstr_pre_len + Num.post + 2);
5585  strcpy(numstr, orgnum);
5586  *(numstr + numstr_pre_len) = '.';
5587  memset(numstr + numstr_pre_len + 1, '0', Num.post);
5588  *(numstr + numstr_pre_len + Num.post + 1) = '\0';
5589  }
5590  else
5591  numstr = orgnum;
5592 
5593  /* needs padding? */
5594  if (numstr_pre_len < Num.pre)
5595  out_pre_spaces = Num.pre - numstr_pre_len;
5596  /* overflowed prefix digit format? */
5597  else if (numstr_pre_len > Num.pre)
5598  {
5599  numstr = (char *) palloc(Num.pre + Num.post + 2);
5600  fill_str(numstr, '#', Num.pre + Num.post + 1);
5601  *(numstr + Num.pre) = '.';
5602  }
5603  }
5604 
5606  PG_RETURN_TEXT_P(result);
5607 }
#define DatumGetInt32(X)
Definition: postgres.h:457
#define IS_EEEE(_f)
Definition: formatting.c:348
#define DirectFunctionCall1(func, arg1)
Definition: fmgr.h:590
#define NUM_TOCHAR_finish
Definition: formatting.c:5202
#define IS_ROMAN(_f)
Definition: formatting.c:346
Datum int8_numeric(PG_FUNCTION_ARGS)
Definition: numeric.c:3132
Datum Float8GetDatum(float8 X)
Definition: fmgr.c:1901
#define PG_GETARG_TEXT_PP(n)
Definition: fmgr.h:278
#define NUM_TOCHAR_prepare
Definition: formatting.c:5189
#define DatumGetCString(X)
Definition: postgres.h:551
#define IS_MULTI(_f)
Definition: formatting.c:347
#define DatumGetInt64(X)
Definition: postgres.h:592
char sign
Definition: informix.c:693
static char * fill_str(char *str, int c, int max)
Definition: formatting.c:3925
Datum int8mul(PG_FUNCTION_ARGS)
Definition: int8.c:568
static char * int_to_roman(int number)
Definition: formatting.c:4123
Datum dtoi8(PG_FUNCTION_ARGS)
Definition: int8.c:1205
int pre
Definition: formatting.c:299
Datum Int64GetDatum(int64 X)
Definition: fmgr.c:1877
static struct @131 value
#define PG_RETURN_TEXT_P(x)
Definition: fmgr.h:336
int post
Definition: formatting.c:299
#define DatumGetNumeric(X)
Definition: numeric.h:49
Datum int84(PG_FUNCTION_ARGS)
Definition: int8.c:1157
void * palloc(Size size)
Definition: mcxt.c:924
int multi
Definition: formatting.c:299
Definition: c.h:516
static char format
Definition: pg_basebackup.c:83
char * numeric_out_sci(Numeric num, int scale)
Definition: numeric.c:730
#define PG_GETARG_INT64(n)
Definition: fmgr.h:252
long val
Definition: informix.c:689
Datum int8out(PG_FUNCTION_ARGS)
Definition: int8.c:146
#define DirectFunctionCall2(func, arg1, arg2)
Definition: fmgr.h:592

◆ int_to_roman()

static char * int_to_roman ( int  number)
static

Definition at line 4123 of file formatting.c.

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

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

4124 {
4125  int len = 0,
4126  num = 0;
4127  char *p = NULL,
4128  *result,
4129  numstr[12];
4130 
4131  result = (char *) palloc(16);
4132  *result = '\0';
4133 
4134  if (number > 3999 || number < 1)
4135  {
4136  fill_str(result, '#', 15);
4137  return result;
4138  }
4139  len = snprintf(numstr, sizeof(numstr), "%d", number);
4140 
4141  for (p = numstr; *p != '\0'; p++, --len)
4142  {
4143  num = *p - 49; /* 48 ascii + 1 */
4144  if (num < 0)
4145  continue;
4146 
4147  if (len > 3)
4148  {
4149  while (num-- != -1)
4150  strcat(result, "M");
4151  }
4152  else
4153  {
4154  if (len == 3)
4155  strcat(result, rm100[num]);
4156  else if (len == 2)
4157  strcat(result, rm10[num]);
4158  else if (len == 1)
4159  strcat(result, rm1[num]);
4160  }
4161  }
4162  return result;
4163 }
int snprintf(char *str, size_t count, const char *fmt,...) pg_attribute_printf(3
static const char *const rm10[]
Definition: formatting.c:264
static const char *const rm1[]
Definition: formatting.c:263
static char * fill_str(char *str, int c, int max)
Definition: formatting.c:3925
static const char *const rm100[]
Definition: formatting.c:265
void * palloc(Size size)
Definition: mcxt.c:924

◆ interval_to_char()

Datum interval_to_char ( PG_FUNCTION_ARGS  )

Definition at line 3522 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, TmToChar::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.

3523 {
3524  Interval *it = PG_GETARG_INTERVAL_P(0);
3525  text *fmt = PG_GETARG_TEXT_PP(1),
3526  *res;
3527  TmToChar tmtc;
3528  struct pg_tm *tm;
3529 
3530  if (VARSIZE_ANY_EXHDR(fmt) <= 0)
3531  PG_RETURN_NULL();
3532 
3533  ZERO_tmtc(&tmtc);
3534  tm = tmtcTm(&tmtc);
3535 
3536  if (interval2tm(*it, tm, &tmtcFsec(&tmtc)) != 0)
3537  PG_RETURN_NULL();
3538 
3539  /* wday is meaningless, yday approximates the total span in days */
3540  tm->tm_yday = (tm->tm_year * MONTHS_PER_YEAR + tm->tm_mon) * DAYS_PER_MONTH + tm->tm_mday;
3541 
3542  if (!(res = datetime_to_char_body(&tmtc, fmt, true, PG_GET_COLLATION())))
3543  PG_RETURN_NULL();
3544 
3545  PG_RETURN_TEXT_P(res);
3546 }
#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:3385
#define ZERO_tmtc(_X)
Definition: formatting.c:472
Definition: pgtime.h:25
#define PG_GET_COLLATION()
Definition: fmgr.h:168
#define tmtcFsec(_X)
Definition: formatting.c:462
#define PG_GETARG_TEXT_PP(n)
Definition: fmgr.h:278
static struct pg_tm tm
Definition: localtime.c:106
#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:336
int tm_year
Definition: pgtime.h:32
#define VARSIZE_ANY_EXHDR(PTR)
Definition: postgres.h:341
int tm_yday
Definition: pgtime.h:34
Definition: c.h:516
#define tmtcTm(_X)
Definition: formatting.c:460
#define PG_RETURN_NULL()
Definition: fmgr.h:310

◆ is_next_separator()

static bool is_next_separator ( FormatNode n)
static

Definition at line 2063 of file formatting.c.

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

Referenced by from_char_parse_int_len().

2064 {
2065  if (n->type == NODE_TYPE_END)
2066  return false;
2067 
2068  if (n->type == NODE_TYPE_ACTION && S_THth(n->suffix))
2069  return true;
2070 
2071  /*
2072  * Next node
2073  */
2074  n++;
2075 
2076  /* end of format string is treated like a non-digit separator */
2077  if (n->type == NODE_TYPE_END)
2078  return true;
2079 
2080  if (n->type == NODE_TYPE_ACTION)
2081  {
2082  if (n->key->is_digit)
2083  return false;
2084 
2085  return true;
2086  }
2087  else if (n->character[1] == '\0' &&
2088  isdigit((unsigned char) n->character[0]))
2089  return false;
2090 
2091  return true; /* some non-digit input (separator) */
2092 }
#define NODE_TYPE_END
Definition: formatting.c:164
const KeyWord * key
Definition: formatting.c:159
#define S_THth(_s)
Definition: formatting.c:510
#define NODE_TYPE_ACTION
Definition: formatting.c:165
char character[MAX_MULTIBYTE_CHAR_LEN+1]
Definition: formatting.c:160
bool is_digit
Definition: formatting.c:152

◆ NUM_cache()

static FormatNode * NUM_cache ( int  len,
NUMDesc Num,
text pars_str,
bool shouldFree 
)
static

Definition at line 4065 of file formatting.c.

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

Referenced by numeric_to_number().

4066 {
4067  FormatNode *format = NULL;
4068  char *str;
4069 
4070  str = text_to_cstring(pars_str);
4071 
4072  if (len > NUM_CACHE_SIZE)
4073  {
4074  /*
4075  * Allocate new memory if format picture is bigger than static cache
4076  * and do not use cache (call parser always)
4077  */
4078  format = (FormatNode *) palloc((len + 1) * sizeof(FormatNode));
4079 
4080  *shouldFree = true;
4081 
4082  zeroize_NUM(Num);
4083 
4084  parse_format(format, str, NUM_keywords,
4085  NULL, NUM_index, NUM_TYPE, Num);
4086  }
4087  else
4088  {
4089  /*
4090  * Use cache buffers
4091  */
4092  NUMCacheEntry *ent = NUM_cache_fetch(str);
4093 
4094  *shouldFree = false;
4095 
4096  format = ent->format;
4097 
4098  /*
4099  * Copy cache to used struct
4100  */
4101  Num->flag = ent->Num.flag;
4102  Num->lsign = ent->Num.lsign;
4103  Num->pre = ent->Num.pre;
4104  Num->post = ent->Num.post;
4105  Num->pre_lsign_num = ent->Num.pre_lsign_num;
4106  Num->need_locale = ent->Num.need_locale;
4107  Num->multi = ent->Num.multi;
4108  Num->zero_start = ent->Num.zero_start;
4109  Num->zero_end = ent->Num.zero_end;
4110  }
4111 
4112 #ifdef DEBUG_TO_FROM_CHAR
4113  /* dump_node(format, len); */
4114  dump_index(NUM_keywords, NUM_index);
4115 #endif
4116 
4117  pfree(str);
4118  return format;
4119 }
NUMDesc Num
Definition: formatting.c:382
int need_locale
Definition: formatting.c:299
int flag
Definition: formatting.c:299
#define NUM_CACHE_SIZE
Definition: formatting.c:363
void pfree(void *pointer)
Definition: mcxt.c:1031
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:1226
int pre
Definition: formatting.c:299
#define NUM_TYPE
Definition: formatting.c:104
static NUMCacheEntry * NUM_cache_fetch(const char *str)
Definition: formatting.c:4037
int post
Definition: formatting.c:299
int zero_end
Definition: formatting.c:299
int pre_lsign_num
Definition: formatting.c:299
static const KeyWord NUM_keywords[]
Definition: formatting.c:824
char * text_to_cstring(const text *t)
Definition: varlena.c:182
void * palloc(Size size)
Definition: mcxt.c:924
FormatNode format[NUM_CACHE_SIZE+1]
Definition: formatting.c:378
int multi
Definition: formatting.c:299
int lsign
Definition: formatting.c:299