*
*
* IDENTIFICATION
- * $PostgreSQL: pgsql/src/backend/utils/adt/date.c,v 1.109 2005/05/26 02:04:13 neilc Exp $
+ * $PostgreSQL: pgsql/src/backend/utils/adt/date.c,v 1.110 2005/06/15 00:34:08 momjian Exp $
*
*-------------------------------------------------------------------------
*/
#include <ctype.h>
#include <limits.h>
#include <float.h>
+#include <time.h>
#include "access/hash.h"
#include "libpq/pqformat.h"
if (TIMESTAMP_NOT_FINITE(timestamp))
PG_RETURN_NULL();
- if (timestamp2tm(timestamp, NULL, tm, &fsec, NULL) !=0)
+ if (timestamp2tm(timestamp, NULL, tm, &fsec, NULL, NULL) !=0)
ereport(ERROR,
(errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
errmsg("timestamp out of range")));
if (TIMESTAMP_NOT_FINITE(timestamp))
PG_RETURN_NULL();
- if (timestamp2tm(timestamp, &tz, tm, &fsec, &tzn) !=0)
+ if (timestamp2tm(timestamp, &tz, tm, &fsec, &tzn, NULL) !=0)
ereport(ERROR,
(errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
errmsg("timestamp out of range")));
if (TIMESTAMP_NOT_FINITE(timestamp))
PG_RETURN_NULL();
- if (timestamp2tm(timestamp, NULL, tm, &fsec, NULL) !=0)
+ if (timestamp2tm(timestamp, NULL, tm, &fsec, NULL, NULL) !=0)
ereport(ERROR,
(errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
errmsg("timestamp out of range")));
if (TIMESTAMP_NOT_FINITE(timestamp))
PG_RETURN_NULL();
- if (timestamp2tm(timestamp, &tz, tm, &fsec, &tzn) !=0)
+ if (timestamp2tm(timestamp, &tz, tm, &fsec, &tzn, NULL) !=0)
ereport(ERROR,
(errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
errmsg("timestamp out of range")));
if (TIMESTAMP_NOT_FINITE(timestamp))
PG_RETURN_NULL();
- if (timestamp2tm(timestamp, &tz, tm, &fsec, &tzn) !=0)
+ if (timestamp2tm(timestamp, &tz, tm, &fsec, &tzn, NULL) !=0)
ereport(ERROR,
(errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
errmsg("timestamp out of range")));
/* timetz_zone()
* Encode time with time zone type with specified time zone.
+ * Applies DST rules as of the current date.
*/
Datum
timetz_zone(PG_FUNCTION_ARGS)
{
text *zone = PG_GETARG_TEXT_P(0);
- TimeTzADT *time = PG_GETARG_TIMETZADT_P(1);
+ TimeTzADT *t = PG_GETARG_TIMETZADT_P(1);
TimeTzADT *result;
int tz;
- int type,
- val;
- char *lowzone;
-
- lowzone = downcase_truncate_identifier(VARDATA(zone),
- VARSIZE(zone) - VARHDRSZ,
- false);
-
- type = DecodeSpecial(0, lowzone, &val);
+ char tzname[TZ_STRLEN_MAX];
+ int len;
+ pg_tz *tzp;
+ struct pg_tm *tm;
+ pg_time_t now;
+
+ /* Find the specified timezone */
+ len = (VARSIZE(zone)-VARHDRSZ>TZ_STRLEN_MAX)?TZ_STRLEN_MAX:(VARSIZE(zone)-VARHDRSZ);
+ memcpy(tzname,VARDATA(zone),len);
+ tzname[len]=0;
+ tzp = pg_tzset(tzname);
+ if (!tzp) {
+ ereport(ERROR,
+ (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
+ errmsg("time zone \"%s\" not recognized", tzname)));
+ PG_RETURN_NULL();
+ }
- result = (TimeTzADT *) palloc(sizeof(TimeTzADT));
+ /* Get the offset-from-GMT that is valid today for the selected zone */
+ if ((now = time(NULL)) < 0 ||
+ (tm = pg_localtime(&now, tzp)) == NULL) {
+ ereport(ERROR,
+ (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
+ errmsg("could not determine current time")));
+ PG_RETURN_NULL();
+ }
- if (type == TZ || type == DTZ)
- {
- tz = val * 60;
+ result = (TimeTzADT *)palloc(sizeof(TimeTzADT));
+
+ tz = -tm->tm_gmtoff;
#ifdef HAVE_INT64_TIMESTAMP
- result->time = time->time + (time->zone - tz) * USECS_PER_SEC;
- while (result->time < INT64CONST(0))
- result->time += USECS_PER_DAY;
- while (result->time >= USECS_PER_DAY)
- result->time -= USECS_PER_DAY;
+ result->time = t->time + (t->zone - tz) * USECS_PER_SEC;
+ while (result->time < INT64CONST(0))
+ result->time += USECS_PER_DAY;
+ while (result->time >= USECS_PER_DAY)
+ result->time -= USECS_PER_DAY;
#else
- result->time = time->time + (time->zone - tz);
- while (result->time < 0)
- result->time += SECS_PER_DAY;
- while (result->time >= SECS_PER_DAY)
- result->time -= SECS_PER_DAY;
+ result->time = t->time + (t->zone - tz);
+ while (result->time < 0)
+ result->time += SECS_PER_DAY;
+ while (result->time >= SECS_PER_DAY)
+ result->time -= SECS_PER_DAY;
#endif
- result->zone = tz;
- }
- else
- {
- ereport(ERROR,
- (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
- errmsg("time zone \"%s\" not recognized", lowzone)));
-
- PG_RETURN_NULL();
- }
+ result->zone = tz;
PG_RETURN_TIMETZADT_P(result);
} /* timetz_zone() */
*
*
* IDENTIFICATION
- * $PostgreSQL: pgsql/src/backend/utils/adt/timestamp.c,v 1.125 2005/06/14 21:04:40 momjian Exp $
+ * $PostgreSQL: pgsql/src/backend/utils/adt/timestamp.c,v 1.126 2005/06/15 00:34:09 momjian Exp $
*
*-------------------------------------------------------------------------
*/
if (TIMESTAMP_NOT_FINITE(timestamp))
EncodeSpecialTimestamp(timestamp, buf);
- else if (timestamp2tm(timestamp, NULL, tm, &fsec, NULL) == 0)
+ else if (timestamp2tm(timestamp, NULL, tm, &fsec, NULL, NULL) == 0)
EncodeDateTime(tm, fsec, NULL, &tzn, DateStyle, buf);
else
ereport(ERROR,
/* rangecheck: see if timestamp_out would like it */
if (TIMESTAMP_NOT_FINITE(timestamp))
/* ok */ ;
- else if (timestamp2tm(timestamp, NULL, tm, &fsec, NULL) !=0)
+ else if (timestamp2tm(timestamp, NULL, tm, &fsec, NULL, NULL) !=0)
ereport(ERROR,
(errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
errmsg("timestamp out of range")));
if (TIMESTAMP_NOT_FINITE(dt))
EncodeSpecialTimestamp(dt, buf);
- else if (timestamp2tm(dt, &tz, tm, &fsec, &tzn) == 0)
+ else if (timestamp2tm(dt, &tz, tm, &fsec, &tzn, NULL) == 0)
EncodeDateTime(tm, fsec, &tz, &tzn, DateStyle, buf);
else
ereport(ERROR,
/* rangecheck: see if timestamptz_out would like it */
if (TIMESTAMP_NOT_FINITE(timestamp))
/* ok */ ;
- else if (timestamp2tm(timestamp, &tz, tm, &fsec, &tzn) !=0)
+ else if (timestamp2tm(timestamp, &tz, tm, &fsec, &tzn, NULL) !=0)
ereport(ERROR,
(errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
errmsg("timestamp out of range")));
* Returns:
* 0 on success
* -1 on out of range
+ *
+ * If attimezone is NULL, the global timezone (including possblly brute forced
+ * timezone) will be used.
*/
int
-timestamp2tm(Timestamp dt, int *tzp, struct pg_tm * tm, fsec_t *fsec, char **tzn)
+timestamp2tm(Timestamp dt, int *tzp, struct pg_tm * tm, fsec_t *fsec, char **tzn, pg_tz *attimezone)
{
Timestamp date;
Timestamp time;
* specified. Go ahead and rotate to the local time zone since we will
* later bypass any calls which adjust the tm fields.
*/
- if (HasCTZSet && (tzp != NULL))
+ if ((attimezone==NULL) && HasCTZSet && (tzp != NULL))
{
#ifdef HAVE_INT64_TIMESTAMP
dt -= CTimeZone * USECS_PER_SEC;
* We have a brute force time zone per SQL99? Then use it without
* change since we have already rotated to the time zone.
*/
- if (HasCTZSet)
+ if ((attimezone==NULL) && HasCTZSet)
{
*tzp = CTimeZone;
tm->tm_isdst = 0;
utime = (pg_time_t) dt;
if ((Timestamp) utime == dt)
{
- struct pg_tm *tx = pg_localtime(&utime, global_timezone);
+ struct pg_tm *tx = pg_localtime(&utime, (attimezone!=NULL)?attimezone:global_timezone);
tm->tm_year = tx->tm_year + 1900;
tm->tm_mon = tx->tm_mon + 1;
*tm = &tt;
fsec_t fsec;
- if (timestamp2tm(timestamp, NULL, tm, &fsec, NULL) !=0)
+ if (timestamp2tm(timestamp, NULL, tm, &fsec, NULL, NULL) !=0)
ereport(ERROR,
(errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
errmsg("timestamp out of range")));
*tm = &tt;
fsec_t fsec;
- if (timestamp2tm(timestamp, &tz, tm, &fsec, &tzn) !=0)
+ if (timestamp2tm(timestamp, &tz, tm, &fsec, &tzn, NULL) !=0)
ereport(ERROR,
(errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
errmsg("timestamp out of range")));
result = (Interval *) palloc(sizeof(Interval));
- if (timestamp2tm(dt1, NULL, tm1, &fsec1, NULL) == 0 &&
- timestamp2tm(dt2, NULL, tm2, &fsec2, NULL) == 0)
+ if (timestamp2tm(dt1, NULL, tm1, &fsec1, NULL, NULL) == 0 &&
+ timestamp2tm(dt2, NULL, tm2, &fsec2, NULL, NULL) == 0)
{
fsec = (fsec1 - fsec2);
tm->tm_sec = tm1->tm_sec - tm2->tm_sec;
result = (Interval *) palloc(sizeof(Interval));
- if (timestamp2tm(dt1, &tz1, tm1, &fsec1, &tzn) == 0 &&
- timestamp2tm(dt2, &tz2, tm2, &fsec2, &tzn) == 0)
+ if (timestamp2tm(dt1, &tz1, tm1, &fsec1, &tzn, NULL) == 0 &&
+ timestamp2tm(dt2, &tz2, tm2, &fsec2, &tzn, NULL) == 0)
{
fsec = fsec1 - fsec2;
tm->tm_sec = tm1->tm_sec - tm2->tm_sec;
if (type == UNITS)
{
- if (timestamp2tm(timestamp, NULL, tm, &fsec, NULL) !=0)
+ if (timestamp2tm(timestamp, NULL, tm, &fsec, NULL, NULL) !=0)
ereport(ERROR,
(errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
errmsg("timestamp out of range")));
if (type == UNITS)
{
- if (timestamp2tm(timestamp, &tz, tm, &fsec, &tzn) !=0)
+ if (timestamp2tm(timestamp, &tz, tm, &fsec, &tzn, NULL) !=0)
ereport(ERROR,
(errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
errmsg("timestamp out of range")));
if (type == UNITS)
{
- if (timestamp2tm(timestamp, NULL, tm, &fsec, NULL) !=0)
+ if (timestamp2tm(timestamp, NULL, tm, &fsec, NULL, NULL) !=0)
ereport(ERROR,
(errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
errmsg("timestamp out of range")));
* convert to timestamptz to produce consistent
* results
*/
- if (timestamp2tm(timestamp, NULL, tm, &fsec, NULL) !=0)
+ if (timestamp2tm(timestamp, NULL, tm, &fsec, NULL, NULL) !=0)
ereport(ERROR,
(errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
errmsg("timestamp out of range")));
break;
}
case DTK_DOW:
- if (timestamp2tm(timestamp, NULL, tm, &fsec, NULL) !=0)
+ if (timestamp2tm(timestamp, NULL, tm, &fsec, NULL, NULL) !=0)
ereport(ERROR,
(errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
errmsg("timestamp out of range")));
break;
case DTK_DOY:
- if (timestamp2tm(timestamp, NULL, tm, &fsec, NULL) !=0)
+ if (timestamp2tm(timestamp, NULL, tm, &fsec, NULL, NULL) !=0)
ereport(ERROR,
(errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
errmsg("timestamp out of range")));
if (type == UNITS)
{
- if (timestamp2tm(timestamp, &tz, tm, &fsec, &tzn) !=0)
+ if (timestamp2tm(timestamp, &tz, tm, &fsec, &tzn, NULL) !=0)
ereport(ERROR,
(errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
errmsg("timestamp out of range")));
break;
case DTK_DOW:
- if (timestamp2tm(timestamp, &tz, tm, &fsec, &tzn) !=0)
+ if (timestamp2tm(timestamp, &tz, tm, &fsec, &tzn, NULL) !=0)
ereport(ERROR,
(errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
errmsg("timestamp out of range")));
break;
case DTK_DOY:
- if (timestamp2tm(timestamp, &tz, tm, &fsec, &tzn) !=0)
+ if (timestamp2tm(timestamp, &tz, tm, &fsec, &tzn, NULL) !=0)
ereport(ERROR,
(errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
errmsg("timestamp out of range")));
{
text *zone = PG_GETARG_TEXT_P(0);
Timestamp timestamp = PG_GETARG_TIMESTAMP(1);
- TimestampTz result;
+ Timestamp result;
int tz;
- int type,
- val;
- char *lowzone;
+ pg_tz *tzp;
+ char tzname[TZ_STRLEN_MAX+1];
+ int len;
+ struct pg_tm tm;
+ fsec_t fsec;
if (TIMESTAMP_NOT_FINITE(timestamp))
PG_RETURN_TIMESTAMPTZ(timestamp);
- lowzone = downcase_truncate_identifier(VARDATA(zone),
- VARSIZE(zone) - VARHDRSZ,
- false);
-
- type = DecodeSpecial(0, lowzone, &val);
-
- if (type == TZ || type == DTZ)
- {
- tz = -(val * 60);
-
- result = dt2local(timestamp, tz);
- }
- else
- {
+ /* Find the specified timezone? */
+ len = (VARSIZE(zone)-VARHDRSZ>TZ_STRLEN_MAX)?TZ_STRLEN_MAX:(VARSIZE(zone)-VARHDRSZ);
+ memcpy(tzname,VARDATA(zone),len);
+ tzname[len] = 0;
+ tzp = pg_tzset(tzname);
+ if (!tzp) {
ereport(ERROR,
(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
- errmsg("time zone \"%s\" not recognized",
- lowzone)));
-
+ errmsg("time zone \"%s\" not recognised",
+ tzname)));
PG_RETURN_NULL();
}
- PG_RETURN_TIMESTAMPTZ(result);
+ /* Apply the timezone change */
+ if (timestamp2tm(timestamp, &tz, &tm, &fsec, NULL, tzp) != 0 ||
+ tm2timestamp(&tm, fsec, NULL, &result) != 0) {
+ ereport(ERROR,
+ (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
+ errmsg("could not convert to time zone \"%s\"",
+ tzname)));
+ PG_RETURN_NULL();
+ }
+ PG_RETURN_TIMESTAMPTZ(timestamp2timestamptz(result));
} /* timestamp_zone() */
/* timestamp_izone()
else
{
- if (timestamp2tm(timestamp, NULL, tm, &fsec, NULL) !=0)
+ if (timestamp2tm(timestamp, NULL, tm, &fsec, NULL, NULL) !=0)
ereport(ERROR,
(errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
errmsg("timestamp out of range")));
else
{
- if (timestamp2tm(timestamp, &tz, tm, &fsec, &tzn) !=0)
+ if (timestamp2tm(timestamp, &tz, tm, &fsec, &tzn, NULL) !=0)
ereport(ERROR,
(errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
errmsg("timestamp out of range")));
(errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
errmsg("timestamp out of range")));
}
-
PG_RETURN_TIMESTAMP(result);
}
Timestamp result;
int tz;
- int type,
- val;
- char *lowzone;
+ pg_tz *tzp;
+ char tzname[TZ_STRLEN_MAX];
+ int len;
+ struct pg_tm tm;
+ fsec_t fsec = 0;
if (TIMESTAMP_NOT_FINITE(timestamp))
PG_RETURN_NULL();
- lowzone = downcase_truncate_identifier(VARDATA(zone),
- VARSIZE(zone) - VARHDRSZ,
- false);
-
- type = DecodeSpecial(0, lowzone, &val);
+ /* Find the specified zone */
+ len = (VARSIZE(zone)-VARHDRSZ>TZ_STRLEN_MAX)?TZ_STRLEN_MAX:(VARSIZE(zone)-VARHDRSZ);
+ memcpy(tzname,VARDATA(zone),len);
+ tzname[len] = 0;
+ tzp = pg_tzset(tzname);
- if (type == TZ || type == DTZ)
- {
- tz = val * 60;
+ if (!tzp) {
+ ereport(ERROR,
+ (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
+ errmsg("time zone \"%s\" not recognized", tzname)));
- result = dt2local(timestamp, tz);
+ PG_RETURN_NULL();
}
- else
- {
- ereport(ERROR,
- (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
- errmsg("time zone \"%s\" not recognized", lowzone)));
+ if (timestamp2tm(timestamp, &tz, &tm, &fsec, NULL, tzp) != 0 ||
+ tm2timestamp(&tm, fsec, NULL, &result)) {
+ ereport(ERROR,
+ (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
+ errmsg("could not to convert to time zone \"%s\"", tzname)));
PG_RETURN_NULL();
}