Re: NSTimestamp calculations?
Re: NSTimestamp calculations?
- Subject: Re: NSTimestamp calculations?
- From: Alex Cone <email@hidden>
- Date: Thu, 27 Jul 2006 10:56:39 -0400
On Jul 27, 2006, at 10:17 AM, email@hidden
wrote:
From: email@hidden
Subject: NSTimestamp calculations?
Whats the easiest way to subtract NSTimestamps from each other?
The methods getMinutes() etc both from NSTimestamp and Date are
unfortunately deprecated :-(
The getTime() method is your friend!
here are a few useful methods from my NSTimestampHelper bag-o-tricks...
public static int deltaDays( NSTimestamp one, NSTimestamp two ) {
long diff = two.getTime() - one.getTime(); // diff is in
milliseconds
long roundingOffset = ( diff >= 0 ) ? 12 : -12; // if diff
is positive, offset up, if negative, offset down
// next, convert diff to hours, offset by .5 days to
round the end result, divide by 24 and truncate.
long days = ( ( diff / ( 1000 * 60 * 60 ) ) +
roundingOffset ) / 24;
return (int)days;
}
public static int deltaHours( NSTimestamp one, NSTimestamp two ) {
long diff = two.getTime() - one.getTime(); // diff is in
milliseconds
long roundingOffset = ( diff >= 0 ) ? 30 : -30; // if diff
is positive, offset up, if negative, offset down
// next, convert diff to minutes, offset by .5 hour to
round the end result, divide by 60 and truncate.
long hours = ( ( diff / ( 1000 * 60 ) ) + roundingOffset ) /
60;
return (int)hours;
}
public static int deltaMinutes( NSTimestamp one, NSTimestamp
two ) {
long diff = two.getTime() - one.getTime(); // diff is in
milliseconds
long roundingOffset = ( diff >= 0 ) ? 30 : -30; // if diff
is positive, offset up, if negative, offset down
// next, convert diff to seconds, offset by .5 minute
to round the end result, divide by 60 and truncate.
long minutes = ( ( diff / ( 1000 ) ) + roundingOffset ) / 60;
return (int)minutes;
}
Enjoy!!
Alex
__alex cone
ceo c o d e f a b inc
email@hidden
212.465.8484 x101
http://www.codefab.com
"The perversity of the universe tends towards a maximum."
Entropy, expressed as Finagle's (Murphy's) 1st Law
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Webobjects-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden