/* Wotonomy: OpenStep design patterns for pure Java applications. Copyright (C) 2000 Blacksmith, Inc. This library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation; either version 2.1 of the License, or (at your option) any later version. This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. You should have received a copy of the GNU Lesser General Public License along with this library; if not, see http://www.gnu.org */ package net.wotonomy.foundation; import java.util.Date; import java.util.GregorianCalendar; import java.util.TimeZone; /** * A pure java implementation of NSDate that extends * java.util.Date for greater java compatibility. * * @author michael@mpowers.net * @author $Author: cgruber $ * @version $Revision: 892 $ */ public class NSDate extends Date { // NSComparisonResult compatibility public static final int NSOrderedAscending = -1; public static final int NSOrderedSame = 0; public static final int NSOrderedDescending = 1; // public static final double TimeIntervalSince1970; // public static final NSDate DateFor1970; /** * Default constructor represents the current date. */ public NSDate () { super(); } /** * Represents the specified number of seconds from the current date. */ public NSDate (double seconds) { super( (long) new NSDate().getTime() + timeIntervalToMilliseconds(seconds) ); } /** * Represents the specified number of seconds from the specified date. */ public NSDate (double seconds, Date sinceDate) { super( (long) sinceDate.getTime() + timeIntervalToMilliseconds(seconds) ); } /** * Returns the interval between this date and 1 January 2001 GMT. */ public double timeIntervalSinceReferenceDate () { GregorianCalendar referenceDate = new GregorianCalendar( TimeZone.getTimeZone( "GMT" ) ); referenceDate.set( 2001, 0, 0, 0, 0, 0 ); return timeIntervalSinceDate( referenceDate.getTime() ); } /** * Returns the interval between this date and the specified date * in seconds. */ public double timeIntervalSinceDate (Date aDate) { return millisecondsToTimeInterval( this.getTime() - aDate.getTime() ); } /** * Returns the interval between this date and the current date * in seconds. */ public double timeIntervalSinceNow () { return timeIntervalSinceDate( new NSDate() ); } /** * Compares this date to the specified date and returns the * earlier date. Unspecified which is returned if both are equal. */ public NSDate earlierDate (NSDate aDate) { if ( aDate == null ) return this; if ( after( aDate ) ) return aDate; return this; } /** * Compares this date to the specified date and returns the * later date. Unspecified which is returned if both are equal. */ public NSDate laterDate (NSDate aDate) { if ( aDate == null ) return this; if ( before( aDate ) ) return aDate; return this; } /** * Returns a negative value if the specified date is later than * this date, a positive value if the specified date is earlier * than this date, or zero if the dates are equal. The return * values are compatible with type NSComparisonResult. */ public int compare (Date aDate) { if ( before( aDate ) ) return NSOrderedAscending; if ( after( aDate ) ) return NSOrderedDescending; return NSOrderedSame; } /** * Returns whether the this date is equal to the specified date, * per the result of equals(). */ public boolean isEqualToDate (Date aDate) { return equals( aDate ); } /** * Returns a date that differs from this date by the specified * number of seconds. */ public NSDate dateByAddingTimeInterval (double seconds) { return new NSDate( seconds, this ); } /** * Returns the number of seconds between now and the reference date. */ public static double currentTimeIntervalSinceReferenceDate () { return new NSDate().timeIntervalSinceReferenceDate(); } /** * Converts seconds to milliseconds. Included for compatibility. */ public static long timeIntervalToMilliseconds (double seconds) { return (long) seconds*1000; } /** * Converts milliseconds to seconds. Included for compatibility. */ public static double millisecondsToTimeInterval (long millis) { return millis/1000.0; } /** * Returns a date that is greater than all representable dates. */ public static NSDate distantFuture () { NSDate result = new NSDate(); result.setTime( Long.MAX_VALUE ); return result; } /** * Returns a date that is less than all representable dates. */ public static NSDate distantPast () { NSDate result = new NSDate(); result.setTime( Long.MIN_VALUE ); return result; } // inherited from java.util.Date // public java.lang.String toString (); // public boolean equals (java.lang.Object); // public int hashCode (); } /* * $Log$ * Revision 1.1 2006/02/16 12:47:16 cgruber * Check in all sources in eclipse-friendly maven-enabled packages. * * Revision 1.1.1.1 2000/12/21 15:47:28 mpowers * Contributing wotonomy. * * Revision 1.3 2000/12/20 16:25:38 michael * Added log to all files. * * */