8
8
*
9
9
*
10
10
* IDENTIFICATION
11
- * $Header: /cvsroot/pgsql/src/backend/utils/adt/datetime.c,v 1.98 2003/01/16 00:26:45 tgl Exp $
11
+ * $Header: /cvsroot/pgsql/src/backend/utils/adt/datetime.c,v 1.99 2003/01/29 01:08:42 tgl Exp $
12
12
*
13
13
*-------------------------------------------------------------------------
14
14
*/
@@ -667,14 +667,13 @@ TrimTrailingZeros(char *str)
667
667
}
668
668
#endif
669
669
670
- /* chop off trailing zeros... */
670
+ /* chop off trailing zeros... but leave at least 2 fractional digits */
671
671
while ((* (str + len - 1 ) == '0' )
672
672
&& (* (str + len - 3 ) != '.' ))
673
673
{
674
674
len -- ;
675
675
* (str + len ) = '\0' ;
676
676
}
677
- return ;
678
677
}
679
678
680
679
@@ -3145,45 +3144,30 @@ EncodeDateOnly(struct tm * tm, int style, char *str)
3145
3144
int
3146
3145
EncodeTimeOnly (struct tm * tm , fsec_t fsec , int * tzp , int style , char * str )
3147
3146
{
3148
- #ifndef HAVE_INT64_TIMESTAMP
3149
- fsec_t sec ;
3150
- #endif
3151
-
3152
3147
if ((tm -> tm_hour < 0 ) || (tm -> tm_hour > 24 ))
3153
3148
return -1 ;
3154
3149
3155
- #ifndef HAVE_INT64_TIMESTAMP
3156
- sec = (tm -> tm_sec + fsec );
3157
- #endif
3158
-
3159
3150
sprintf (str , "%02d:%02d" , tm -> tm_hour , tm -> tm_min );
3160
3151
3161
3152
/*
3162
- * If we have fractional seconds, then include a decimal point We will
3163
- * do up to 6 fractional digits, and we have rounded any inputs to
3164
- * eliminate anything to the right of 6 digits anyway. If there are no
3165
- * fractional seconds, then do not bother printing a decimal point at
3166
- * all. - thomas 2001-09-29
3153
+ * Print fractional seconds if any. The field widths here should be
3154
+ * at least equal to the larger of MAX_TIME_PRECISION and
3155
+ * MAX_TIMESTAMP_PRECISION.
3167
3156
*/
3168
3157
if (fsec != 0 )
3169
3158
{
3170
3159
#ifdef HAVE_INT64_TIMESTAMP
3171
- sprintf ((str + strlen (str )), ":%02d" , tm -> tm_sec );
3172
- sprintf ((str + strlen (str )), ".%06d" , fsec );
3160
+ sprintf ((str + strlen (str )), ":%02d.%06d" , tm -> tm_sec , fsec );
3173
3161
#else
3174
- sprintf ((str + strlen (str )), ":%013.10f" , sec );
3162
+ sprintf ((str + strlen (str )), ":%013.10f" , tm -> tm_sec + fsec );
3175
3163
#endif
3176
3164
/* chop off trailing pairs of zeros... */
3177
3165
while ((strcmp ((str + strlen (str ) - 2 ), "00" ) == 0 )
3178
3166
&& (* (str + strlen (str ) - 3 ) != '.' ))
3179
3167
* (str + strlen (str ) - 2 ) = '\0' ;
3180
3168
}
3181
3169
else
3182
- #ifdef HAVE_INT64_TIMESTAMP
3183
3170
sprintf ((str + strlen (str )), ":%02d" , tm -> tm_sec );
3184
- #else
3185
- sprintf ((str + strlen (str )), ":%02.0f" , sec );
3186
- #endif
3187
3171
3188
3172
if (tzp != NULL )
3189
3173
{
@@ -3217,20 +3201,12 @@ EncodeDateTime(struct tm * tm, fsec_t fsec, int *tzp, char **tzn, int style, cha
3217
3201
hour ,
3218
3202
min ;
3219
3203
3220
- #ifndef HAVE_INT64_TIMESTAMP
3221
- fsec_t sec ;
3222
- #endif
3223
-
3224
3204
/*
3225
3205
* Why are we checking only the month field? Change this to an
3226
3206
* assert... if ((tm->tm_mon < 1) || (tm->tm_mon > 12)) return -1;
3227
3207
*/
3228
3208
Assert ((tm -> tm_mon >= 1 ) && (tm -> tm_mon <= 12 ));
3229
3209
3230
- #ifndef HAVE_INT64_TIMESTAMP
3231
- sec = (tm -> tm_sec + fsec );
3232
- #endif
3233
-
3234
3210
switch (style )
3235
3211
{
3236
3212
case USE_ISO_DATES :
@@ -3241,21 +3217,20 @@ EncodeDateTime(struct tm * tm, fsec_t fsec, int *tzp, char **tzn, int style, cha
3241
3217
tm -> tm_mon , tm -> tm_mday , tm -> tm_hour , tm -> tm_min );
3242
3218
3243
3219
/*
3244
- * If we have fractional seconds, then include a decimal point
3245
- * We will do up to 6 fractional digits, and we have rounded
3246
- * any inputs to eliminate anything to the right of 6 digits
3247
- * anyway. If there are no fractional seconds, then do not
3248
- * bother printing a decimal point at all. - thomas 2001-09-29
3220
+ * Print fractional seconds if any. The field widths here should
3221
+ * be at least equal to MAX_TIMESTAMP_PRECISION.
3222
+ *
3223
+ * In float mode, don't print fractional seconds before 1 AD,
3224
+ * since it's unlikely there's any precision left ...
3249
3225
*/
3250
3226
#ifdef HAVE_INT64_TIMESTAMP
3251
3227
if (fsec != 0 )
3252
3228
{
3253
- sprintf ((str + strlen (str )), ":%02d" , tm -> tm_sec );
3254
- sprintf ((str + strlen (str )), ".%06d" , fsec );
3229
+ sprintf ((str + strlen (str )), ":%02d.%06d" , tm -> tm_sec , fsec );
3255
3230
#else
3256
3231
if ((fsec != 0 ) && (tm -> tm_year > 0 ))
3257
3232
{
3258
- sprintf ((str + strlen (str )), ":%013.10f " , sec );
3233
+ sprintf ((str + strlen (str )), ":%09.6f " , tm -> tm_sec + fsec );
3259
3234
#endif
3260
3235
TrimTrailingZeros (str );
3261
3236
}
@@ -3292,21 +3267,20 @@ EncodeDateTime(struct tm * tm, fsec_t fsec, int *tzp, char **tzn, int style, cha
3292
3267
tm -> tm_hour , tm -> tm_min );
3293
3268
3294
3269
/*
3295
- * If we have fractional seconds, then include a decimal point
3296
- * We will do up to 6 fractional digits, and we have rounded
3297
- * any inputs to eliminate anything to the right of 6 digits
3298
- * anyway. If there are no fractional seconds, then do not
3299
- * bother printing a decimal point at all. - thomas 2001-09-29
3270
+ * Print fractional seconds if any. The field widths here should
3271
+ * be at least equal to MAX_TIMESTAMP_PRECISION.
3272
+ *
3273
+ * In float mode, don't print fractional seconds before 1 AD,
3274
+ * since it's unlikely there's any precision left ...
3300
3275
*/
3301
3276
#ifdef HAVE_INT64_TIMESTAMP
3302
3277
if (fsec != 0 )
3303
3278
{
3304
- sprintf ((str + strlen (str )), ":%02d" , tm -> tm_sec );
3305
- sprintf ((str + strlen (str )), ".%06d" , fsec );
3279
+ sprintf ((str + strlen (str )), ":%02d.%06d" , tm -> tm_sec , fsec );
3306
3280
#else
3307
3281
if ((fsec != 0 ) && (tm -> tm_year > 0 ))
3308
3282
{
3309
- sprintf ((str + strlen (str )), ":%013.10f " , sec );
3283
+ sprintf ((str + strlen (str )), ":%09.6f " , tm -> tm_sec + fsec );
3310
3284
#endif
3311
3285
TrimTrailingZeros (str );
3312
3286
}
@@ -3339,21 +3313,20 @@ EncodeDateTime(struct tm * tm, fsec_t fsec, int *tzp, char **tzn, int style, cha
3339
3313
tm -> tm_hour , tm -> tm_min );
3340
3314
3341
3315
/*
3342
- * If we have fractional seconds, then include a decimal point
3343
- * We will do up to 6 fractional digits, and we have rounded
3344
- * any inputs to eliminate anything to the right of 6 digits
3345
- * anyway. If there are no fractional seconds, then do not
3346
- * bother printing a decimal point at all. - thomas 2001-09-29
3316
+ * Print fractional seconds if any. The field widths here should
3317
+ * be at least equal to MAX_TIMESTAMP_PRECISION.
3318
+ *
3319
+ * In float mode, don't print fractional seconds before 1 AD,
3320
+ * since it's unlikely there's any precision left ...
3347
3321
*/
3348
3322
#ifdef HAVE_INT64_TIMESTAMP
3349
3323
if (fsec != 0 )
3350
3324
{
3351
- sprintf ((str + strlen (str )), ":%02d" , tm -> tm_sec );
3352
- sprintf ((str + strlen (str )), ".%06d" , fsec );
3325
+ sprintf ((str + strlen (str )), ":%02d.%06d" , tm -> tm_sec , fsec );
3353
3326
#else
3354
3327
if ((fsec != 0 ) && (tm -> tm_year > 0 ))
3355
3328
{
3356
- sprintf ((str + strlen (str )), ":%013.10f " , sec );
3329
+ sprintf ((str + strlen (str )), ":%09.6f " , tm -> tm_sec + fsec );
3357
3330
#endif
3358
3331
TrimTrailingZeros (str );
3359
3332
}
@@ -3394,21 +3367,20 @@ EncodeDateTime(struct tm * tm, fsec_t fsec, int *tzp, char **tzn, int style, cha
3394
3367
sprintf ((str + 10 ), " %02d:%02d" , tm -> tm_hour , tm -> tm_min );
3395
3368
3396
3369
/*
3397
- * If we have fractional seconds, then include a decimal point
3398
- * We will do up to 6 fractional digits, and we have rounded
3399
- * any inputs to eliminate anything to the right of 6 digits
3400
- * anyway. If there are no fractional seconds, then do not
3401
- * bother printing a decimal point at all. - thomas 2001-09-29
3370
+ * Print fractional seconds if any. The field widths here should
3371
+ * be at least equal to MAX_TIMESTAMP_PRECISION.
3372
+ *
3373
+ * In float mode, don't print fractional seconds before 1 AD,
3374
+ * since it's unlikely there's any precision left ...
3402
3375
*/
3403
3376
#ifdef HAVE_INT64_TIMESTAMP
3404
3377
if (fsec != 0 )
3405
3378
{
3406
- sprintf ((str + strlen (str )), ":%02d" , tm -> tm_sec );
3407
- sprintf ((str + strlen (str )), ".%06d" , fsec );
3379
+ sprintf ((str + strlen (str )), ":%02d.%06d" , tm -> tm_sec , fsec );
3408
3380
#else
3409
3381
if ((fsec != 0 ) && (tm -> tm_year > 0 ))
3410
3382
{
3411
- sprintf ((str + strlen (str )), ":%013.10f " , sec );
3383
+ sprintf ((str + strlen (str )), ":%09.6f " , tm -> tm_sec + fsec );
3412
3384
#endif
3413
3385
TrimTrailingZeros (str );
3414
3386
}
0 commit comments