1
0
mirror of https://github.com/postgres/postgres.git synced 2025-06-14 18:42:34 +03:00

* to_char:

- full support for IW (ISO week) and vice versa conversion for IW too
    (the to_char 'week' support is now complete and I hope correct).

  Thomas, I use for IW code from timestamp.c, for this I create separate
  function date2isoweek() from original 'case DTK_WEEK:' code in the
  timestamp_part(). I mean will better use one code for same feature in
  date_part() and in to_char(). The isoweek2date() is added to timestamp.c
  too. Right?

  IMHO in 7.1 will all to_char's features complete. It is cca 41 templates
  for date/time and cca 21 for numbers.

 * to_ascii:

   - gcc, is it correct now? :-)


  In the patch is documentation for to_char's IW and for to_ascii().

                                                        Karel
This commit is contained in:
Bruce Momjian
2000-08-29 04:41:48 +00:00
parent d4f626507c
commit dffd8cac3d
5 changed files with 168 additions and 60 deletions

View File

@ -8,7 +8,7 @@
*
*
* IDENTIFICATION
* $Header: /cvsroot/pgsql/src/backend/utils/adt/timestamp.c,v 1.34 2000/07/17 03:05:18 tgl Exp $
* $Header: /cvsroot/pgsql/src/backend/utils/adt/timestamp.c,v 1.35 2000/08/29 04:41:47 momjian Exp $
*
*-------------------------------------------------------------------------
*/
@ -1796,7 +1796,81 @@ interval_trunc(PG_FUNCTION_ARGS)
PG_RETURN_INTERVAL_P(result);
}
/* isoweek2date()
*
* Convert ISO week of year number to date. An year must be already set.
* karel 2000/08/07
*/
void
isoweek2date( int woy, int *year, int *mon, int *mday)
{
int day0, day4, dayn;
if (!*year)
elog(ERROR, "isoweek2date(): can't convert without year information");
/* fourth day of current year */
day4 = date2j(*year, 1, 4);
/* day0 == offset to first day of week (Monday) */
day0 = (j2day(day4 - 1) % 7);
dayn = ((woy - 1) * 7) + (day4 - day0);
j2date(dayn, year, mon, mday);
}
/* date2isoweek()
*
* Returns ISO week number of year.
*/
int
date2isoweek(int year, int mon, int mday)
{
float8 result;
int day0, day4, dayn;
/* current day */
dayn = date2j(year, mon, mday);
/* fourth day of current year */
day4 = date2j(year, 1, 4);
/* day0 == offset to first day of week (Monday) */
day0 = (j2day(day4 - 1) % 7);
/* We need the first week containing a Thursday,
* otherwise this day falls into the previous year
* for purposes of counting weeks
*/
if (dayn < (day4 - day0))
{
day4 = date2j((year - 1), 1, 4);
/* day0 == offset to first day of week (Monday) */
day0 = (j2day(day4 - 1) % 7);
}
result = (((dayn - (day4 - day0)) / 7) + 1);
/* Sometimes the last few days in a year will fall into
* the first week of the next year, so check for this.
*/
if (result >= 53)
{
day4 = date2j((year + 1), 1, 4);
/* day0 == offset to first day of week (Monday) */
day0 = (j2day(day4 - 1) % 7);
if (dayn >= (day4 - day0))
result = (((dayn - (day4 - day0)) / 7) + 1);
}
return (int) result;
}
/* timestamp_part()
* Extract specified field from timestamp.
*/
@ -1897,35 +1971,7 @@ timestamp_part(PG_FUNCTION_ARGS)
break;
case DTK_WEEK:
{
int day0, day4, dayn;
dayn = date2j(tm->tm_year, tm->tm_mon, tm->tm_mday);
day4 = date2j(tm->tm_year, 1, 4);
/* day0 == offset to first day of week (Monday) */
day0 = (j2day(day4 - 1) % 7);
/* We need the first week containing a Thursday,
* otherwise this day falls into the previous year
* for purposes of counting weeks
*/
if (dayn < (day4 - day0))
{
day4 = date2j((tm->tm_year - 1), 1, 4);
/* day0 == offset to first day of week (Monday) */
day0 = (j2day(day4 - 1) % 7);
}
result = (((dayn - (day4 - day0)) / 7) + 1);
/* Sometimes the last few days in a year will fall into
* the first week of the next year, so check for this.
*/
if (result >= 53)
{
day4 = date2j((tm->tm_year + 1), 1, 4);
/* day0 == offset to first day of week (Monday) */
day0 = (j2day(day4 - 1) % 7);
if (dayn >= (day4 - day0))
result = (((dayn - (day4 - day0)) / 7) + 1);
}
}
result = (float8) date2isoweek(tm->tm_year, tm->tm_mon, tm->tm_mday);
break;
case DTK_YEAR: