88 *
99 *
1010 * IDENTIFICATION
11- * $PostgreSQL: pgsql/src/backend/utils/adt/datetime.c,v 1.177 2007/02/19 17:41:39 momjian Exp $
11+ * $PostgreSQL: pgsql/src/backend/utils/adt/datetime.c,v 1.178 2007/03/01 14:52:03 petere Exp $
1212 *
1313 *-------------------------------------------------------------------------
1414 */
@@ -3153,7 +3153,7 @@ datebsearch(const char *key, const datetkn *base, int nel)
31533153 * Append representation of a numeric timezone offset to str.
31543154 */
31553155static void
3156- EncodeTimezone (char * str , int tz )
3156+ EncodeTimezone (char * str , int tz , int style )
31573157{
31583158 int hour ,
31593159 min ,
@@ -3171,7 +3171,7 @@ EncodeTimezone(char *str, int tz)
31713171
31723172 if (sec != 0 )
31733173 sprintf (str , "%02d:%02d:%02d" , hour , min , sec );
3174- else if (min != 0 )
3174+ else if (min != 0 || style == USE_XSD_DATES )
31753175 sprintf (str , "%02d:%02d" , hour , min );
31763176 else
31773177 sprintf (str , "%02d" , hour );
@@ -3189,6 +3189,7 @@ EncodeDateOnly(struct pg_tm * tm, int style, char *str)
31893189 switch (style )
31903190 {
31913191 case USE_ISO_DATES :
3192+ case USE_XSD_DATES :
31923193 /* compatible with ISO date formats */
31933194 if (tm -> tm_year > 0 )
31943195 sprintf (str , "%04d-%02d-%02d" ,
@@ -3266,7 +3267,7 @@ EncodeTimeOnly(struct pg_tm * tm, fsec_t fsec, int *tzp, int style, char *str)
32663267 sprintf (str + strlen (str ), ":%02d" , tm -> tm_sec );
32673268
32683269 if (tzp != NULL )
3269- EncodeTimezone (str , * tzp );
3270+ EncodeTimezone (str , * tzp , style );
32703271
32713272 return TRUE;
32723273} /* EncodeTimeOnly() */
@@ -3279,6 +3280,7 @@ EncodeTimeOnly(struct pg_tm * tm, fsec_t fsec, int *tzp, int style, char *str)
32793280 * SQL - mm/dd/yyyy hh:mm:ss.ss tz
32803281 * ISO - yyyy-mm-dd hh:mm:ss+/-tz
32813282 * German - dd.mm.yyyy hh:mm:ss tz
3283+ * XSD - yyyy-mm-ddThh:mm:ss.ss+/-tz
32823284 * Variants (affects order of month and day for Postgres and SQL styles):
32833285 * US - mm/dd/yyyy
32843286 * European - dd/mm/yyyy
@@ -3297,11 +3299,18 @@ EncodeDateTime(struct pg_tm * tm, fsec_t fsec, int *tzp, char **tzn, int style,
32973299 switch (style )
32983300 {
32993301 case USE_ISO_DATES :
3302+ case USE_XSD_DATES :
33003303 /* Compatible with ISO-8601 date formats */
33013304
3302- sprintf (str , "%04d-%02d-%02d %02d:%02d" ,
3305+ if (style == USE_ISO_DATES )
3306+ sprintf (str , "%04d-%02d-%02d %02d:%02d" ,
33033307 (tm -> tm_year > 0 ) ? tm -> tm_year : - (tm -> tm_year - 1 ),
33043308 tm -> tm_mon , tm -> tm_mday , tm -> tm_hour , tm -> tm_min );
3309+ else
3310+ sprintf (str , "%04d-%02d-%02dT%02d:%02d" ,
3311+ (tm -> tm_year > 0 ) ? tm -> tm_year : - (tm -> tm_year - 1 ),
3312+ tm -> tm_mon , tm -> tm_mday , tm -> tm_hour , tm -> tm_min );
3313+
33053314
33063315 /*
33073316 * Print fractional seconds if any. The field widths here should
@@ -3333,7 +3342,7 @@ EncodeDateTime(struct pg_tm * tm, fsec_t fsec, int *tzp, char **tzn, int style,
33333342 * a valid time zone translation.
33343343 */
33353344 if (tzp != NULL && tm -> tm_isdst >= 0 )
3336- EncodeTimezone (str , * tzp );
3345+ EncodeTimezone (str , * tzp , style );
33373346
33383347 if (tm -> tm_year <= 0 )
33393348 sprintf (str + strlen (str ), " BC" );
@@ -3379,7 +3388,7 @@ EncodeDateTime(struct pg_tm * tm, fsec_t fsec, int *tzp, char **tzn, int style,
33793388 if (* tzn != NULL )
33803389 sprintf (str + strlen (str ), " %.*s" , MAXTZLEN , * tzn );
33813390 else
3382- EncodeTimezone (str , * tzp );
3391+ EncodeTimezone (str , * tzp , style );
33833392 }
33843393
33853394 if (tm -> tm_year <= 0 )
@@ -3423,7 +3432,7 @@ EncodeDateTime(struct pg_tm * tm, fsec_t fsec, int *tzp, char **tzn, int style,
34233432 if (* tzn != NULL )
34243433 sprintf (str + strlen (str ), " %.*s" , MAXTZLEN , * tzn );
34253434 else
3426- EncodeTimezone (str , * tzp );
3435+ EncodeTimezone (str , * tzp , style );
34273436 }
34283437
34293438 if (tm -> tm_year <= 0 )
@@ -3486,7 +3495,7 @@ EncodeDateTime(struct pg_tm * tm, fsec_t fsec, int *tzp, char **tzn, int style,
34863495 * the date/time parser later. - thomas 2001-10-19
34873496 */
34883497 sprintf (str + strlen (str ), " " );
3489- EncodeTimezone (str , * tzp );
3498+ EncodeTimezone (str , * tzp , style );
34903499 }
34913500 }
34923501
0 commit comments