I ran into this bug quite a while ago! I found this code
somewhere ... I don't recall where ... I think it may have been the
Houdah framework. Anyway, I'm not going to take credit for it, but it
works!
/** 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());
}
Cheers,
Michael.
On 2009-10-07, at 4:35 PM, [email protected] wrote:
Right, but according to the docs at
http://developer.apple.com/legacy/mac/library/documentation/InternetWeb/Reference/WO542Reference/com/webobjects/foundation/NSTimestamp.html#timestampByAddingGregorianUnits(int,%20int,%20int,%20int,%20int,%20int)
"This method preserves "clock time" across changes in Daylight
Savings
Time zones and leap years. For example, adding one month to an
NSTimestamp with a time of 12 noon correctly maintains time at 12
noon."
So the time of 12:00:00 AM should have been preserved by
timestampByAddingGregorianUnits
Yes. You are absolutely correct. I believe it is a bug. I assumed that
timestampByAddingGregorianUnits would not preserve "clock time" but
that
certainly isn't what the API says.
Ramsey
On Oct 7, 2009, at 4:21 PM, David Avendasora wrote:
Isn't this the daylight-savings-time switch over? Currently we are 4
hours behind GMT, once DST is off, then we will be 5 hours behind
GMT.
Dave
On Oct 7, 2009, at 4:13 PM, Ramsey Gurley wrote:
Hi all,
Executing the following code with a default time zone of US Eastern
time
NSTimestamp ts1 = new
NSTimestamp(2009,11,1,0,0,0,NSTimeZone.defaultTimeZone());
NSTimestamp ts2 = ts1.timestampByAddingGregorianUnits( 0, 0, 1, 0,
0, 0 );
NSTimestamp ts3 = new
NSTimestamp(2009,11,2,0,0,0,NSTimeZone.defaultTimeZone());
System.out.println(NSTimeZone.defaultTimeZone().getID());
System.out.println(ts1 + "\n" + ts2 + "\n" + ts3);
produces the following result :-/
America/New_York
2009-11-01 04:00:00 Etc/GMT
2009-11-02 04:00:00 Etc/GMT
2009-11-02 05:00:00 Etc/GMT
Is this a known bug?
Ramsey
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Webobjects-dev mailing list ([email protected])
Help/Unsubscribe/Update your Subscription:
http://lists.apple.com/mailman/options/webobjects-dev/webobjects%40avendasora.com
This email sent to [email protected]
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Webobjects-dev mailing list ([email protected])
Help/Unsubscribe/Update your Subscription:
http://lists.apple.com/mailman/options/webobjects-dev/michael.halliday%40eadiefleet.com
This email sent to [email protected]
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Webobjects-dev mailing list ([email protected])
Help/Unsubscribe/Update your Subscription:
http://lists.apple.com/mailman/options/webobjects-dev/archive%40mail-archive.com
This email sent to [email protected]