|
8 | 8 | * |
9 | 9 | * |
10 | 10 | * IDENTIFICATION |
11 | | - * $PostgreSQL: pgsql/src/backend/utils/adt/timestamp.c,v 1.117 2004/12/31 22:01:22 pgsql Exp $ |
| 11 | + * $PostgreSQL: pgsql/src/backend/utils/adt/timestamp.c,v 1.118 2005/04/01 14:25:23 momjian Exp $ |
12 | 12 | * |
13 | 13 | *------------------------------------------------------------------------- |
14 | 14 | */ |
@@ -2754,12 +2754,23 @@ timestamp_trunc(PG_FUNCTION_ARGS) |
2754 | 2754 | switch (val) |
2755 | 2755 | { |
2756 | 2756 | caseDTK_WEEK: |
2757 | | -isoweek2date(date2isoweek(tm->tm_year,tm->tm_mon,tm->tm_mday),&(tm->tm_year),&(tm->tm_mon),&(tm->tm_mday)); |
| 2757 | +{ |
| 2758 | +intwoy; |
| 2759 | + |
| 2760 | +woy=date2isoweek(tm->tm_year,tm->tm_mon,tm->tm_mday); |
| 2761 | +/* |
| 2762 | + *If it is week 52/53 and the month is January, |
| 2763 | + *then the week must belong to the previous year. |
| 2764 | + */ |
| 2765 | +if (woy >=52&&tm->tm_mon==1) |
| 2766 | +--tm->tm_year; |
| 2767 | +isoweek2date(woy,&(tm->tm_year),&(tm->tm_mon),&(tm->tm_mday)); |
2758 | 2768 | tm->tm_hour=0; |
2759 | 2769 | tm->tm_min=0; |
2760 | 2770 | tm->tm_sec=0; |
2761 | 2771 | fsec=0; |
2762 | 2772 | break; |
| 2773 | +} |
2763 | 2774 | caseDTK_MILLENNIUM: |
2764 | 2775 | /* see comments in timestamptz_trunc */ |
2765 | 2776 | if (tm->tm_year>0) |
@@ -2874,13 +2885,24 @@ timestamptz_trunc(PG_FUNCTION_ARGS) |
2874 | 2885 | switch (val) |
2875 | 2886 | { |
2876 | 2887 | caseDTK_WEEK: |
2877 | | -isoweek2date(date2isoweek(tm->tm_year,tm->tm_mon,tm->tm_mday),&(tm->tm_year),&(tm->tm_mon),&(tm->tm_mday)); |
| 2888 | +{ |
| 2889 | +intwoy; |
| 2890 | + |
| 2891 | +woy=date2isoweek(tm->tm_year,tm->tm_mon,tm->tm_mday); |
| 2892 | +/* |
| 2893 | + *If it is week 52/53 and the month is January, |
| 2894 | + *then the week must belong to the previous year. |
| 2895 | + */ |
| 2896 | +if (woy >=52&&tm->tm_mon==1) |
| 2897 | +--tm->tm_year; |
| 2898 | +isoweek2date(woy,&(tm->tm_year),&(tm->tm_mon),&(tm->tm_mday)); |
2878 | 2899 | tm->tm_hour=0; |
2879 | 2900 | tm->tm_min=0; |
2880 | 2901 | tm->tm_sec=0; |
2881 | 2902 | fsec=0; |
2882 | 2903 | redotz= true; |
2883 | 2904 | break; |
| 2905 | +} |
2884 | 2906 | /* one may consider DTK_THOUSAND and DTK_HUNDRED... */ |
2885 | 2907 | caseDTK_MILLENNIUM: |
2886 | 2908 |
|
@@ -3142,7 +3164,7 @@ date2isoweek(int year, int mon, int mday) |
3142 | 3164 | * Sometimes the last few days in a year will fall into the first week |
3143 | 3165 | * of the next year, so check for this. |
3144 | 3166 | */ |
3145 | | -if (result >=53) |
| 3167 | +if (result >=52) |
3146 | 3168 | { |
3147 | 3169 | day4=date2j(year+1,1,4); |
3148 | 3170 |
|
@@ -3198,7 +3220,7 @@ date2isoyear(int year, int mon, int mday) |
3198 | 3220 | * Sometimes the last few days in a year will fall into the first week |
3199 | 3221 | * of the next year, so check for this. |
3200 | 3222 | */ |
3201 | | -if (result >=53) |
| 3223 | +if (result >=52) |
3202 | 3224 | { |
3203 | 3225 | day4=date2j(year+1,1,4); |
3204 | 3226 |
|
|