RE: Date Math Question and Daylight Savings Time
RE: Date Math Question and Daylight Savings Time
- Subject: RE: Date Math Question and Daylight Savings Time
- From: <email@hidden>
- Date: Fri, 21 Jan 2005 11:21:28 +0100
- Thread-topic: Date Math Question and Daylight Savings Time
I agree with Stephen's assessment.
Here is the code I use to maipulate NSTimestamps when I want to "preserve clock time":
/** Adds time to a date.<BR>
* This utility method replaces NSTimestamp.
* timestampByAddingGregorianUnits() which does all its computations in the
* GMT timezone. This leads to several problems. For one DST changes don't
* happen as expected. Adding 1 month to a CET midnight of a first of a
* month does not bring us to the first of the next month, but only as many
* days further as there are days in the preceeding month.
*
* @param date the original timestamp or date
* @param year number of years to add
* @param month number of months to add
* @param day number of days to add
* @param hour number of hours to add
* @param minute number of minutes to add
* @param second number of seconds to add
*
* @return a new date corresponding to original date to which we add specified time.
**/
public static NSTimestamp timestampByAddingGregorianUnits(
Date date,
int year,
int month,
int day,
int hour,
int minute,
int second)
{
GregorianCalendar calendar = new GregorianCalendar();
calendar.setTime(date);
return new NSTimestamp(
calendar.get(GregorianCalendar.YEAR) + year,
calendar.get(GregorianCalendar.MONTH) + month + 1,
calendar.get(GregorianCalendar.DAY_OF_MONTH) + day,
calendar.get(GregorianCalendar.HOUR_OF_DAY) + hour,
calendar.get(GregorianCalendar.MINUTE) + minute,
calendar.get(GregorianCalendar.SECOND) + second,
TimeZone.getDefault());
}
Pierre
-----Original Message-----
From: webobjects-dev-bounces+pierre.bernard=email@hidden
[mailto:webobjects-dev-bounces+pierre.bernard=email@hidden]On
Behalf Of Stephen Todd
Sent: Thursday, January 20, 2005 9:39 PM
To: James Cicenia
Cc: WOdev Dev
Subject: Re: Date Math Question and Daylight Savings Time
I recently dealt with this problem myself, and I found a lot of trouble
in using NSTimestamp. The temptation is to call
timestampByAddingGregorianUnits() to modify timestamps. The docs say
that "This method preserves "clock time" across changes in Daylight
Savings Time zones and leap years." While the fact that it accounts
for leap years might be true, it does _not_ take care of Daylight
Saving. We do separate queries from midnight to midnight time periods
across a a week. You would think that you could just
timestampByAddingGregorianUnits() it by adding a day, but in reality,
only 24 hours are added (Oct 31, 2004, for example, has 25 hours).
Well, what you should read between the lines in the docs is that
NSTimestamps are in GMT and doesn't participate in DLS.
The solution that I found that worked is to convert it the timestamp to
a GregorianCalendar, make your changes, and then create a new time
stamp using the info from the GregorianCalendar. It's a pain, but it
seems to work for me. So, what I concluded was that don't do any
calculations or manipulations on NSTimestamps, just use a
GregorianCalendar.
I don't use GregorianCalendars extensively so YMMV, but this seemed to
work for us.
Stephen
On Jan 20, 2005, at 11:20 AM, James Cicenia wrote:
> Hello All -
>
> I am doing some date math calculations and during daylight savings
> time my calculations seems to be off a day.
>
> Any thoughts on why?
>
> - James Cicenia
>
> _______________________________________________
> Do not post admin requests to the list. They will be ignored.
> Webobjects-dev mailing list (email@hidden)
> Help/Unsubscribe/Update your Subscription:
> email@hidden
>
> This email sent to email@hidden
>
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Webobjects-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden
**********************************************************************
This email and any files transmitted with it are intended solely for
the use of the individual or entity to whom they are addressed.
If you have received this email in error please notify the sender
of this message. (email@hidden)
This email message has been checked for the presence of computer
viruses; however this protection does not ensure this message is
virus free.
Banque centrale du Luxembourg; Tel ++352-4774-1; http://www.bcl.lu
**********************************************************************
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Webobjects-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden