Re: NSNumberFormatter & NSTimestamp -- Not Happening
Re: NSNumberFormatter & NSTimestamp -- Not Happening
- Subject: Re: NSNumberFormatter & NSTimestamp -- Not Happening
- From: Albert Jagnow <email@hidden>
- Date: Wed, 17 Sep 2003 11:24:02 -0500
I am not totally sure I get your date example below, I will assume you
mean April 1 2003 - April 7 2003. We will assume time changed from 2 -
3 AM on April 6, so if I rented a car at 12:00AM local time on April
1st and returned it to the same place at 12:00AM local time on April
7th I would have had the car for 143 hours or 5.9583 days. So your
time calculation is correct. It is not really 6 days between those
two times. But that is not what you want now is it? You really only
care about the days not the time. What you really want seems to be the
number of different days the person had the car on, which is 7. For
example if I rented the car at 11:59PM on Sunday and returned it at
12:01AM on Monday that would be two days on my rental agreement even
though I only had the car for two minutes. Geesh, no wonder rentals
cars always add up to more than I expect. Below is some example code
that will do what you want. The code is a little sloppy and there
might be a better way to do it but it was the first idea I had. Also
there are two example that may give you some other options to deal with
timestamps over DST changes.
public void exampleStuff(){
//Count the number of unique days between two dates
NSTimeZone timeZone =
NSTimeZone.timeZoneWithName("America/Chicago",true);
NSTimestamp inDate = new NSTimestamp(2003, 9, 17, 23, 59, 0,
timeZone);
NSTimestamp outDate = new NSTimestamp(2003, 9, 18, 0, 1, 0,
timeZone);
System.out.println("Diff: "+getDayDiff(inDate,outDate));
GregorianCalendar myCal = new GregorianCalendar();
myCal.setTime(inDate);
int inYear = myCal.get(GregorianCalendar.YEAR);
int inMonth = myCal.get(GregorianCalendar.MONTH) + 1;
int inDay = myCal.get(GregorianCalendar.DAY_OF_MONTH);
long inValue = inYear*10000000 + inMonth*1000 + inDay;
myCal.setTime(outDate);
long compareValue =
myCal.get(GregorianCalendar.YEAR)*10000000+(myCal.get(GregorianCalendar.
MONTH)+1)*1000+myCal.get(GregorianCalendar.DAY_OF_MONTH);
int days = 1;
while(compareValue > inValue){
myCal.add(Calendar.DATE,-1);
compareValue =
myCal.get(GregorianCalendar.YEAR)*10000000+(myCal.get(GregorianCalendar.
MONTH)+1)*1000+myCal.get(GregorianCalendar.DAY_OF_MONTH);
days++;
}
System.out.println("DAYS: "+days);
//Example 1 - Check if a date is in DST
System.out.println("-- Example 1 --");
NSTimeZone timeZone1 = NSTimeZone.systemTimeZone();
NSTimestamp inDate1 = new NSTimestamp(2003, 4, 1, 0, 0, 0,
timeZone1);
NSTimestamp outDate1 = new NSTimestamp(2003, 4, 7, 0, 0, 0,
timeZone1);
if(underDST(timeZone1,inDate1)){
System.out.println("In Date is in DST");
}
if(underDST(timeZone1,outDate1)){
System.out.println("Out Date is in DST");
}
//Example 2 - Create timestamp in GMT
System.out.println("-- Example 2 --");
NSTimeZone timeZone2 = NSTimeZone.getGMT();
NSTimestamp inDate2 = new NSTimestamp(2003, 4, 1, 0, 0, 0,
timeZone2);
NSTimestamp outDate2 = new NSTimestamp(2003, 4, 7, 0, 0, 0,
timeZone2);
System.out.println("Diff: "+getDayDiff(inDate2,outDate2));
}
public boolean underDST(NSTimeZone tz, NSTimestamp ts){
Date dt = new Date(ts.getTime());
return ( tz.inDaylightTime(dt) );
}
public int getDayDiff(NSTimestamp start,NSTimestamp end){
long diff = (end.getTime() - start.getTime()); //diff in ms
int days = (int)( diff / (24*60*60*1000) );
return days;
}
As Chuck said it probably would be better to create the timestamp with
the time as noon if all you care about is the date.
--Albert
On Wednesday, September 17, 2003, at 02:19 AM, Denis Stanton wrote:
> Hi Albert
>
> Thank you for taking the time to look into this.
>
> On Wednesday, September 17, 2003, at 04:52 PM, Albert Jagnow wrote:
>
>> I am pretty sure that NSTimestamp is stored in UTC which is not
>> affected by daylight savings time. If you compare two timestamps
>> where one was stored on October 25 2003 at 2:00AM local time and the
>> other was stored on October 26 2003 at 1:00AM local time after the
>> time change, these dates are 24 hours apart even though one is at
>> 2:00 local time and the other is at 1:00 local time. If you look at
>> the UTC time for each in a database (assuming local time is central
>> time) you should see something like 2003-10-25 07:00:00 for the first
>> time and 2003-10-26 07:00:00 for the second. The difference
>> calculation below should give you a diff of 24 hours or 1 day. This
>> is the correct difference is it not?
>
> That's not the way it works out in practice. I am prepared to believe
> that that I am causing this through my own ignorance, but my
> experience so far suggests that not all days are equal.
>
> I am developing an application for booking rental cars. The user sets
> outDate and returnDate by selecting day number, month name and year
> number from three pop-up menus. These are used to form dates with
> approximately the following code:
>
> NSTimeZone timeZone = NSTimeZone.systemTimeZone();
> outDate = new NSTimestamp(dateYear, dateMonth, dateDay, 0, 0,
> 0, timeZone);
>
> If I set an outDate of 1 October and a return date of 7th October, and
> then use System.out.println to display the results without formatting
> I will see:
>
> 2003-01-01 12:00:00 Etc/GMT
> 2003-01-07 11:00:00 Etc/GMT
>
> The user intends renting for a whole week, but performing the
> calculation discussed would return a duration of 6.9583 days,
> truncated to 6 days.
>
> The workaround is to always add 1 hour before performing the division.
> Some weeks will then be 7 days and others 7.0416. All will truncate
> to 7.
>
> Maybe the better answer is to set a timezone of UTC in the first
> place, but I haven't figured out how to do this so I'm using system
> time.
>
> Denis
_______________________________________________
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.