• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: NSNumberFormatter & NSTimestamp -- Not Happening
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: NSNumberFormatter & NSTimestamp -- Not Happening


  • Subject: Re: NSNumberFormatter & NSTimestamp -- Not Happening
  • From: Denis Stanton <email@hidden>
  • Date: Thu, 18 Sep 2003 18:38:04 +1200

Hi Albert

Thanks for your interest and suggestions.

On Thursday, September 18, 2003, at 04:24  AM, Albert Jagnow wrote:

> I am not totally sure I get your date example below, I will assume you
> mean April 1 2003 - April 7 2003.

Sorry, I should have explained a bit more background.  First, I am in
the southern hemisphere.  It is early spring here. Summertime will
start for us on 5 October, so that is why I chose 1 October to 7
October.  By all means switch my example from October to April to fit
most of our audience.  :-)

> 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.

To be fair to the rental car industry I should also explain that this
is a special case.  We are renting out campers not cars and we charge
in whole days including the pick-up day and the drop-off day, with a 7
day minimum.  You won't get charged 2 days for 2 minutes as in your
example. Although your calculation is correct for us, it doesn't happen.

> 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.

I think the example can be abbreviated to :

	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("Days: "+getDayDiff(inDate,outDate));
	}

     public int getDayDiff(NSTimestamp start,NSTimestamp end){
         long diff = (end.getTime() - start.getTime() + 60*60*1000);
//diff in ms, plus one hour just in case
         int days = (int)( diff / (24*60*60*1000) );
         return days;
     }

The added hour either brings the 6.85 day example up to 7 days, or
takes a normal 7 days to 7.15.  Eihter way the truncation to int beings
it out at 7 days even.

>
> 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
>>
Denis Stanton
email@hidden
Home:  (09) 533 0391
mobile: 021 1433622
_______________________________________________
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.

References: 
 >Re: NSNumberFormatter & NSTimestamp -- Not Happening (From: Albert Jagnow <email@hidden>)

  • Prev by Date: Re: Using Custom Data types in EOModeler
  • Next by Date: Re: NSNumberFormatter & NSTimestamp -- Not Happening
  • Previous by thread: Re: NSNumberFormatter & NSTimestamp -- Not Happening
  • Next by thread: Re: NSNumberFormatter & NSTimestamp -- Not Happening
  • Index(es):
    • Date
    • Thread