RE: NSTimestamp bug??
RE: NSTimestamp bug??
- Subject: RE: NSTimestamp bug??
- From: <email@hidden>
- Date: Mon, 5 Apr 2004 18:09:22 +0200
- Thread-topic: NSTimestamp bug??
IMHO, there is a bug in timestampByAddingGregorianUnits(). It does not "preserve clock time" by my definition. It preserves time shift: you add one day, you get exactly 24 hours of elapsed time.
I have documented this repeatedly on the mailing lists and submitted twice as bug report to Apple. The WebObjects don't agree on this being a bug.
To get the expoected behavior, you may use the following:
/** 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: Jonathan Rochkind [mailto:email@hidden]
Sent: Monday, April 05, 2004 5:57 PM
To: email@hidden; email@hidden
Subject: Re: NSTimestamp bug??
Figuring out how to properly do date arithmetic in WO is
confusing---there are too many date/time classes in Java already, and
then Apple adds one more, NSTimestamp. And both the Java classes
_and_ the Apple class have a history of bugs---I can never remember
the current status of such bugs in a given Java/WO version. I don't
know for sure why you are seeing the behavior you are seeing, but
given the day you are trying it is right around the US daylight
savings time switch, I suspect daylight savings time might be
implicated. (So if you want to test doing this a different way, you
might want to make sure to use the same input, so it's still right
around the DST switch, to make sure a different way is truly
different).
When I do date arithmetic, I always use Java GregorianCalendar
instead of NSTimestamp's own methods. Which at one point is what the
NSTimestamp documentation reccomended you do, but I'm not sure it
does anymore. But I seem to have had luck with it.
NSTimestamp timestamp; //assume exists
//Create a GregorianCalendar matching the timestamp value
GregorianCalendar myCalendar = new GregorianCalendar();
myCalendar.setTime(myNSTimestamp);
//go back one calendar day
myCalendar.add( GregorianCalendar.DATE, -1 );
//translate back to NSTimestamp
timestamp = new NSTimestamp( myCalendar.getTime() );
--Jonathan
At 11:25 AM -0400 4/5/04, Tom Pelaia wrote:
>It appears to me that the timestampByAddingGregorianUnits() method
>of NSTimestamp does not properly handle changing by day increments.
>The documentation claims that the method preserves "clock time"
>across daylight savings changes, but that doesn't appear to be
>happening in my application. Instead when I start from 00:00 on
>today (Monday) and go back one day I get 23:00 on Saturday. Has
>anyone else seen this issue or does it indicate something wrong with
>the system clock on our server?
>
>thanks,
>-tom
>
>--
>Tom Pelaia
>SNS Project, 701 Scarboro Road, MS-6473, Oak Ridge, TN 37830
>phone: (865)574-6421, fax: (865)574-6617
>_______________________________________________
>webobjects-dev mailing list | email@hidden
>Help/Unsubscribe/Archives:
>http://www.lists.apple.com/mailman/listinfo/webobjects-dev
>Do not post admin requests to the list. They will be ignored.
_______________________________________________
webobjects-dev mailing list | email@hidden
Help/Unsubscribe/Archives: http://www.lists.apple.com/mailman/listinfo/webobjects-dev
Do not post admin requests to the list. They will be ignored.
**********************************************************************
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
**********************************************************************
_______________________________________________
webobjects-dev mailing list | email@hidden
Help/Unsubscribe/Archives: http://www.lists.apple.com/mailman/listinfo/webobjects-dev
Do not post admin requests to the list. They will be ignored.