77 *
88 *
99 * IDENTIFICATION
10- * $Header: /cvsroot/pgsql/src/backend/utils/adt/Attic/dt.c,v 1.45 1997/12/04 23:30:52 thomas Exp $
10+ * $Header: /cvsroot/pgsql/src/backend/utils/adt/Attic/dt.c,v 1.46 1997/12/17 23:22:17 thomas Exp $
1111 *
1212 *-------------------------------------------------------------------------
1313 */
3131#endif
3232#include "utils/builtins.h"
3333
34- static int DecodeDate (char * str ,int fmask ,int * tmask ,struct tm * tm );
34+ static int DecodeDate (char * str ,int fmask ,int * tmask ,struct tm * tm );
3535static int
3636DecodeNumber (int flen ,char * field ,
3737int fmask ,int * tmask ,struct tm * tm ,double * fsec );
3838static int
3939DecodeNumberField (int len ,char * str ,
4040int fmask ,int * tmask ,struct tm * tm ,double * fsec );
41- static int DecodeSpecial (int field ,char * lowtoken ,int * val );
41+ static int DecodeSpecial (int field ,char * lowtoken ,int * val );
4242static int
4343DecodeTime (char * str ,int fmask ,int * tmask ,
4444struct tm * tm ,double * fsec );
45- static int DecodeTimezone (char * str ,int * tzp );
46- static int DecodeUnits (int field ,char * lowtoken ,int * val );
47- static int EncodeSpecialDateTime (DateTime dt ,char * str );
45+ static int DecodeTimezone (char * str ,int * tzp );
46+ static int DecodeUnits (int field ,char * lowtoken ,int * val );
47+ static int EncodeSpecialDateTime (DateTime dt ,char * str );
4848static datetkn * datebsearch (char * key ,datetkn * base ,unsignedint nel );
4949static DateTime dt2local (DateTime dt ,int timezone );
5050static void dt2time (DateTime dt ,int * hour ,int * min ,double * sec );
51- static int j2day (int jd );
52- static int timespan2tm (TimeSpan span ,struct tm * tm ,float8 * fsec );
53- static int tm2timespan (struct tm * tm ,double fsec ,TimeSpan * span );
51+ static int j2day (int jd );
5452
5553#define USE_DATE_CACHE 1
5654#define ROUND_ALL 0
@@ -297,8 +295,6 @@ datetime_finite(DateTime *datetime)
297295return (!DATETIME_NOT_FINITE (* datetime ));
298296}/* datetime_finite() */
299297
300-
301- #ifdef NOT_USED
302298bool
303299timespan_finite (TimeSpan * timespan )
304300{
@@ -308,7 +304,6 @@ timespan_finite(TimeSpan *timespan)
308304return (!TIMESPAN_NOT_FINITE (* timespan ));
309305}/* timespan_finite() */
310306
311- #endif
312307
313308/*----------------------------------------------------------
314309 *Relational operators for datetime.
@@ -1368,8 +1363,7 @@ timespan_text(TimeSpan *timespan)
13681363 * Text type may not be null terminated, so copy to temporary string
13691364 *then call the standard input routine.
13701365 */
1371- #ifdef NOT_USED
1372- TimeSpan *
1366+ TimeSpan *
13731367text_timespan (text * str )
13741368{
13751369TimeSpan * result ;
@@ -1392,8 +1386,6 @@ text_timespan(text *str)
13921386return (result );
13931387}/* text_timespan() */
13941388
1395- #endif
1396-
13971389/* datetime_trunc()
13981390 * Extract specified field from datetime.
13991391 */
@@ -2573,7 +2565,7 @@ tm2datetime(struct tm * tm, double fsec, int *tzp, DateTime *result)
25732565/* timespan2tm()
25742566 * Convert a timespan data type to a tm structure.
25752567 */
2576- static int
2568+ int
25772569timespan2tm (TimeSpan span ,struct tm * tm ,float8 * fsec )
25782570{
25792571double time ;
@@ -2610,7 +2602,7 @@ timespan2tm(TimeSpan span, struct tm * tm, float8 *fsec)
26102602return 0 ;
26112603}/* timespan2tm() */
26122604
2613- static int
2605+ int
26142606tm2timespan (struct tm * tm ,double fsec ,TimeSpan * span )
26152607{
26162608span -> month = ((tm -> tm_year * 12 )+ tm -> tm_mon );
@@ -4369,10 +4361,19 @@ EncodeTimeSpan(struct tm * tm, double fsec, int style, char *str)
43694361{
43704362int is_before = FALSE;
43714363int is_nonzero = FALSE;
4372- char * cp ;
4364+ char * cp = str ;
43734365
4374- strcpy (str ,"@" );
4375- cp = str + strlen (str );
4366+ switch (style )
4367+ {
4368+ /* compatible with ISO date formats */
4369+ case USE_ISO_DATES :
4370+ break ;
4371+
4372+ default :
4373+ strcpy (cp ,"@" );
4374+ cp += strlen (cp );
4375+ break ;
4376+ }
43764377
43774378if (tm -> tm_year != 0 )
43784379{
@@ -4398,39 +4399,73 @@ EncodeTimeSpan(struct tm * tm, double fsec, int style, char *str)
43984399cp += strlen (cp );
43994400}
44004401
4401- if ( tm -> tm_hour != 0 )
4402+ switch ( style )
44024403{
4403- is_nonzero = TRUE;
4404- is_before |= (tm -> tm_hour < 0 );
4405- sprintf (cp ," %d hour%s" ,abs (tm -> tm_hour ), ((abs (tm -> tm_hour )!= 1 ) ?"s" :"" ));
4406- cp += strlen (cp );
4407- }
4404+ /* compatible with ISO date formats */
4405+ case USE_ISO_DATES :
4406+ if ((tm -> tm_hour != 0 )|| (tm -> tm_min != 0 ))
4407+ is_nonzero = TRUE;
4408+ is_before |= ((tm -> tm_hour < 0 )|| (tm -> tm_min < 0 ));
4409+ sprintf (cp ," %02d:%02d" ,abs (tm -> tm_hour ),abs (tm -> tm_min ));
4410+ cp += strlen (cp );
4411+
4412+ /* fractional seconds? */
4413+ if (fsec != 0 )
4414+ {
4415+ is_nonzero = TRUE;
4416+ fsec += tm -> tm_sec ;
4417+ is_before |= (fsec < 0 );
4418+ sprintf (cp ,":%05.2f" ,fabs (fsec ));
4419+ cp += strlen (cp );
44084420
4409- if (tm -> tm_min != 0 )
4410- {
4411- is_nonzero = TRUE;
4412- is_before |= (tm -> tm_min < 0 );
4413- sprintf (cp ," %d min%s" ,abs (tm -> tm_min ), ((abs (tm -> tm_min )!= 1 ) ?"s" :"" ));
4414- cp += strlen (cp );
4415- }
4421+ /* otherwise, integer seconds only? */
4422+ }
4423+ else if (tm -> tm_sec != 0 )
4424+ {
4425+ is_nonzero = TRUE;
4426+ is_before |= (tm -> tm_sec < 0 );
4427+ sprintf (cp ,":%02d" ,abs (tm -> tm_sec ));
4428+ cp += strlen (cp );
4429+ }
4430+ break ;
44164431
4417- /* fractional seconds? */
4418- if (fsec != 0 )
4419- {
4420- is_nonzero = TRUE;
4421- fsec += tm -> tm_sec ;
4422- is_before |= (fsec < 0 );
4423- sprintf (cp ," %.2f secs" ,fabs (fsec ));
4424- cp += strlen (cp );
4432+ case USE_POSTGRES_DATES :
4433+ default :
4434+ if (tm -> tm_hour != 0 )
4435+ {
4436+ is_nonzero = TRUE;
4437+ is_before |= (tm -> tm_hour < 0 );
4438+ sprintf (cp ," %d hour%s" ,abs (tm -> tm_hour ), ((abs (tm -> tm_hour )!= 1 ) ?"s" :"" ));
4439+ cp += strlen (cp );
4440+ }
44254441
4426- /* otherwise, integer seconds only? */
4427- }
4428- else if (tm -> tm_sec != 0 )
4429- {
4430- is_nonzero = TRUE;
4431- is_before |= (tm -> tm_sec < 0 );
4432- sprintf (cp ," %d sec%s" ,abs (tm -> tm_sec ), ((abs (tm -> tm_sec )!= 1 ) ?"s" :"" ));
4433- cp += strlen (cp );
4442+ if (tm -> tm_min != 0 )
4443+ {
4444+ is_nonzero = TRUE;
4445+ is_before |= (tm -> tm_min < 0 );
4446+ sprintf (cp ," %d min%s" ,abs (tm -> tm_min ), ((abs (tm -> tm_min )!= 1 ) ?"s" :"" ));
4447+ cp += strlen (cp );
4448+ }
4449+
4450+ /* fractional seconds? */
4451+ if (fsec != 0 )
4452+ {
4453+ is_nonzero = TRUE;
4454+ fsec += tm -> tm_sec ;
4455+ is_before |= (fsec < 0 );
4456+ sprintf (cp ," %.2f secs" ,fabs (fsec ));
4457+ cp += strlen (cp );
4458+
4459+ /* otherwise, integer seconds only? */
4460+ }
4461+ else if (tm -> tm_sec != 0 )
4462+ {
4463+ is_nonzero = TRUE;
4464+ is_before |= (tm -> tm_sec < 0 );
4465+ sprintf (cp ," %d sec%s" ,abs (tm -> tm_sec ), ((abs (tm -> tm_sec )!= 1 ) ?"s" :"" ));
4466+ cp += strlen (cp );
4467+ }
4468+ break ;
44344469}
44354470
44364471/* identically zero? then put in a unitless zero... */