ISO year.
ISO year.
This patch allows you to use IYYYY as format specifier to get the ISO year, the year correspondeing to the ISO week number (IW). Kurt
Re: ISO year.
От:
Tom Lane <tgl@sss.pgh.pa.us>
Дата:
Kurt Roeckx writes: > This patch allows you to use IYYYY as format specifier to get the > ISO year, the year correspondeing to the ISO week number (IW). The purpose of to_char() as I understand it is to be 100% Oracle compatible, not to invent new features at random. Is this duplicating some Oracle functionality that was left out? regards, tom lane
Re: ISO year.
От:
Kurt Roeckx <Q@ping.be>
Дата:
On Thu, Dec 18, 2003 at 11:41:18AM -0500, Tom Lane wrote: > Kurt Roeckx writes: > > This patch allows you to use IYYYY as format specifier to get the > > ISO year, the year correspondeing to the ISO week number (IW). > > The purpose of to_char() as I understand it is to be 100% Oracle > compatible, not to invent new features at random. Is this duplicating > some Oracle functionality that was left out? 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. You could use the normal year instead of the iso year, but around newyear this always gives problems. Kurt
Re: ISO year.
От:
Peter Eisentraut <peter_e@gmx.net>
Дата:
Kurt Roeckx wrote:
> 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')
Re: ISO year.
От:
Kurt Roeckx <Q@ping.be>
Дата:
On Thu, Dec 18, 2003 at 06:47:41PM +0100, Peter Eisentraut wrote:
> Kurt Roeckx wrote:
> > 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')
I'll update the patch to support all of them.
Kurt
Re: ISO year.
On Thu, Dec 18, 2003 at 06:47:41PM +0100, Peter Eisentraut wrote:
> Kurt Roeckx wrote:
> > 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
Re: ISO year.
От:
Karel Zak <zakkr@zf.jcu.cz>
Дата:
On Thu, Dec 18, 2003 at 04:33:44PM +0100, Kurt Roeckx wrote:
> + DCH_IYYYY,
^^^^^^^^^^^
> DCH_YYYY,
> DCH_YYY,
> DCH_YY,
There is a bug.. it's wrong position. Please, add the "enum" of
DCH_IYYYY to same position as you have "IYYYY" in next array. It means
"enum" DCH_IYYYY must be behind DCH_IW. If you will add others IY the
order must be:
DCH_IW
DCH_IYYYY
DCH_IYYY
DCH_IYY
DCH_IY
.. and same for lower case version:
> @@ -582,6 +583,7 @@
> DCH_ww,
> DCH_w,
> DCH_y_yyy,
> + DCH_iyyyy,
^^^^^^^^^
Thanks!
Karel
--
Karel Zak
http://home.zf.jcu.cz/~zakkr/
Re: ISO year.
От:
Karel Zak <zakkr@zf.jcu.cz>
Дата:
On Thu, Dec 18, 2003 at 11:41:18AM -0500, Tom Lane wrote:
> Kurt Roeckx writes:
> > This patch allows you to use IYYYY as format specifier to get the
> > ISO year, the year correspondeing to the ISO week number (IW).
>
> The purpose of to_char() as I understand it is to be 100% Oracle
> compatible, not to invent new features at random. Is this duplicating
> some Oracle functionality that was left out?
Oracle has IYYYY too. So thanks to Kurt, because now we approximate to
100% compatibity :-)
BTW, I'm open for to_char() PostgreSQL extensions if it will good
described in docs and marked as PostgreSQL extension.
Karel
--
Karel Zak
http://home.zf.jcu.cz/~zakkr/
Re: ISO year.
От:
Karel Zak <zakkr@zf.jcu.cz>
Дата:
On Thu, Dec 18, 2003 at 08:18:08PM +0100, Kurt Roeckx wrote:
> DCH_IW,
> + DCH_IYYY,
> + DCH_IYY,
> + DCH_IY,
> + DCH_I,
It's better :-) This patch is OK for me.
Karel
--
Karel Zak
http://home.zf.jcu.cz/~zakkr/
Re: ISO year.
От:
Bruce Momjian <pgman@candle.pha.pa.us>
Дата:
Karel Zak wrote:
> On Thu, Dec 18, 2003 at 08:18:08PM +0100, Kurt Roeckx wrote:
> > DCH_IW,
> > + DCH_IYYY,
> > + DCH_IYY,
> > + DCH_IY,
> > + DCH_I,
>
> It's better :-) This patch is OK for me.
Which patch is OK? The one attached? You looked like you were making
changes to this patch in your later emails.
--
Bruce Momjian | http://candle.pha.pa.us
pgman@candle.pha.pa.us | (610) 359-1001
+ If your life is a hard drive, | 13 Roberts Road
+ Christ can be your backup. | Newtown Square, Pennsylvania 19073
Index: doc/src/sgml/func.sgml
===================================================================
RCS file: /projects/cvsroot/pgsql-server/doc/src/sgml/func.sgml,v
retrieving revision 1.182
diff -u -r1.182 func.sgml
--- doc/src/sgml/func.sgml 16 Dec 2003 15:27:58 -0000 1.182
+++ doc/src/sgml/func.sgml 18 Dec 2003 18:57:56 -0000
@@ -3981,6 +3981,22 @@
last digit of year
+ IYYY
+ ISO year (4 and more digits)
+
+
+ IYY
+ last 3 digits of ISO year
+
+
+ IY
+ last 2 digits of ISO year
+
+
+ I
+ last digits of ISO year
+
+
BC or B.C. or
AD or A.D.
era indicator (upper case)
Index: src/include/utils/timestamp.h
===================================================================
RCS file: /projects/cvsroot/pgsql-server/src/include/utils/timestamp.h,v
retrieving revision 1.32
diff -u -r1.32 timestamp.h
--- src/include/utils/timestamp.h 29 Nov 2003 22:41:16 -0000 1.32
+++ src/include/utils/timestamp.h 18 Dec 2003 18:57:57 -0000
@@ -248,5 +248,6 @@
extern void isoweek2date(int woy, int *year, int *mon, int *mday);
extern int date2isoweek(int year, int mon, int mday);
+extern int date2isoyear(int year, int mon, int mday);
#endif /* TIMESTAMP_H */
Index: src/backend/utils/adt/formatting.c
===================================================================
RCS file: /projects/cvsroot/pgsql-server/src/backend/utils/adt/formatting.c,v
retrieving revision 1.70
diff -u -r1.70 formatting.c
--- src/backend/utils/adt/formatting.c 29 Nov 2003 19:51:58 -0000 1.70
+++ src/backend/utils/adt/formatting.c 18 Dec 2003 18:57:58 -0000
@@ -525,6 +525,10 @@
DCH_HH12,
DCH_HH,
DCH_IW,
+ DCH_IYYY,
+ DCH_IYY,
+ DCH_IY,
+ DCH_I,
DCH_J,
DCH_MI,
DCH_MM,
@@ -565,6 +569,10 @@
DCH_hh12,
DCH_hh,
DCH_iw,
+ DCH_iyyy,
+ DCH_iyy,
+ DCH_iy,
+ DCH_i,
DCH_j,
DCH_mi,
DCH_mm,
@@ -659,6 +667,10 @@
{"HH12", 4, dch_time, DCH_HH12, TRUE},
{"HH", 2, dch_time, DCH_HH, TRUE},
{"IW", 2, dch_date, DCH_IW, TRUE}, /* I */
+ {"IYYY", 4, dch_date, DCH_IYYY, TRUE},
+ {"IYY", 3, dch_date, DCH_IYY, TRUE},
+ {"IY", 2, dch_date, DCH_IY, TRUE},
+ {"I", 1, dch_date, DCH_I, TRUE},
{"J", 1, dch_date, DCH_J, TRUE}, /* J */
{"MI", 2, dch_time, DCH_MI, TRUE},
{"MM", 2, dch_date, DCH_MM, TRUE},
@@ -699,6 +711,10 @@
{"hh12", 4, dch_time, DCH_HH12, TRUE},
{"hh", 2, dch_time, DCH_HH, TRUE},
{"iw", 2, dch_date, DCH_IW, TRUE}, /* i */
+ {"iyyy", 4, dch_date, DCH_IYYY, TRUE},
+ {"iyy", 3, dch_date, DCH_IYY, TRUE},
+ {"iy", 2, dch_date, DCH_IY, TRUE},
+ {"i", 1, dch_date, DCH_I, TRUE},
{"j", 1, dch_time, DCH_J, TRUE}, /* j */
{"mi", 2, dch_time, DCH_MI, TRUE}, /* m */
{"mm", 2, dch_date, DCH_MM, TRUE},
@@ -2447,12 +2463,26 @@
}
break;
case DCH_YYYY:
+ case DCH_IYYY:
if (flag == TO_CHAR)
{
if (tm->tm_year <= 9999 && tm->tm_year >= -9998)
- sprintf(inout, "%0*d", S_FM(suf) ? 0 : 4, YEAR_ABS(tm->tm_year));
- else
- sprintf(inout, "%d", YEAR_ABS(tm->tm_year));
+ sprintf(inout, "%0*d",
+ S_FM(suf) ? 0 : 4,
+ arg == DCH_YYYY ?
+ YEAR_ABS(tm->tm_year) :
+ YEAR_ABS(date2isoyear(
+ tm->tm_year,
+ tm->tm_mon,
+ tm->tm_mday)));
+ else
+ sprintf(inout, "%d",
+ arg == DCH_YYYY ?
+ YEAR_ABS(tm->tm_year) :
+ YEAR_ABS(date2isoyear(
+ tm->tm_year,
+ tm->tm_mon,
+ tm->tm_mday)));
if (S_THth(suf))
str_numth(p_inout, inout, S_TH_TYPE(suf));
return strlen(p_inout) - 1;
@@ -2472,9 +2502,14 @@
}
break;
case DCH_YYY:
+ case DCH_IYY:
if (flag == TO_CHAR)
{
- snprintf(buff, sizeof(buff), "%03d", YEAR_ABS(tm->tm_year));
+ snprintf(buff, sizeof(buff), "%03d",
+ arg == DCH_YYY ?
+ YEAR_ABS(tm->tm_year) :
+ YEAR_ABS(date2isoyear(tm->tm_year,
+ tm->tm_mon, tm->tm_mday)));
i = strlen(buff);
strcpy(inout, buff + (i - 3));
if (S_THth(suf))
@@ -2502,9 +2537,14 @@
}
break;
case DCH_YY:
+ case DCH_IY:
if (flag == TO_CHAR)
{
- snprintf(buff, sizeof(buff), "%02d", YEAR_ABS(tm->tm_year));
+ snprintf(buff, sizeof(buff), "%02d",
+ arg == DCH_YY ?
+ YEAR_ABS(tm->tm_year) :
+ YEAR_ABS(date2isoyear(tm->tm_year,
+ tm->tm_mon, tm->tm_mday)));
i = strlen(buff);
strcpy(inout, buff + (i - 2));
if (S_THth(suf))
@@ -2532,9 +2572,14 @@
}
break;
case DCH_Y:
+ case DCH_I:
if (flag == TO_CHAR)
{
- snprintf(buff, sizeof(buff), "%1d", YEAR_ABS(tm->tm_year));
+ snprintf(buff, sizeof(buff), "%1d",
+ arg == DCH_Y ?
+ YEAR_ABS(tm->tm_year) :
+ YEAR_ABS(date2isoyear(tm->tm_year,
+ tm->tm_mon, tm->tm_mday)));
i = strlen(buff);
strcpy(inout, buff + (i - 1));
if (S_THth(suf))
Index: src/backend/utils/adt/timestamp.c
===================================================================
RCS file: /projects/cvsroot/pgsql-server/src/backend/utils/adt/timestamp.c,v
retrieving revision 1.97
diff -u -r1.97 timestamp.c
--- src/backend/utils/adt/timestamp.c 29 Nov 2003 19:51:59 -0000 1.97
+++ src/backend/utils/adt/timestamp.c 18 Dec 2003 18:57:59 -0000
@@ -2840,7 +2840,7 @@
/* 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
@@ -2917,6 +2917,64 @@
}
return (int) result;
+}
+
+
+/* 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;
}
Index: src/test/regress/expected/timestamptz.out
===================================================================
RCS file: /projects/cvsroot/pgsql-server/src/test/regress/expected/timestamptz.out,v
retrieving revision 1.13
diff -u -r1.13 timestamptz.out
--- src/test/regress/expected/timestamptz.out 25 Sep 2003 06:58:06 -0000 1.13
+++ src/test/regress/expected/timestamptz.out 18 Dec 2003 18:57:59 -0000
@@ -1317,6 +1317,76 @@
| 2001 A.D. 2001 a.d. 2001 ad 05:32:01 P.M. 05:32:01 p.m. 05:32:01 pm
(64 rows)
+SELECT '' AS to_char_10, to_char(d1, 'YYYY WW IYYY IYY IY I IW')
+ FROM TIMESTAMPTZ_TBL;
+ to_char_10 | to_char
+------------+--------------------------
+ |
+ |
+ | 1969 53 1970 970 70 0 01
+ | 1997 06 1997 997 97 7 07
+ | 1997 06 1997 997 97 7 07
+ | 1997 06 1997 997 97 7 07
+ | 1997 06 1997 997 97 7 07
+ | 1997 06 1997 997 97 7 07
+ | 1997 06 1997 997 97 7 07
+ | 1997 01 1997 997 97 7 01
+ | 1997 01 1997 997 97 7 01
+ | 1997 06 1997 997 97 7 07
+ | 1997 06 1997 997 97 7 07
+ | 1997 06 1997 997 97 7 07
+ | 1997 06 1997 997 97 7 07
+ | 1997 23 1997 997 97 7 24
+ | 2001 38 2001 001 01 1 38
+ | 2000 11 2000 000 00 0 11
+ | 2000 11 2000 000 00 0 11
+ | 2000 11 2000 000 00 0 11
+ | 2000 11 2000 000 00 0 11
+ | 2000 11 2000 000 00 0 11
+ | 1997 06 1997 997 97 7 07
+ | 1997 06 1997 997 97 7 07
+ | 1997 06 1997 997 97 7 07
+ | 1997 06 1997 997 97 7 07
+ | 1997 06 1997 997 97 7 07
+ | 1997 06 1997 997 97 7 07
+ | 1997 06 1997 997 97 7 07
+ | 1997 06 1997 997 97 7 07
+ | 1997 06 1997 997 97 7 07
+ | 1997 06 1997 997 97 7 07
+ | 1997 06 1997 997 97 7 07
+ | 1997 23 1997 997 97 7 24
+ | 1997 06 1997 997 97 7 07
+ | 1997 06 1997 997 97 7 07
+ | 1997 07 1997 997 97 7 07
+ | 1997 07 1997 997 97 7 07
+ | 1997 07 1997 997 97 7 07
+ | 1997 07 1997 997 97 7 07
+ | 1997 07 1997 997 97 7 07
+ | 0097 07 0097 097 97 7 07
+ | 0097 07 0097 097 97 7 07
+ | 0597 07 0597 597 97 7 07
+ | 1097 07 1097 097 97 7 07
+ | 1697 07 1697 697 97 7 07
+ | 1797 07 1797 797 97 7 07
+ | 1897 07 1897 897 97 7 07
+ | 1997 07 1997 997 97 7 07
+ | 2097 07 2097 097 97 7 07
+ | 1996 09 1996 996 96 6 09
+ | 1996 09 1996 996 96 6 09
+ | 1996 09 1996 996 96 6 09
+ | 1996 53 1997 997 97 7 01
+ | 1996 53 1997 997 97 7 01
+ | 1997 01 1997 997 97 7 01
+ | 1997 09 1997 997 97 7 09
+ | 1997 09 1997 997 97 7 09
+ | 1997 52 1998 998 98 8 01
+ | 1997 53 1998 998 98 8 01
+ | 1999 53 1999 999 99 9 52
+ | 2000 01 1999 999 99 9 52
+ | 2000 53 2000 000 00 0 52
+ | 2001 01 2001 001 01 1 01
+(64 rows)
+
-- TO_TIMESTAMP()
SELECT '' AS to_timestamp_1, to_timestamp('0097/Feb/16 --> 08:14:30', 'YYYY/Mon/DD --> HH:MI:SS');
to_timestamp_1 | to_timestamp
@@ -1400,6 +1470,18 @@
to_timestamp_14 | to_timestamp
-----------------+------------------------------
| Thu Nov 16 00:00:00 1995 PST
+(1 row)
+
+SELECT '' AS to_timestamp_15, to_timestamp('200401', 'IYYYIW');
+ to_timestamp_15 | to_timestamp
+-----------------+------------------------------
+ | Mon Dec 29 00:00:00 2003 PST
+(1 row)
+
+SELECT '' AS to_timestamp_16, to_timestamp('200401', 'YYYYWW');
+ to_timestamp_16 | to_timestamp
+-----------------+------------------------------
+ | Thu Jan 01 00:00:00 2004 PST
(1 row)
SET DateStyle TO DEFAULT;
Index: src/test/regress/sql/timestamptz.sql
===================================================================
RCS file: /projects/cvsroot/pgsql-server/src/test/regress/sql/timestamptz.sql,v
retrieving revision 1.4
diff -u -r1.4 timestamptz.sql
--- src/test/regress/sql/timestamptz.sql 29 Jul 2003 00:03:19 -0000 1.4
+++ src/test/regress/sql/timestamptz.sql 18 Dec 2003 18:58:00 -0000
@@ -189,6 +189,9 @@
SELECT '' AS to_char_9, to_char(d1, 'YYYY A.D. YYYY a.d. YYYY bc HH:MI:SS P.M. HH:MI:SS p.m. HH:MI:SS pm')
FROM TIMESTAMPTZ_TBL;
+SELECT '' AS to_char_10, to_char(d1, 'YYYY WW IYYY IYY IY I IW')
+ FROM TIMESTAMPTZ_TBL;
+
-- TO_TIMESTAMP()
SELECT '' AS to_timestamp_1, to_timestamp('0097/Feb/16 --> 08:14:30', 'YYYY/Mon/DD --> HH:MI:SS');
@@ -219,5 +222,10 @@
SELECT '' AS to_timestamp_13, to_timestamp('95-1116', 'YY-MMDD');
SELECT '' AS to_timestamp_14, to_timestamp('995-1116', 'YYY-MMDD');
+
+SELECT '' AS to_timestamp_15, to_timestamp('200401', 'IYYYIW');
+
+SELECT '' AS to_timestamp_16, to_timestamp('200401', 'YYYYWW');
+
SET DateStyle TO DEFAULT;
Re: ISO year.
От:
Kurt Roeckx <Q@ping.be>
Дата:
On Fri, Dec 19, 2003 at 07:14:53PM -0500, Bruce Momjian wrote: > > Which patch is OK? The one attached? You looked like you were making > changes to this patch in your later emails. That is the changed/good patch. Kurt
Re: ISO year.
От:
Bruce Momjian <pgman@candle.pha.pa.us>
Дата:
Your patch has been added to the PostgreSQL unapplied patches list at:
http://momjian.postgresql.org/cgi-bin/pgpatches
I will try to apply it within the next 48 hours.
---------------------------------------------------------------------------
Kurt Roeckx wrote:
> On Thu, Dec 18, 2003 at 06:47:41PM +0100, Peter Eisentraut wrote:
> > Kurt Roeckx wrote:
> > > 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
>
[ Attachment, skipping... ]
>
> ---------------------------(end of broadcast)---------------------------
> TIP 9: the planner will ignore your desire to choose an index scan if your
> joining column's datatypes do not match
--
Bruce Momjian | http://candle.pha.pa.us
pgman@candle.pha.pa.us | (610) 359-1001
+ If your life is a hard drive, | 13 Roberts Road
+ Christ can be your backup. | Newtown Square, Pennsylvania 19073
Re: ISO year.
От:
Bruce Momjian <pgman@candle.pha.pa.us>
Дата:
Patch applied. Thanks.
---------------------------------------------------------------------------
Kurt Roeckx wrote:
> On Thu, Dec 18, 2003 at 06:47:41PM +0100, Peter Eisentraut wrote:
> > Kurt Roeckx wrote:
> > > 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
>
[ Attachment, skipping... ]
>
> ---------------------------(end of broadcast)---------------------------
> TIP 9: the planner will ignore your desire to choose an index scan if your
> joining column's datatypes do not match
--
Bruce Momjian | http://candle.pha.pa.us
pgman@candle.pha.pa.us | (610) 359-1001
+ If your life is a hard drive, | 13 Roberts Road
+ Christ can be your backup. | Newtown Square, Pennsylvania 19073