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 <wchar.h>
#include "catalog/pg_collation.h"
#include "catalog/pg_type.h"
#include "mb/pg_wchar.h"
#include "parser/scansup.h"
#include "utils/builtins.h"
#include "utils/date.h"
#include "utils/datetime.h"
#include "utils/formatting.h"
#include "utils/int8.h"
#include "utils/memutils.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 RETURN_ERROR(throw_error)
 
#define CHECK_ERROR
 
#define DCH_FLAG   0x1 /* DATE-TIME flag */
 
#define NUM_FLAG   0x2 /* NUMBER flag */
 
#define STD_FLAG   0x4 /* STANDARD flag */
 
#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 NODE_TYPE_SEPARATOR   4
 
#define NODE_TYPE_SPACE   5
 
#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 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 DCH_CACHE_OVERHEAD   MAXALIGN(sizeof(bool) + sizeof(int))
 
#define NUM_CACHE_OVERHEAD   MAXALIGN(sizeof(bool) + sizeof(int) + sizeof(NUMDesc))
 
#define DCH_CACHE_SIZE   ((2048 - DCH_CACHE_OVERHEAD) / (sizeof(FormatNode) + sizeof(char)) - 1)
 
#define NUM_CACHE_SIZE   ((1024 - NUM_CACHE_OVERHEAD) / (sizeof(FormatNode) + sizeof(char)) - 1)
 
#define DCH_CACHE_ENTRIES   20
 
#define NUM_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 DCH_DATED   0x01
 
#define DCH_TIMED   0x02
 
#define DCH_ZONED   0x04
 
#define SKIP_THth(ptr, _suf)
 
#define DCH_to_char_fsec(frac_fmt, frac_val)
 
#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_FF1, DCH_FF2,
  DCH_FF3, DCH_FF4, DCH_FF5, DCH_FF6,
  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_SSSSS, 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_ff1, DCH_ff2,
  DCH_ff3, DCH_ff4, DCH_ff5, DCH_ff6,
  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_sssss, 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 bool is_separator_char (const char *str)
 
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, uint32 flags, 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, const char *in, TmFromChar *out, bool std, bool *have_error)
 
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 (const char *str)
 
static void from_char_set_mode (TmFromChar *tmfc, const FromCharDateMode mode, bool *have_error)
 
static void from_char_set_int (int *dest, const int value, const FormatNode *node, bool *have_error)
 
static int from_char_parse_int_len (int *dest, const char **src, const int len, FormatNode *node, bool *have_error)
 
static int from_char_parse_int (int *dest, const char **src, FormatNode *node, bool *have_error)
 
static int seq_search (const char *name, const char *const *array, int *len)
 
static int from_char_seq_search (int *dest, const char **src, const char *const *array, FormatNode *node, bool *have_error)
 
static void do_to_timestamp (text *date_txt, text *fmt, bool std, struct pg_tm *tm, fsec_t *fsec, int *fprec, uint32 *flags, bool *have_error)
 
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, bool std)
 
static DCHCacheEntryDCH_cache_search (const char *str, bool std)
 
static DCHCacheEntryDCH_cache_fetch (const char *str, bool std)
 
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 void DCH_prevent_counter_overflow (void)
 
static int DCH_datetime_type (FormatNode *node, bool *have_error)
 
static textdatetime_to_char_body (TmToChar *tmtc, text *fmt, bool is_interval, Oid collid)
 
Datum timestamp_to_char (PG_FUNCTION_ARGS)
 
Datum timestamptz_to_char (PG_FUNCTION_ARGS)
 
Datum interval_to_char (PG_FUNCTION_ARGS)
 
Datum to_timestamp (PG_FUNCTION_ARGS)
 
Datum to_date (PG_FUNCTION_ARGS)
 
Datum parse_datetime (text *date_txt, text *fmt, bool strict, Oid *typid, int32 *typmod, int *tz, bool *have_error)
 
static void NUM_prevent_counter_overflow (void)
 
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 DCHCacheEntryDCHCache [DCH_CACHE_ENTRIES]
 
static int n_DCHCache = 0
 
static int DCHCounter = 0
 
static NUMCacheEntryNUMCache [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 238 of file formatting.c.

Referenced by DCH_to_char().

◆ a_d_STR

#define a_d_STR   "a.d."

Definition at line 239 of file formatting.c.

Referenced by DCH_to_char().

◆ A_M_STR

#define A_M_STR   "A.M."

Definition at line 265 of file formatting.c.

Referenced by DCH_to_char().

◆ a_m_STR

#define a_m_STR   "a.m."

Definition at line 266 of file formatting.c.

Referenced by DCH_to_char().

◆ AD_STR

#define AD_STR   "AD"

Definition at line 240 of file formatting.c.

Referenced by DCH_to_char().

◆ ad_STR

#define ad_STR   "ad"

Definition at line 241 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 236 of file formatting.c.

Referenced by DCH_to_char().

◆ AM_STR

#define AM_STR   "AM"

Definition at line 267 of file formatting.c.

Referenced by DCH_to_char().

◆ am_STR

#define am_STR   "am"

Definition at line 268 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 4976 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 243 of file formatting.c.

Referenced by DCH_to_char().

◆ b_c_STR

#define b_c_STR   "b.c."

Definition at line 244 of file formatting.c.

Referenced by DCH_to_char().

◆ BC_STR

#define BC_STR   "BC"

Definition at line 245 of file formatting.c.

Referenced by DCH_to_char().

◆ bc_STR

#define bc_STR   "bc"

Definition at line 246 of file formatting.c.

Referenced by DCH_to_char().

◆ CHECK_ERROR

#define CHECK_ERROR
Value:
do { \
if (have_error && *have_error) \
goto on_error; \
} while (0)

Definition at line 131 of file formatting.c.

Referenced by DCH_from_char(), do_to_timestamp(), from_char_parse_int_len(), and parse_datetime().

◆ CLOCK_12_HOUR

#define CLOCK_12_HOUR   1

Definition at line 213 of file formatting.c.

Referenced by DCH_from_char(), and do_to_timestamp().

◆ CLOCK_24_HOUR

#define CLOCK_24_HOUR   0

Definition at line 212 of file formatting.c.

◆ DCH_CACHE_ENTRIES

#define DCH_CACHE_ENTRIES   20

Definition at line 407 of file formatting.c.

Referenced by DCH_cache_getnew().

◆ DCH_CACHE_OVERHEAD

#define DCH_CACHE_OVERHEAD   MAXALIGN(sizeof(bool) + sizeof(int))

Definition at line 397 of file formatting.c.

◆ DCH_CACHE_SIZE

#define DCH_CACHE_SIZE   ((2048 - DCH_CACHE_OVERHEAD) / (sizeof(FormatNode) + sizeof(char)) - 1)

Definition at line 402 of file formatting.c.

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

◆ DCH_DATED

#define DCH_DATED   0x01

Definition at line 1022 of file formatting.c.

Referenced by DCH_datetime_type(), and parse_datetime().

◆ DCH_FLAG

#define DCH_FLAG   0x1 /* DATE-TIME flag */

◆ DCH_MAX_ITEM_SIZ

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

Definition at line 156 of file formatting.c.

Referenced by datetime_to_char_body(), DCH_to_char(), and from_char_parse_int_len().

◆ DCH_S_FM

#define DCH_S_FM   0x01

Definition at line 544 of file formatting.c.

◆ DCH_S_SP

#define DCH_S_SP   0x08

Definition at line 547 of file formatting.c.

◆ DCH_S_TH

#define DCH_S_TH   0x02

Definition at line 545 of file formatting.c.

◆ DCH_S_th

#define DCH_S_th   0x04

Definition at line 546 of file formatting.c.

◆ DCH_S_TM

#define DCH_S_TM   0x10

Definition at line 548 of file formatting.c.

◆ DCH_TIMED

#define DCH_TIMED   0x02

Definition at line 1023 of file formatting.c.

Referenced by DCH_datetime_type(), and parse_datetime().

◆ DCH_to_char_fsec

#define DCH_to_char_fsec (   frac_fmt,
  frac_val 
)
Value:
sprintf(s, frac_fmt, (int) (frac_val)); \
if (S_THth(n->suffix)) \
str_numth(s, s, S_TH_TYPE(n->suffix)); \
s += strlen(s);
#define S_THth(_s)
Definition: formatting.c:554
#define sprintf
Definition: port.h:194
#define S_TH_TYPE(_s)
Definition: formatting.c:557

Referenced by DCH_to_char().

◆ DCH_ZONED

#define DCH_ZONED   0x04

Definition at line 1024 of file formatting.c.

Referenced by DCH_datetime_type(), and parse_datetime().

◆ DEBUG_TM

#define DEBUG_TM (   _X)

Definition at line 490 of file formatting.c.

Referenced by do_to_timestamp().

◆ DEBUG_TMFC

#define DEBUG_TMFC (   _X)

Definition at line 489 of file formatting.c.

Referenced by do_to_timestamp().

◆ 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:1069
int errcode(int sqlerrcode)
Definition: elog.c:608
#define ERROR
Definition: elog.h:43
int errmsg(const char *fmt,...)
Definition: elog.c:822

Definition at line 527 of file formatting.c.

Referenced by DCH_to_char().

◆ IS_BLANK

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

Definition at line 369 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 370 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 367 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 372 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 374 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:368

Definition at line 5187 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 368 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 150 of file formatting.c.

Referenced by index_seq_search().

◆ KeyWord_INDEX_SIZE

#define KeyWord_INDEX_SIZE   ('~' - ' ')

Definition at line 149 of file formatting.c.

◆ NODE_TYPE_ACTION

#define NODE_TYPE_ACTION   2

◆ NODE_TYPE_CHAR

#define NODE_TYPE_CHAR   3

Definition at line 205 of file formatting.c.

Referenced by parse_format().

◆ NODE_TYPE_END

#define NODE_TYPE_END   1

◆ NODE_TYPE_SEPARATOR

#define NODE_TYPE_SEPARATOR   4

Definition at line 206 of file formatting.c.

Referenced by DCH_from_char(), and parse_format().

◆ NODE_TYPE_SPACE

#define NODE_TYPE_SPACE   5

Definition at line 207 of file formatting.c.

Referenced by DCH_from_char(), and parse_format().

◆ NUM_CACHE_ENTRIES

#define NUM_CACHE_ENTRIES   20

Definition at line 408 of file formatting.c.

Referenced by NUM_cache_getnew().

◆ NUM_CACHE_OVERHEAD

#define NUM_CACHE_OVERHEAD   MAXALIGN(sizeof(bool) + sizeof(int) + sizeof(NUMDesc))

Definition at line 399 of file formatting.c.

◆ NUM_CACHE_SIZE

#define NUM_CACHE_SIZE   ((1024 - NUM_CACHE_OVERHEAD) / (sizeof(FormatNode) + sizeof(char)) - 1)

Definition at line 404 of file formatting.c.

Referenced by NUM_cache(), and NUM_cache_getnew().

◆ NUM_F_BLANK

#define NUM_F_BLANK   (1 << 4)

Definition at line 346 of file formatting.c.

Referenced by NUMDesc_prepare().

◆ NUM_F_BRACKET

#define NUM_F_BRACKET   (1 << 7)

Definition at line 349 of file formatting.c.

Referenced by NUM_processor(), and NUMDesc_prepare().

◆ NUM_F_DECIMAL

#define NUM_F_DECIMAL   (1 << 1)

Definition at line 343 of file formatting.c.

Referenced by NUMDesc_prepare().

◆ NUM_F_EEEE

#define NUM_F_EEEE   (1 << 14)

Definition at line 356 of file formatting.c.

Referenced by NUMDesc_prepare().

◆ NUM_F_FILLMODE

#define NUM_F_FILLMODE   (1 << 5)

Definition at line 347 of file formatting.c.

Referenced by NUM_processor(), and NUMDesc_prepare().

◆ NUM_F_LDECIMAL

#define NUM_F_LDECIMAL   (1 << 2)

Definition at line 344 of file formatting.c.

Referenced by NUMDesc_prepare().

◆ NUM_F_LSIGN

#define NUM_F_LSIGN   (1 << 6)

Definition at line 348 of file formatting.c.

Referenced by NUMDesc_prepare().

◆ NUM_F_MINUS

#define NUM_F_MINUS   (1 << 8)

Definition at line 350 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 355 of file formatting.c.

Referenced by NUMDesc_prepare().

◆ NUM_F_MULTI

#define NUM_F_MULTI   (1 << 11)

Definition at line 353 of file formatting.c.

Referenced by NUMDesc_prepare().

◆ NUM_F_PLUS

#define NUM_F_PLUS   (1 << 9)

Definition at line 351 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 354 of file formatting.c.

Referenced by NUMDesc_prepare().

◆ NUM_F_ROMAN

#define NUM_F_ROMAN   (1 << 10)

Definition at line 352 of file formatting.c.

Referenced by NUM_processor(), and NUMDesc_prepare().

◆ NUM_F_ZERO

#define NUM_F_ZERO   (1 << 3)

Definition at line 345 of file formatting.c.

Referenced by NUMDesc_prepare().

◆ NUM_FLAG

#define NUM_FLAG   0x2 /* NUMBER flag */

Definition at line 142 of file formatting.c.

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

◆ NUM_LSIGN_NONE

#define NUM_LSIGN_NONE   0

Definition at line 360 of file formatting.c.

Referenced by NUMDesc_prepare().

◆ NUM_LSIGN_POST

#define NUM_LSIGN_POST   1

Definition at line 359 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 157 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:561
#define PG_GET_COLLATION()
Definition: fmgr.h:193
char sign
Definition: informix.c:668
static char format
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:5401

Definition at line 5895 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:561
static FormatNode * NUM_cache(int len, NUMDesc *Num, text *pars_str, bool *shouldFree)
Definition: formatting.c:4758
void * palloc0(Size size)
Definition: mcxt.c:980
text * cstring_to_text(const char *s)
Definition: varlena.c:171
#define NUM_MAX_ITEM_SIZ
Definition: formatting.c:157
#define VARSIZE_ANY_EXHDR(PTR)
Definition: postgres.h:341
Definition: c.h:555

Definition at line 5882 of file formatting.c.

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

◆ OVERLOAD_TEST

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

Definition at line 4975 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 270 of file formatting.c.

Referenced by DCH_to_char().

◆ p_m_STR

#define p_m_STR   "p.m."

Definition at line 271 of file formatting.c.

Referenced by DCH_to_char().

◆ PM_STR

#define PM_STR   "PM"

Definition at line 272 of file formatting.c.

Referenced by DCH_to_char().

◆ pm_STR

#define pm_STR   "pm"

Definition at line 273 of file formatting.c.

Referenced by DCH_to_char().

◆ RETURN_ERROR

#define RETURN_ERROR (   throw_error)
Value:
do { \
if (have_error) \
{ \
*have_error = true; \
goto on_error; \
} \
else \
{ \
throw_error; \
} \
} while (0)

Definition at line 118 of file formatting.c.

Referenced by DCH_datetime_type(), DCH_from_char(), do_to_timestamp(), from_char_parse_int_len(), from_char_seq_search(), from_char_set_int(), from_char_set_mode(), and parse_datetime().

◆ S_FM

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

Definition at line 560 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 561 of file formatting.c.

◆ S_TH

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

Definition at line 555 of file formatting.c.

◆ S_th

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

Definition at line 556 of file formatting.c.

◆ S_TH_TYPE

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

Definition at line 557 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 554 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 562 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:554
int pg_mblen(const char *mbstr)
Definition: mbutils.c:802

Definition at line 2146 of file formatting.c.

Referenced by DCH_from_char().

◆ STD_FLAG

#define STD_FLAG   0x4 /* STANDARD flag */

Definition at line 143 of file formatting.c.

Referenced by DCH_cache_fetch(), do_to_timestamp(), and parse_format().

◆ SUFFTYPE_POSTFIX

#define SUFFTYPE_POSTFIX   2

Definition at line 210 of file formatting.c.

Referenced by parse_format().

◆ SUFFTYPE_PREFIX

#define SUFFTYPE_PREFIX   1

Definition at line 209 of file formatting.c.

Referenced by parse_format().

◆ TH_LOWER

#define TH_LOWER   2

Definition at line 320 of file formatting.c.

Referenced by NUM_processor().

◆ TH_UPPER

#define TH_UPPER   1

Definition at line 319 of file formatting.c.

Referenced by get_th(), and NUM_processor().

◆ TM_SUFFIX_LEN

#define TM_SUFFIX_LEN   2

Definition at line 568 of file formatting.c.

Referenced by DCH_to_char().

◆ tmtcFsec

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

Definition at line 506 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 504 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 505 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 508 of file formatting.c.

Referenced by do_to_timestamp().

◆ ZERO_tmfc

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

Definition at line 470 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:504

Definition at line 516 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 4623 of file formatting.c.

Referenced by NUM_cache(), and NUM_cache_fetch().

Typedef Documentation

◆ NUMProc

typedef struct NUMProc NUMProc

◆ TmToChar

typedef struct TmToChar 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_FF1 
DCH_FF2 
DCH_FF3 
DCH_FF4 
DCH_FF5 
DCH_FF6 
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_SSSSS 
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_ff1 
DCH_ff2 
DCH_ff3 
DCH_ff4 
DCH_ff5 
DCH_ff6 
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_sssss 
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 611 of file formatting.c.

612 {
613  DCH_A_D,
614  DCH_A_M,
615  DCH_AD,
616  DCH_AM,
617  DCH_B_C,
618  DCH_BC,
619  DCH_CC,
620  DCH_DAY,
621  DCH_DDD,
622  DCH_DD,
623  DCH_DY,
624  DCH_Day,
625  DCH_Dy,
626  DCH_D,
627  DCH_FF1,
628  DCH_FF2,
629  DCH_FF3,
630  DCH_FF4,
631  DCH_FF5,
632  DCH_FF6,
633  DCH_FX, /* global suffix */
634  DCH_HH24,
635  DCH_HH12,
636  DCH_HH,
637  DCH_IDDD,
638  DCH_ID,
639  DCH_IW,
640  DCH_IYYY,
641  DCH_IYY,
642  DCH_IY,
643  DCH_I,
644  DCH_J,
645  DCH_MI,
646  DCH_MM,
647  DCH_MONTH,
648  DCH_MON,
649  DCH_MS,
650  DCH_Month,
651  DCH_Mon,
652  DCH_OF,
653  DCH_P_M,
654  DCH_PM,
655  DCH_Q,
656  DCH_RM,
657  DCH_SSSSS,
658  DCH_SSSS,
659  DCH_SS,
660  DCH_TZH,
661  DCH_TZM,
662  DCH_TZ,
663  DCH_US,
664  DCH_WW,
665  DCH_W,
666  DCH_Y_YYY,
667  DCH_YYYY,
668  DCH_YYY,
669  DCH_YY,
670  DCH_Y,
671  DCH_a_d,
672  DCH_a_m,
673  DCH_ad,
674  DCH_am,
675  DCH_b_c,
676  DCH_bc,
677  DCH_cc,
678  DCH_day,
679  DCH_ddd,
680  DCH_dd,
681  DCH_dy,
682  DCH_d,
683  DCH_ff1,
684  DCH_ff2,
685  DCH_ff3,
686  DCH_ff4,
687  DCH_ff5,
688  DCH_ff6,
689  DCH_fx,
690  DCH_hh24,
691  DCH_hh12,
692  DCH_hh,
693  DCH_iddd,
694  DCH_id,
695  DCH_iw,
696  DCH_iyyy,
697  DCH_iyy,
698  DCH_iy,
699  DCH_i,
700  DCH_j,
701  DCH_mi,
702  DCH_mm,
703  DCH_month,
704  DCH_mon,
705  DCH_ms,
706  DCH_p_m,
707  DCH_pm,
708  DCH_q,
709  DCH_rm,
710  DCH_sssss,
711  DCH_ssss,
712  DCH_ss,
713  DCH_tz,
714  DCH_us,
715  DCH_ww,
716  DCH_w,
717  DCH_y_yyy,
718  DCH_yyyy,
719  DCH_yyy,
720  DCH_yy,
721  DCH_y,
722 
723  /* last */
724  _DCH_last_
725 } DCH_poz;
DCH_poz
Definition: formatting.c:611

◆ FromCharDateMode

Enumerator
FROM_CHAR_DATE_NONE 
FROM_CHAR_DATE_GREGORIAN 
FROM_CHAR_DATE_ISOWEEK 

Definition at line 179 of file formatting.c.

180 {
181  FROM_CHAR_DATE_NONE = 0, /* Value does not affect date mode. */
182  FROM_CHAR_DATE_GREGORIAN, /* Gregorian (day, month, year) style date */
183  FROM_CHAR_DATE_ISOWEEK /* ISO 8601 week date */
FromCharDateMode
Definition: formatting.c:179

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

728 {
729  NUM_COMMA,
730  NUM_DEC,
731  NUM_0,
732  NUM_9,
733  NUM_B,
734  NUM_C,
735  NUM_D,
736  NUM_E,
737  NUM_FM,
738  NUM_G,
739  NUM_L,
740  NUM_MI,
741  NUM_PL,
742  NUM_PR,
743  NUM_RN,
744  NUM_SG,
745  NUM_SP,
746  NUM_S,
747  NUM_TH,
748  NUM_V,
749  NUM_b,
750  NUM_c,
751  NUM_d,
752  NUM_e,
753  NUM_fm,
754  NUM_g,
755  NUM_l,
756  NUM_mi,
757  NUM_pl,
758  NUM_pr,
759  NUM_rn,
760  NUM_sg,
761  NUM_sp,
762  NUM_s,
763  NUM_th,
764  NUM_v,
765 
766  /* last */
767  _NUM_last_
768 } NUM_poz;
NUM_poz
Definition: formatting.c:727

Function Documentation

◆ adjust_partial_year_to_2020()

static int adjust_partial_year_to_2020 ( int  year)
static

Definition at line 2227 of file formatting.c.

Referenced by DCH_from_char().

2228 {
2229  /*
2230  * Adjust all dates toward 2020; this is effectively what happens when we
2231  * assume '70' is 1970 and '69' is 2069.
2232  */
2233  /* Force 0-69 into the 2000's */
2234  if (year < 70)
2235  return year + 2000;
2236  /* Force 70-99 into the 1900's */
2237  else if (year < 100)
2238  return year + 1900;
2239  /* Force 100-519 into the 2000's */
2240  else if (year < 520)
2241  return year + 2000;
2242  /* Force 520-999 into the 1000's */
2243  else if (year < 1000)
2244  return year + 1000;
2245  else
2246  return year;
2247 }

◆ asc_initcap()

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

Definition at line 2077 of file formatting.c.

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

Referenced by str_initcap().

2078 {
2079  char *result;
2080  char *p;
2081  int wasalnum = false;
2082 
2083  if (!buff)
2084  return NULL;
2085 
2086  result = pnstrdup(buff, nbytes);
2087 
2088  for (p = result; *p; p++)
2089  {
2090  char c;
2091 
2092  if (wasalnum)
2093  *p = c = pg_ascii_tolower((unsigned char) *p);
2094  else
2095  *p = c = pg_ascii_toupper((unsigned char) *p);
2096  /* we don't trust isalnum() here */
2097  wasalnum = ((c >= 'A' && c <= 'Z') ||
2098  (c >= 'a' && c <= 'z') ||
2099  (c >= '0' && c <= '9'));
2100  }
2101 
2102  return result;
2103 }
char * pnstrdup(const char *in, Size len)
Definition: mcxt.c:1197
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 2031 of file formatting.c.

References pg_ascii_tolower(), and pnstrdup().

Referenced by asc_tolower_z(), get_collation_actual_version(), and str_tolower().

2032 {
2033  char *result;
2034  char *p;
2035 
2036  if (!buff)
2037  return NULL;
2038 
2039  result = pnstrdup(buff, nbytes);
2040 
2041  for (p = result; *p; p++)
2042  *p = pg_ascii_tolower((unsigned char) *p);
2043 
2044  return result;
2045 }
char * pnstrdup(const char *in, Size len)
Definition: mcxt.c:1197
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 2126 of file formatting.c.

References asc_tolower().

Referenced by DCH_to_char(), and NUM_processor().

2127 {
2128  return asc_tolower(buff, strlen(buff));
2129 }
char * asc_tolower(const char *buff, size_t nbytes)
Definition: formatting.c:2031

◆ asc_toupper()

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

Definition at line 2054 of file formatting.c.

References pg_ascii_toupper(), and pnstrdup().

Referenced by asc_toupper_z(), and str_toupper().

2055 {
2056  char *result;
2057  char *p;
2058 
2059  if (!buff)
2060  return NULL;
2061 
2062  result = pnstrdup(buff, nbytes);
2063 
2064  for (p = result; *p; p++)
2065  *p = pg_ascii_toupper((unsigned char) *p);
2066 
2067  return result;
2068 }
char * pnstrdup(const char *in, Size len)
Definition: mcxt.c:1197
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 2132 of file formatting.c.

References asc_toupper().

Referenced by DCH_to_char().

2133 {
2134  return asc_toupper(buff, strlen(buff));
2135 }
char * asc_toupper(const char *buff, size_t nbytes)
Definition: formatting.c:2054

◆ datetime_to_char_body()

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

Definition at line 3859 of file formatting.c.

References cstring_to_text(), DCH_cache_fetch(), DCH_CACHE_SIZE, DCH_FLAG, DCH_index, DCH_MAX_ITEM_SIZ, DCH_to_char(), format, DCHCacheEntry::format, palloc(), parse_format(), pfree(), and text_to_cstring().

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

3860 {
3861  FormatNode *format;
3862  char *fmt_str,
3863  *result;
3864  bool incache;
3865  int fmt_len;
3866  text *res;
3867 
3868  /*
3869  * Convert fmt to C string
3870  */
3871  fmt_str = text_to_cstring(fmt);
3872  fmt_len = strlen(fmt_str);
3873 
3874  /*
3875  * Allocate workspace for result as C string
3876  */
3877  result = palloc((fmt_len * DCH_MAX_ITEM_SIZ) + 1);
3878  *result = '\0';
3879 
3880  if (fmt_len > DCH_CACHE_SIZE)
3881  {
3882  /*
3883  * Allocate new memory if format picture is bigger than static cache
3884  * and do not use cache (call parser always)
3885  */
3886  incache = false;
3887 
3888  format = (FormatNode *) palloc((fmt_len + 1) * sizeof(FormatNode));
3889 
3890  parse_format(format, fmt_str, DCH_keywords,
3891  DCH_suff, DCH_index, DCH_FLAG, NULL);
3892  }
3893  else
3894  {
3895  /*
3896  * Use cache buffers
3897  */
3898  DCHCacheEntry *ent = DCH_cache_fetch(fmt_str, false);
3899 
3900  incache = true;
3901  format = ent->format;
3902  }
3903 
3904  /* The real work is here */
3905  DCH_to_char(format, is_interval, tmtc, result, collid);
3906 
3907  if (!incache)
3908  pfree(format);
3909 
3910  pfree(fmt_str);
3911 
3912  /* convert C-string result to TEXT format */
3913  res = cstring_to_text(result);
3914 
3915  pfree(result);
3916  return res;
3917 }
static const KeySuffix DCH_suff[]
Definition: formatting.c:570
static const int DCH_index[KeyWord_INDEX_SIZE]
Definition: formatting.c:944
void pfree(void *pointer)
Definition: mcxt.c:1056
#define DCH_CACHE_SIZE
Definition: formatting.c:402
static void DCH_to_char(FormatNode *node, bool is_interval, TmToChar *in, char *out, Oid collid)
Definition: formatting.c:2565
FormatNode format[DCH_CACHE_SIZE+1]
Definition: formatting.c:412
static void parse_format(FormatNode *node, const char *str, const KeyWord *kw, const KeySuffix *suf, const int *index, uint32 flags, NUMDesc *Num)
Definition: formatting.c:1321
#define DCH_FLAG
Definition: formatting.c:141
text * cstring_to_text(const char *s)
Definition: varlena.c:171
char * text_to_cstring(const text *t)
Definition: varlena.c:204
void * palloc(Size size)
Definition: mcxt.c:949
static const KeyWord DCH_keywords[]
Definition: formatting.c:774
Definition: c.h:555
static char format
#define DCH_MAX_ITEM_SIZ
Definition: formatting.c:156
static DCHCacheEntry * DCH_cache_fetch(const char *str, bool std)
Definition: formatting.c:3832

◆ DCH_cache_fetch()

static DCHCacheEntry * DCH_cache_fetch ( const char *  str,
bool  std 
)
static

Definition at line 3832 of file formatting.c.

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

Referenced by datetime_to_char_body(), and do_to_timestamp().

3833 {
3834  DCHCacheEntry *ent;
3835 
3836  if ((ent = DCH_cache_search(str, std)) == NULL)
3837  {
3838  /*
3839  * Not in the cache, must run parser and save a new format-picture to
3840  * the cache. Do not mark the cache entry valid until parsing
3841  * succeeds.
3842  */
3843  ent = DCH_cache_getnew(str, std);
3844 
3846  DCH_FLAG | (std ? STD_FLAG : 0), NULL);
3847 
3848  ent->valid = true;
3849  }
3850  return ent;
3851 }
static DCHCacheEntry * DCH_cache_getnew(const char *str, bool std)
Definition: formatting.c:3751
static const KeySuffix DCH_suff[]
Definition: formatting.c:570
static const int DCH_index[KeyWord_INDEX_SIZE]
Definition: formatting.c:944
FormatNode format[DCH_CACHE_SIZE+1]
Definition: formatting.c:412
static void parse_format(FormatNode *node, const char *str, const KeyWord *kw, const KeySuffix *suf, const int *index, uint32 flags, NUMDesc *Num)
Definition: formatting.c:1321
static DCHCacheEntry * DCH_cache_search(const char *str, bool std)
Definition: formatting.c:3811
#define DCH_FLAG
Definition: formatting.c:141
static const KeyWord DCH_keywords[]
Definition: formatting.c:774
#define STD_FLAG
Definition: formatting.c:143

◆ DCH_cache_getnew()

static DCHCacheEntry * DCH_cache_getnew ( const char *  str,
bool  std 
)
static

Definition at line 3751 of file formatting.c.

References DCHCacheEntry::age, Assert, DCH_CACHE_ENTRIES, DCH_CACHE_SIZE, DCH_prevent_counter_overflow(), DCHCounter, elog, i, MemoryContextAllocZero(), n_DCHCache, DCHCacheEntry::std, DCHCacheEntry::str, StrNCpy, TopMemoryContext, and DCHCacheEntry::valid.

Referenced by DCH_cache_fetch().

3752 {
3753  DCHCacheEntry *ent;
3754 
3755  /* Ensure we can advance DCHCounter below */
3757 
3758  /*
3759  * If cache is full, remove oldest entry (or recycle first not-valid one)
3760  */
3762  {
3763  DCHCacheEntry *old = DCHCache[0];
3764 
3765 #ifdef DEBUG_TO_FROM_CHAR
3766  elog(DEBUG_elog_output, "cache is full (%d)", n_DCHCache);
3767 #endif
3768  if (old->valid)
3769  {
3770  for (int i = 1; i < DCH_CACHE_ENTRIES; i++)
3771  {
3772  ent = DCHCache[i];
3773  if (!ent->valid)
3774  {
3775  old = ent;
3776  break;
3777  }
3778  if (ent->age < old->age)
3779  old = ent;
3780  }
3781  }
3782 #ifdef DEBUG_TO_FROM_CHAR
3783  elog(DEBUG_elog_output, "OLD: '%s' AGE: %d", old->str, old->age);
3784 #endif
3785  old->valid = false;
3786  StrNCpy(old->str, str, DCH_CACHE_SIZE + 1);
3787  old->age = (++DCHCounter);
3788  /* caller is expected to fill format, then set valid */
3789  return old;
3790  }
3791  else
3792  {
3793 #ifdef DEBUG_TO_FROM_CHAR
3794  elog(DEBUG_elog_output, "NEW (%d)", n_DCHCache);
3795 #endif
3796  Assert(DCHCache[n_DCHCache] == NULL);
3797  DCHCache[n_DCHCache] = ent = (DCHCacheEntry *)
3799  ent->valid = false;
3800  StrNCpy(ent->str, str, DCH_CACHE_SIZE + 1);
3801  ent->std = std;
3802  ent->age = (++DCHCounter);
3803  /* caller is expected to fill format, then set valid */
3804  ++n_DCHCache;
3805  return ent;
3806  }
3807 }
char str[DCH_CACHE_SIZE+1]
Definition: formatting.c:413
#define DCH_CACHE_SIZE
Definition: formatting.c:402
static int n_DCHCache
Definition: formatting.c:430
MemoryContext TopMemoryContext
Definition: mcxt.c:44
#define DCH_CACHE_ENTRIES
Definition: formatting.c:407
void * MemoryContextAllocZero(MemoryContext context, Size size)
Definition: mcxt.c:839
#define Assert(condition)
Definition: c.h:738
#define StrNCpy(dst, src, len)
Definition: c.h:944
static void DCH_prevent_counter_overflow(void)
Definition: formatting.c:3631
#define elog(elevel,...)
Definition: elog.h:228
int i
static DCHCacheEntry * DCHCache[DCH_CACHE_ENTRIES]
Definition: formatting.c:429
static int DCHCounter
Definition: formatting.c:431

◆ DCH_cache_search()

static DCHCacheEntry * DCH_cache_search ( const char *  str,
bool  std 
)
static

Definition at line 3811 of file formatting.c.

References DCHCacheEntry::age, DCH_prevent_counter_overflow(), DCHCounter, i, n_DCHCache, DCHCacheEntry::std, DCHCacheEntry::str, and DCHCacheEntry::valid.

Referenced by DCH_cache_fetch().

3812 {
3813  /* Ensure we can advance DCHCounter below */
3815 
3816  for (int i = 0; i < n_DCHCache; i++)
3817  {
3818  DCHCacheEntry *ent = DCHCache[i];
3819 
3820  if (ent->valid && strcmp(ent->str, str) == 0 && ent->std == std)
3821  {
3822  ent->age = (++DCHCounter);
3823  return ent;
3824  }
3825  }
3826 
3827  return NULL;
3828 }
char str[DCH_CACHE_SIZE+1]
Definition: formatting.c:413
static int n_DCHCache
Definition: formatting.c:430
static void DCH_prevent_counter_overflow(void)
Definition: formatting.c:3631
int i
static DCHCacheEntry * DCHCache[DCH_CACHE_ENTRIES]
Definition: formatting.c:429
static int DCHCounter
Definition: formatting.c:431

◆ DCH_datetime_type()

static int DCH_datetime_type ( FormatNode node,
bool have_error 
)
static

Definition at line 3647 of file formatting.c.

References 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_DATED, DCH_DAY, DCH_Day, DCH_day, DCH_DD, DCH_DDD, DCH_DY, DCH_Dy, DCH_dy, DCH_FF1, DCH_FF2, DCH_FF3, DCH_FF4, DCH_FF5, DCH_FF6, DCH_FX, DCH_HH, DCH_HH12, DCH_HH24, DCH_I, DCH_ID, DCH_IDDD, 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_TIMED, 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, DCH_ZONED, ereport, errcode(), errmsg(), ERROR, KeyWord::id, FormatNode::key, KeyWord::name, NODE_TYPE_ACTION, NODE_TYPE_END, RETURN_ERROR, and FormatNode::type.

Referenced by do_to_timestamp().

3648 {
3649  FormatNode *n;
3650  int flags = 0;
3651 
3652  for (n = node; n->type != NODE_TYPE_END; n++)
3653  {
3654  if (n->type != NODE_TYPE_ACTION)
3655  continue;
3656 
3657  switch (n->key->id)
3658  {
3659  case DCH_FX:
3660  break;
3661  case DCH_A_M:
3662  case DCH_P_M:
3663  case DCH_a_m:
3664  case DCH_p_m:
3665  case DCH_AM:
3666  case DCH_PM:
3667  case DCH_am:
3668  case DCH_pm:
3669  case DCH_HH:
3670  case DCH_HH12:
3671  case DCH_HH24:
3672  case DCH_MI:
3673  case DCH_SS:
3674  case DCH_MS: /* millisecond */
3675  case DCH_US: /* microsecond */
3676  case DCH_FF1:
3677  case DCH_FF2:
3678  case DCH_FF3:
3679  case DCH_FF4:
3680  case DCH_FF5:
3681  case DCH_FF6:
3682  case DCH_SSSS:
3683  flags |= DCH_TIMED;
3684  break;
3685  case DCH_tz:
3686  case DCH_TZ:
3687  case DCH_OF:
3689  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
3690  errmsg("formatting field \"%s\" is only supported in to_char",
3691  n->key->name))));
3692  flags |= DCH_ZONED;
3693  break;
3694  case DCH_TZH:
3695  case DCH_TZM:
3696  flags |= DCH_ZONED;
3697  break;
3698  case DCH_A_D:
3699  case DCH_B_C:
3700  case DCH_a_d:
3701  case DCH_b_c:
3702  case DCH_AD:
3703  case DCH_BC:
3704  case DCH_ad:
3705  case DCH_bc:
3706  case DCH_MONTH:
3707  case DCH_Month:
3708  case DCH_month:
3709  case DCH_MON:
3710  case DCH_Mon:
3711  case DCH_mon:
3712  case DCH_MM:
3713  case DCH_DAY:
3714  case DCH_Day:
3715  case DCH_day:
3716  case DCH_DY:
3717  case DCH_Dy:
3718  case DCH_dy:
3719  case DCH_DDD:
3720  case DCH_IDDD:
3721  case DCH_DD:
3722  case DCH_D:
3723  case DCH_ID:
3724  case DCH_WW:
3725  case DCH_Q:
3726  case DCH_CC:
3727  case DCH_Y_YYY:
3728  case DCH_YYYY:
3729  case DCH_IYYY:
3730  case DCH_YYY:
3731  case DCH_IYY:
3732  case DCH_YY:
3733  case DCH_IY:
3734  case DCH_Y:
3735  case DCH_I:
3736  case DCH_RM:
3737  case DCH_rm:
3738  case DCH_W:
3739  case DCH_J:
3740  flags |= DCH_DATED;
3741  break;
3742  }
3743  }
3744 
3745 on_error:
3746  return flags;
3747 }
#define NODE_TYPE_END
Definition: formatting.c:203
uint8 type
Definition: formatting.c:197
const KeyWord * key
Definition: formatting.c:200
int errcode(int sqlerrcode)
Definition: elog.c:608
int id
Definition: formatting.c:190
#define DCH_TIMED
Definition: formatting.c:1023
#define DCH_DATED
Definition: formatting.c:1022
#define NODE_TYPE_ACTION
Definition: formatting.c:204
#define ERROR
Definition: elog.h:43
#define DCH_ZONED
Definition: formatting.c:1024
#define ereport(elevel, rest)
Definition: elog.h:141
const char * name
Definition: formatting.c:188
int errmsg(const char *fmt,...)
Definition: elog.c:822
#define RETURN_ERROR(throw_error)
Definition: formatting.c:118

◆ DCH_from_char()

static void DCH_from_char ( FormatNode node,
const char *  in,
TmFromChar out,
bool  std,
bool have_error 
)
static

Definition at line 3161 of file formatting.c.

References adbc_strings, adbc_strings_long, adjust_partial_year_to_2020(), ampm_strings, ampm_strings_long, Assert, TmFromChar::bc, TmFromChar::cc, FormatNode::character, CHECK_ERROR, TmFromChar::clock, CLOCK_12_HOUR, TmFromChar::d, KeyWord::date_mode, 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_FF1, DCH_FF2, DCH_FF3, DCH_FF4, DCH_FF5, DCH_FF6, 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, TmFromChar::ff, 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, is_separator_char(), TmFromChar::j, FormatNode::key, TmFromChar::mi, TmFromChar::mm, months, months_full, MONTHS_PER_YEAR, TmFromChar::ms, KeyWord::name, NODE_TYPE_ACTION, NODE_TYPE_END, NODE_TYPE_SEPARATOR, NODE_TYPE_SPACE, pg_mblen(), TmFromChar::pm, RETURN_ERROR, rm_months_lower, 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().

3163 {
3164  FormatNode *n;
3165  const char *s;
3166  int len,
3167  value;
3168  bool fx_mode = std;
3169 
3170  /* number of extra skipped characters (more than given in format string) */
3171  int extra_skip = 0;
3172 
3173  for (n = node, s = in; n->type != NODE_TYPE_END && *s != '\0'; n++)
3174  {
3175  /*
3176  * Ignore spaces at the beginning of the string and before fields when
3177  * not in FX (fixed width) mode.
3178  */
3179  if (!fx_mode && (n->type != NODE_TYPE_ACTION || n->key->id != DCH_FX) &&
3180  (n->type == NODE_TYPE_ACTION || n == node))
3181  {
3182  while (*s != '\0' && isspace((unsigned char) *s))
3183  {
3184  s++;
3185  extra_skip++;
3186  }
3187  }
3188 
3189  if (n->type == NODE_TYPE_SPACE || n->type == NODE_TYPE_SEPARATOR)
3190  {
3191  if (std)
3192  {
3193  /*
3194  * Standard mode requires strict matching between format
3195  * string separators/spaces and input string.
3196  */
3197  Assert(n->character[0] && !n->character[1]);
3198 
3199  if (*s == n->character[0])
3200  s++;
3201  else
3203  (errcode(ERRCODE_INVALID_DATETIME_FORMAT),
3204  errmsg("unmatched format separator \"%c\"",
3205  n->character[0]))));
3206  }
3207  else if (!fx_mode)
3208  {
3209  /*
3210  * In non FX (fixed format) mode one format string space or
3211  * separator match to one space or separator in input string.
3212  * Or match nothing if there is no space or separator in the
3213  * current position of input string.
3214  */
3215  extra_skip--;
3216  if (isspace((unsigned char) *s) || is_separator_char(s))
3217  {
3218  s++;
3219  extra_skip++;
3220  }
3221  }
3222  else
3223  {
3224  /*
3225  * In FX mode, on format string space or separator we consume
3226  * exactly one character from input string. Notice we don't
3227  * insist that the consumed character match the format's
3228  * character.
3229  */
3230  s += pg_mblen(s);
3231  }
3232  continue;
3233  }
3234  else if (n->type != NODE_TYPE_ACTION)
3235  {
3236  /*
3237  * Text character, so consume one character from input string.
3238  * Notice we don't insist that the consumed character match the
3239  * format's character.
3240  */
3241  if (!fx_mode)
3242  {
3243  /*
3244  * In non FX mode we might have skipped some extra characters
3245  * (more than specified in format string) before. In this
3246  * case we don't skip input string character, because it might
3247  * be part of field.
3248  */
3249  if (extra_skip > 0)
3250  extra_skip--;
3251  else
3252  s += pg_mblen(s);
3253  }
3254  else
3255  {
3256  s += pg_mblen(s);
3257  }
3258  continue;
3259  }
3260 
3261  from_char_set_mode(out, n->key->date_mode, have_error);
3262  CHECK_ERROR;
3263 
3264  switch (n->key->id)
3265  {
3266  case DCH_FX:
3267  fx_mode = true;
3268  break;
3269  case DCH_A_M:
3270  case DCH_P_M:
3271  case DCH_a_m:
3272  case DCH_p_m:
3274  n, have_error);
3275  CHECK_ERROR;
3276  from_char_set_int(&out->pm, value % 2, n, have_error);
3277  CHECK_ERROR;
3278  out->clock = CLOCK_12_HOUR;
3279  break;
3280  case DCH_AM:
3281  case DCH_PM:
3282  case DCH_am:
3283  case DCH_pm:
3284  from_char_seq_search(&value, &s, ampm_strings,
3285  n, have_error);
3286  CHECK_ERROR;
3287  from_char_set_int(&out->pm, value % 2, n, have_error);
3288  CHECK_ERROR;
3289  out->clock = CLOCK_12_HOUR;
3290  break;
3291  case DCH_HH:
3292  case DCH_HH12:
3293  from_char_parse_int_len(&out->hh, &s, 2, n, have_error);
3294  CHECK_ERROR;
3295  out->clock = CLOCK_12_HOUR;
3296  SKIP_THth(s, n->suffix);
3297  break;
3298  case DCH_HH24:
3299  from_char_parse_int_len(&out->hh, &s, 2, n, have_error);
3300  CHECK_ERROR;
3301  SKIP_THth(s, n->suffix);
3302  break;
3303  case DCH_MI:
3304  from_char_parse_int(&out->mi, &s, n, have_error);
3305  CHECK_ERROR;
3306  SKIP_THth(s, n->suffix);
3307  break;
3308  case DCH_SS:
3309  from_char_parse_int(&out->ss, &s, n, have_error);
3310  CHECK_ERROR;
3311  SKIP_THth(s, n->suffix);
3312  break;
3313  case DCH_MS: /* millisecond */
3314  len = from_char_parse_int_len(&out->ms, &s, 3, n, have_error);
3315  CHECK_ERROR;
3316 
3317  /*
3318  * 25 is 0.25 and 250 is 0.25 too; 025 is 0.025 and not 0.25
3319  */
3320  out->ms *= len == 1 ? 100 :
3321  len == 2 ? 10 : 1;
3322 
3323  SKIP_THth(s, n->suffix);
3324  break;
3325  case DCH_FF1:
3326  case DCH_FF2:
3327  case DCH_FF3:
3328  case DCH_FF4:
3329  case DCH_FF5:
3330  case DCH_FF6:
3331  out->ff = n->key->id - DCH_FF1 + 1;
3332  /* fall through */
3333  case DCH_US: /* microsecond */
3334  len = from_char_parse_int_len(&out->us, &s,
3335  n->key->id == DCH_US ? 6 :
3336  out->ff, n, have_error);
3337  CHECK_ERROR;
3338 
3339  out->us *= len == 1 ? 100000 :
3340  len == 2 ? 10000 :
3341  len == 3 ? 1000 :
3342  len == 4 ? 100 :
3343  len == 5 ? 10 : 1;
3344 
3345  SKIP_THth(s, n->suffix);
3346  break;
3347  case DCH_SSSS:
3348  from_char_parse_int(&out->ssss, &s, n, have_error);
3349  CHECK_ERROR;
3350  SKIP_THth(s, n->suffix);
3351  break;
3352  case DCH_tz:
3353  case DCH_TZ:
3354  case DCH_OF:
3356  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
3357  errmsg("formatting field \"%s\" is only supported in to_char",
3358  n->key->name))));
3359  CHECK_ERROR;
3360  break;
3361  case DCH_TZH:
3362 
3363  /*
3364  * Value of TZH might be negative. And the issue is that we
3365  * might swallow minus sign as the separator. So, if we have
3366  * skipped more characters than specified in the format
3367  * string, then we consider prepending last skipped minus to
3368  * TZH.
3369  */
3370  if (*s == '+' || *s == '-' || *s == ' ')
3371  {
3372  out->tzsign = *s == '-' ? -1 : +1;
3373  s++;
3374  }
3375  else
3376  {
3377  if (extra_skip > 0 && *(s - 1) == '-')
3378  out->tzsign = -1;
3379  else
3380  out->tzsign = +1;
3381  }
3382 
3383  from_char_parse_int_len(&out->tzh, &s, 2, n, have_error);
3384  CHECK_ERROR;
3385  break;
3386  case DCH_TZM:
3387  /* assign positive timezone sign if TZH was not seen before */
3388  if (!out->tzsign)
3389  out->tzsign = +1;
3390  from_char_parse_int_len(&out->tzm, &s, 2, n, have_error);
3391  CHECK_ERROR;
3392  break;
3393  case DCH_A_D:
3394  case DCH_B_C:
3395  case DCH_a_d:
3396  case DCH_b_c:
3398  n, have_error);
3399  CHECK_ERROR;
3400  from_char_set_int(&out->bc, value % 2, n, have_error);
3401  CHECK_ERROR;
3402  break;
3403  case DCH_AD:
3404  case DCH_BC:
3405  case DCH_ad:
3406  case DCH_bc:
3407  from_char_seq_search(&value, &s, adbc_strings,
3408  n, have_error);
3409  CHECK_ERROR;
3410  from_char_set_int(&out->bc, value % 2, n, have_error);
3411  CHECK_ERROR;
3412  break;
3413  case DCH_MONTH:
3414  case DCH_Month:
3415  case DCH_month:
3416  from_char_seq_search(&value, &s, months_full,
3417  n, have_error);
3418  CHECK_ERROR;
3419  from_char_set_int(&out->mm, value + 1, n, have_error);
3420  CHECK_ERROR;
3421  break;
3422  case DCH_MON:
3423  case DCH_Mon:
3424  case DCH_mon:
3425  from_char_seq_search(&value, &s, months,
3426  n, have_error);
3427  CHECK_ERROR;
3428  from_char_set_int(&out->mm, value + 1, n, have_error);
3429  CHECK_ERROR;
3430  break;
3431  case DCH_MM:
3432  from_char_parse_int(&out->mm, &s, n, have_error);
3433  CHECK_ERROR;
3434  SKIP_THth(s, n->suffix);
3435  break;
3436  case DCH_DAY:
3437  case DCH_Day:
3438  case DCH_day:
3439  from_char_seq_search(&value, &s, days,
3440  n, have_error);
3441  CHECK_ERROR;
3442  from_char_set_int(&out->d, value, n, have_error);
3443  CHECK_ERROR;
3444  out->d++;
3445  break;
3446  case DCH_DY:
3447  case DCH_Dy:
3448  case DCH_dy:
3449  from_char_seq_search(&value, &s, days_short,
3450  n, have_error);
3451  CHECK_ERROR;
3452  from_char_set_int(&out->d, value, n, have_error);
3453  CHECK_ERROR;
3454  out->d++;
3455  break;
3456  case DCH_DDD:
3457  from_char_parse_int(&out->ddd, &s, n, have_error);
3458  CHECK_ERROR;
3459  SKIP_THth(s, n->suffix);
3460  break;
3461  case DCH_IDDD:
3462  from_char_parse_int_len(&out->ddd, &s, 3, n, have_error);
3463  CHECK_ERROR;
3464  SKIP_THth(s, n->suffix);
3465  break;
3466  case DCH_DD:
3467  from_char_parse_int(&out->dd, &s, n, have_error);
3468  CHECK_ERROR;
3469  SKIP_THth(s, n->suffix);
3470  break;
3471  case DCH_D:
3472  from_char_parse_int(&out->d, &s, n, have_error);
3473  CHECK_ERROR;
3474  SKIP_THth(s, n->suffix);
3475  break;
3476  case DCH_ID:
3477  from_char_parse_int_len(&out->d, &s, 1, n, have_error);
3478  CHECK_ERROR;
3479  /* Shift numbering to match Gregorian where Sunday = 1 */
3480  if (++out->d > 7)
3481  out->d = 1;
3482  SKIP_THth(s, n->suffix);
3483  break;
3484  case DCH_WW:
3485  case DCH_IW:
3486  from_char_parse_int(&out->ww, &s, n, have_error);
3487  CHECK_ERROR;
3488  SKIP_THth(s, n->suffix);
3489  break;
3490  case DCH_Q:
3491 
3492  /*
3493  * We ignore 'Q' when converting to date because it is unclear
3494  * which date in the quarter to use, and some people specify
3495  * both quarter and month, so if it was honored it might
3496  * conflict with the supplied month. That is also why we don't
3497  * throw an error.
3498  *
3499  * We still parse the source string for an integer, but it
3500  * isn't stored anywhere in 'out'.
3501  */
3502  from_char_parse_int((int *) NULL, &s, n, have_error);
3503  CHECK_ERROR;
3504  SKIP_THth(s, n->suffix);
3505  break;
3506  case DCH_CC:
3507  from_char_parse_int(&out->cc, &s, n, have_error);
3508  CHECK_ERROR;
3509  SKIP_THth(s, n->suffix);
3510  break;
3511  case DCH_Y_YYY:
3512  {
3513  int matched,
3514  years,
3515  millennia,
3516  nch;
3517 
3518  matched = sscanf(s, "%d,%03d%n", &millennia, &years, &nch);
3519  if (matched < 2)
3521  (errcode(ERRCODE_INVALID_DATETIME_FORMAT),
3522  errmsg("invalid input string for \"Y,YYY\""))));
3523  years += (millennia * 1000);
3524  from_char_set_int(&out->year, years, n, have_error);
3525  CHECK_ERROR;
3526  out->yysz = 4;
3527  s += nch;
3528  SKIP_THth(s, n->suffix);
3529  }
3530  break;
3531  case DCH_YYYY:
3532  case DCH_IYYY:
3533  from_char_parse_int(&out->year, &s, n, have_error);
3534  CHECK_ERROR;
3535  out->yysz = 4;
3536  SKIP_THth(s, n->suffix);
3537  break;
3538  case DCH_YYY:
3539  case DCH_IYY:
3540  len = from_char_parse_int(&out->year, &s, n, have_error);
3541  CHECK_ERROR;
3542  if (len < 4)
3543  out->year = adjust_partial_year_to_2020(out->year);
3544  out->yysz = 3;
3545  SKIP_THth(s, n->suffix);
3546  break;
3547  case DCH_YY:
3548  case DCH_IY:
3549  len = from_char_parse_int(&out->year, &s, n, have_error);
3550  CHECK_ERROR;
3551  if (len < 4)
3552  out->year = adjust_partial_year_to_2020(out->year);
3553  out->yysz = 2;
3554  SKIP_THth(s, n->suffix);
3555  break;
3556  case DCH_Y:
3557  case DCH_I:
3558  len = from_char_parse_int(&out->year, &s, n, have_error);
3559  CHECK_ERROR;
3560  if (len < 4)
3561  out->year = adjust_partial_year_to_2020(out->year);
3562  out->yysz = 1;
3563  SKIP_THth(s, n->suffix);
3564  break;
3565  case DCH_RM:
3566  case DCH_rm:
3568  n, have_error);
3569  CHECK_ERROR;
3570  from_char_set_int(&out->mm, MONTHS_PER_YEAR - value,
3571  n, have_error);
3572  CHECK_ERROR;
3573  break;
3574  case DCH_W:
3575  from_char_parse_int(&out->w, &s, n, have_error);
3576  CHECK_ERROR;
3577  SKIP_THth(s, n->suffix);
3578  break;
3579  case DCH_J:
3580  from_char_parse_int(&out->j, &s, n, have_error);
3581  CHECK_ERROR;
3582  SKIP_THth(s, n->suffix);
3583  break;
3584  }
3585 
3586  /* Ignore all spaces after fields */
3587  if (!fx_mode)
3588  {
3589  extra_skip = 0;
3590  while (*s != '\0' && isspace((unsigned char) *s))
3591  {
3592  s++;
3593  extra_skip++;
3594  }
3595  }
3596  }
3597 
3598  /*
3599  * Standard parsing mode doesn't allow unmatched format patterns or
3600  * trailing characters in the input string.
3601  */
3602  if (std)
3603  {
3604  if (n->type != NODE_TYPE_END)
3606  (errcode(ERRCODE_INVALID_DATETIME_FORMAT),
3607  errmsg("input string is too short for datetime format"))));
3608 
3609  while (*s != '\0' && isspace((unsigned char) *s))
3610  s++;
3611 
3612  if (*s != '\0')
3614  (errcode(ERRCODE_INVALID_DATETIME_FORMAT),
3615  errmsg("trailing characters remain in input string "
3616  "after datetime format"))));
3617  }
3618 
3619 on_error:
3620  return;
3621 }
#define NODE_TYPE_END
Definition: formatting.c:203
uint8 type
Definition: formatting.c:197
const KeyWord * key
Definition: formatting.c:200
static const char *const adbc_strings_long[]
Definition: formatting.c:259
FromCharDateMode date_mode
Definition: formatting.c:192
static struct @145 value
int errcode(int sqlerrcode)
Definition: elog.c:608
static void from_char_set_mode(TmFromChar *tmfc, const FromCharDateMode mode, bool *have_error)
Definition: formatting.c:2272
int id
Definition: formatting.c:190
static const char *const ampm_strings_long[]
Definition: formatting.c:286
#define CHECK_ERROR
Definition: formatting.c:131
static const char *const adbc_strings[]
Definition: formatting.c:258
#define MONTHS_PER_YEAR
Definition: timestamp.h:69
const char *const months[]
Definition: datetime.c:65
#define NODE_TYPE_ACTION
Definition: formatting.c:204
#define ERROR
Definition: elog.h:43
char character[MAX_MULTIBYTE_CHAR_LEN+1]
Definition: formatting.c:198
#define SKIP_THth(ptr, _suf)
Definition: formatting.c:2146
static bool is_separator_char(const char *str)
Definition: formatting.c:1132
#define NODE_TYPE_SPACE
Definition: formatting.c:207
const char *const days[]
Definition: datetime.c:68
#define NODE_TYPE_SEPARATOR
Definition: formatting.c:206
#define ereport(elevel, rest)
Definition: elog.h:141
#define CLOCK_12_HOUR
Definition: formatting.c:213
static const char *const ampm_strings[]
Definition: formatting.c:285
const char * name
Definition: formatting.c:188
static const char *const months_full[]
Definition: formatting.c:220
static int adjust_partial_year_to_2020(int year)
Definition: formatting.c:2227
#define Assert(condition)
Definition: c.h:738
int pg_mblen(const char *mbstr)
Definition: mbutils.c:802
uint8 suffix
Definition: formatting.c:199
static int from_char_seq_search(int *dest, const char **src, const char *const *array, FormatNode *node, bool *have_error)
Definition: formatting.c:2520
static int from_char_parse_int(int *dest, const char **src, FormatNode *node, bool *have_error)
Definition: formatting.c:2443
int errmsg(const char *fmt,...)
Definition: elog.c:822
static void from_char_set_int(int *dest, const int value, const FormatNode *node, bool *have_error)
Definition: formatting.c:2299
static const char *const rm_months_lower[]
Definition: formatting.c:297
#define RETURN_ERROR(throw_error)
Definition: formatting.c:118
static int from_char_parse_int_len(int *dest, const char **src, const int len, FormatNode *node, bool *have_error)
Definition: formatting.c:2340
static const char *const days_short[]
Definition: formatting.c:225

◆ DCH_prevent_counter_overflow()

static void DCH_prevent_counter_overflow ( void  )
inlinestatic

Definition at line 3631 of file formatting.c.

References DCHCounter, i, and n_DCHCache.

Referenced by DCH_cache_getnew(), and DCH_cache_search().

3632 {
3633  if (DCHCounter >= (INT_MAX - 1))
3634  {
3635  for (int i = 0; i < n_DCHCache; i++)
3636  DCHCache[i]->age >>= 1;
3637  DCHCounter >>= 1;
3638  }
3639 }
static int n_DCHCache
Definition: formatting.c:430
int i
static DCHCacheEntry * DCHCache[DCH_CACHE_ENTRIES]
Definition: formatting.c:429
static int DCHCounter
Definition: formatting.c:431

◆ DCH_to_char()

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

Definition at line 2565 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_FF1, DCH_FF2, DCH_FF3, DCH_FF4, DCH_FF5, DCH_FF6, 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_to_char_fsec, 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, sprintf, 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().

2566 {
2567  FormatNode *n;
2568  char *s;
2569  struct pg_tm *tm = &in->tm;
2570  int i;
2571 
2572  /* cache localized days and months */
2574 
2575  s = out;
2576  for (n = node; n->type != NODE_TYPE_END; n++)
2577  {
2578  if (n->type != NODE_TYPE_ACTION)
2579  {
2580  strcpy(s, n->character);
2581  s += strlen(s);
2582  continue;
2583  }
2584 
2585  switch (n->key->id)
2586  {
2587  case DCH_A_M:
2588  case DCH_P_M:
2589  strcpy(s, (tm->tm_hour % HOURS_PER_DAY >= HOURS_PER_DAY / 2)
2590  ? P_M_STR : A_M_STR);
2591  s += strlen(s);
2592  break;
2593  case DCH_AM:
2594  case DCH_PM:
2595  strcpy(s, (tm->tm_hour % HOURS_PER_DAY >= HOURS_PER_DAY / 2)
2596  ? PM_STR : AM_STR);
2597  s += strlen(s);
2598  break;
2599  case DCH_a_m:
2600  case DCH_p_m:
2601  strcpy(s, (tm->tm_hour % HOURS_PER_DAY >= HOURS_PER_DAY / 2)
2602  ? p_m_STR : a_m_STR);
2603  s += strlen(s);
2604  break;
2605  case DCH_am:
2606  case DCH_pm:
2607  strcpy(s, (tm->tm_hour % HOURS_PER_DAY >= HOURS_PER_DAY / 2)
2608  ? pm_STR : am_STR);
2609  s += strlen(s);
2610  break;
2611  case DCH_HH:
2612  case DCH_HH12:
2613 
2614  /*
2615  * display time as shown on a 12-hour clock, even for
2616  * intervals
2617  */
2618  sprintf(s, "%0*d", S_FM(n->suffix) ? 0 : (tm->tm_hour >= 0) ? 2 : 3,
2619  tm->tm_hour % (HOURS_PER_DAY / 2) == 0 ? HOURS_PER_DAY / 2 :
2620  tm->tm_hour % (HOURS_PER_DAY / 2));
2621  if (S_THth(n->suffix))
2622  str_numth(s, s, S_TH_TYPE(n->suffix));
2623  s += strlen(s);
2624  break;
2625  case DCH_HH24:
2626  sprintf(s, "%0*d", S_FM(n->suffix) ? 0 : (tm->tm_hour >= 0) ? 2 : 3,
2627  tm->tm_hour);
2628  if (S_THth(n->suffix))
2629  str_numth(s, s, S_TH_TYPE(n->suffix));
2630  s += strlen(s);
2631  break;
2632  case DCH_MI:
2633  sprintf(s, "%0*d", S_FM(n->suffix) ? 0 : (tm->tm_min >= 0) ? 2 : 3,
2634  tm->tm_min);
2635  if (S_THth(n->suffix))
2636  str_numth(s, s, S_TH_TYPE(n->suffix));
2637  s += strlen(s);
2638  break;
2639  case DCH_SS:
2640  sprintf(s, "%0*d", S_FM(n->suffix) ? 0 : (tm->tm_sec >= 0) ? 2 : 3,
2641  tm->tm_sec);
2642  if (S_THth(n->suffix))
2643  str_numth(s, s, S_TH_TYPE(n->suffix));
2644  s += strlen(s);
2645  break;
2646 #define DCH_to_char_fsec(frac_fmt, frac_val) \
2647  sprintf(s, frac_fmt, (int) (frac_val)); \
2648  if (S_THth(n->suffix)) \
2649  str_numth(s, s, S_TH_TYPE(n->suffix)); \
2650  s += strlen(s);
2651  case DCH_FF1: /* tenth of second */
2652  DCH_to_char_fsec("%01d", in->fsec / 100000);
2653  break;
2654  case DCH_FF2: /* hundredth of second */
2655  DCH_to_char_fsec("%02d", in->fsec / 10000);
2656  break;
2657  case DCH_FF3:
2658  case DCH_MS: /* millisecond */
2659  DCH_to_char_fsec("%03d", in->fsec / 1000);
2660  break;
2661  case DCH_FF4: /* tenth of a millisecond */
2662  DCH_to_char_fsec("%04d", in->fsec / 100);
2663  break;
2664  case DCH_FF5: /* hundredth of a millisecond */
2665  DCH_to_char_fsec("%05d", in->fsec / 10);
2666  break;
2667  case DCH_FF6:
2668  case DCH_US: /* microsecond */
2669  DCH_to_char_fsec("%06d", in->fsec);
2670  break;
2671 #undef DCH_to_char_fsec
2672  case DCH_SSSS:
2673  sprintf(s, "%d", tm->tm_hour * SECS_PER_HOUR +
2674  tm->tm_min * SECS_PER_MINUTE +
2675  tm->tm_sec);
2676  if (S_THth(n->suffix))
2677  str_numth(s, s, S_TH_TYPE(n->suffix));
2678  s += strlen(s);
2679  break;
2680  case DCH_tz:
2682  if (tmtcTzn(in))
2683  {
2684  /* We assume here that timezone names aren't localized */
2685  char *p = asc_tolower_z(tmtcTzn(in));
2686 
2687  strcpy(s, p);
2688  pfree(p);
2689  s += strlen(s);
2690  }
2691  break;
2692  case DCH_TZ:
2694  if (tmtcTzn(in))
2695  {
2696  strcpy(s, tmtcTzn(in));
2697  s += strlen(s);
2698  }
2699  break;
2700  case DCH_TZH:
2702  sprintf(s, "%c%02d",
2703  (tm->tm_gmtoff >= 0) ? '+' : '-',
2704  abs((int) tm->tm_gmtoff) / SECS_PER_HOUR);
2705  s += strlen(s);
2706  break;
2707  case DCH_TZM:
2709  sprintf(s, "%02d",
2710  (abs((int) tm->tm_gmtoff) % SECS_PER_HOUR) / SECS_PER_MINUTE);
2711  s += strlen(s);
2712  break;
2713  case DCH_OF:
2715  sprintf(s, "%c%0*d",
2716  (tm->tm_gmtoff >= 0) ? '+' : '-',
2717  S_FM(n->suffix) ? 0 : 2,
2718  abs((int) tm->tm_gmtoff) / SECS_PER_HOUR);
2719  s += strlen(s);
2720  if (abs((int) tm->tm_gmtoff) % SECS_PER_HOUR != 0)
2721  {
2722  sprintf(s, ":%02d",
2723  (abs((int) tm->tm_gmtoff) % SECS_PER_HOUR) / SECS_PER_MINUTE);
2724  s += strlen(s);
2725  }
2726  break;
2727  case DCH_A_D:
2728  case DCH_B_C:
2730  strcpy(s, (tm->tm_year <= 0 ? B_C_STR : A_D_STR));
2731  s += strlen(s);
2732  break;
2733  case DCH_AD:
2734  case DCH_BC:
2736  strcpy(s, (tm->tm_year <= 0 ? BC_STR : AD_STR));
2737  s += strlen(s);
2738  break;
2739  case DCH_a_d:
2740  case DCH_b_c:
2742  strcpy(s, (tm->tm_year <= 0 ? b_c_STR : a_d_STR));
2743  s += strlen(s);
2744  break;
2745  case DCH_ad:
2746  case DCH_bc:
2748  strcpy(s, (tm->tm_year <= 0 ? bc_STR : ad_STR));
2749  s += strlen(s);
2750  break;
2751  case DCH_MONTH:
2753  if (!tm->tm_mon)
2754  break;
2755  if (S_TM(n->suffix))
2756  {
2757  char *str = str_toupper_z(localized_full_months[tm->tm_mon - 1], collid);
2758 
2759  if (strlen(str) <= (n->key->len + TM_SUFFIX_LEN) * DCH_MAX_ITEM_SIZ)
2760  strcpy(s, str);
2761  else
2762  ereport(ERROR,
2763  (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
2764  errmsg("localized string format value too long")));
2765  }
2766  else
2767  sprintf(s, "%*s", S_FM(n->suffix) ? 0 : -9,
2768  asc_toupper_z(months_full[tm->tm_mon - 1]));
2769  s += strlen(s);
2770  break;
2771  case DCH_Month:
2773  if (!tm->tm_mon)
2774  break;
2775  if (S_TM(n->suffix))
2776  {
2777  char *str = str_initcap_z(localized_full_months[tm->tm_mon - 1], collid);
2778 
2779  if (strlen(str) <= (n->key->len + TM_SUFFIX_LEN) * DCH_MAX_ITEM_SIZ)
2780  strcpy(s, str);
2781  else
2782  ereport(ERROR,
2783  (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
2784  errmsg("localized string format value too long")));
2785  }
2786  else
2787  sprintf(s, "%*s", S_FM(n->suffix) ? 0 : -9,
2788  months_full[tm->tm_mon - 1]);
2789  s += strlen(s);
2790  break;
2791  case DCH_month:
2793  if (!tm->tm_mon)
2794  break;
2795  if (S_TM(n->suffix))
2796  {
2797  char *str = str_tolower_z(localized_full_months[tm->tm_mon - 1], collid);
2798 
2799  if (strlen(str) <= (n->key->len + TM_SUFFIX_LEN) * DCH_MAX_ITEM_SIZ)
2800  strcpy(s, str);
2801  else
2802  ereport(ERROR,
2803  (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
2804  errmsg("localized string format value too long")));
2805  }
2806  else
2807  sprintf(s, "%*s", S_FM(n->suffix) ? 0 : -9,
2808  asc_tolower_z(months_full[tm->tm_mon - 1]));
2809  s += strlen(s);
2810  break;
2811  case DCH_MON:
2813  if (!tm->tm_mon)
2814  break;
2815  if (S_TM(n->suffix))
2816  {
2817  char *str = str_toupper_z(localized_abbrev_months[tm->tm_mon - 1], collid);
2818 
2819  if (strlen(str) <= (n->key->len + TM_SUFFIX_LEN) * DCH_MAX_ITEM_SIZ)
2820  strcpy(s, str);
2821  else
2822  ereport(ERROR,
2823  (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
2824  errmsg("localized string format value too long")));
2825  }
2826  else
2827  strcpy(s, asc_toupper_z(months[tm->tm_mon - 1]));
2828  s += strlen(s);
2829  break;
2830  case DCH_Mon:
2832  if (!tm->tm_mon)
2833  break;
2834  if (S_TM(n->suffix))
2835  {
2836  char *str = str_initcap_z(localized_abbrev_months[tm->tm_mon - 1], collid);
2837 
2838  if (strlen(str) <= (n->key->len + TM_SUFFIX_LEN) * DCH_MAX_ITEM_SIZ)
2839  strcpy(s, str);
2840  else
2841  ereport(ERROR,
2842  (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
2843  errmsg("localized string format value too long")));
2844  }
2845  else
2846  strcpy(s, months[tm->tm_mon - 1]);
2847  s += strlen(s);
2848  break;
2849  case DCH_mon:
2851  if (!tm->tm_mon)
2852  break;
2853  if (S_TM(n->suffix))
2854  {
2855  char *str = str_tolower_z(localized_abbrev_months[tm->tm_mon - 1], collid);
2856 
2857  if (strlen(str) <= (n->key->len + TM_SUFFIX_LEN) * DCH_MAX_ITEM_SIZ)
2858  strcpy(s, str);
2859  else
2860  ereport(ERROR,
2861  (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
2862  errmsg("localized string format value too long")));
2863  }
2864  else
2865  strcpy(s, asc_tolower_z(months[tm->tm_mon - 1]));
2866  s += strlen(s);
2867  break;
2868  case DCH_MM:
2869  sprintf(s, "%0*d", S_FM(n->suffix) ? 0 : (tm->tm_mon >= 0) ? 2 : 3,
2870  tm->tm_mon);
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_DAY:
2877  if (S_TM(n->suffix))
2878  {
2879  char *str = str_toupper_z(localized_full_days[tm->tm_wday], collid);
2880 
2881  if (strlen(str) <= (n->key->len + TM_SUFFIX_LEN) * DCH_MAX_ITEM_SIZ)
2882  strcpy(s, str);
2883  else
2884  ereport(ERROR,
2885  (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
2886  errmsg("localized string format value too long")));
2887  }
2888  else
2889  sprintf(s, "%*s", S_FM(n->suffix) ? 0 : -9,
2890  asc_toupper_z(days[tm->tm_wday]));
2891  s += strlen(s);
2892  break;
2893  case DCH_Day:
2895  if (S_TM(n->suffix))
2896  {
2897  char *str = str_initcap_z(localized_full_days[tm->tm_wday], collid);
2898 
2899  if (strlen(str) <= (n->key->len + TM_SUFFIX_LEN) * DCH_MAX_ITEM_SIZ)
2900  strcpy(s, str);
2901  else
2902  ereport(ERROR,
2903  (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
2904  errmsg("localized string format value too long")));
2905  }
2906  else
2907  sprintf(s, "%*s", S_FM(n->suffix) ? 0 : -9,
2908  days[tm->tm_wday]);
2909  s += strlen(s);
2910  break;
2911  case DCH_day:
2913  if (S_TM(n->suffix))
2914  {
2915  char *str = str_tolower_z(localized_full_days[tm->tm_wday], collid);
2916 
2917  if (strlen(str) <= (n->key->len + TM_SUFFIX_LEN) * DCH_MAX_ITEM_SIZ)
2918  strcpy(s, str);
2919  else
2920  ereport(ERROR,
2921  (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
2922  errmsg("localized string format value too long")));
2923  }
2924  else
2925  sprintf(s, "%*s", S_FM(n->suffix) ? 0 : -9,
2926  asc_tolower_z(days[tm->tm_wday]));
2927  s += strlen(s);
2928  break;
2929  case DCH_DY:
2931  if (S_TM(n->suffix))
2932  {
2933  char *str = str_toupper_z(localized_abbrev_days[tm->tm_wday], collid);
2934 
2935  if (strlen(str) <= (n->key->len + TM_SUFFIX_LEN) * DCH_MAX_ITEM_SIZ)
2936  strcpy(s, str);
2937  else
2938  ereport(ERROR,
2939  (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
2940  errmsg("localized string format value too long")));
2941  }
2942  else
2943  strcpy(s, asc_toupper_z(days_short[tm->tm_wday]));
2944  s += strlen(s);
2945  break;
2946  case DCH_Dy:
2948  if (S_TM(n->suffix))
2949  {
2950  char *str = str_initcap_z(localized_abbrev_days[tm->tm_wday], collid);
2951 
2952  if (strlen(str) <= (n->key->len + TM_SUFFIX_LEN) * DCH_MAX_ITEM_SIZ)
2953  strcpy(s, str);
2954  else
2955  ereport(ERROR,
2956  (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
2957  errmsg("localized string format value too long")));
2958  }
2959  else
2960  strcpy(s, days_short[tm->tm_wday]);
2961  s += strlen(s);
2962  break;
2963  case DCH_dy:
2965  if (S_TM(n->suffix))
2966  {
2967  char *str = str_tolower_z(localized_abbrev_days[tm->tm_wday], collid);
2968 
2969  if (strlen(str) <= (n->key->len + TM_SUFFIX_LEN) * DCH_MAX_ITEM_SIZ)
2970  strcpy(s, str);
2971  else
2972  ereport(ERROR,
2973  (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
2974  errmsg("localized string format value too long")));
2975  }
2976  else
2977  strcpy(s, asc_tolower_z(days_short[tm->tm_wday]));
2978  s += strlen(s);
2979  break;
2980  case DCH_DDD:
2981  case DCH_IDDD:
2982  sprintf(s, "%0*d", S_FM(n->suffix) ? 0 : 3,
2983  (n->key->id == DCH_DDD) ?
2984  tm->tm_yday :
2985  date2isoyearday(tm->tm_year, tm->tm_mon, tm->tm_mday));
2986  if (S_THth(n->suffix))
2987  str_numth(s, s, S_TH_TYPE(n->suffix));
2988  s += strlen(s);
2989  break;
2990  case DCH_DD:
2991  sprintf(s, "%0*d", S_FM(n->suffix) ? 0 : 2, tm->tm_mday);
2992  if (S_THth(n->suffix))
2993  str_numth(s, s, S_TH_TYPE(n->suffix));
2994  s += strlen(s);
2995  break;
2996  case DCH_D:
2998  sprintf(s, "%d", tm->tm_wday + 1);
2999  if (S_THth(n->suffix))
3000  str_numth(s, s, S_TH_TYPE(n->suffix));
3001  s += strlen(s);
3002  break;
3003  case DCH_ID:
3005  sprintf(s, "%d", (tm->tm_wday == 0) ? 7 : tm->tm_wday);
3006  if (S_THth(n->suffix))
3007  str_numth(s, s, S_TH_TYPE(n->suffix));
3008  s += strlen(s);
3009  break;
3010  case DCH_WW:
3011  sprintf(s, "%0*d", S_FM(n->suffix) ? 0 : 2,
3012  (tm->tm_yday - 1) / 7 + 1);
3013  if (S_THth(n->suffix))
3014  str_numth(s, s, S_TH_TYPE(n->suffix));
3015  s += strlen(s);
3016  break;
3017  case DCH_IW:
3018  sprintf(s, "%0*d", S_FM(n->suffix) ? 0 : 2,
3019  date2isoweek(tm->tm_year, tm->tm_mon, tm->tm_mday));
3020  if (S_THth(n->suffix))
3021  str_numth(s, s, S_TH_TYPE(n->suffix));
3022  s += strlen(s);
3023  break;
3024  case DCH_Q:
3025  if (!tm->tm_mon)
3026  break;
3027  sprintf(s, "%d", (tm->tm_mon - 1) / 3 + 1);
3028  if (S_THth(n->suffix))
3029  str_numth(s, s, S_TH_TYPE(n->suffix));
3030  s += strlen(s);
3031  break;
3032  case DCH_CC:
3033  if (is_interval) /* straight calculation */
3034  i = tm->tm_year / 100;
3035  else
3036  {
3037  if (tm->tm_year > 0)
3038  /* Century 20 == 1901 - 2000 */
3039  i = (tm->tm_year - 1) / 100 + 1;
3040  else
3041  /* Century 6BC == 600BC - 501BC */
3042  i = tm->tm_year / 100 - 1;
3043  }
3044  if (i <= 99 && i >= -99)
3045  sprintf(s, "%0*d", S_FM(n->suffix) ? 0 : (i >= 0) ? 2 : 3, i);
3046  else
3047  sprintf(s, "%d", i);
3048  if (S_THth(n->suffix))
3049  str_numth(s, s, S_TH_TYPE(n->suffix));
3050  s += strlen(s);
3051  break;
3052  case DCH_Y_YYY:
3053  i = ADJUST_YEAR(tm->tm_year, is_interval) / 1000;
3054  sprintf(s, "%d,%03d", i,
3055  ADJUST_YEAR(tm->tm_year, is_interval) - (i * 1000));
3056  if (S_THth(n->suffix))
3057  str_numth(s, s, S_TH_TYPE(n->suffix));
3058  s += strlen(s);
3059  break;
3060  case DCH_YYYY:
3061  case DCH_IYYY:
3062  sprintf(s, "%0*d",
3063  S_FM(n->suffix) ? 0 :
3064  (ADJUST_YEAR(tm->tm_year, is_interval) >= 0) ? 4 : 5,
3065  (n->key->id == DCH_YYYY ?
3066  ADJUST_YEAR(tm->tm_year, is_interval) :
3068  tm->tm_mon,
3069  tm->tm_mday),
3070  is_interval)));
3071  if (S_THth(n->suffix))
3072  str_numth(s, s, S_TH_TYPE(n->suffix));
3073  s += strlen(s);
3074  break;
3075  case DCH_YYY:
3076  case DCH_IYY:
3077  sprintf(s, "%0*d",
3078  S_FM(n->suffix) ? 0 :
3079  (ADJUST_YEAR(tm->tm_year, is_interval) >= 0) ? 3 : 4,
3080  (n->key->id == DCH_YYY ?
3081  ADJUST_YEAR(tm->tm_year, is_interval) :
3083  tm->tm_mon,
3084  tm->tm_mday),
3085  is_interval)) % 1000);
3086  if (S_THth(n->suffix))
3087  str_numth(s, s, S_TH_TYPE(n->suffix));
3088  s += strlen(s);
3089  break;
3090  case DCH_YY:
3091  case DCH_IY:
3092  sprintf(s, "%0*d",
3093  S_FM(n->suffix) ? 0 :
3094  (ADJUST_YEAR(tm->tm_year, is_interval) >= 0) ? 2 : 3,
3095  (n->key->id == DCH_YY ?
3096  ADJUST_YEAR(tm->tm_year, is_interval) :
3098  tm->tm_mon,
3099  tm->tm_mday),
3100  is_interval)) % 100);
3101  if (S_THth(n->suffix))
3102  str_numth(s, s, S_TH_TYPE(n->suffix));
3103  s += strlen(s);
3104  break;
3105  case DCH_Y:
3106  case DCH_I:
3107  sprintf(s, "%1d",
3108  (n->key->id == DCH_Y ?
3109  ADJUST_YEAR(tm->tm_year, is_interval) :
3111  tm->tm_mon,
3112  tm->tm_mday),
3113  is_interval)) % 10);
3114  if (S_THth(n->suffix))
3115  str_numth(s, s, S_TH_TYPE(n->suffix));
3116  s += strlen(s);
3117  break;
3118  case DCH_RM:
3119  if (!tm->tm_mon)
3120  break;
3121  sprintf(s, "%*s", S_FM(n->suffix) ? 0 : -4,
3123  s += strlen(s);
3124  break;
3125  case DCH_rm:
3126  if (!tm->tm_mon)
3127  break;
3128  sprintf(s, "%*s", S_FM(n->suffix) ? 0 : -4,
3130  s += strlen(s);
3131  break;
3132  case DCH_W:
3133  sprintf(s, "%d", (tm->tm_mday - 1) / 7 + 1);
3134  if (S_THth(n->suffix))
3135  str_numth(s, s, S_TH_TYPE(n->suffix));
3136  s += strlen(s);
3137  break;
3138  case DCH_J:
3139  sprintf(s, "%d", date2j(tm->tm_year, tm->tm_mon, tm->tm_mday));
3140  if (S_THth(n->suffix))
3141  str_numth(s, s, S_TH_TYPE(n->suffix));
3142  s += strlen(s);
3143  break;
3144  }
3145  }
3146 
3147  *s = '\0';
3148 }
#define a_m_STR
Definition: formatting.c:266
#define NODE_TYPE_END
Definition: formatting.c:203
uint8 type
Definition: formatting.c:197
const KeyWord * key
Definition: formatting.c:200
int tm_wday
Definition: pgtime.h:33
#define b_c_STR
Definition: formatting.c:244
#define S_THth(_s)
Definition: formatting.c:554
int tm_hour
Definition: pgtime.h:29
#define BC_STR
Definition: formatting.c:245
#define A_M_STR
Definition: formatting.c:265
static char * str_initcap_z(const char *buff, Oid collid)
Definition: formatting.c:2120
fsec_t fsec
Definition: formatting.c:500
static char * asc_tolower_z(const char *buff)
Definition: formatting.c:2126
#define AD_STR
Definition: formatting.c:240
int errcode(int sqlerrcode)
Definition: elog.c:608
struct pg_tm tm
Definition: formatting.c:499
#define p_m_STR
Definition: formatting.c:271
#define am_STR
Definition: formatting.c:268
static char * str_tolower_z(const char *buff, Oid collid)
Definition: formatting.c:2108
long int tm_gmtoff
Definition: pgtime.h:36
Definition: pgtime.h:25
char * localized_abbrev_months[12]
Definition: pg_locale.c:102
static char * str_numth(char *dest, char *num, int type)
Definition: formatting.c:1553
static const char *const rm_months_upper[]
Definition: formatting.c:294
int id
Definition: formatting.c:190
#define pm_STR
Definition: formatting.c:273
static struct pg_tm tm
Definition: localtime.c:108
#define MONTHS_PER_YEAR
Definition: timestamp.h:69
#define sprintf
Definition: port.h:194
void pfree(void *pointer)
Definition: mcxt.c:1056
const char *const months[]
Definition: datetime.c:65
#define NODE_TYPE_ACTION
Definition: formatting.c:204
#define ERROR
Definition: elog.h:43
char character[MAX_MULTIBYTE_CHAR_LEN+1]
Definition: formatting.c:198
int date2isoweek(int year, int mon, int mday)
Definition: timestamp.c:4324
#define S_FM(_s)
Definition: formatting.c:560
int date2isoyear(int year, int mon, int mday)
Definition: timestamp.c:4379
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:68
#define A_D_STR
Definition: formatting.c:238
#define a_d_STR
Definition: formatting.c:239
#define ereport(elevel, rest)
Definition: elog.h:141
#define SECS_PER_HOUR
Definition: timestamp.h:87
#define bc_STR
Definition: formatting.c:246
#define DCH_to_char_fsec(frac_fmt, frac_val)
#define S_TH_TYPE(_s)
Definition: formatting.c:557
static const char *const months_full[]
Definition: formatting.c:220
int len
Definition: formatting.c:189
char * localized_full_days[7]
Definition: pg_locale.c:101
int date2j(int y, int m, int d)
Definition: datetime.c:269
#define ad_STR
Definition: formatting.c:241
void cache_locale_time(void)
Definition: pg_locale.c:774
#define P_M_STR
Definition: formatting.c:270
static char * asc_toupper_z(const char *buff)
Definition: formatting.c:2132
#define ADJUST_YEAR(year, is_interval)
Definition: formatting.c:236
uint8 suffix
Definition: formatting.c:199
#define tmtcTzn(_X)
Definition: formatting.c:505
int tm_year
Definition: pgtime.h:32
int errmsg(const char *fmt,...)
Definition: elog.c:822
static char * str_toupper_z(const char *buff, Oid collid)
Definition: formatting.c:2114
#define INVALID_FOR_INTERVAL
Definition: formatting.c:527
int date2isoyearday(int year, int mon, int mday)
Definition: timestamp.c:4436
int i
#define S_TM(_s)
Definition: formatting.c:562
int tm_yday
Definition: pgtime.h:34
#define PM_STR
Definition: formatting.c:272
#define AM_STR
Definition: formatting.c:267
#define TM_SUFFIX_LEN
Definition: formatting.c:568
int tm_sec
Definition: pgtime.h:27
char * localized_full_months[12]
Definition: pg_locale.c:103
#define DCH_MAX_ITEM_SIZ
Definition: formatting.c:156
char * localized_abbrev_days[7]
Definition: pg_locale.c:100
int tm_min
Definition: pgtime.h:28
static const char *const rm_months_lower[]
Definition: formatting.c:297
static const char *const days_short[]
Definition: formatting.c:225
#define B_C_STR
Definition: formatting.c:243

◆ do_to_timestamp()

static void do_to_timestamp ( text date_txt,
text fmt,
bool  std,
struct pg_tm tm,
fsec_t fsec,
int *  fprec,
uint32 flags,
bool have_error 
)
static

Definition at line 4295 of file formatting.c.

References Assert, TmFromChar::bc, TmFromChar::cc, CHECK_ERROR, TmFromChar::clock, CLOCK_12_HOUR, TmFromChar::d, DateTimeParseError(), DAY, DCH_cache_fetch(), DCH_CACHE_SIZE, DCH_datetime_type(), DCH_FLAG, DCH_from_char(), DCH_index, TmFromChar::dd, TmFromChar::ddd, DEBUG_TM, DEBUG_TMFC, DTERR_FIELD_OVERFLOW, DTERR_TZDISP_OVERFLOW, DTK_DATE_M, DTK_M, ereport, errcode(), errhint(), errmsg(), ERROR, TmFromChar::ff, 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(), RETURN_ERROR, SECS_PER_HOUR, SECS_PER_MINUTE, TmFromChar::ss, TmFromChar::ssss, STD_FLAG, 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 parse_datetime(), to_date(), and to_timestamp().

4298 {
4299  FormatNode *format = NULL;
4300  TmFromChar tmfc;
4301  int fmt_len;
4302  char *date_str;
4303  int fmask;
4304  bool incache = false;
4305 
4306  Assert(tm != NULL);
4307  Assert(fsec != NULL);
4308 
4309  date_str = text_to_cstring(date_txt);
4310 
4311  ZERO_tmfc(&tmfc);
4312  ZERO_tm(tm);
4313  *fsec = 0;
4314  if (fprec)
4315  *fprec = 0;
4316  if (flags)
4317  *flags = 0;
4318  fmask = 0; /* bit mask for ValidateDate() */
4319 
4320  fmt_len = VARSIZE_ANY_EXHDR(fmt);
4321 
4322  if (fmt_len)
4323  {
4324  char *fmt_str;
4325 
4326  fmt_str = text_to_cstring(fmt);
4327 
4328  if (fmt_len > DCH_CACHE_SIZE)
4329  {
4330  /*
4331  * Allocate new memory if format picture is bigger than static
4332  * cache and do not use cache (call parser always)
4333  */
4334  format = (FormatNode *) palloc((fmt_len + 1) * sizeof(FormatNode));
4335 
4336  parse_format(format, fmt_str, DCH_keywords, DCH_suff, DCH_index,
4337  DCH_FLAG | (std ? STD_FLAG : 0), NULL);
4338  }
4339  else
4340  {
4341  /*
4342  * Use cache buffers
4343  */
4344  DCHCacheEntry *ent = DCH_cache_fetch(fmt_str, std);
4345 
4346  incache = true;
4347  format = ent->format;
4348  }
4349 
4350 #ifdef DEBUG_TO_FROM_CHAR
4351  /* dump_node(format, fmt_len); */
4352  /* dump_index(DCH_keywords, DCH_index); */
4353 #endif
4354 
4355  DCH_from_char(format, date_str, &tmfc, std, have_error);
4356  CHECK_ERROR;
4357 
4358  pfree(fmt_str);
4359 
4360  if (flags)
4361  *flags = DCH_datetime_type(format, have_error);
4362 
4363  if (!incache)
4364  {
4365  pfree(format);
4366  format = NULL;
4367  }
4368 
4369  CHECK_ERROR;
4370  }
4371 
4372  DEBUG_TMFC(&tmfc);
4373 
4374  /*
4375  * Convert to_date/to_timestamp input fields to standard 'tm'
4376  */
4377  if (tmfc.ssss)
4378  {
4379  int x = tmfc.ssss;
4380 
4381  tm->tm_hour = x / SECS_PER_HOUR;
4382  x %= SECS_PER_HOUR;
4383  tm->tm_min = x / SECS_PER_MINUTE;
4384  x %= SECS_PER_MINUTE;
4385  tm->tm_sec = x;
4386  }
4387 
4388  if (tmfc.ss)
4389  tm->tm_sec = tmfc.ss;
4390  if (tmfc.mi)
4391  tm->tm_min = tmfc.mi;
4392  if (tmfc.hh)
4393  tm->tm_hour = tmfc.hh;
4394 
4395  if (tmfc.clock == CLOCK_12_HOUR)
4396  {
4397  if (tm->tm_hour < 1 || tm->tm_hour > HOURS_PER_DAY / 2)
4398  {
4400  (errcode(ERRCODE_INVALID_DATETIME_FORMAT),
4401  errmsg("hour \"%d\" is invalid for the 12-hour clock",
4402  tm->tm_hour),
4403  errhint("Use the 24-hour clock, or give an hour between 1 and 12."))));
4404  }
4405 
4406  if (tmfc.pm && tm->tm_hour < HOURS_PER_DAY / 2)
4407  tm->tm_hour += HOURS_PER_DAY / 2;
4408  else if (!tmfc.pm && tm->tm_hour == HOURS_PER_DAY / 2)
4409  tm->tm_hour = 0;
4410  }
4411 
4412  if (tmfc.year)
4413  {
4414  /*
4415  * If CC and YY (or Y) are provided, use YY as 2 low-order digits for
4416  * the year in the given century. Keep in mind that the 21st century
4417  * AD runs from 2001-2100, not 2000-2099; 6th century BC runs from
4418  * 600BC to 501BC.
4419  */
4420  if (tmfc.cc && tmfc.yysz <= 2)
4421  {
4422  if (tmfc.bc)
4423  tmfc.cc = -tmfc.cc;
4424  tm->tm_year = tmfc.year % 100;
4425  if (tm->tm_year)
4426  {
4427  if (tmfc.cc >= 0)
4428  tm->tm_year += (tmfc.cc - 1) * 100;
4429  else
4430  tm->tm_year = (tmfc.cc + 1) * 100 - tm->tm_year + 1;
4431  }
4432  else
4433  {
4434  /* find century year for dates ending in "00" */
4435  tm->tm_year = tmfc.cc * 100 + ((tmfc.cc >= 0) ? 0 : 1);
4436  }
4437  }
4438  else
4439  {
4440  /* If a 4-digit year is provided, we use that and ignore CC. */
4441  tm->tm_year = tmfc.year;
4442  if (tmfc.bc && tm->tm_year > 0)
4443  tm->tm_year = -(tm->tm_year - 1);
4444  }
4445  fmask |= DTK_M(YEAR);
4446  }
4447  else if (tmfc.cc)
4448  {
4449  /* use first year of century */
4450  if (tmfc.bc)
4451  tmfc.cc = -tmfc.cc;
4452  if (tmfc.cc >= 0)
4453  /* +1 because 21st century started in 2001 */
4454  tm->tm_year = (tmfc.cc - 1) * 100 + 1;
4455  else
4456  /* +1 because year == 599 is 600 BC */
4457  tm->tm_year = tmfc.cc * 100 + 1;
4458  fmask |= DTK_M(YEAR);
4459  }
4460 
4461  if (tmfc.j)
4462  {
4463  j2date(tmfc.j, &tm->tm_year, &tm->tm_mon, &tm->tm_mday);
4464  fmask |= DTK_DATE_M;
4465  }
4466 
4467  if (tmfc.ww)
4468  {
4469  if (tmfc.mode == FROM_CHAR_DATE_ISOWEEK)
4470  {
4471  /*
4472  * If tmfc.d is not set, then the date is left at the beginning of
4473  * the ISO week (Monday).
4474  */
4475  if (tmfc.d)
4476  isoweekdate2date(tmfc.ww, tmfc.d, &tm->tm_year, &tm->tm_mon, &tm->tm_mday);
4477  else
4478  isoweek2date(tmfc.ww, &tm->tm_year, &tm->tm_mon, &tm->tm_mday);
4479  fmask |= DTK_DATE_M;
4480  }
4481  else
4482  tmfc.ddd = (tmfc.ww - 1) * 7 + 1;
4483  }
4484 
4485  if (tmfc.w)
4486  tmfc.dd = (tmfc.w - 1) * 7 + 1;
4487  if (tmfc.dd)
4488  {
4489  tm->tm_mday = tmfc.dd;
4490  fmask |= DTK_M(DAY);
4491  }
4492  if (tmfc.mm)
4493  {
4494  tm->tm_mon = tmfc.mm;
4495  fmask |= DTK_M(MONTH);
4496  }
4497 
4498  if (tmfc.ddd && (tm->tm_mon <= 1 || tm->tm_mday <= 1))
4499  {
4500  /*
4501  * The month and day field have not been set, so we use the
4502  * day-of-year field to populate them. Depending on the date mode,
4503  * this field may be interpreted as a Gregorian day-of-year, or an ISO
4504  * week date day-of-year.
4505  */
4506 
4507  if (!tm->tm_year && !tmfc.bc)
4508  {
4510  (errcode(ERRCODE_INVALID_DATETIME_FORMAT),
4511  errmsg("cannot calculate day of year without year information"))));
4512  }
4513 
4514  if (tmfc.mode == FROM_CHAR_DATE_ISOWEEK)
4515  {
4516  int j0; /* zeroth day of the ISO year, in Julian */
4517 
4518  j0 = isoweek2j(tm->tm_year, 1) - 1;
4519 
4520  j2date(j0 + tmfc.ddd, &tm->tm_year, &tm->tm_mon, &tm->tm_mday);
4521  fmask |= DTK_DATE_M;
4522  }
4523  else
4524  {
4525  const int *y;
4526  int i;
4527 
4528  static const int ysum[2][13] = {
4529  {0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365},
4530  {0, 31, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335, 366}};
4531 
4532  y = ysum[isleap(tm->tm_year)];
4533 
4534  for (i = 1; i <= MONTHS_PER_YEAR; i++)
4535  {
4536  if (tmfc.ddd <= y[i])
4537  break;
4538  }
4539  if (tm->tm_mon <= 1)
4540  tm->tm_mon = i;
4541 
4542  if (tm->tm_mday <= 1)
4543  tm->tm_mday = tmfc.ddd - y[i - 1];
4544 
4545  fmask |= DTK_M(MONTH) | DTK_M(DAY);
4546  }
4547  }
4548 
4549  if (tmfc.ms)
4550  *fsec += tmfc.ms * 1000;
4551  if (tmfc.us)
4552  *fsec += tmfc.us;
4553  if (fprec)
4554  *fprec = tmfc.ff; /* fractional precision, if specified */
4555 
4556  /* Range-check date fields according to bit mask computed above */
4557  if (fmask != 0)
4558  {
4559  /* We already dealt with AD/BC, so pass isjulian = true */
4560  int dterr = ValidateDate(fmask, true, false, false, tm);
4561 
4562  if (dterr != 0)
4563  {
4564  /*
4565  * Force the error to be DTERR_FIELD_OVERFLOW even if ValidateDate
4566  * said DTERR_MD_FIELD_OVERFLOW, because we don't want to print an
4567  * irrelevant hint about datestyle.
4568  */
4569  RETURN_ERROR(DateTimeParseError(DTERR_FIELD_OVERFLOW, date_str, "timestamp"));
4570  }
4571  }
4572 
4573  /* Range-check time fields too */
4574  if (tm->tm_hour < 0 || tm->tm_hour >= HOURS_PER_DAY ||
4575  tm->tm_min < 0 || tm->tm_min >= MINS_PER_HOUR ||
4576  tm->tm_sec < 0 || tm->tm_sec >= SECS_PER_MINUTE ||
4577  *fsec < INT64CONST(0) || *fsec >= USECS_PER_SEC)
4578  {
4579  RETURN_ERROR(DateTimeParseError(DTERR_FIELD_OVERFLOW, date_str, "timestamp"));
4580  }
4581 
4582  /* Save parsed time-zone into tm->tm_zone if it was specified */
4583  if (tmfc.tzsign)
4584  {
4585  char *tz;
4586 
4587  if (tmfc.tzh < 0 || tmfc.tzh > MAX_TZDISP_HOUR ||
4588  tmfc.tzm < 0 || tmfc.tzm >= MINS_PER_HOUR)
4589  {
4590  RETURN_ERROR(DateTimeParseError(DTERR_TZDISP_OVERFLOW, date_str, "timestamp"));
4591  }
4592 
4593  tz = psprintf("%c%02d:%02d",
4594  tmfc.tzsign > 0 ? '+' : '-', tmfc.tzh, tmfc.tzm);
4595 
4596  tm->tm_zone = tz;
4597  }
4598 
4599  DEBUG_TM(tm);
4600 
4601 on_error:
4602 
4603  if (format && !incache)
4604  pfree(format);
4605 
4606  pfree(date_str);
4607 }
#define DEBUG_TM(_X)
Definition: formatting.c:490
#define DAY
Definition: datetime.h:94
int errhint(const char *fmt,...)
Definition: elog.c:1069
void DateTimeParseError(int dterr, const char *str, const char *datatype)
Definition: datetime.c:3736
static void DCH_from_char(FormatNode *node, const char *in, TmFromChar *out, bool std, bool *have_error)
Definition: formatting.c:3161
#define USECS_PER_SEC
Definition: timestamp.h:94
static int DCH_datetime_type(FormatNode *node, bool *have_error)
Definition: formatting.c:3647
#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:271
static const KeySuffix DCH_suff[]
Definition: formatting.c:570
static const int DCH_index[KeyWord_INDEX_SIZE]
Definition: formatting.c:944
int errcode(int sqlerrcode)
Definition: elog.c:608
void isoweekdate2date(int isoweek, int wday, int *year, int *mon, int *mday)
Definition: timestamp.c:4306
FromCharDateMode mode
Definition: formatting.c:444
#define CHECK_ERROR
Definition: formatting.c:131
#define DTK_DATE_M
Definition: datetime.h:192
#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:1056
#define DCH_CACHE_SIZE
Definition: formatting.c:402
#define ERROR
Definition: elog.h:43
#define DTERR_FIELD_OVERFLOW
Definition: datetime.h:281
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:412
static void parse_format(FormatNode *node, const char *str, const KeyWord *kw, const KeySuffix *suf, const int *index, uint32 flags, NUMDesc *Num)
Definition: formatting.c:1321
const char * tm_zone
Definition: pgtime.h:37
#define ereport(elevel, rest)
Definition: elog.h:141
void j2date(int jd, int *year, int *month, int *day)
Definition: datetime.c:294
#define SECS_PER_HOUR
Definition: timestamp.h:87
#define CLOCK_12_HOUR
Definition: formatting.c:213
#define MONTH
Definition: datetime.h:92
#define DTERR_TZDISP_OVERFLOW
Definition: datetime.h:284
void isoweek2date(int woy, int *year, int *mon, int *mday)
Definition: timestamp.c:4293
#define ZERO_tmfc(_X)
Definition: formatting.c:470
int isoweek2j(int year, int week)
Definition: timestamp.c:4273
#define DCH_FLAG
Definition: formatting.c:141
#define Assert(condition)
Definition: c.h:738
#define DEBUG_TMFC(_X)
Definition: formatting.c:489
char * text_to_cstring(const text *t)
Definition: varlena.c:204
int tm_year
Definition: pgtime.h:32
#define VARSIZE_ANY_EXHDR(PTR)
Definition: postgres.h:341
void * palloc(Size size)
Definition: mcxt.c:949
int errmsg(const char *fmt,...)
Definition: elog.c:822
static const KeyWord DCH_keywords[]
Definition: formatting.c:774
int i
#define STD_FLAG
Definition: formatting.c:143
#define DTK_M(t)
Definition: datetime.h:188
static char format
int tm_sec
Definition: pgtime.h:27
#define ZERO_tm(_X)
Definition: formatting.c:508
int tm_min
Definition: pgtime.h:28
static DCHCacheEntry * DCH_cache_fetch(const char *str, bool std)
Definition: formatting.c:3832
#define RETURN_ERROR(throw_error)
Definition: formatting.c:118
int ValidateDate(int fmask, bool isjulian, bool is2digits, bool bc, struct pg_tm *tm)
Definition: datetime.c:2448

◆ fill_str()

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

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

4617 {
4618  memset(str, c, max);
4619  *(str + max) = '\0';
4620  return str;
4621 }
char * c

◆ float4_to_char()

Datum float4_to_char ( PG_FUNCTION_ARGS  )

Definition at line 6306 of file formatting.c.

References fill_str(), format, int_to_roman(), IS_EEEE, 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(), sign, val, and value.

6307 {
6309  text *fmt = PG_GETARG_TEXT_PP(1);
6310  NUMDesc Num;
6311  FormatNode *format;
6312  text *result;
6313  bool shouldFree;
6314  int out_pre_spaces = 0,
6315  sign = 0;
6316  char *numstr,
6317  *orgnum,
6318  *p;
6319 
6321 
6322  if (IS_ROMAN(&Num))
6323  numstr = orgnum = int_to_roman((int) rint(value));
6324  else if (IS_EEEE(&Num))
6325  {
6326  if (isnan(value) || isinf(value))
6327  {
6328  /*
6329  * Allow 6 characters for the leading sign, the decimal point,
6330  * "e", the exponent's sign and two exponent digits.
6331  */
6332  numstr = (char *) palloc(Num.pre + Num.post + 7);
6333  fill_str(numstr, '#', Num.pre + Num.post + 6);
6334  *numstr = ' ';
6335  *(numstr + Num.pre + 1) = '.';
6336  }
6337  else
6338  {
6339  numstr = orgnum = psprintf("%+.*e", Num.post, value);
6340 
6341  /*
6342  * Swap a leading positive sign for a space.
6343  */
6344  if (*orgnum == '+')
6345  *orgnum = ' ';
6346 
6347  numstr = orgnum;
6348  }
6349  }
6350  else
6351  {
6352  float4 val = value;
6353  int numstr_pre_len;
6354 
6355  if (IS_MULTI(&Num))
6356  {
6357  float multi = pow((double) 10, (double) Num.multi);
6358 
6359  val = value * multi;
6360  Num.pre += Num.multi;
6361  }
6362 
6363  orgnum = (char *) psprintf("%.0f", fabs(val));
6364  numstr_pre_len = strlen(orgnum);
6365 
6366  /* adjust post digits to fit max float digits */
6367  if (numstr_pre_len >= FLT_DIG)
6368  Num.post = 0;
6369  else if (numstr_pre_len + Num.post > FLT_DIG)
6370  Num.post = FLT_DIG - numstr_pre_len;
6371  orgnum = psprintf("%.*f", Num.post, val);
6372 
6373  if (*orgnum == '-')
6374  { /* < 0 */
6375  sign = '-';
6376  numstr = orgnum + 1;
6377  }
6378  else
6379  {
6380  sign = '+';
6381  numstr = orgnum;
6382  }
6383 
6384  if ((p = strchr(numstr, '.')))
6385  numstr_pre_len = p - numstr;
6386  else
6387  numstr_pre_len = strlen(numstr);
6388 
6389  /* needs padding? */
6390  if (numstr_pre_len < Num.pre)
6391  out_pre_spaces = Num.pre - numstr_pre_len;
6392  /* overflowed prefix digit format? */
6393  else if (numstr_pre_len > Num.pre)
6394  {
6395  numstr = (char *) palloc(Num.pre + Num.post + 2);
6396  fill_str(numstr, '#', Num.pre + Num.post + 1);
6397  *(numstr + Num.pre) = '.';
6398  }
6399  }
6400 
6402  PG_RETURN_TEXT_P(result);
6403 }
char * psprintf(const char *fmt,...)
Definition: psprintf.c:46
#define IS_EEEE(_f)
Definition: formatting.c:377
static struct @145 value
#define NUM_TOCHAR_finish
Definition: formatting.c:5895
#define IS_ROMAN(_f)
Definition: formatting.c:375
#define PG_GETARG_TEXT_PP(n)
Definition: fmgr.h:303
#define NUM_TOCHAR_prepare
Definition: formatting.c:5882
#define IS_MULTI(_f)
Definition: formatting.c:376
char sign
Definition: informix.c:668
static char * fill_str(char *str, int c, int max)
Definition: formatting.c:4616
static char * int_to_roman(int number)
Definition: formatting.c:4816
int pre
Definition: formatting.c:328
#define PG_GETARG_FLOAT4(n)
Definition: fmgr.h:275
float float4
Definition: c.h:490
#define PG_RETURN_TEXT_P(x)
Definition: fmgr.h:361
int post
Definition: formatting.c:328
void * palloc(Size size)
Definition: mcxt.c:949
int multi
Definition: formatting.c:328
Definition: c.h:555
static char format
long val
Definition: informix.c:664

◆ float8_to_char()

Datum float8_to_char ( PG_FUNCTION_ARGS  )

Definition at line 6410 of file formatting.c.

References fill_str(), format, int_to_roman(), IS_EEEE, 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(), sign, val, and value.

6411 {
6413  text *fmt = PG_GETARG_TEXT_PP(1);
6414  NUMDesc Num;
6415  FormatNode *format;
6416  text *result;
6417  bool shouldFree;
6418  int out_pre_spaces = 0,
6419  sign = 0;
6420  char *numstr,
6421  *orgnum,
6422  *p;
6423 
6425 
6426  if (IS_ROMAN(&Num))
6427  numstr = orgnum = int_to_roman((int) rint(value));
6428  else if (IS_EEEE(&Num))
6429  {
6430  if (isnan(value) || isinf(value))
6431  {
6432  /*
6433  * Allow 6 characters for the leading sign, the decimal point,
6434  * "e", the exponent's sign and two exponent digits.
6435  */
6436  numstr = (char *) palloc(Num.pre + Num.post + 7);
6437  fill_str(numstr, '#', Num.pre + Num.post + 6);
6438  *numstr = ' ';
6439  *(numstr + Num.pre + 1) = '.';
6440  }
6441  else
6442  {
6443  numstr = orgnum = (char *) psprintf("%+.*e", Num.post, value);
6444 
6445  /*
6446  * Swap a leading positive sign for a space.
6447  */
6448  if (*orgnum == '+')
6449  *orgnum = ' ';
6450 
6451  numstr = orgnum;
6452  }
6453  }
6454  else
6455  {
6456  float8 val = value;
6457  int numstr_pre_len;
6458 
6459  if (IS_MULTI(&Num))
6460  {
6461  double multi = pow((double) 10, (double) Num.multi);
6462 
6463  val = value * multi;
6464  Num.pre += Num.multi;
6465  }
6466  orgnum = psprintf("%.0f", fabs(val));
6467  numstr_pre_len = strlen(orgnum);
6468 
6469  /* adjust post digits to fit max double digits */
6470  if (numstr_pre_len >= DBL_DIG)
6471  Num.post = 0;
6472  else if (numstr_pre_len + Num.post > DBL_DIG)
6473  Num.post = DBL_DIG - numstr_pre_len;
6474  orgnum = psprintf("%.*f", Num.post, val);
6475 
6476  if (*orgnum == '-')
6477  { /* < 0 */
6478  sign = '-';
6479  numstr = orgnum + 1;
6480  }
6481  else
6482  {
6483  sign = '+';
6484  numstr = orgnum;
6485  }
6486 
6487  if ((p = strchr(numstr, '.')))
6488  numstr_pre_len = p - numstr;
6489  else
6490  numstr_pre_len = strlen(numstr);
6491 
6492  /* needs padding? */
6493  if (numstr_pre_len < Num.pre)
6494  out_pre_spaces = Num.pre - numstr_pre_len;
6495  /* overflowed prefix digit format? */
6496  else if (numstr_pre_len > Num.pre)
6497  {
6498  numstr = (char *) palloc(Num.pre + Num.post + 2);
6499  fill_str(numstr, '#', Num.pre + Num.post + 1);
6500  *(numstr + Num.pre) = '.';
6501  }
6502  }
6503 
6505  PG_RETURN_TEXT_P(result);
6506 }
#define PG_GETARG_FLOAT8(n)
Definition: fmgr.h:276
char * psprintf(const char *fmt,...)
Definition: psprintf.c:46
#define IS_EEEE(_f)
Definition: formatting.c:377
static struct @145 value
#define NUM_TOCHAR_finish
Definition: formatting.c:5895
#define IS_ROMAN(_f)
Definition: formatting.c:375
#define PG_GETARG_TEXT_PP(n)
Definition: fmgr.h:303
#define NUM_TOCHAR_prepare
Definition: formatting.c:5882
double float8
Definition: c.h:491
#define IS_MULTI(_f)
Definition: formatting.c:376
char sign
Definition: informix.c:668
static char * fill_str(char *str, int c, int max)
Definition: formatting.c:4616
static char * int_to_roman(int number)
Definition: formatting.c:4816
int pre
Definition: formatting.c:328
#define PG_RETURN_TEXT_P(x)
Definition: fmgr.h:361
int post
Definition: formatting.c:328
void * palloc(Size size)
Definition: mcxt.c:949
int multi
Definition: formatting.c:328
Definition: c.h:555
static char format
long val
Definition: informix.c:664

◆ from_char_parse_int()

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

Definition at line 2443 of file formatting.c.

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

Referenced by DCH_from_char().

2444 {
2445  return from_char_parse_int_len(dest, src, node->key->len, node, have_error);
2446 }
const KeyWord * key
Definition: formatting.c:200
int len
Definition: formatting.c:189
static int from_char_parse_int_len(int *dest, const char **src, const int len, FormatNode *node, bool *have_error)
Definition: formatting.c:2340

◆ from_char_parse_int_len()

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

Definition at line 2340 of file formatting.c.

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

Referenced by DCH_from_char(), and from_char_parse_int().

2342 {
2343  long result;
2344  char copy[DCH_MAX_ITEM_SIZ + 1];
2345  const char *init = *src;
2346  int used;
2347 
2348  /*
2349  * Skip any whitespace before parsing the integer.
2350  */
2351  *src += strspace_len(*src);
2352 
2353  Assert(len <= DCH_MAX_ITEM_SIZ);
2354  used = (int) strlcpy(copy, *src, len + 1);
2355 
2356  if (S_FM(node->suffix) || is_next_separator(node))
2357  {
2358  /*
2359  * This node is in Fill Mode, or the next node is known to be a
2360  * non-digit value, so we just slurp as many characters as we can get.
2361  */
2362  char *endptr;
2363 
2364  errno = 0;
2365  result = strtol(init, &endptr, 10);
2366  *src = endptr;
2367  }
2368  else
2369  {
2370  /*
2371  * We need to pull exactly the number of characters given in 'len' out
2372  * of the string, and convert those.
2373  */
2374  char *last;
2375 
2376  if (used < len)
2378  (errcode(ERRCODE_INVALID_DATETIME_FORMAT),
2379  errmsg("source string too short for \"%s\" "
2380  "formatting field",
2381  node->key->name),
2382  errdetail("Field requires %d characters, "
2383  "but only %d remain.",
2384  len, used),
2385  errhint("If your source string is not fixed-width, "
2386  "try using the \"FM\" modifier."))));
2387 
2388  errno = 0;
2389  result = strtol(copy, &last, 10);
2390  used = last - copy;
2391 
2392  if (used > 0 && used < len)
2394  (errcode(ERRCODE_INVALID_DATETIME_FORMAT),
2395  errmsg("invalid value \"%s\" for \"%s\"",
2396  copy, node->key->name),
2397  errdetail("Field requires %d characters, "
2398  "but only %d could be parsed.",
2399  len, used),
2400  errhint("If your source string is not fixed-width, "
2401  "try using the \"FM\" modifier."))));
2402 
2403  *src += used;
2404  }
2405 
2406  if (*src == init)
2408  (errcode(ERRCODE_INVALID_DATETIME_FORMAT),
2409  errmsg("invalid value \"%s\" for \"%s\"",
2410  copy, node->key->name),
2411  errdetail("Value must be an integer."))));
2412 
2413  if (errno == ERANGE || result < INT_MIN || result > INT_MAX)
2415  (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
2416  errmsg("value for \"%s\" in source string is out of range",
2417  node->key->name),
2418  errdetail("Value must be in the range %d to %d.",
2419  INT_MIN, INT_MAX))));
2420 
2421  if (dest != NULL)
2422  {
2423  from_char_set_int(dest, (int) result, node, have_error);
2424  CHECK_ERROR;
2425  }
2426 
2427  return *src - init;
2428 
2429 on_error:
2430  return -1;
2431 }
int errhint(const char *fmt,...)
Definition: elog.c:1069
static int strspace_len(const char *str)
Definition: formatting.c:2251
const KeyWord * key
Definition: formatting.c:200
int errcode(int sqlerrcode)
Definition: elog.c:608
#define CHECK_ERROR
Definition: formatting.c:131
#define ERROR
Definition: elog.h:43
static bool is_next_separator(FormatNode *n)
Definition: formatting.c:2194
#define S_FM(_s)
Definition: formatting.c:560
int errdetail(const char *fmt,...)
Definition: elog.c:955
#define init()
#define ereport(elevel, rest)
Definition: elog.h:141
const char * name
Definition: formatting.c:188
size_t strlcpy(char *dst, const char *src, size_t siz)
Definition: strlcpy.c:45
#define Assert(condition)
Definition: c.h:738
uint8 suffix
Definition: formatting.c:199
int errmsg(const char *fmt,...)
Definition: elog.c:822
#define DCH_MAX_ITEM_SIZ
Definition: formatting.c:156
static void from_char_set_int(int *dest, const int value, const FormatNode *node, bool *have_error)
Definition: formatting.c:2299
#define RETURN_ERROR(throw_error)
Definition: formatting.c:118

◆ from_char_seq_search()

static int from_char_seq_search ( int *  dest,
const char **  src,
const char *const *  array,
FormatNode node,
bool have_error 
)
static

Definition at line 2520 of file formatting.c.

References ereport, errcode(), errdetail(), errmsg(), ERROR, FormatNode::key, KeyWord::name, pstrdup(), RETURN_ERROR, scanner_isspace(), and seq_search().

Referenced by DCH_from_char().

2522 {
2523  int len;
2524 
2525  *dest = seq_search(*src, array, &len);
2526 
2527  if (len <= 0)
2528  {
2529  /*
2530  * In the error report, truncate the string at the next whitespace (if
2531  * any) to avoid including irrelevant data.
2532  */
2533  char *copy = pstrdup(*src);
2534  char *c;
2535 
2536  for (c = copy; *c; c++)
2537  {
2538  if (scanner_isspace(*c))
2539  {
2540  *c = '\0';
2541  break;
2542  }
2543  }
2544 
2546  (errcode(ERRCODE_INVALID_DATETIME_FORMAT),
2547  errmsg("invalid value \"%s\" for \"%s\"",
2548  copy, node->key->name),
2549  errdetail("The given value did not match any of "
2550  "the allowed values for this field."))));
2551  }
2552  *src += len;
2553  return len;
2554 
2555 on_error:
2556  return -1;
2557 }
const KeyWord * key
Definition: formatting.c:200
char * pstrdup(const char *in)
Definition: mcxt.c:1186
int errcode(int sqlerrcode)
Definition: elog.c:608
#define ERROR
Definition: elog.h:43
char * c
static int seq_search(const char *name, const char *const *array, int *len)
Definition: formatting.c:2460
int errdetail(const char *fmt,...)
Definition: elog.c:955
#define ereport(elevel, rest)
Definition: elog.h:141
const char * name
Definition: formatting.c:188
bool scanner_isspace(char ch)
Definition: scansup.c:220
int errmsg(const char *fmt,...)
Definition: elog.c:822
#define RETURN_ERROR(throw_error)
Definition: formatting.c:118

◆ from_char_set_int()

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

Definition at line 2299 of file formatting.c.

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

Referenced by DCH_from_char(), and from_char_parse_int_len().

2301 {
2302  if (*dest != 0 && *dest != value)
2304  (errcode(ERRCODE_INVALID_DATETIME_FORMAT),
2305  errmsg("conflicting values for \"%s\" field in "
2306  "formatting string",
2307  node->key->name),
2308  errdetail("This value contradicts a previous setting "
2309