1
0
mirror of https://github.com/postgres/postgres.git synced 2025-06-16 06:01:02 +03:00

> > I have no idea if this in Oracle or not. But it's something I

> > needed, and other people in the past asked about it too.
>
> It is in Oracle, but you aren't exactly on the spot.  It should be
>
> IYYY - 4 digits  ('2003')
> IYY  - 3 digits  ('003')
> IY   - 2 digits  ('03')
> I    - 1 digit   ('3')

Here is an updated patch that does that.

Kurt Roeckx
This commit is contained in:
Bruce Momjian
2003-12-25 03:36:24 +00:00
parent 03caf7649b
commit 1c757c49fa
6 changed files with 220 additions and 10 deletions

View File

@ -8,7 +8,7 @@
*
*
* IDENTIFICATION
* $PostgreSQL: pgsql/src/backend/utils/adt/timestamp.c,v 1.97 2003/11/29 19:51:59 pgsql Exp $
* $PostgreSQL: pgsql/src/backend/utils/adt/timestamp.c,v 1.98 2003/12/25 03:36:23 momjian Exp $
*
*-------------------------------------------------------------------------
*/
@ -2840,7 +2840,7 @@ interval_trunc(PG_FUNCTION_ARGS)
/* isoweek2date()
* Convert ISO week of year number to date.
* The year field must be specified!
* The year field must be specified with the ISO year!
* karel 2000/08/07
*/
void
@ -2920,6 +2920,64 @@ date2isoweek(int year, int mon, int mday)
}
/* date2isoyear()
*
* Returns ISO 8601 year number.
*/
int
date2isoyear(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);
/*
* 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);
year--;
}
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);
if (dayn >= (day4 - day0))
{
year++;
}
}
return year;
}
/* timestamp_part()
* Extract specified field from timestamp.
*/