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, Oid collid, 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_ascii (const char *name, const char *const *array, int *len)
 
static int seq_search_localized (const char *name, char **array, int *len, Oid collid)
 
static int from_char_seq_search (int *dest, const char **src, const char *const *array, char **localized_array, Oid collid, FormatNode *node, bool *have_error)
 
static void do_to_timestamp (text *date_txt, text *fmt, Oid collid, 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, Oid collid, 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 5093 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:195
#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:1071
int errcode(int sqlerrcode)
Definition: elog.c:610
#define ERROR
Definition: elog.h:43
int errmsg(const char *fmt,...)
Definition: elog.c:824

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 5304 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:568
#define PG_GET_COLLATION()
Definition: fmgr.h:198
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:5518

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

Definition at line 5999 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 5092 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_from_char(), and 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:907

Definition at line 2149 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 4740 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 2230 of file formatting.c.

Referenced by DCH_from_char().

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

◆ asc_initcap()

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

Definition at line 2080 of file formatting.c.

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

Referenced by str_initcap().

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

References pg_ascii_tolower(), and pnstrdup().

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

2035 {
2036  char *result;
2037  char *p;
2038 
2039  if (!buff)
2040  return NULL;
2041 
2042  result = pnstrdup(buff, nbytes);
2043 
2044  for (p = result; *p; p++)
2045  *p = pg_ascii_tolower((unsigned char) *p);
2046 
2047  return result;
2048 }
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 2129 of file formatting.c.

References asc_tolower().

Referenced by DCH_to_char(), and NUM_processor().

2130 {
2131  return asc_tolower(buff, strlen(buff));
2132 }
char * asc_tolower(const char *buff, size_t nbytes)
Definition: formatting.c:2034

◆ asc_toupper()

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

Definition at line 2057 of file formatting.c.

References pg_ascii_toupper(), and pnstrdup().

Referenced by asc_toupper_z(), and str_toupper().

2058 {
2059  char *result;
2060  char *p;
2061 
2062  if (!buff)
2063  return NULL;
2064 
2065  result = pnstrdup(buff, nbytes);
2066 
2067  for (p = result; *p; p++)
2068  *p = pg_ascii_toupper((unsigned char) *p);
2069 
2070  return result;
2071 }
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 2135 of file formatting.c.

References asc_toupper().

Referenced by DCH_to_char().

2136 {
2137  return asc_toupper(buff, strlen(buff));
2138 }
char * asc_toupper(const char *buff, size_t nbytes)
Definition: formatting.c:2057

◆ datetime_to_char_body()

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

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

3967 {
3968  FormatNode *format;
3969  char *fmt_str,
3970  *result;
3971  bool incache;
3972  int fmt_len;
3973  text *res;
3974 
3975  /*
3976  * Convert fmt to C string
3977  */
3978  fmt_str = text_to_cstring(fmt);
3979  fmt_len = strlen(fmt_str);
3980 
3981  /*
3982  * Allocate workspace for result as C string
3983  */
3984  result = palloc((fmt_len * DCH_MAX_ITEM_SIZ) + 1);
3985  *result = '\0';
3986 
3987  if (fmt_len > DCH_CACHE_SIZE)
3988  {
3989  /*
3990  * Allocate new memory if format picture is bigger than static cache
3991  * and do not use cache (call parser always)
3992  */
3993  incache = false;
3994 
3995  format = (FormatNode *) palloc((fmt_len + 1) * sizeof(FormatNode));
3996 
3997  parse_format(format, fmt_str, DCH_keywords,
3998  DCH_suff, DCH_index, DCH_FLAG, NULL);
3999  }
4000  else
4001  {
4002  /*
4003  * Use cache buffers
4004  */
4005  DCHCacheEntry *ent = DCH_cache_fetch(fmt_str, false);
4006 
4007  incache = true;
4008  format = ent->format;
4009  }
4010 
4011  /* The real work is here */
4012  DCH_to_char(format, is_interval, tmtc, result, collid);
4013 
4014  if (!incache)
4015  pfree(format);
4016 
4017  pfree(fmt_str);
4018 
4019  /* convert C-string result to TEXT format */
4020  res = cstring_to_text(result);
4021 
4022  pfree(result);
4023  return res;
4024 }
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:2653
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:1324
#define DCH_FLAG
Definition: formatting.c:141
text * cstring_to_text(const char *s)
Definition: varlena.c:172
char * text_to_cstring(const text *t)
Definition: varlena.c:205
void * palloc(Size size)
Definition: mcxt.c:949
static const KeyWord DCH_keywords[]
Definition: formatting.c:774
Definition: c.h:562
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:3939

◆ DCH_cache_fetch()

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

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

3940 {
3941  DCHCacheEntry *ent;
3942 
3943  if ((ent = DCH_cache_search(str, std)) == NULL)
3944  {
3945  /*
3946  * Not in the cache, must run parser and save a new format-picture to
3947  * the cache. Do not mark the cache entry valid until parsing
3948  * succeeds.
3949  */
3950  ent = DCH_cache_getnew(str, std);
3951 
3953  DCH_FLAG | (std ? STD_FLAG : 0), NULL);
3954 
3955  ent->valid = true;
3956  }
3957  return ent;
3958 }
static DCHCacheEntry * DCH_cache_getnew(const char *str, bool std)
Definition: formatting.c:3858
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:1324
static DCHCacheEntry * DCH_cache_search(const char *str, bool std)
Definition: formatting.c:3918
#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 3858 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().

3859 {
3860  DCHCacheEntry *ent;
3861 
3862  /* Ensure we can advance DCHCounter below */
3864 
3865  /*
3866  * If cache is full, remove oldest entry (or recycle first not-valid one)
3867  */
3869  {
3870  DCHCacheEntry *old = DCHCache[0];
3871 
3872 #ifdef DEBUG_TO_FROM_CHAR
3873  elog(DEBUG_elog_output, "cache is full (%d)", n_DCHCache);
3874 #endif
3875  if (old->valid)
3876  {
3877  for (int i = 1; i < DCH_CACHE_ENTRIES; i++)
3878  {
3879  ent = DCHCache[i];
3880  if (!ent->valid)
3881  {
3882  old = ent;
3883  break;
3884  }
3885  if (ent->age < old->age)
3886  old = ent;
3887  }
3888  }
3889 #ifdef DEBUG_TO_FROM_CHAR
3890  elog(DEBUG_elog_output, "OLD: '%s' AGE: %d", old->str, old->age);
3891 #endif
3892  old->valid = false;
3893  StrNCpy(old->str, str, DCH_CACHE_SIZE + 1);
3894  old->age = (++DCHCounter);
3895  /* caller is expected to fill format, then set valid */
3896  return old;
3897  }
3898  else
3899  {
3900 #ifdef DEBUG_TO_FROM_CHAR
3901  elog(DEBUG_elog_output, "NEW (%d)", n_DCHCache);
3902 #endif
3903  Assert(DCHCache[n_DCHCache] == NULL);
3904  DCHCache[n_DCHCache] = ent = (DCHCacheEntry *)
3906  ent->valid = false;
3907  StrNCpy(ent->str, str, DCH_CACHE_SIZE + 1);
3908  ent->std = std;
3909  ent->age = (++DCHCounter);
3910  /* caller is expected to fill format, then set valid */
3911  ++n_DCHCache;
3912  return ent;
3913  }
3914 }
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:745
#define StrNCpy(dst, src, len)
Definition: c.h:951
static void DCH_prevent_counter_overflow(void)
Definition: formatting.c:3738
#define elog(elevel,...)
Definition: elog.h:214
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 3918 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().

3919 {
3920  /* Ensure we can advance DCHCounter below */
3922 
3923  for (int i = 0; i < n_DCHCache; i++)
3924  {
3925  DCHCacheEntry *ent = DCHCache[i];
3926 
3927  if (ent->valid && strcmp(ent->str, str) == 0 && ent->std == std)
3928  {
3929  ent->age = (++DCHCounter);
3930  return ent;
3931  }
3932  }
3933 
3934  return NULL;
3935 }
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:3738
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 3754 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().

3755 {
3756  FormatNode *n;
3757  int flags = 0;
3758 
3759  for (n = node; n->type != NODE_TYPE_END; n++)
3760  {
3761  if (n->type != NODE_TYPE_ACTION)
3762  continue;
3763 
3764  switch (n->key->id)
3765  {
3766  case DCH_FX:
3767  break;
3768  case DCH_A_M:
3769  case DCH_P_M:
3770  case DCH_a_m:
3771  case DCH_p_m:
3772  case DCH_AM:
3773  case DCH_PM:
3774  case DCH_am:
3775  case DCH_pm:
3776  case DCH_HH:
3777  case DCH_HH12:
3778  case DCH_HH24:
3779  case DCH_MI:
3780  case DCH_SS:
3781  case DCH_MS: /* millisecond */
3782  case DCH_US: /* microsecond */
3783  case DCH_FF1:
3784  case DCH_FF2:
3785  case DCH_FF3:
3786  case DCH_FF4:
3787  case DCH_FF5:
3788  case DCH_FF6:
3789  case DCH_SSSS:
3790  flags |= DCH_TIMED;
3791  break;
3792  case DCH_tz:
3793  case DCH_TZ:
3794  case DCH_OF:
3796  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
3797  errmsg("formatting field \"%s\" is only supported in to_char",
3798  n->key->name))));
3799  flags |= DCH_ZONED;
3800  break;
3801  case DCH_TZH:
3802  case DCH_TZM:
3803  flags |= DCH_ZONED;
3804  break;
3805  case DCH_A_D:
3806  case DCH_B_C:
3807  case DCH_a_d:
3808  case DCH_b_c:
3809  case DCH_AD:
3810  case DCH_BC:
3811  case DCH_ad:
3812  case DCH_bc:
3813  case DCH_MONTH:
3814  case DCH_Month:
3815  case DCH_month:
3816  case DCH_MON:
3817  case DCH_Mon:
3818  case DCH_mon:
3819  case DCH_MM:
3820  case DCH_DAY:
3821  case DCH_Day:
3822  case DCH_day:
3823  case DCH_DY:
3824  case DCH_Dy:
3825  case DCH_dy:
3826  case DCH_DDD:
3827  case DCH_IDDD:
3828  case DCH_DD:
3829  case DCH_D:
3830  case DCH_ID:
3831  case DCH_WW:
3832  case DCH_Q:
3833  case DCH_CC:
3834  case DCH_Y_YYY:
3835  case DCH_YYYY:
3836  case DCH_IYYY:
3837  case DCH_YYY:
3838  case DCH_IYY:
3839  case DCH_YY:
3840  case DCH_IY:
3841  case DCH_Y:
3842  case DCH_I:
3843  case DCH_RM:
3844  case DCH_rm:
3845  case DCH_W:
3846  case DCH_J:
3847  flags |= DCH_DATED;
3848  break;
3849  }
3850  }
3851 
3852 on_error:
3853  return flags;
3854 }
#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:610
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
const char * name
Definition: formatting.c:188
#define ereport(elevel,...)
Definition: elog.h:144
int errmsg(const char *fmt,...)
Definition: elog.c:824
#define RETURN_ERROR(throw_error)
Definition: formatting.c:118

◆ DCH_from_char()

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

Definition at line 3252 of file formatting.c.

References adbc_strings, adbc_strings_long, adjust_partial_year_to_2020(), ampm_strings, ampm_strings_long, Assert, TmFromChar::bc, cache_locale_time(), 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, InvalidOid, is_separator_char(), TmFromChar::j, FormatNode::key, localized_abbrev_days, localized_abbrev_months, localized_full_days, localized_full_months, 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, S_TM, 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().

3254 {
3255  FormatNode *n;
3256  const char *s;
3257  int len,
3258  value;
3259  bool fx_mode = std;
3260 
3261  /* number of extra skipped characters (more than given in format string) */
3262  int extra_skip = 0;
3263 
3264  /* cache localized days and months */
3266 
3267  for (n = node, s = in; n->type != NODE_TYPE_END && *s != '\0'; n++)
3268  {
3269  /*
3270  * Ignore spaces at the beginning of the string and before fields when
3271  * not in FX (fixed width) mode.
3272  */
3273  if (!fx_mode && (n->type != NODE_TYPE_ACTION || n->key->id != DCH_FX) &&
3274  (n->type == NODE_TYPE_ACTION || n == node))
3275  {
3276  while (*s != '\0' && isspace((unsigned char) *s))
3277  {
3278  s++;
3279  extra_skip++;
3280  }
3281  }
3282 
3283  if (n->type == NODE_TYPE_SPACE || n->type == NODE_TYPE_SEPARATOR)
3284  {
3285  if (std)
3286  {
3287  /*
3288  * Standard mode requires strict matching between format
3289  * string separators/spaces and input string.
3290  */
3291  Assert(n->character[0] && !n->character[1]);
3292 
3293  if (*s == n->character[0])
3294  s++;
3295  else
3297  (errcode(ERRCODE_INVALID_DATETIME_FORMAT),
3298  errmsg("unmatched format separator \"%c\"",
3299  n->character[0]))));
3300  }
3301  else if (!fx_mode)
3302  {
3303  /*
3304  * In non FX (fixed format) mode one format string space or
3305  * separator match to one space or separator in input string.
3306  * Or match nothing if there is no space or separator in the
3307  * current position of input string.
3308  */
3309  extra_skip--;
3310  if (isspace((unsigned char) *s) || is_separator_char(s))
3311  {
3312  s++;
3313  extra_skip++;
3314  }
3315  }
3316  else
3317  {
3318  /*
3319  * In FX mode, on format string space or separator we consume
3320  * exactly one character from input string. Notice we don't
3321  * insist that the consumed character match the format's
3322  * character.
3323  */
3324  s += pg_mblen(s);
3325  }
3326  continue;
3327  }
3328  else if (n->type != NODE_TYPE_ACTION)
3329  {
3330  /*
3331  * Text character, so consume one character from input string.
3332  * Notice we don't insist that the consumed character match the
3333  * format's character.
3334  */
3335  if (!fx_mode)
3336  {
3337  /*
3338  * In non FX mode we might have skipped some extra characters
3339  * (more than specified in format string) before. In this
3340  * case we don't skip input string character, because it might
3341  * be part of field.
3342  */
3343  if (extra_skip > 0)
3344  extra_skip--;
3345  else
3346  s += pg_mblen(s);
3347  }
3348  else
3349  {
3350  s += pg_mblen(s);
3351  }
3352  continue;
3353  }
3354 
3355  from_char_set_mode(out, n->key->date_mode, have_error);
3356  CHECK_ERROR;
3357 
3358  switch (n->key->id)
3359  {
3360  case DCH_FX:
3361  fx_mode = true;
3362  break;
3363  case DCH_A_M:
3364  case DCH_P_M:
3365  case DCH_a_m:
3366  case DCH_p_m:
3368  NULL, InvalidOid,
3369  n, have_error);
3370  CHECK_ERROR;
3371  from_char_set_int(&out->pm, value % 2, n, have_error);
3372  CHECK_ERROR;
3373  out->clock = CLOCK_12_HOUR;
3374  break;
3375  case DCH_AM:
3376  case DCH_PM:
3377  case DCH_am:
3378  case DCH_pm:
3379  from_char_seq_search(&value, &s, ampm_strings,
3380  NULL, InvalidOid,
3381  n, have_error);
3382  CHECK_ERROR;
3383  from_char_set_int(&out->pm, value % 2, n, have_error);
3384  CHECK_ERROR;
3385  out->clock = CLOCK_12_HOUR;
3386  break;
3387  case DCH_HH:
3388  case DCH_HH12:
3389  from_char_parse_int_len(&out->hh, &s, 2, n, have_error);
3390  CHECK_ERROR;
3391  out->clock = CLOCK_12_HOUR;
3392  SKIP_THth(s, n->suffix);
3393  break;
3394  case DCH_HH24:
3395  from_char_parse_int_len(&out->hh, &s, 2, n, have_error);
3396  CHECK_ERROR;
3397  SKIP_THth(s, n->suffix);
3398  break;
3399  case DCH_MI:
3400  from_char_parse_int(&out->mi, &s, n, have_error);
3401  CHECK_ERROR;
3402  SKIP_THth(s, n->suffix);
3403  break;
3404  case DCH_SS:
3405  from_char_parse_int(&out->ss, &s, n, have_error);
3406  CHECK_ERROR;
3407  SKIP_THth(s, n->suffix);
3408  break;
3409  case DCH_MS: /* millisecond */
3410  len = from_char_parse_int_len(&out->ms, &s, 3, n, have_error);
3411  CHECK_ERROR;
3412 
3413  /*
3414  * 25 is 0.25 and 250 is 0.25 too; 025 is 0.025 and not 0.25
3415  */
3416  out->ms *= len == 1 ? 100 :
3417  len == 2 ? 10 : 1;
3418 
3419  SKIP_THth(s, n->suffix);
3420  break;
3421  case DCH_FF1:
3422  case DCH_FF2:
3423  case DCH_FF3:
3424  case DCH_FF4:
3425  case DCH_FF5:
3426  case DCH_FF6:
3427  out->ff = n->key->id - DCH_FF1 + 1;
3428  /* fall through */
3429  case DCH_US: /* microsecond */
3430  len = from_char_parse_int_len(&out->us, &s,
3431  n->key->id == DCH_US ? 6 :
3432  out->ff, n, have_error);
3433  CHECK_ERROR;
3434 
3435  out->us *= len == 1 ? 100000 :
3436  len == 2 ? 10000 :
3437  len == 3 ? 1000 :
3438  len == 4 ? 100 :
3439  len == 5 ? 10 : 1;
3440 
3441  SKIP_THth(s, n->suffix);
3442  break;
3443  case DCH_SSSS:
3444  from_char_parse_int(&out->ssss, &s, n, have_error);
3445  CHECK_ERROR;
3446  SKIP_THth(s, n->suffix);
3447  break;
3448  case DCH_tz:
3449  case DCH_TZ:
3450  case DCH_OF:
3452  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
3453  errmsg("formatting field \"%s\" is only supported in to_char",
3454  n->key->name))));
3455  CHECK_ERROR;
3456  break;
3457  case DCH_TZH:
3458 
3459  /*
3460  * Value of TZH might be negative. And the issue is that we
3461  * might swallow minus sign as the separator. So, if we have
3462  * skipped more characters than specified in the format
3463  * string, then we consider prepending last skipped minus to
3464  * TZH.
3465  */
3466  if (*s == '+' || *s == '-' || *s == ' ')
3467  {
3468  out->tzsign = *s == '-' ? -1 : +1;
3469  s++;
3470  }
3471  else
3472  {
3473  if (extra_skip > 0 && *(s - 1) == '-')
3474  out->tzsign = -1;
3475  else
3476  out->tzsign = +1;
3477  }
3478 
3479  from_char_parse_int_len(&out->tzh, &s, 2, n, have_error);
3480  CHECK_ERROR;
3481  break;
3482  case DCH_TZM:
3483  /* assign positive timezone sign if TZH was not seen before */
3484  if (!out->tzsign)
3485  out->tzsign = +1;
3486  from_char_parse_int_len(&out->tzm, &s, 2, n, have_error);
3487  CHECK_ERROR;
3488  break;
3489  case DCH_A_D:
3490  case DCH_B_C:
3491  case DCH_a_d:
3492  case DCH_b_c:
3494  NULL, InvalidOid,
3495  n, have_error);
3496  CHECK_ERROR;
3497  from_char_set_int(&out->bc, value % 2, n, have_error);
3498  CHECK_ERROR;
3499  break;
3500  case DCH_AD:
3501  case DCH_BC:
3502  case DCH_ad:
3503  case DCH_bc:
3504  from_char_seq_search(&value, &s, adbc_strings,
3505  NULL, InvalidOid,
3506  n, have_error);
3507  CHECK_ERROR;
3508  from_char_set_int(&out->bc, value % 2, n, have_error);
3509  CHECK_ERROR;
3510  break;
3511  case DCH_MONTH:
3512  case DCH_Month:
3513  case DCH_month:
3514  from_char_seq_search(&value, &s, months_full,
3515  S_TM(n->suffix) ? localized_full_months : NULL,
3516  collid,
3517  n, have_error);
3518  CHECK_ERROR;
3519  from_char_set_int(&out->mm, value + 1, n, have_error);
3520  CHECK_ERROR;
3521  break;
3522  case DCH_MON:
3523  case DCH_Mon:
3524  case DCH_mon:
3525  from_char_seq_search(&value, &s, months,
3526  S_TM(n->suffix) ? localized_abbrev_months : NULL,
3527  collid,
3528  n, have_error);
3529  CHECK_ERROR;
3530  from_char_set_int(&out->mm, value + 1, n, have_error);
3531  CHECK_ERROR;
3532  break;
3533  case DCH_MM:
3534  from_char_parse_int(&out->mm, &s, n, have_error);
3535  CHECK_ERROR;
3536  SKIP_THth(s, n->suffix);
3537  break;
3538  case DCH_DAY:
3539  case DCH_Day:
3540  case DCH_day:
3541  from_char_seq_search(&value, &s, days,
3542  S_TM(n->suffix) ? localized_full_days : NULL,
3543  collid,
3544  n, have_error);
3545  CHECK_ERROR;
3546  from_char_set_int(&out->d, value, n, have_error);
3547  CHECK_ERROR;
3548  out->d++;
3549  break;
3550  case DCH_DY:
3551  case DCH_Dy:
3552  case DCH_dy:
3553  from_char_seq_search(&value, &s, days_short,
3554  S_TM(n->suffix) ? localized_abbrev_days : NULL,
3555  collid,
3556  n, have_error);
3557  CHECK_ERROR;
3558  from_char_set_int(&out->d, value, n, have_error);
3559  CHECK_ERROR;
3560  out->d++;
3561  break;
3562  case DCH_DDD:
3563  from_char_parse_int(&out->ddd, &s, n, have_error);
3564  CHECK_ERROR;
3565  SKIP_THth(s, n->suffix);
3566  break;
3567  case DCH_IDDD:
3568  from_char_parse_int_len(&out->ddd, &s, 3, n, have_error);
3569  CHECK_ERROR;
3570  SKIP_THth(s, n->suffix);
3571  break;
3572  case DCH_DD:
3573  from_char_parse_int(&out->dd, &s, n, have_error);
3574  CHECK_ERROR;
3575  SKIP_THth(s, n->suffix);
3576  break;
3577  case DCH_D:
3578  from_char_parse_int(&out->d, &s, n, have_error);
3579  CHECK_ERROR;
3580  SKIP_THth(s, n->suffix);
3581  break;
3582  case DCH_ID:
3583  from_char_parse_int_len(&out->d, &s, 1, n, have_error);
3584  CHECK_ERROR;
3585  /* Shift numbering to match Gregorian where Sunday = 1 */
3586  if (++out->d > 7)
3587  out->d = 1;
3588  SKIP_THth(s, n->suffix);
3589  break;
3590  case DCH_WW:
3591  case DCH_IW:
3592  from_char_parse_int(&out->ww, &s, n, have_error);
3593  CHECK_ERROR;
3594  SKIP_THth(s, n->suffix);
3595  break;
3596  case DCH_Q:
3597 
3598  /*
3599  * We ignore 'Q' when converting to date because it is unclear
3600  * which date in the quarter to use, and some people specify
3601  * both quarter and month, so if it was honored it might
3602  * conflict with the supplied month. That is also why we don't
3603  * throw an error.
3604  *
3605  * We still parse the source string for an integer, but it
3606  * isn't stored anywhere in 'out'.
3607  */
3608  from_char_parse_int((int *) NULL, &s, n, have_error);
3609  CHECK_ERROR;
3610  SKIP_THth(s, n->suffix);
3611  break;
3612  case DCH_CC:
3613  from_char_parse_int(&out->cc, &s, n, have_error);
3614  CHECK_ERROR;
3615  SKIP_THth(s, n->suffix);
3616  break;
3617  case DCH_Y_YYY:
3618  {
3619  int matched,
3620  years,
3621  millennia,
3622  nch;
3623 
3624  matched = sscanf(s, "%d,%03d%n", &millennia, &years, &nch);
3625  if (matched < 2)
3627  (errcode(ERRCODE_INVALID_DATETIME_FORMAT),
3628  errmsg("invalid input string for \"Y,YYY\""))));
3629  years += (millennia * 1000);
3630  from_char_set_int(&out->year, years, n, have_error);
3631  CHECK_ERROR;
3632  out->yysz = 4;
3633  s += nch;
3634  SKIP_THth(s, n->suffix);
3635  }
3636  break;
3637  case DCH_YYYY:
3638  case DCH_IYYY:
3639  from_char_parse_int(&out->year, &s, n, have_error);
3640  CHECK_ERROR;
3641  out->yysz = 4;
3642  SKIP_THth(s, n->suffix);
3643  break;
3644  case DCH_YYY:
3645  case DCH_IYY:
3646  len = from_char_parse_int(&out->year, &s, n, have_error);
3647  CHECK_ERROR;
3648  if (len < 4)
3649  out->year = adjust_partial_year_to_2020(out->year);
3650  out->yysz = 3;
3651  SKIP_THth(s, n->suffix);
3652  break;
3653  case DCH_YY:
3654  case DCH_IY:
3655  len = from_char_parse_int(&out->year, &s, n, have_error);
3656  CHECK_ERROR;
3657  if (len < 4)
3658  out->year = adjust_partial_year_to_2020(out->year);
3659  out->yysz = 2;
3660  SKIP_THth(s, n->suffix);
3661  break;
3662  case DCH_Y:
3663  case DCH_I:
3664  len = from_char_parse_int(&out->year, &s, n, have_error);
3665  CHECK_ERROR;
3666  if (len < 4)
3667  out->year = adjust_partial_year_to_2020(out->year);
3668  out->yysz = 1;
3669  SKIP_THth(s, n->suffix);
3670  break;
3671  case DCH_RM:
3672  case DCH_rm:
3674  NULL, InvalidOid,
3675  n, have_error);
3676  CHECK_ERROR;
3677  from_char_set_int(&out->mm, MONTHS_PER_YEAR - value,
3678  n, have_error);
3679  CHECK_ERROR;
3680  break;
3681  case DCH_W:
3682  from_char_parse_int(&out->w, &s, n, have_error);
3683  CHECK_ERROR;
3684  SKIP_THth(s, n->suffix);
3685  break;
3686  case DCH_J:
3687  from_char_parse_int(&out->j, &s, n, have_error);
3688  CHECK_ERROR;
3689  SKIP_THth(s, n->suffix);
3690  break;
3691  }
3692 
3693  /* Ignore all spaces after fields */
3694  if (!fx_mode)
3695  {
3696  extra_skip = 0;
3697  while (*s != '\0' && isspace((unsigned char) *s))
3698  {
3699  s++;
3700  extra_skip++;
3701  }
3702  }
3703  }
3704 
3705  /*
3706  * Standard parsing mode doesn't allow unmatched format patterns or
3707  * trailing characters in the input string.
3708  */
3709  if (std)
3710  {
3711  if (n->type != NODE_TYPE_END)
3713  (errcode(ERRCODE_INVALID_DATETIME_FORMAT),
3714  errmsg("input string is too short for datetime format"))));
3715 
3716  while (*s != '\0' && isspace((unsigned char) *s))
3717  s++;
3718 
3719  if (*s != '\0')
3721  (errcode(ERRCODE_INVALID_DATETIME_FORMAT),
3722  errmsg("trailing characters remain in input string "
3723  "after datetime format"))));
3724  }
3725 
3726 on_error:
3727  return;
3728 }
#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
int errcode(int sqlerrcode)
Definition: elog.c:610
static void from_char_set_mode(TmFromChar *tmfc, const FromCharDateMode mode, bool *have_error)
Definition: formatting.c:2275
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 int from_char_seq_search(int *dest, const char **src, const char *const *array, char **localized_array, Oid collid, FormatNode *node, bool *have_error)
Definition: formatting.c:2604
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:2149
char * localized_abbrev_months[12+1]
Definition: pg_locale.c:108
static bool is_separator_char(const char *str)
Definition: formatting.c:1135
#define NODE_TYPE_SPACE
Definition: formatting.c:207
const char *const days[]
Definition: datetime.c:68
#define NODE_TYPE_SEPARATOR
Definition: formatting.c:206
char * localized_full_days[7+1]
Definition: pg_locale.c:107
#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
#define InvalidOid
Definition: postgres_ext.h:36
static struct @143 value
#define ereport(elevel,...)
Definition: elog.h:144
char * localized_full_months[12+1]
Definition: pg_locale.c:109
static int adjust_partial_year_to_2020(int year)
Definition: formatting.c:2230
#define Assert(condition)
Definition: c.h:745
void cache_locale_time(void)
Definition: pg_locale.c:781
int pg_mblen(const char *mbstr)
Definition: mbutils.c:907
uint8 suffix
Definition: formatting.c:199
static int from_char_parse_int(int *dest, const char **src, FormatNode *node, bool *have_error)
Definition: formatting.c:2446
int errmsg(const char *fmt,...)
Definition: elog.c:824
#define S_TM(_s)
Definition: formatting.c:562
char * localized_abbrev_days[7+1]
Definition: pg_locale.c:106
static void from_char_set_int(int *dest, const int value, const FormatNode *node, bool *have_error)
Definition: formatting.c:2302
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:2343
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 3738 of file formatting.c.

References DCHCounter, i, and n_DCHCache.

Referenced by DCH_cache_getnew(), and DCH_cache_search().

3739 {
3740  if (DCHCounter >= (INT_MAX - 1))
3741  {
3742  for (int i = 0; i < n_DCHCache; i++)
3743  DCHCache[i]->age >>= 1;
3744  DCHCounter >>= 1;
3745  }
3746 }
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 2653 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().

2654 {
2655  FormatNode *n;
2656  char *s;
2657  struct pg_tm *tm = &in->tm;
2658  int i;
2659 
2660  /* cache localized days and months */
2662 
2663  s = out;
2664  for (n = node; n->type != NODE_TYPE_END; n++)
2665  {
2666  if (n->type != NODE_TYPE_ACTION)
2667  {
2668  strcpy(s, n->character);
2669  s += strlen(s);
2670  continue;
2671  }
2672 
2673  switch (n->key->id)
2674  {
2675  case DCH_A_M:
2676  case DCH_P_M:
2677  strcpy(s, (tm->tm_hour % HOURS_PER_DAY >= HOURS_PER_DAY / 2)
2678  ? P_M_STR : A_M_STR);
2679  s += strlen(s);
2680  break;
2681  case DCH_AM:
2682  case DCH_PM:
2683  strcpy(s, (tm->tm_hour % HOURS_PER_DAY >= HOURS_PER_DAY / 2)
2684  ? PM_STR : AM_STR);
2685  s += strlen(s);
2686  break;
2687  case DCH_a_m:
2688  case DCH_p_m:
2689  strcpy(s, (tm->tm_hour % HOURS_PER_DAY >= HOURS_PER_DAY / 2)
2690  ? p_m_STR : a_m_STR);
2691  s += strlen(s);
2692  break;
2693  case DCH_am:
2694  case DCH_pm:
2695  strcpy(s, (tm->tm_hour % HOURS_PER_DAY >= HOURS_PER_DAY / 2)
2696  ? pm_STR : am_STR);
2697  s += strlen(s);
2698  break;
2699  case DCH_HH:
2700  case DCH_HH12:
2701 
2702  /*
2703  * display time as shown on a 12-hour clock, even for
2704  * intervals
2705  */
2706  sprintf(s, "%0*d", S_FM(n->suffix) ? 0 : (tm->tm_hour >= 0) ? 2 : 3,
2707  tm->tm_hour % (HOURS_PER_DAY / 2) == 0 ? HOURS_PER_DAY / 2 :
2708  tm->tm_hour % (HOURS_PER_DAY / 2));
2709  if (S_THth(n->suffix))
2710  str_numth(s, s, S_TH_TYPE(n->suffix));
2711  s += strlen(s);
2712  break;
2713  case DCH_HH24:
2714  sprintf(s, "%0*d", S_FM(n->suffix) ? 0 : (tm->tm_hour >= 0) ? 2 : 3,
2715  tm->tm_hour);
2716  if (S_THth(n->suffix))
2717  str_numth(s, s, S_TH_TYPE(n->suffix));
2718  s += strlen(s);
2719  break;
2720  case DCH_MI:
2721  sprintf(s, "%0*d", S_FM(n->suffix) ? 0 : (tm->tm_min >= 0) ? 2 : 3,
2722  tm->tm_min);
2723  if (S_THth(n->suffix))
2724  str_numth(s, s, S_TH_TYPE(n->suffix));
2725  s += strlen(s);
2726  break;
2727  case DCH_SS:
2728  sprintf(s, "%0*d", S_FM(n->suffix) ? 0 : (tm->tm_sec >= 0) ? 2 : 3,
2729  tm->tm_sec);
2730  if (S_THth(n->suffix))
2731  str_numth(s, s, S_TH_TYPE(n->suffix));
2732  s += strlen(s);
2733  break;
2734 
2735 #define DCH_to_char_fsec(frac_fmt, frac_val) \
2736  sprintf(s, frac_fmt, (int) (frac_val)); \
2737  if (S_THth(n->suffix)) \
2738  str_numth(s, s, S_TH_TYPE(n->suffix)); \
2739  s += strlen(s)
2740 
2741  case DCH_FF1: /* tenth of second */
2742  DCH_to_char_fsec("%01d", in->fsec / 100000);
2743  break;
2744  case DCH_FF2: /* hundredth of second */
2745  DCH_to_char_fsec("%02d", in->fsec / 10000);
2746  break;
2747  case DCH_FF3:
2748  case DCH_MS: /* millisecond */
2749  DCH_to_char_fsec("%03d", in->fsec / 1000);
2750  break;
2751  case DCH_FF4: /* tenth of a millisecond */
2752  DCH_to_char_fsec("%04d", in->fsec / 100);
2753  break;
2754  case DCH_FF5: /* hundredth of a millisecond */
2755  DCH_to_char_fsec("%05d", in->fsec / 10);
2756  break;
2757  case DCH_FF6:
2758  case DCH_US: /* microsecond */
2759  DCH_to_char_fsec("%06d", in->fsec);
2760  break;
2761 #undef DCH_to_char_fsec
2762  case DCH_SSSS:
2763  sprintf(s, "%d", tm->tm_hour * SECS_PER_HOUR +
2764  tm->tm_min * SECS_PER_MINUTE +
2765  tm->tm_sec);
2766  if (S_THth(n->suffix))
2767  str_numth(s, s, S_TH_TYPE(n->suffix));
2768  s += strlen(s);
2769  break;
2770  case DCH_tz:
2772  if (tmtcTzn(in))
2773  {
2774  /* We assume here that timezone names aren't localized */
2775  char *p = asc_tolower_z(tmtcTzn(in));
2776 
2777  strcpy(s, p);
2778  pfree(p);
2779  s += strlen(s);
2780  }
2781  break;
2782  case DCH_TZ:
2784  if (tmtcTzn(in))
2785  {
2786  strcpy(s, tmtcTzn(in));
2787  s += strlen(s);
2788  }
2789  break;
2790  case DCH_TZH:
2792  sprintf(s, "%c%02d",
2793  (tm->tm_gmtoff >= 0) ? '+' : '-',
2794  abs((int) tm->tm_gmtoff) / SECS_PER_HOUR);
2795  s += strlen(s);
2796  break;
2797  case DCH_TZM:
2799  sprintf(s, "%02d",
2800  (abs((int) tm->tm_gmtoff) % SECS_PER_HOUR) / SECS_PER_MINUTE);
2801  s += strlen(s);
2802  break;
2803  case DCH_OF:
2805  sprintf(s, "%c%0*d",
2806  (tm->tm_gmtoff >= 0) ? '+' : '-',
2807  S_FM(n->suffix) ? 0 : 2,
2808  abs((int) tm->tm_gmtoff) / SECS_PER_HOUR);
2809  s += strlen(s);
2810  if (abs((int) tm->tm_gmtoff) % SECS_PER_HOUR != 0)
2811  {
2812  sprintf(s, ":%02d",
2813  (abs((int) tm->tm_gmtoff) % SECS_PER_HOUR) / SECS_PER_MINUTE);
2814  s += strlen(s);
2815  }
2816  break;
2817  case DCH_A_D:
2818  case DCH_B_C:
2820  strcpy(s, (tm->tm_year <= 0 ? B_C_STR : A_D_STR));
2821  s += strlen(s);
2822  break;
2823  case DCH_AD:
2824  case DCH_BC:
2826  strcpy(s, (tm->tm_year <= 0 ? BC_STR : AD_STR));
2827  s += strlen(s);
2828  break;
2829  case DCH_a_d:
2830  case DCH_b_c:
2832  strcpy(s, (tm->tm_year <= 0 ? b_c_STR : a_d_STR));
2833  s += strlen(s);
2834  break;
2835  case DCH_ad:
2836  case DCH_bc:
2838  strcpy(s, (tm->tm_year <= 0 ? bc_STR : ad_STR));
2839  s += strlen(s);
2840  break;
2841  case DCH_MONTH:
2843  if (!tm->tm_mon)
2844  break;
2845  if (S_TM(n->suffix))
2846  {
2847  char *str = str_toupper_z(localized_full_months[tm->tm_mon - 1], collid);
2848 
2849  if (strlen(str) <= (n->key->len + TM_SUFFIX_LEN) * DCH_MAX_ITEM_SIZ)
2850  strcpy(s, str);
2851  else
2852  ereport(ERROR,
2853  (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
2854  errmsg("localized string format value too long")));
2855  }
2856  else
2857  sprintf(s, "%*s", S_FM(n->suffix) ? 0 : -9,
2858  asc_toupper_z(months_full[tm->tm_mon - 1]));
2859  s += strlen(s);
2860  break;
2861  case DCH_Month:
2863  if (!tm->tm_mon)
2864  break;
2865  if (S_TM(n->suffix))
2866  {
2867  char *str = str_initcap_z(localized_full_months[tm->tm_mon - 1], collid);
2868 
2869  if (strlen(str) <= (n->key->len + TM_SUFFIX_LEN) * DCH_MAX_ITEM_SIZ)
2870  strcpy(s, str);
2871  else
2872  ereport(ERROR,
2873  (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
2874  errmsg("localized string format value too long")));
2875  }
2876  else
2877  sprintf(s, "%*s", S_FM(n->suffix) ? 0 : -9,
2878  months_full[tm->tm_mon - 1]);
2879  s += strlen(s);
2880  break;
2881  case DCH_month:
2883  if (!tm->tm_mon)
2884  break;
2885  if (S_TM(n->suffix))
2886  {
2887  char *str = str_tolower_z(localized_full_months[tm->tm_mon - 1], collid);
2888 
2889  if (strlen(str) <= (n->key->len + TM_SUFFIX_LEN) * DCH_MAX_ITEM_SIZ)
2890  strcpy(s, str);
2891  else
2892  ereport(ERROR,
2893  (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
2894  errmsg("localized string format value too long")));
2895  }
2896  else
2897  sprintf(s, "%*s", S_FM(n->suffix) ? 0 : -9,
2898  asc_tolower_z(months_full[tm->tm_mon - 1]));
2899  s += strlen(s);
2900  break;
2901  case DCH_MON:
2903  if (!tm->tm_mon)
2904  break;
2905  if (S_TM(n->suffix))
2906  {
2907  char *str = str_toupper_z(localized_abbrev_months[tm->tm_mon - 1], collid);
2908 
2909  if (strlen(str) <= (n->key->len + TM_SUFFIX_LEN) * DCH_MAX_ITEM_SIZ)
2910  strcpy(s, str);
2911  else
2912  ereport(ERROR,
2913  (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
2914  errmsg("localized string format value too long")));
2915  }
2916  else
2917  strcpy(s, asc_toupper_z(months[tm->tm_mon - 1]));
2918  s += strlen(s);
2919  break;
2920  case DCH_Mon:
2922  if (!tm->tm_mon)
2923  break;
2924  if (S_TM(n->suffix))
2925  {
2926  char *str = str_initcap_z(localized_abbrev_months[tm->tm_mon - 1], collid);
2927 
2928  if (strlen(str) <= (n->key->len + TM_SUFFIX_LEN) * DCH_MAX_ITEM_SIZ)
2929  strcpy(s, str);
2930  else
2931  ereport(ERROR,
2932  (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
2933  errmsg("localized string format value too long")));
2934  }
2935  else
2936  strcpy(s, months[tm->tm_mon - 1]);
2937  s += strlen(s);
2938  break;
2939  case DCH_mon:
2941  if (!tm->tm_mon)
2942  break;
2943  if (S_TM(n->suffix))
2944  {
2945  char *str = str_tolower_z(localized_abbrev_months[tm->tm_mon - 1], collid);
2946 
2947  if (strlen(str) <= (n->key->len + TM_SUFFIX_LEN) * DCH_MAX_ITEM_SIZ)
2948  strcpy(s, str);
2949  else
2950  ereport(ERROR,
2951  (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
2952  errmsg("localized string format value too long")));
2953  }
2954  else
2955  strcpy(s, asc_tolower_z(months[tm->tm_mon - 1]));
2956  s += strlen(s);
2957  break;
2958  case DCH_MM:
2959  sprintf(s, "%0*d", S_FM(n->suffix) ? 0 : (tm->tm_mon >= 0) ? 2 : 3,
2960  tm->tm_mon);
2961  if (S_THth(n->suffix))
2962  str_numth(s, s, S_TH_TYPE(n->suffix));
2963  s += strlen(s);
2964  break;
2965  case DCH_DAY:
2967  if (S_TM(n->suffix))
2968  {
2969  char *str = str_toupper_z(localized_full_days[tm->tm_wday], collid);
2970 
2971  if (strlen(str) <= (n->key->len + TM_SUFFIX_LEN) * DCH_MAX_ITEM_SIZ)
2972  strcpy(s, str);
2973  else
2974  ereport(ERROR,
2975  (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
2976  errmsg("localized string format value too long")));
2977  }
2978  else
2979  sprintf(s, "%*s", S_FM(n->suffix) ? 0 : -9,
2980  asc_toupper_z(days[tm->tm_wday]));
2981  s += strlen(s);
2982  break;
2983  case DCH_Day:
2985  if (S_TM(n->suffix))
2986  {
2987  char *str = str_initcap_z(localized_full_days[tm->tm_wday], collid);
2988 
2989  if (strlen(str) <= (n->key->len + TM_SUFFIX_LEN) * DCH_MAX_ITEM_SIZ)
2990  strcpy(s, str);
2991  else
2992  ereport(ERROR,
2993  (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
2994  errmsg("localized string format value too long")));
2995  }
2996  else
2997  sprintf(s, "%*s", S_FM(n->suffix) ? 0 : -9,
2998  days[tm->tm_wday]);
2999  s += strlen(s);
3000  break;
3001  case DCH_day:
3003  if (S_TM(n->suffix))
3004  {
3005  char *str = str_tolower_z(localized_full_days[tm->tm_wday], collid);
3006 
3007  if (strlen(str) <= (n->key->len + TM_SUFFIX_LEN) * DCH_MAX_ITEM_SIZ)
3008  strcpy(s, str);
3009  else
3010  ereport(ERROR,
3011  (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
3012  errmsg("localized string format value too long")));
3013  }
3014  else
3015  sprintf(s, "%*s", S_FM(n->suffix) ? 0 : -9,
3016  asc_tolower_z(days[tm->tm_wday]));
3017  s += strlen(s);
3018  break;
3019  case DCH_DY:
3021  if (S_TM(n->suffix))
3022  {
3023  char *str = str_toupper_z(localized_abbrev_days[tm->tm_wday], collid);
3024 
3025  if (strlen(str) <= (n->key->len + TM_SUFFIX_LEN) * DCH_MAX_ITEM_SIZ)
3026  strcpy(s, str);
3027  else
3028  ereport(ERROR,
3029  (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
3030  errmsg("localized string format value too long")));
3031  }
3032  else
3033  strcpy(s, asc_toupper_z(days_short[tm->tm_wday]));
3034  s += strlen(s);
3035  break;
3036  case DCH_Dy:
3038  if (S_TM(n->suffix))
3039  {
3040  char *str = str_initcap_z(localized_abbrev_days[tm->tm_wday], collid);
3041 
3042  if (strlen(str) <= (n->key->len + TM_SUFFIX_LEN) * DCH_MAX_ITEM_SIZ)
3043  strcpy(s, str);
3044  else
3045  ereport(ERROR,
3046  (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
3047  errmsg("localized string format value too long")));
3048  }
3049  else
3050  strcpy(s, days_short[tm->tm_wday]);
3051  s += strlen(s);
3052  break;
3053  case DCH_dy:
3055  if (S_TM(n->suffix))
3056  {
3057  char *str = str_tolower_z(localized_abbrev_days[tm->tm_wday], collid);
3058 
3059  if (strlen(str) <= (n->key->len + TM_SUFFIX_LEN) * DCH_MAX_ITEM_SIZ)
3060  strcpy(s, str);
3061  else
3062  ereport(ERROR,
3063  (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
3064  errmsg("localized string format value too long")));
3065  }
3066  else
3067  strcpy(s, asc_tolower_z(days_short[tm->tm_wday]));
3068  s += strlen(s);
3069  break;
3070  case DCH_DDD:
3071  case DCH_IDDD:
3072  sprintf(s, "%0*d", S_FM(n->suffix) ? 0 : 3,
3073  (n->key->id == DCH_DDD) ?
3074  tm->tm_yday :
3075  date2isoyearday(tm->tm_year, tm->tm_mon, tm->tm_mday));
3076  if (S_THth(n->suffix))
3077  str_numth(s, s, S_TH_TYPE(n->suffix));
3078  s += strlen(s);
3079  break;
3080  case DCH_DD:
3081  sprintf(s, "%0*d", S_FM(n->suffix) ? 0 : 2, tm->tm_mday);
3082  if (S_THth(n->suffix))
3083  str_numth(s, s, S_TH_TYPE(n->suffix));
3084  s += strlen(s);
3085  break;
3086  case DCH_D:
3088  sprintf(s, "%d", tm->tm_wday + 1);
3089  if (S_THth(n->suffix))
3090  str_numth(s, s, S_TH_TYPE(n->suffix));
3091  s += strlen(s);
3092  break;
3093  case DCH_ID:
3095  sprintf(s, "%d", (tm->tm_wday == 0) ? 7 : tm->tm_wday);
3096  if (S_THth(n->suffix))
3097  str_numth(s, s, S_TH_TYPE(n->suffix));
3098  s += strlen(s);
3099  break;
3100  case DCH_WW:
3101  sprintf(s, "%0*d", S_FM(n->suffix) ? 0 : 2,
3102  (tm->tm_yday - 1) / 7 + 1);
3103  if (S_THth(n->suffix))
3104  str_numth(s, s, S_TH_TYPE(n->suffix));
3105  s += strlen(s);
3106  break;
3107  case DCH_IW:
3108  sprintf(s, "%0*d", S_FM(n->suffix) ? 0 : 2,
3109  date2isoweek(tm->tm_year, tm->tm_mon, tm->tm_mday));
3110  if (S_THth(n->suffix))
3111  str_numth(s, s, S_TH_TYPE(n->suffix));
3112  s += strlen(s);
3113  break;
3114  case DCH_Q:
3115  if (!tm->tm_mon)
3116  break;
3117  sprintf(s, "%d", (tm->tm_mon - 1) / 3 + 1);
3118  if (S_THth(n->suffix))
3119  str_numth(s, s, S_TH_TYPE(n->suffix));
3120  s += strlen(s);
3121  break;
3122  case DCH_CC:
3123  if (is_interval) /* straight calculation */
3124  i = tm->tm_year / 100;
3125  else
3126  {
3127  if (tm->tm_year > 0)
3128  /* Century 20 == 1901 - 2000 */
3129  i = (tm->tm_year - 1) / 100 + 1;
3130  else
3131  /* Century 6BC == 600BC - 501BC */
3132  i = tm->tm_year / 100 - 1;
3133  }
3134  if (i <= 99 && i >= -99)
3135  sprintf(s, "%0*d", S_FM(n->suffix) ? 0 : (i >= 0) ? 2 : 3, i);
3136  else
3137  sprintf(s, "%d", i);
3138  if (S_THth(n->suffix))
3139  str_numth(s, s, S_TH_TYPE(n->suffix));
3140  s += strlen(s);
3141  break;
3142  case DCH_Y_YYY:
3143  i = ADJUST_YEAR(tm->tm_year, is_interval) / 1000;
3144  sprintf(s, "%d,%03d", i,
3145  ADJUST_YEAR(tm->tm_year, is_interval) - (i * 1000));
3146  if (S_THth(n->suffix))
3147  str_numth(s, s, S_TH_TYPE(n->suffix));
3148  s += strlen(s);
3149  break;
3150  case DCH_YYYY:
3151  case DCH_IYYY:
3152  sprintf(s, "%0*d",
3153  S_FM(n->suffix) ? 0 :
3154  (ADJUST_YEAR(tm->tm_year, is_interval) >= 0) ? 4 : 5,
3155  (n->key->id == DCH_YYYY ?
3156  ADJUST_YEAR(tm->tm_year, is_interval) :
3158  tm->tm_mon,
3159  tm->tm_mday),
3160  is_interval)));
3161  if (S_THth(n->suffix))
3162  str_numth(s, s, S_TH_TYPE(n->suffix));
3163  s += strlen(s);
3164  break;
3165  case DCH_YYY:
3166  case DCH_IYY:
3167  sprintf(s, "%0*d",
3168  S_FM(n->suffix) ? 0 :
3169  (ADJUST_YEAR(tm->tm_year, is_interval) >= 0) ? 3 : 4,
3170  (n->key->id == DCH_YYY ?
3171  ADJUST_YEAR(tm->tm_year, is_interval) :
3173  tm->tm_mon,
3174  tm->tm_mday),
3175  is_interval)) % 1000);
3176  if (S_THth(n->suffix))
3177  str_numth(s, s, S_TH_TYPE(n->suffix));
3178  s += strlen(s);
3179  break;
3180  case DCH_YY:
3181  case DCH_IY:
3182  sprintf(s, "%0*d",
3183  S_FM(n->suffix) ? 0 :
3184  (ADJUST_YEAR(tm->tm_year, is_interval) >= 0) ? 2 : 3,
3185  (n->key->id == DCH_YY ?
3186  ADJUST_YEAR(tm->tm_year, is_interval) :
3188  tm->tm_mon,
3189  tm->tm_mday),
3190  is_interval)) % 100);
3191  if (S_THth(n->suffix))
3192  str_numth(s, s, S_TH_TYPE(n->suffix));
3193  s += strlen(s);
3194  break;
3195  case DCH_Y:
3196  case DCH_I:
3197  sprintf(s, "%1d",
3198  (n->key->id == DCH_Y ?
3199  ADJUST_YEAR(tm->tm_year, is_interval) :
3201  tm->tm_mon,
3202  tm->tm_mday),
3203  is_interval)) % 10);
3204  if (S_THth(n->suffix))
3205  str_numth(s, s, S_TH_TYPE(n->suffix));
3206  s += strlen(s);
3207  break;
3208  case DCH_RM:
3209  if (!tm->tm_mon)
3210  break;
3211  sprintf(s, "%*s", S_FM(n->suffix) ? 0 : -4,
3213  s += strlen(s);
3214  break;
3215  case DCH_rm:
3216  if (!tm->tm_mon)
3217  break;
3218  sprintf(s, "%*s", S_FM(n->suffix) ? 0 : -4,
3220  s += strlen(s);
3221  break;
3222  case DCH_W:
3223  sprintf(s, "%d", (tm->tm_mday - 1) / 7 + 1);
3224  if (S_THth(n->suffix))
3225  str_numth(s, s, S_TH_TYPE(n->suffix));
3226  s += strlen(s);
3227  break;
3228  case DCH_J:
3229  sprintf(s, "%d", date2j(tm->tm_year, tm->tm_mon, tm->tm_mday));
3230  if (S_THth(n->suffix))
3231  str_numth(s, s, S_TH_TYPE(n->suffix));
3232  s += strlen(s);
3233  break;
3234  }
3235  }
3236 
3237  *s = '\0';
3238 }
#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:2123
fsec_t fsec
Definition: formatting.c:500
static char * asc_tolower_z(const char *buff)
Definition: formatting.c:2129
#define AD_STR
Definition: formatting.c:240
int errcode(int sqlerrcode)
Definition: elog.c:610
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:2111
long int tm_gmtoff
Definition: pgtime.h:36
Definition: pgtime.h:25
static char * str_numth(char *dest, char *num, int type)
Definition: formatting.c:1556
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:102
#define MONTHS_PER_YEAR
Definition: timestamp.h:69
#define sprintf
Definition: port.h:195
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
char * localized_abbrev_months[12+1]
Definition: pg_locale.c:108
int date2isoweek(int year, int mon, int mday)
Definition: timestamp.c:4317
#define S_FM(_s)
Definition: formatting.c:560
int date2isoyear(int year, int mon, int mday)
Definition: timestamp.c:4372
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
char * localized_full_days[7+1]
Definition: pg_locale.c:107
#define a_d_STR
Definition: formatting.c:239
#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
int date2j(int y, int m, int d)
Definition: datetime.c:269
#define ereport(elevel,...)
Definition: elog.h:144
#define ad_STR
Definition: formatting.c:241
char * localized_full_months[12+1]
Definition: pg_locale.c:109
void cache_locale_time(void)
Definition: pg_locale.c:781
#define P_M_STR
Definition: formatting.c:270
static char * asc_toupper_z(const char *buff)
Definition: formatting.c:2135
#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:824
static char * str_toupper_z(const char *buff, Oid collid)
Definition: formatting.c:2117
#define INVALID_FOR_INTERVAL
Definition: formatting.c:527
int date2isoyearday(int year, int mon, int mday)
Definition: timestamp.c:4429
int i
#define S_TM(_s)
Definition: formatting.c:562
char * localized_abbrev_days[7+1]
Definition: pg_locale.c:106
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
#define DCH_MAX_ITEM_SIZ
Definition: formatting.c:156
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,
Oid  collid,
bool  std,
struct pg_tm tm,
fsec_t fsec,
int *  fprec,
uint32 flags,
bool have_error 
)
static

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

4415 {
4416  FormatNode *format = NULL;
4417  TmFromChar tmfc;
4418  int fmt_len;
4419  char *date_str;
4420  int fmask;
4421  bool incache = false;
4422 
4423  Assert(tm != NULL);
4424  Assert(fsec != NULL);
4425 
4426  date_str = text_to_cstring(date_txt);
4427 
4428  ZERO_tmfc(&tmfc);
4429  ZERO_tm(tm);
4430  *fsec = 0;
4431  if (fprec)
4432  *fprec = 0;
4433  if (flags)
4434  *flags = 0;
4435  fmask = 0; /* bit mask for ValidateDate() */
4436 
4437  fmt_len = VARSIZE_ANY_EXHDR(fmt);
4438 
4439  if (fmt_len)
4440  {
4441  char *fmt_str;
4442 
4443  fmt_str = text_to_cstring(fmt);
4444 
4445  if (fmt_len > DCH_CACHE_SIZE)
4446  {
4447  /*
4448  * Allocate new memory if format picture is bigger than static
4449  * cache and do not use cache (call parser always)
4450  */
4451  format = (FormatNode *) palloc((fmt_len + 1) * sizeof(FormatNode));
4452 
4453  parse_format(format, fmt_str, DCH_keywords, DCH_suff, DCH_index,
4454  DCH_FLAG | (std ? STD_FLAG : 0), NULL);
4455  }
4456  else
4457  {
4458  /*
4459  * Use cache buffers
4460  */
4461  DCHCacheEntry *ent = DCH_cache_fetch(fmt_str, std);
4462 
4463  incache = true;
4464  format = ent->format;
4465  }
4466 
4467 #ifdef DEBUG_TO_FROM_CHAR
4468  /* dump_node(format, fmt_len); */
4469  /* dump_index(DCH_keywords, DCH_index); */
4470 #endif
4471 
4472  DCH_from_char(format, date_str, &tmfc, collid, std, have_error);
4473  CHECK_ERROR;
4474 
4475  pfree(fmt_str);
4476 
4477  if (flags)
4478  *flags = DCH_datetime_type(format, have_error);
4479 
4480  if (!incache)
4481  {
4482  pfree(format);
4483  format = NULL;
4484  }
4485 
4486  CHECK_ERROR;
4487  }
4488 
4489  DEBUG_TMFC(&tmfc);
4490 
4491  /*
4492  * Convert to_date/to_timestamp input fields to standard 'tm'
4493  */
4494  if (tmfc.ssss)
4495  {
4496  int x = tmfc.ssss;
4497 
4498  tm->tm_hour = x / SECS_PER_HOUR;
4499  x %= SECS_PER_HOUR;
4500  tm->tm_min = x / SECS_PER_MINUTE;
4501  x %= SECS_PER_MINUTE;
4502  tm->tm_sec = x;
4503  }
4504 
4505  if (tmfc.ss)
4506  tm->tm_sec = tmfc.ss;
4507  if (tmfc.mi)
4508  tm->tm_min = tmfc.mi;
4509  if (tmfc.hh)
4510  tm->tm_hour = tmfc.hh;
4511 
4512  if (tmfc.clock == CLOCK_12_HOUR)
4513  {
4514  if (tm->tm_hour < 1 || tm->tm_hour > HOURS_PER_DAY / 2)
4515  {
4517  (errcode(ERRCODE_INVALID_DATETIME_FORMAT),
4518  errmsg("hour \"%d\" is invalid for the 12-hour clock",
4519  tm->tm_hour),
4520  errhint("Use the 24-hour clock, or give an hour between 1 and 12."))));
4521  }
4522 
4523  if (tmfc.pm && tm->tm_hour < HOURS_PER_DAY / 2)
4524  tm->tm_hour += HOURS_PER_DAY / 2;
4525  else if (!tmfc.pm && tm->tm_hour == HOURS_PER_DAY / 2)
4526  tm->tm_hour = 0;
4527  }
4528 
4529  if (tmfc.year)
4530  {
4531  /*
4532  * If CC and YY (or Y) are provided, use YY as 2 low-order digits for
4533  * the year in the given century. Keep in mind that the 21st century
4534  * AD runs from 2001-2100, not 2000-2099; 6th century BC runs from
4535  * 600BC to 501BC.
4536  */
4537  if (tmfc.cc && tmfc.yysz <= 2)
4538  {
4539  if (tmfc.bc)
4540  tmfc.cc = -tmfc.cc;
4541  tm->tm_year = tmfc.year % 100;
4542  if (tm->tm_year)
4543  {
4544  if (tmfc.cc >= 0)
4545  tm->tm_year += (tmfc.cc - 1) * 100;
4546  else
4547  tm->tm_year = (tmfc.cc + 1) * 100 - tm->tm_year + 1;
4548  }
4549  else
4550  {
4551  /* find century year for dates ending in "00" */
4552  tm->tm_year = tmfc.cc * 100 + ((tmfc.cc >= 0) ? 0 : 1);
4553  }
4554  }
4555  else
4556  {
4557  /* If a 4-digit year is provided, we use that and ignore CC. */
4558  tm->tm_year = tmfc.year;
4559  if (tmfc.bc && tm->tm_year > 0)
4560  tm->tm_year = -(tm->tm_year - 1);
4561  }
4562  fmask |= DTK_M(YEAR);
4563  }
4564  else if (tmfc.cc)
4565  {
4566  /* use first year of century */
4567  if (tmfc.bc)
4568  tmfc.cc = -tmfc.cc;
4569  if (tmfc.cc >= 0)
4570  /* +1 because 21st century started in 2001 */
4571  tm->tm_year = (tmfc.cc - 1) * 100 + 1;
4572  else
4573  /* +1 because year == 599 is 600 BC */
4574  tm->tm_year = tmfc.cc * 100 + 1;
4575  fmask |= DTK_M(YEAR);
4576  }
4577 
4578  if (tmfc.j)
4579  {
4580  j2date(tmfc.j, &tm->tm_year, &tm->tm_mon, &tm->tm_mday);
4581  fmask |= DTK_DATE_M;
4582  }
4583 
4584  if (tmfc.ww)
4585  {
4586  if (tmfc.mode == FROM_CHAR_DATE_ISOWEEK)
4587  {
4588  /*
4589  * If tmfc.d is not set, then the date is left at the beginning of
4590  * the ISO week (Monday).
4591  */
4592  if (tmfc.d)
4593  isoweekdate2date(tmfc.ww, tmfc.d, &tm->tm_year, &tm->tm_mon, &tm->tm_mday);
4594  else
4595  isoweek2date(tmfc.ww, &tm->tm_year, &tm->tm_mon, &tm->tm_mday);
4596  fmask |= DTK_DATE_M;
4597  }
4598  else
4599  tmfc.ddd = (tmfc.ww - 1) * 7 + 1;
4600  }
4601 
4602  if (tmfc.w)
4603  tmfc.dd = (tmfc.w - 1) * 7 + 1;
4604  if (tmfc.dd)
4605  {
4606  tm->tm_mday = tmfc.dd;
4607  fmask |= DTK_M(DAY);
4608  }
4609  if (tmfc.mm)
4610  {
4611  tm->tm_mon = tmfc.mm;
4612  fmask |= DTK_M(MONTH);
4613  }
4614 
4615  if (tmfc.ddd && (tm->tm_mon <= 1 || tm->tm_mday <= 1))
4616  {
4617  /*
4618  * The month and day field have not been set, so we use the
4619  * day-of-year field to populate them. Depending on the date mode,
4620  * this field may be interpreted as a Gregorian day-of-year, or an ISO
4621  * week date day-of-year.
4622  */
4623 
4624  if (!tm->tm_year && !tmfc.bc)
4625  {
4627  (errcode(ERRCODE_INVALID_DATETIME_FORMAT),
4628  errmsg("cannot calculate day of year without year information"))));
4629  }
4630 
4631  if (tmfc.mode == FROM_CHAR_DATE_ISOWEEK)
4632  {
4633  int j0; /* zeroth day of the ISO year, in Julian */
4634 
4635  j0 = isoweek2j(tm->tm_year, 1) - 1;
4636 
4637  j2date(j0 + tmfc.ddd, &tm->tm_year, &tm->tm_mon, &tm->tm_mday);
4638  fmask |= DTK_DATE_M;
4639  }
4640  else
4641  {
4642  const int *y;
4643  int i;
4644 
4645  static const int ysum[2][13] = {
4646  {0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365},
4647  {0, 31, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335, 366}};
4648 
4649  y = ysum[isleap(tm->tm_year)];
4650 
4651  for (i = 1; i <= MONTHS_PER_YEAR; i++)
4652  {
4653  if (tmfc.ddd <= y[i])
4654  break;
4655  }
4656  if (tm->tm_mon <= 1)
4657  tm->tm_mon = i;
4658 
4659  if (tm->tm_mday <= 1)
4660  tm->tm_mday = tmfc.ddd - y[i - 1];
4661 
4662  fmask |= DTK_M(MONTH) | DTK_M(DAY);
4663  }
4664  }
4665 
4666  if (tmfc.ms)
4667  *fsec += tmfc.ms * 1000;
4668  if (tmfc.us)
4669  *fsec += tmfc.us;
4670  if (fprec)
4671  *fprec = tmfc.ff; /* fractional precision, if specified */
4672 
4673  /* Range-check date fields according to bit mask computed above */
4674  if (fmask != 0)
4675  {
4676  /* We already dealt with AD/BC, so pass isjulian = true */
4677  int dterr = ValidateDate(fmask, true, false, false, tm);
4678 
4679  if (dterr != 0)
4680  {
4681  /*
4682  * Force the error to be DTERR_FIELD_OVERFLOW even if ValidateDate
4683  * said DTERR_MD_FIELD_OVERFLOW, because we don't want to print an
4684  * irrelevant hint about datestyle.
4685  */
4686  RETURN_ERROR(DateTimeParseError(DTERR_FIELD_OVERFLOW, date_str, "timestamp"));
4687  }
4688  }
4689 
4690  /* Range-check time fields too */
4691  if (tm->tm_hour < 0 || tm->tm_hour >= HOURS_PER_DAY ||
4692  tm->tm_min < 0 || tm->tm_min >= MINS_PER_HOUR ||
4693  tm->tm_sec < 0 || tm->tm_sec >= SECS_PER_MINUTE ||
4694  *fsec < INT64CONST(0) || *fsec >= USECS_PER_SEC)
4695  {
4696  RETURN_ERROR(DateTimeParseError(DTERR_FIELD_OVERFLOW, date_str, "timestamp"));
4697  }
4698 
4699  /* Save parsed time-zone into tm->tm_zone if it was specified */
4700  if (tmfc.tzsign)
4701  {
4702  char *tz;
4703 
4704  if (tmfc.tzh < 0 || tmfc.tzh > MAX_TZDISP_HOUR ||
4705  tmfc.tzm < 0 || tmfc.tzm >= MINS_PER_HOUR)
4706  {
4707  RETURN_ERROR(DateTimeParseError(DTERR_TZDISP_OVERFLOW, date_str, "timestamp"));
4708  }
4709 
4710  tz = psprintf("%c%02d:%02d",
4711  tmfc.tzsign > 0 ? '+' : '-', tmfc.tzh, tmfc.tzm);
4712 
4713  tm->tm_zone = tz;
4714  }
4715 
4716  DEBUG_TM(tm);
4717 
4718 on_error:
4719 
4720  if (format && !incache)
4721  pfree(format);
4722 
4723  pfree(date_str);
4724 }
#define DEBUG_TM(_X)
Definition: formatting.c:490
#define DAY
Definition: datetime.h:94
int errhint(const char *fmt,...)
Definition: elog.c:1071
void DateTimeParseError(int dterr, const char *str, const char *datatype)
Definition: datetime.c:3723
#define USECS_PER_SEC
Definition: timestamp.h:94
static int DCH_datetime_type(FormatNode *node, bool *have_error)
Definition: formatting.c:3754
#define YEAR
Definition: datetime.h:93
static void DCH_from_char(FormatNode *node, const char *in, TmFromChar *out, Oid collid, bool std, bool *have_error)
Definition: formatting.c:3252
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:610
void isoweekdate2date(int isoweek, int wday, int *year, int *mon, int *mday)
Definition: timestamp.c:4299
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:1324
const char * tm_zone
Definition: pgtime.h:37
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:4286
#define ZERO_tmfc(_X)
Definition: formatting.c:470
int isoweek2j(int year, int week)
Definition: timestamp.c:4266
#define DCH_FLAG
Definition: formatting.c:141
#define ereport(elevel,...)
Definition: elog.h:144
#define Assert(condition)
Definition: c.h:745
#define DEBUG_TMFC(_X)
Definition: formatting.c:489
char * text_to_cstring(const text *t)
Definition: varlena.c:205
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:824
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:3939
#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:2435

◆ fill_str()

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

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

4734 {
4735  memset(str, c, max);
4736  *(str + max) = '\0';
4737  return str;
4738 }
char * c

◆ float4_to_char()

Datum float4_to_char ( PG_FUNCTION_ARGS  )

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

6427 {
6429  text *fmt = PG_GETARG_TEXT_PP(1);
6430  NUMDesc Num;
6431  FormatNode *format;
6432  text *result;
6433  bool shouldFree;
6434  int out_pre_spaces = 0,
6435  sign = 0;
6436  char *numstr,
6437  *orgnum,
6438  *p;
6439 
6441 
6442  if (IS_ROMAN(&Num))
6443  numstr = orgnum = int_to_roman((int) rint(value));
6444  else if (IS_EEEE(&Num))
6445  {
6446  if (isnan(value) || isinf(value))
6447  {
6448  /*
6449  * Allow 6 characters for the leading sign, the decimal point,
6450  * "e", the exponent's sign and two exponent digits.
6451  */
6452  numstr = (char *) palloc(Num.pre + Num.post + 7);
6453  fill_str(numstr, '#', Num.pre + Num.post + 6);
6454  *numstr = ' ';
6455  *(numstr + Num.pre + 1) = '.';
6456  }
6457  else
6458  {
6459  numstr = orgnum = psprintf("%+.*e", Num.post, value);
6460 
6461  /*
6462  * Swap a leading positive sign for a space.
6463  */
6464  if (*orgnum == '+')
6465  *orgnum = ' ';
6466 
6467  numstr = orgnum;
6468  }
6469  }
6470  else
6471  {
6472  float4 val = value;
6473  int numstr_pre_len;
6474 
6475  if (IS_MULTI(&Num))
6476  {
6477  float multi = pow((double) 10, (double) Num.multi);
6478 
6479  val = value * multi;
6480  Num.pre += Num.multi;
6481  }
6482 
6483  orgnum = (char *) psprintf("%.0f", fabs(val));
6484  numstr_pre_len = strlen(orgnum);
6485 
6486  /* adjust post digits to fit max float digits */
6487  if (numstr_pre_len >= FLT_DIG)
6488  Num.post = 0;
6489  else if (numstr_pre_len + Num.post > FLT_DIG)
6490  Num.post = FLT_DIG - numstr_pre_len;
6491  orgnum = psprintf("%.*f", Num.post, val);
6492 
6493  if (*orgnum == '-')
6494  { /* < 0 */
6495  sign = '-';
6496  numstr = orgnum + 1;
6497  }
6498  else
6499  {
6500  sign = '+';
6501  numstr = orgnum;
6502  }
6503 
6504  if ((p = strchr(numstr, '.')))
6505  numstr_pre_len = p - numstr;
6506  else
6507  numstr_pre_len = strlen(numstr);
6508 
6509  /* needs padding? */
6510  if (numstr_pre_len < Num.pre)
6511  out_pre_spaces = Num.pre - numstr_pre_len;
6512  /* overflowed prefix digit format? */
6513  else if (numstr_pre_len > Num.pre)
6514  {
6515  numstr = (char *) palloc(Num.pre + Num.post + 2);
6516  fill_str(numstr, '#', Num.pre + Num.post + 1);
6517  *(numstr + Num.pre) = '.';
6518  }
6519  }
6520 
6522  PG_RETURN_TEXT_P(result);
6523 }
char * psprintf(const char *fmt,...)
Definition: psprintf.c:46
#define IS_EEEE(_f)
Definition: formatting.c:377
#define NUM_TOCHAR_finish
Definition: formatting.c:6012
#define IS_ROMAN(_f)
Definition: formatting.c:375
#define PG_GETARG_TEXT_PP(n)
Definition: fmgr.h:308
#define NUM_TOCHAR_prepare
Definition: formatting.c:5999
#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:4733
static char * int_to_roman(int number)
Definition: formatting.c:4933
int pre
Definition: formatting.c:328
#define PG_GETARG_FLOAT4(n)
Definition: fmgr.h:280
float float4
Definition: c.h:497
static struct @143 value
#define PG_RETURN_TEXT_P(x)
Definition: fmgr.h:370
int post
Definition: formatting.c:328
void * palloc(Size size)
Definition: mcxt.c:949
int multi
Definition: formatting.c:328
Definition: c.h:562
static char format
long val
Definition: informix.c:664

◆ float8_to_char()

Datum float8_to_char ( PG_FUNCTION_ARGS  )

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

6531 {
6533  text *fmt = PG_GETARG_TEXT_PP(1);
6534  NUMDesc Num;
6535  FormatNode *format;
6536  text *result;
6537  bool shouldFree;
6538  int out_pre_spaces = 0,
6539  sign = 0;
6540  char *numstr,
6541  *orgnum,
6542  *p;
6543 
6545 
6546  if (IS_ROMAN(&Num))
6547  numstr = orgnum = int_to_roman((int) rint(value));
6548  else if (IS_EEEE(&Num))
6549  {
6550  if (isnan(value) || isinf(value))
6551  {
6552  /*
6553  * Allow 6 characters for the leading sign, the decimal point,
6554  * "e", the exponent's sign and two exponent digits.
6555  */
6556  numstr = (char *) palloc(Num.pre + Num.post + 7);
6557  fill_str(numstr, '#', Num.pre + Num.post + 6);
6558  *numstr = ' ';
6559  *(numstr + Num.pre + 1) = '.';
6560  }
6561  else
6562  {
6563  numstr = orgnum = (char *) psprintf("%+.*e", Num.post, value);
6564 
6565  /*
6566  * Swap a leading positive sign for a space.
6567  */
6568  if (*orgnum == '+')
6569  *orgnum = ' ';
6570 
6571  numstr = orgnum;
6572  }
6573  }
6574  else
6575  {
6576  float8 val = value;
6577  int numstr_pre_len;
6578 
6579  if (IS_MULTI(&Num))
6580  {
6581  double multi = pow((double) 10, (double) Num.multi);
6582 
6583  val = value * multi;
6584  Num.pre += Num.multi;
6585  }
6586  orgnum = psprintf("%.0f", fabs(val));
6587  numstr_pre_len = strlen(orgnum);
6588 
6589  /* adjust post digits to fit max double digits */
6590  if (numstr_pre_len >= DBL_DIG)
6591  Num.post = 0;
6592  else if (numstr_pre_len + Num.post > DBL_DIG)
6593  Num.post = DBL_DIG - numstr_pre_len;
6594  orgnum = psprintf("%.*f", Num.post, val);
6595 
6596  if (*orgnum == '-')
6597  { /* < 0 */
6598  sign = '-';
6599  numstr = orgnum + 1;
6600  }
6601  else
6602  {
6603  sign = '+';
6604  numstr = orgnum;
6605  }
6606 
6607  if ((p = strchr(numstr, '.')))
6608  numstr_pre_len = p - numstr;
6609  else
6610  numstr_pre_len = strlen(numstr);
6611 
6612  /* needs padding? */
6613  if (numstr_pre_len < Num.pre)
6614  out_pre_spaces = Num.pre - numstr_pre_len;
6615  /* overflowed prefix digit format? */
6616  else if (numstr_pre_len > Num.pre)
6617  {
6618  numstr = (char *) palloc(Num.pre + Num.post + 2);
6619  fill_str(numstr, '#', Num.pre + Num.post + 1);
6620  *(numstr + Num.pre) = '.';
6621  }
6622  }
6623 
6625  PG_RETURN_TEXT_P(result);
6626 }
#define PG_GETARG_FLOAT8(n)
Definition: fmgr.h:281
char * psprintf(const char *fmt,...)
Definition: psprintf.c:46
#define IS_EEEE(_f)
Definition: formatting.c:377
#define NUM_TOCHAR_finish
Definition: formatting.c:6012
#define IS_ROMAN(_f)
Definition: formatting.c:375
#define PG_GETARG_TEXT_PP(n)
Definition: fmgr.h:308
#define NUM_TOCHAR_prepare
Definition: formatting.c:5999
double float8
Definition: c.h:498
#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:4733
static char * int_to_roman(int number)
Definition: formatting.c:4933
int pre
Definition: formatting.c:328
static struct @143 value
#define PG_RETURN_TEXT_P(x)
Definition: fmgr.h:370
int post
Definition: formatting.c:328
void * palloc(Size size)
Definition: mcxt.c:949
int multi
Definition: formatting.c:328
Definition: c.h:562
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 2446 of file formatting.c.

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

Referenced by DCH_from_char().

2447 {
2448  return from_char_parse_int_len(dest, src, node->key->len, node, have_error);
2449 }
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:2343

◆ 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 2343 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().

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

Definition at line 2604 of file formatting.c.

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

Referenced by DCH_from_char().

2607 {
2608  int len;
2609 
2610  if (localized_array == NULL)
2611  *dest = seq_search_ascii(*src, array, &len);
2612  else
2613  *dest = seq_search_localized(*src, localized_array, &len, collid);
2614 
2615  if (len <= 0)
2616  {
2617  /*
2618  * In the error report, truncate the string at the next whitespace (if
2619  * any) to avoid including irrelevant data.
2620  */
2621  char *copy = pstrdup(*src);
2622  char *c;
2623 
2624  for (c = copy; *c; c++)
2625  {
2626  if (scanner_isspace(*c))
2627  {
2628  *c = '\0';
2629  break;
2630  }
2631  }
2632 
2634  (errcode(ERRCODE_INVALID_DATETIME_FORMAT),
2635  errmsg("invalid value \"%s\" for \"%s\"",
2636  copy, node->key->name),
2637  errdetail("The given value did not match any of "
2638  "the allowed values for this field."))));
2639  }
2640  *src += len;
2641  return len;
2642 
2643