Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitad4fb0d

Browse files
committed
Improve EncodeDateTime and EncodeTimeOnly APIs
Use an explicit argument to tell whether to include the time zone inthe output, rather than using some undocumented pointer magic.
1 parent942b631 commitad4fb0d

File tree

9 files changed

+84
-71
lines changed

9 files changed

+84
-71
lines changed

‎src/backend/utils/adt/date.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1131,7 +1131,7 @@ time_out(PG_FUNCTION_ARGS)
11311131
charbuf[MAXDATELEN+1];
11321132

11331133
time2tm(time,tm,&fsec);
1134-
EncodeTimeOnly(tm,fsec,NULL,DateStyle,buf);
1134+
EncodeTimeOnly(tm,fsec,false,0,DateStyle,buf);
11351135

11361136
result=pstrdup(buf);
11371137
PG_RETURN_CSTRING(result);
@@ -1918,7 +1918,7 @@ timetz_out(PG_FUNCTION_ARGS)
19181918
charbuf[MAXDATELEN+1];
19191919

19201920
timetz2tm(time,tm,&fsec,&tz);
1921-
EncodeTimeOnly(tm,fsec,&tz,DateStyle,buf);
1921+
EncodeTimeOnly(tm,fsec,true,tz,DateStyle,buf);
19221922

19231923
result=pstrdup(buf);
19241924
PG_RETURN_CSTRING(result);

‎src/backend/utils/adt/datetime.c

Lines changed: 37 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -3679,39 +3679,54 @@ EncodeDateOnly(struct pg_tm * tm, int style, char *str)
36793679

36803680
/* EncodeTimeOnly()
36813681
* Encode time fields only.
3682+
*
3683+
* tm and fsec are the value to encode, print_tz determines whether to include
3684+
* a time zone (the difference between time and timetz types), tz is the
3685+
* numeric time zone offset, style is the date style, str is where to write the
3686+
* output.
36823687
*/
36833688
void
3684-
EncodeTimeOnly(structpg_tm*tm,fsec_tfsec,int*tzp,intstyle,char*str)
3689+
EncodeTimeOnly(structpg_tm*tm,fsec_tfsec,boolprint_tz,inttz,intstyle,char*str)
36853690
{
36863691
sprintf(str,"%02d:%02d:",tm->tm_hour,tm->tm_min);
36873692
str+=strlen(str);
36883693

36893694
AppendSeconds(str,tm->tm_sec,fsec,MAX_TIME_PRECISION, true);
36903695

3691-
if (tzp!=NULL)
3692-
EncodeTimezone(str,*tzp,style);
3696+
if (print_tz)
3697+
EncodeTimezone(str,tz,style);
36933698
}
36943699

36953700

36963701
/* EncodeDateTime()
36973702
* Encode date and time interpreted as local time.
3698-
* Support several date styles:
3703+
*
3704+
* tm and fsec are the value to encode, print_tz determines whether to include
3705+
* a time zone (the difference between timestamp and timestamptz types), tz is
3706+
* the numeric time zone offset, tzn is the textual time zone, which if
3707+
* specified will be used instead of tz by some styles, style is the date
3708+
* style, str is where to write the output.
3709+
*
3710+
* Supported date styles:
36993711
*Postgres - day mon hh:mm:ss yyyy tz
37003712
*SQL - mm/dd/yyyy hh:mm:ss.ss tz
37013713
*ISO - yyyy-mm-dd hh:mm:ss+/-tz
37023714
*German - dd.mm.yyyy hh:mm:ss tz
37033715
*XSD - yyyy-mm-ddThh:mm:ss.ss+/-tz
3704-
* Variants (affects order of month and day for Postgres and SQL styles):
3705-
*US - mm/dd/yyyy
3706-
*European - dd/mm/yyyy
37073716
*/
37083717
void
3709-
EncodeDateTime(structpg_tm*tm,fsec_tfsec,int*tzp,char**tzn,intstyle,char*str)
3718+
EncodeDateTime(structpg_tm*tm,fsec_tfsec,boolprint_tz,inttz,constchar*tzn,intstyle,char*str)
37103719
{
37113720
intday;
37123721

37133722
Assert(tm->tm_mon >=1&&tm->tm_mon <=MONTHS_PER_YEAR);
37143723

3724+
/*
3725+
* Negative tm_isdst means we have no valid time zone translation.
3726+
*/
3727+
if (tm->tm_isdst<0)
3728+
print_tz= false;
3729+
37153730
switch (style)
37163731
{
37173732
caseUSE_ISO_DATES:
@@ -3729,14 +3744,8 @@ EncodeDateTime(struct pg_tm * tm, fsec_t fsec, int *tzp, char **tzn, int style,
37293744

37303745
AppendTimestampSeconds(str+strlen(str),tm,fsec);
37313746

3732-
/*
3733-
* tzp == NULL indicates that we don't want *any* time zone info
3734-
* in the output string. *tzn != NULL indicates that we have alpha
3735-
* time zone info available. tm_isdst != -1 indicates that we have
3736-
* a valid time zone translation.
3737-
*/
3738-
if (tzp!=NULL&&tm->tm_isdst >=0)
3739-
EncodeTimezone(str,*tzp,style);
3747+
if (print_tz)
3748+
EncodeTimezone(str,tz,style);
37403749

37413750
if (tm->tm_year <=0)
37423751
sprintf(str+strlen(str)," BC");
@@ -3762,12 +3771,12 @@ EncodeDateTime(struct pg_tm * tm, fsec_t fsec, int *tzp, char **tzn, int style,
37623771
* TZ abbreviations in the Olson database are plain ASCII.
37633772
*/
37643773

3765-
if (tzp!=NULL&&tm->tm_isdst >=0)
3774+
if (print_tz)
37663775
{
3767-
if (*tzn!=NULL)
3768-
sprintf(str+strlen(str)," %.*s",MAXTZLEN,*tzn);
3776+
if (tzn)
3777+
sprintf(str+strlen(str)," %.*s",MAXTZLEN,tzn);
37693778
else
3770-
EncodeTimezone(str,*tzp,style);
3779+
EncodeTimezone(str,tz,style);
37713780
}
37723781

37733782
if (tm->tm_year <=0)
@@ -3785,12 +3794,12 @@ EncodeDateTime(struct pg_tm * tm, fsec_t fsec, int *tzp, char **tzn, int style,
37853794

37863795
AppendTimestampSeconds(str+strlen(str),tm,fsec);
37873796

3788-
if (tzp!=NULL&&tm->tm_isdst >=0)
3797+
if (print_tz)
37893798
{
3790-
if (*tzn!=NULL)
3791-
sprintf(str+strlen(str)," %.*s",MAXTZLEN,*tzn);
3799+
if (tzn)
3800+
sprintf(str+strlen(str)," %.*s",MAXTZLEN,tzn);
37923801
else
3793-
EncodeTimezone(str,*tzp,style);
3802+
EncodeTimezone(str,tz,style);
37943803
}
37953804

37963805
if (tm->tm_year <=0)
@@ -3819,10 +3828,10 @@ EncodeDateTime(struct pg_tm * tm, fsec_t fsec, int *tzp, char **tzn, int style,
38193828
sprintf(str+strlen(str)," %04d",
38203829
(tm->tm_year>0) ?tm->tm_year :-(tm->tm_year-1));
38213830

3822-
if (tzp!=NULL&&tm->tm_isdst >=0)
3831+
if (print_tz)
38233832
{
3824-
if (*tzn!=NULL)
3825-
sprintf(str+strlen(str)," %.*s",MAXTZLEN,*tzn);
3833+
if (tzn)
3834+
sprintf(str+strlen(str)," %.*s",MAXTZLEN,tzn);
38263835
else
38273836
{
38283837
/*
@@ -3832,7 +3841,7 @@ EncodeDateTime(struct pg_tm * tm, fsec_t fsec, int *tzp, char **tzn, int style,
38323841
* the date/time parser later. - thomas 2001-10-19
38333842
*/
38343843
sprintf(str+strlen(str)," ");
3835-
EncodeTimezone(str,*tzp,style);
3844+
EncodeTimezone(str,tz,style);
38363845
}
38373846
}
38383847

‎src/backend/utils/adt/nabstime.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -311,7 +311,7 @@ abstimeout(PG_FUNCTION_ARGS)
311311
break;
312312
default:
313313
abstime2tm(time,&tz,tm,&tzn);
314-
EncodeDateTime(tm,fsec,&tz,&tzn,DateStyle,buf);
314+
EncodeDateTime(tm,fsec,true,tz,tzn,DateStyle,buf);
315315
break;
316316
}
317317

‎src/backend/utils/adt/timestamp.c

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -215,13 +215,12 @@ timestamp_out(PG_FUNCTION_ARGS)
215215
structpg_tmtt,
216216
*tm=&tt;
217217
fsec_tfsec;
218-
char*tzn=NULL;
219218
charbuf[MAXDATELEN+1];
220219

221220
if (TIMESTAMP_NOT_FINITE(timestamp))
222221
EncodeSpecialTimestamp(timestamp,buf);
223222
elseif (timestamp2tm(timestamp,NULL,tm,&fsec,NULL,NULL)==0)
224-
EncodeDateTime(tm,fsec,NULL,&tzn,DateStyle,buf);
223+
EncodeDateTime(tm,fsec,false,0,NULL,DateStyle,buf);
225224
else
226225
ereport(ERROR,
227226
(errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
@@ -501,7 +500,7 @@ timestamptz_out(PG_FUNCTION_ARGS)
501500
if (TIMESTAMP_NOT_FINITE(dt))
502501
EncodeSpecialTimestamp(dt,buf);
503502
elseif (timestamp2tm(dt,&tz,tm,&fsec,&tzn,NULL)==0)
504-
EncodeDateTime(tm,fsec,&tz,&tzn,DateStyle,buf);
503+
EncodeDateTime(tm,fsec,true,tz,tzn,DateStyle,buf);
505504
else
506505
ereport(ERROR,
507506
(errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
@@ -1422,7 +1421,7 @@ timestamptz_to_str(TimestampTz t)
14221421
if (TIMESTAMP_NOT_FINITE(t))
14231422
EncodeSpecialTimestamp(t,buf);
14241423
elseif (timestamp2tm(t,&tz,tm,&fsec,&tzn,NULL)==0)
1425-
EncodeDateTime(tm,fsec,&tz,&tzn,USE_ISO_DATES,buf);
1424+
EncodeDateTime(tm,fsec,true,tz,tzn,USE_ISO_DATES,buf);
14261425
else
14271426
strlcpy(buf,"(timestamp out of range)",sizeof(buf));
14281427

‎src/backend/utils/adt/xml.c

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1987,7 +1987,6 @@ map_sql_value_to_xml_value(Datum value, Oid type, bool xml_escape_strings)
19871987
Timestamptimestamp;
19881988
structpg_tmtm;
19891989
fsec_tfsec;
1990-
char*tzn=NULL;
19911990
charbuf[MAXDATELEN+1];
19921991

19931992
timestamp=DatumGetTimestamp(value);
@@ -1999,7 +1998,7 @@ map_sql_value_to_xml_value(Datum value, Oid type, bool xml_escape_strings)
19991998
errmsg("timestamp out of range"),
20001999
errdetail("XML does not support infinite timestamp values.")));
20012000
elseif (timestamp2tm(timestamp,NULL,&tm,&fsec,NULL,NULL)==0)
2002-
EncodeDateTime(&tm,fsec,NULL,&tzn,USE_XSD_DATES,buf);
2001+
EncodeDateTime(&tm,fsec,false,0,NULL,USE_XSD_DATES,buf);
20032002
else
20042003
ereport(ERROR,
20052004
(errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
@@ -2026,7 +2025,7 @@ map_sql_value_to_xml_value(Datum value, Oid type, bool xml_escape_strings)
20262025
errmsg("timestamp out of range"),
20272026
errdetail("XML does not support infinite timestamp values.")));
20282027
elseif (timestamp2tm(timestamp,&tz,&tm,&fsec,&tzn,NULL)==0)
2029-
EncodeDateTime(&tm,fsec,&tz,&tzn,USE_XSD_DATES,buf);
2028+
EncodeDateTime(&tm,fsec,true,tz,tzn,USE_XSD_DATES,buf);
20302029
else
20312030
ereport(ERROR,
20322031
(errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),

‎src/include/utils/datetime.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -290,8 +290,8 @@ extern void DateTimeParseError(int dterr, const char *str,
290290
externintDetermineTimeZoneOffset(structpg_tm*tm,pg_tz*tzp);
291291

292292
externvoidEncodeDateOnly(structpg_tm*tm,intstyle,char*str);
293-
externvoidEncodeTimeOnly(structpg_tm*tm,fsec_tfsec,int*tzp,intstyle,char*str);
294-
externvoidEncodeDateTime(structpg_tm*tm,fsec_tfsec,int*tzp,char**tzn,intstyle,char*str);
293+
externvoidEncodeTimeOnly(structpg_tm*tm,fsec_tfsec,boolprint_tz,inttz,intstyle,char*str);
294+
externvoidEncodeDateTime(structpg_tm*tm,fsec_tfsec,boolprint_tz,inttz,constchar*tzn,intstyle,char*str);
295295
externvoidEncodeInterval(structpg_tm*tm,fsec_tfsec,intstyle,char*str);
296296

297297
externintDecodeSpecial(intfield,char*lowtoken,int*val);

‎src/interfaces/ecpg/pgtypeslib/dt.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -334,12 +334,12 @@ do { \
334334

335335
intDecodeInterval(char**,int*,int,int*,structtm*,fsec_t*);
336336
intDecodeTime(char*,int*,structtm*,fsec_t*);
337-
intEncodeDateTime(structtm*,fsec_t,int*,char**,int,char*,bool);
338-
intEncodeInterval(structtm*,fsec_t,int,char*);
337+
intEncodeDateTime(structtm*tm,fsec_tfsec,boolprint_tz,inttz,constchar*tzn,intstyle,char*str,boolEuroDates);
338+
intEncodeInterval(structtm*tm,fsec_tfsec,intstyle,char*str);
339339
inttm2timestamp(structtm*,fsec_t,int*,timestamp*);
340340
intDecodeUnits(intfield,char*lowtoken,int*val);
341341
boolCheckDateTokenTables(void);
342-
intEncodeDateOnly(structtm*,int,char*,bool);
342+
intEncodeDateOnly(structtm*tm,intstyle,char*str,boolEuroDates);
343343
intGetEpochTime(structtm*);
344344
intParseDateTime(char*,char*,char**,int*,int*,char**);
345345
intDecodeDateTime(char**,int*,int,int*,structtm*,fsec_t*,bool);

‎src/interfaces/ecpg/pgtypeslib/dt_common.c

Lines changed: 33 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -759,7 +759,14 @@ TrimTrailingZeros(char *str)
759759

760760
/* EncodeDateTime()
761761
* Encode date and time interpreted as local time.
762-
* Support several date styles:
762+
*
763+
* tm and fsec are the value to encode, print_tz determines whether to include
764+
* a time zone (the difference between timestamp and timestamptz types), tz is
765+
* the numeric time zone offset, tzn is the textual time zone, which if
766+
* specified will be used instead of tz by some styles, style is the date
767+
* style, str is where to write the output.
768+
*
769+
* Supported date styles:
763770
*Postgres - day mon hh:mm:ss yyyy tz
764771
*SQL - mm/dd/yyyy hh:mm:ss.ss tz
765772
*ISO - yyyy-mm-dd hh:mm:ss+/-tz
@@ -769,12 +776,18 @@ TrimTrailingZeros(char *str)
769776
*European - dd/mm/yyyy
770777
*/
771778
int
772-
EncodeDateTime(structtm*tm,fsec_tfsec,int*tzp,char**tzn,intstyle,char*str,boolEuroDates)
779+
EncodeDateTime(structtm*tm,fsec_tfsec,boolprint_tz,inttz,constchar*tzn,intstyle,char*str,boolEuroDates)
773780
{
774781
intday,
775782
hour,
776783
min;
777784

785+
/*
786+
* Negative tm_isdst means we have no valid time zone translation.
787+
*/
788+
if (tm->tm_isdst<0)
789+
print_tz= false;
790+
778791
switch (style)
779792
{
780793
caseUSE_ISO_DATES:
@@ -808,16 +821,10 @@ EncodeDateTime(struct tm * tm, fsec_t fsec, int *tzp, char **tzn, int style, cha
808821
if (tm->tm_year <=0)
809822
sprintf(str+strlen(str)," BC");
810823

811-
/*
812-
* tzp == NULL indicates that we don't want *any* time zone info
813-
* in the output string. *tzn != NULL indicates that we have alpha
814-
* time zone info available. tm_isdst != -1 indicates that we have
815-
* a valid time zone translation.
816-
*/
817-
if (tzp!=NULL&&tm->tm_isdst >=0)
824+
if (print_tz)
818825
{
819-
hour=-(*tzp /SECS_PER_HOUR);
820-
min= (abs(*tzp) /MINS_PER_HOUR) %MINS_PER_HOUR;
826+
hour=-(tz /SECS_PER_HOUR);
827+
min= (abs(tz) /MINS_PER_HOUR) %MINS_PER_HOUR;
821828
if (min!=0)
822829
sprintf(str+strlen(str),"%+03d:%02d",hour,min);
823830
else
@@ -867,14 +874,14 @@ EncodeDateTime(struct tm * tm, fsec_t fsec, int *tzp, char **tzn, int style, cha
867874
* TZ abbreviations in the Olson database are plain ASCII.
868875
*/
869876

870-
if (tzp!=NULL&&tm->tm_isdst >=0)
877+
if (print_tz)
871878
{
872-
if (*tzn!=NULL)
873-
sprintf(str+strlen(str)," %.*s",MAXTZLEN,*tzn);
879+
if (tzn)
880+
sprintf(str+strlen(str)," %.*s",MAXTZLEN,tzn);
874881
else
875882
{
876-
hour=-(*tzp /SECS_PER_HOUR);
877-
min= (abs(*tzp) /MINS_PER_HOUR) %MINS_PER_HOUR;
883+
hour=-(tz /SECS_PER_HOUR);
884+
min= (abs(tz) /MINS_PER_HOUR) %MINS_PER_HOUR;
878885
if (min!=0)
879886
sprintf(str+strlen(str),"%+03d:%02d",hour,min);
880887
else
@@ -916,14 +923,14 @@ EncodeDateTime(struct tm * tm, fsec_t fsec, int *tzp, char **tzn, int style, cha
916923
if (tm->tm_year <=0)
917924
sprintf(str+strlen(str)," BC");
918925

919-
if (tzp!=NULL&&tm->tm_isdst >=0)
926+
if (print_tz)
920927
{
921-
if (*tzn!=NULL)
922-
sprintf(str+strlen(str)," %.*s",MAXTZLEN,*tzn);
928+
if (tzn)
929+
sprintf(str+strlen(str)," %.*s",MAXTZLEN,tzn);
923930
else
924931
{
925-
hour=-(*tzp /SECS_PER_HOUR);
926-
min= (abs(*tzp) /MINS_PER_HOUR) %MINS_PER_HOUR;
932+
hour=-(tz /SECS_PER_HOUR);
933+
min= (abs(tz) /MINS_PER_HOUR) %MINS_PER_HOUR;
927934
if (min!=0)
928935
sprintf(str+strlen(str),"%+03d:%02d",hour,min);
929936
else
@@ -975,10 +982,10 @@ EncodeDateTime(struct tm * tm, fsec_t fsec, int *tzp, char **tzn, int style, cha
975982
if (tm->tm_year <=0)
976983
sprintf(str+strlen(str)," BC");
977984

978-
if (tzp!=NULL&&tm->tm_isdst >=0)
985+
if (print_tz)
979986
{
980-
if (*tzn!=NULL)
981-
sprintf(str+strlen(str)," %.*s",MAXTZLEN,*tzn);
987+
if (tzn)
988+
sprintf(str+strlen(str)," %.*s",MAXTZLEN,tzn);
982989
else
983990
{
984991
/*
@@ -987,8 +994,8 @@ EncodeDateTime(struct tm * tm, fsec_t fsec, int *tzp, char **tzn, int style, cha
987994
* avoid formatting something which would be rejected by
988995
* the date/time parser later. - thomas 2001-10-19
989996
*/
990-
hour=-(*tzp /SECS_PER_HOUR);
991-
min= (abs(*tzp) /MINS_PER_HOUR) %MINS_PER_HOUR;
997+
hour=-(tz /SECS_PER_HOUR);
998+
min= (abs(tz) /MINS_PER_HOUR) %MINS_PER_HOUR;
992999
if (min!=0)
9931000
sprintf(str+strlen(str)," %+03d:%02d",hour,min);
9941001
else

‎src/interfaces/ecpg/pgtypeslib/timestamp.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -354,15 +354,14 @@ PGTYPEStimestamp_to_asc(timestamp tstamp)
354354
structtmtt,
355355
*tm=&tt;
356356
charbuf[MAXDATELEN+1];
357-
char*tzn=NULL;
358357
fsec_tfsec;
359358
intDateStyle=1;/* this defaults to ISO_DATES, shall we make
360359
* it an option? */
361360

362361
if (TIMESTAMP_NOT_FINITE(tstamp))
363362
EncodeSpecialTimestamp(tstamp,buf);
364363
elseif (timestamp2tm(tstamp,NULL,tm,&fsec,NULL)==0)
365-
EncodeDateTime(tm,fsec,NULL,&tzn,DateStyle,buf,0);
364+
EncodeDateTime(tm,fsec,false,0,NULL,DateStyle,buf,0);
366365
else
367366
{
368367
errno=PGTYPES_TS_BAD_TIMESTAMP;

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp