mirror of
https://github.com/postgres/postgres.git
synced 2025-07-09 22:41:56 +03:00
Improve EncodeDateTime and EncodeTimeOnly APIs
Use an explicit argument to tell whether to include the time zone in the output, rather than using some undocumented pointer magic.
This commit is contained in:
@ -334,12 +334,12 @@ do { \
|
||||
|
||||
int DecodeInterval(char **, int *, int, int *, struct tm *, fsec_t *);
|
||||
int DecodeTime(char *, int *, struct tm *, fsec_t *);
|
||||
int EncodeDateTime(struct tm *, fsec_t, int *, char **, int, char *, bool);
|
||||
int EncodeInterval(struct tm *, fsec_t, int, char *);
|
||||
int EncodeDateTime(struct tm *tm, fsec_t fsec, bool print_tz, int tz, const char *tzn, int style, char *str, bool EuroDates);
|
||||
int EncodeInterval(struct tm *tm, fsec_t fsec, int style, char *str);
|
||||
int tm2timestamp(struct tm *, fsec_t, int *, timestamp *);
|
||||
int DecodeUnits(int field, char *lowtoken, int *val);
|
||||
bool CheckDateTokenTables(void);
|
||||
int EncodeDateOnly(struct tm *, int, char *, bool);
|
||||
int EncodeDateOnly(struct tm *tm, int style, char *str, bool EuroDates);
|
||||
int GetEpochTime(struct tm *);
|
||||
int ParseDateTime(char *, char *, char **, int *, int *, char **);
|
||||
int DecodeDateTime(char **, int *, int, int *, struct tm *, fsec_t *, bool);
|
||||
|
@ -759,7 +759,14 @@ TrimTrailingZeros(char *str)
|
||||
|
||||
/* EncodeDateTime()
|
||||
* Encode date and time interpreted as local time.
|
||||
* Support several date styles:
|
||||
*
|
||||
* tm and fsec are the value to encode, print_tz determines whether to include
|
||||
* a time zone (the difference between timestamp and timestamptz types), tz is
|
||||
* the numeric time zone offset, tzn is the textual time zone, which if
|
||||
* specified will be used instead of tz by some styles, style is the date
|
||||
* style, str is where to write the output.
|
||||
*
|
||||
* Supported date styles:
|
||||
* Postgres - day mon hh:mm:ss yyyy tz
|
||||
* SQL - mm/dd/yyyy hh:mm:ss.ss tz
|
||||
* ISO - yyyy-mm-dd hh:mm:ss+/-tz
|
||||
@ -769,12 +776,18 @@ TrimTrailingZeros(char *str)
|
||||
* European - dd/mm/yyyy
|
||||
*/
|
||||
int
|
||||
EncodeDateTime(struct tm * tm, fsec_t fsec, int *tzp, char **tzn, int style, char *str, bool EuroDates)
|
||||
EncodeDateTime(struct tm * tm, fsec_t fsec, bool print_tz, int tz, const char *tzn, int style, char *str, bool EuroDates)
|
||||
{
|
||||
int day,
|
||||
hour,
|
||||
min;
|
||||
|
||||
/*
|
||||
* Negative tm_isdst means we have no valid time zone translation.
|
||||
*/
|
||||
if (tm->tm_isdst < 0)
|
||||
print_tz = false;
|
||||
|
||||
switch (style)
|
||||
{
|
||||
case USE_ISO_DATES:
|
||||
@ -808,16 +821,10 @@ EncodeDateTime(struct tm * tm, fsec_t fsec, int *tzp, char **tzn, int style, cha
|
||||
if (tm->tm_year <= 0)
|
||||
sprintf(str + strlen(str), " BC");
|
||||
|
||||
/*
|
||||
* tzp == NULL indicates that we don't want *any* time zone info
|
||||
* in the output string. *tzn != NULL indicates that we have alpha
|
||||
* time zone info available. tm_isdst != -1 indicates that we have
|
||||
* a valid time zone translation.
|
||||
*/
|
||||
if (tzp != NULL && tm->tm_isdst >= 0)
|
||||
if (print_tz)
|
||||
{
|
||||
hour = -(*tzp / SECS_PER_HOUR);
|
||||
min = (abs(*tzp) / MINS_PER_HOUR) % MINS_PER_HOUR;
|
||||
hour = -(tz / SECS_PER_HOUR);
|
||||
min = (abs(tz) / MINS_PER_HOUR) % MINS_PER_HOUR;
|
||||
if (min != 0)
|
||||
sprintf(str + strlen(str), "%+03d:%02d", hour, min);
|
||||
else
|
||||
@ -867,14 +874,14 @@ EncodeDateTime(struct tm * tm, fsec_t fsec, int *tzp, char **tzn, int style, cha
|
||||
* TZ abbreviations in the Olson database are plain ASCII.
|
||||
*/
|
||||
|
||||
if (tzp != NULL && tm->tm_isdst >= 0)
|
||||
if (print_tz)
|
||||
{
|
||||
if (*tzn != NULL)
|
||||
sprintf(str + strlen(str), " %.*s", MAXTZLEN, *tzn);
|
||||
if (tzn)
|
||||
sprintf(str + strlen(str), " %.*s", MAXTZLEN, tzn);
|
||||
else
|
||||
{
|
||||
hour = -(*tzp / SECS_PER_HOUR);
|
||||
min = (abs(*tzp) / MINS_PER_HOUR) % MINS_PER_HOUR;
|
||||
hour = -(tz / SECS_PER_HOUR);
|
||||
min = (abs(tz) / MINS_PER_HOUR) % MINS_PER_HOUR;
|
||||
if (min != 0)
|
||||
sprintf(str + strlen(str), "%+03d:%02d", hour, min);
|
||||
else
|
||||
@ -916,14 +923,14 @@ EncodeDateTime(struct tm * tm, fsec_t fsec, int *tzp, char **tzn, int style, cha
|
||||
if (tm->tm_year <= 0)
|
||||
sprintf(str + strlen(str), " BC");
|
||||
|
||||
if (tzp != NULL && tm->tm_isdst >= 0)
|
||||
if (print_tz)
|
||||
{
|
||||
if (*tzn != NULL)
|
||||
sprintf(str + strlen(str), " %.*s", MAXTZLEN, *tzn);
|
||||
if (tzn)
|
||||
sprintf(str + strlen(str), " %.*s", MAXTZLEN, tzn);
|
||||
else
|
||||
{
|
||||
hour = -(*tzp / SECS_PER_HOUR);
|
||||
min = (abs(*tzp) / MINS_PER_HOUR) % MINS_PER_HOUR;
|
||||
hour = -(tz / SECS_PER_HOUR);
|
||||
min = (abs(tz) / MINS_PER_HOUR) % MINS_PER_HOUR;
|
||||
if (min != 0)
|
||||
sprintf(str + strlen(str), "%+03d:%02d", hour, min);
|
||||
else
|
||||
@ -975,10 +982,10 @@ EncodeDateTime(struct tm * tm, fsec_t fsec, int *tzp, char **tzn, int style, cha
|
||||
if (tm->tm_year <= 0)
|
||||
sprintf(str + strlen(str), " BC");
|
||||
|
||||
if (tzp != NULL && tm->tm_isdst >= 0)
|
||||
if (print_tz)
|
||||
{
|
||||
if (*tzn != NULL)
|
||||
sprintf(str + strlen(str), " %.*s", MAXTZLEN, *tzn);
|
||||
if (tzn)
|
||||
sprintf(str + strlen(str), " %.*s", MAXTZLEN, tzn);
|
||||
else
|
||||
{
|
||||
/*
|
||||
@ -987,8 +994,8 @@ EncodeDateTime(struct tm * tm, fsec_t fsec, int *tzp, char **tzn, int style, cha
|
||||
* avoid formatting something which would be rejected by
|
||||
* the date/time parser later. - thomas 2001-10-19
|
||||
*/
|
||||
hour = -(*tzp / SECS_PER_HOUR);
|
||||
min = (abs(*tzp) / MINS_PER_HOUR) % MINS_PER_HOUR;
|
||||
hour = -(tz / SECS_PER_HOUR);
|
||||
min = (abs(tz) / MINS_PER_HOUR) % MINS_PER_HOUR;
|
||||
if (min != 0)
|
||||
sprintf(str + strlen(str), " %+03d:%02d", hour, min);
|
||||
else
|
||||
|
@ -354,7 +354,6 @@ PGTYPEStimestamp_to_asc(timestamp tstamp)
|
||||
struct tm tt,
|
||||
*tm = &tt;
|
||||
char buf[MAXDATELEN + 1];
|
||||
char *tzn = NULL;
|
||||
fsec_t fsec;
|
||||
int DateStyle = 1; /* this defaults to ISO_DATES, shall we make
|
||||
* it an option? */
|
||||
@ -362,7 +361,7 @@ PGTYPEStimestamp_to_asc(timestamp tstamp)
|
||||
if (TIMESTAMP_NOT_FINITE(tstamp))
|
||||
EncodeSpecialTimestamp(tstamp, buf);
|
||||
else if (timestamp2tm(tstamp, NULL, tm, &fsec, NULL) == 0)
|
||||
EncodeDateTime(tm, fsec, NULL, &tzn, DateStyle, buf, 0);
|
||||
EncodeDateTime(tm, fsec, false, 0, NULL, DateStyle, buf, 0);
|
||||
else
|
||||
{
|
||||
errno = PGTYPES_TS_BAD_TIMESTAMP;
|
||||
|
Reference in New Issue
Block a user