• 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: sick and not thinking right...
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: sick and not thinking right...


  • Subject: Re: sick and not thinking right...
  • From: "Louis C. Sacha" <email@hidden>
  • Date: Tue, 9 Mar 2004 20:38:44 -0800

Hello...

this is killing me among other things whats wrong with this?

Other than the wonderfully descriptive subject line? Just kidding :)


When you compare two dates, if the date you send the message to is earlier than the date provided as an argument, the resulting values will be negative.

NSCalendarDate *now = [NSCalendarDate calendarDate];
int hours, minutes, seconds;
[startTime years:NULL months:NULL days:NULL hours:&hours
minutes:&minutes seconds:&seconds sinceDate:now];
NSCalendarDate *dateDiff = [NSCalendarDate dateWithYear:NULL month:NULL
day:NULL hour:hours minute:minutes second:seconds timeZone:[NSTimeZone
systemTimeZone]];
[statusItem setTitle:[dateDiff
descriptionWithCalendarFormat:@"%H:%M:%S"]];

startTime is a previously declared time.

So, if startTime is a half hour before "now", dateDiff will end up being equivalent to 23:30, which probably isn't quite what you are trying to get.

You can either switch around the comparison

[[NSCalendarDate calendarDate] years:NULL months:NULL days:NULL hours:&hours minutes:&minutes seconds:&seconds sinceDate:startTime];

or negate the returned values when you make your dateDiff

NSCalendarDate *dateDiff = [NSCalendarDate dateWithYear:NULL month:NULL day:NULL hour:(-hours) minute:(-minutes) second:(-seconds) timeZone:[NSTimeZone systemTimeZone]];


Also, if you aren't using the hour/minute/second values or dateDiff for anything else, you could do the following instead, which is a bit simpler:

NSDate *dateDiff = [NSDate dateWithTimeIntervalSinceReferenceDate:(-[startTime timeIntervalSinceNow])];
[statusItem setTitle:[dateDiff descriptionWithCalendarFormat:@"%H:%M:%S" timeZone:[NSTimeZone timeZoneWithAbbreviation:@"GMT"] locale:nil]];


Hope that helps,

Louis
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives: http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.


References: 
 >sick and not thinking right... (From: email@hidden)

  • Prev by Date: Re: sick and not thinking right...
  • Next by Date: Re: NSWorkspace or NSFileManager?
  • Previous by thread: Re: sick and not thinking right...
  • Next by thread: How to update or redraw NSMenuItem while it is being displayed
  • Index(es):
    • Date
    • Thread