home *** CD-ROM | disk | FTP | other *** search
/ OpenStep 4.2J (Developer) / os42jdev.iso / NextDeveloper / OpenStepConversion / IntermediateFrameworks3 / Foundation.framework / Headers / NSDate.h < prev    next >
Text File  |  1996-11-20  |  15KB  |  368 lines

  1. /*    NSDate.h
  2.     Primitives for manipulating time
  3.     Copyright 1994, NeXT Computer, Inc.
  4.     All rights reserved.
  5. */
  6.  
  7. /*    
  8.     Portions of the NSDate implementation are derived from
  9.     public domain software described in
  10.      ``Calendrical Calculations'' by Nachum Dershowitz and
  11.      Edward M. Reingold, Software---Practice & Experience,
  12.     vol. 20, no. 9 (September, 1990), pp. 899--928 and from
  13.     ``Calendrical Calculations, II: Three Historical Calendars''
  14.     by Edward M. Reingold,  Nachum Dershowitz, and Stewart M. Clamen,
  15.     Software---Practice & Experience, vol. 23, to appear.
  16.  
  17. */
  18.  
  19. #import <Foundation/NSObject.h>
  20.  
  21. @class NSString, NSArray, NSDictionary;
  22.  
  23. /* This module implements the 'date and time' concept.  Features:
  24.     - it is easy to compare dates and deal with time intervals;
  25.     - fast (can use the native representation);
  26.     - accuracy (other date representations can be added to the framework);
  27.     - can support user-oriented representations (Gregorian, etc ...);
  28.     - dates are immutable;
  29.     - an absolute reference date to ease conversion to other representations; 
  30. Our absolute reference date is the first instant of Jan 1st, 2001.
  31. All representations must be able to convert to/from that absolute reference.
  32. We ignore leap second accounting (e.g. pretend that they don't happen).
  33. Our reference date corresponds to 978307200 seconds after the UNIX base (e.g.
  34. 1/1/1970 to 1/1/2001 is (31*365 + 8 (leaps: 1972, .., 2000))*24*60*60)
  35.  
  36. Other interesting numbers are the number of days to the Common Era 1/1/1 ((2000*365+25*20-20+5+1), and for astronomers, from there to the Julian Epoch, 1721425.5.
  37.  
  38. */
  39.  
  40. typedef double NSTimeInterval;
  41.     /* always in seconds; 
  42.     yields sub-millisecond precision over a range of 10000 years */
  43.  
  44. #define NSTimeIntervalSince1970  978307200.0L
  45.  
  46. @class NSCalendarDate; // expresses Western dates (see below)
  47. @class NSTimeZone; // expresses a time zone (see below)
  48.  
  49. /***************    Date basics        ***************/
  50.  
  51. @interface NSDate:NSObject <NSCopying, NSCoding>
  52.  
  53. - (NSTimeInterval)timeIntervalSinceReferenceDate;
  54.     /* >0 if self is in the 3rd millenium */
  55.  
  56. @end
  57.  
  58. @interface NSDate (NSExtendedDate)
  59.  
  60. - (NSTimeInterval)timeIntervalSinceDate:(NSDate *)anotherDate;
  61.     /* >0 if self is ahead of anotherDate;
  62.     Conversion between two dates using the same representation should be exact */
  63.  
  64. - (NSTimeInterval)timeIntervalSinceNow;
  65.     /* >0 if self is in the future */
  66.  
  67. - (NSTimeInterval)timeIntervalSince1970;
  68.     /* convenience for POSIX calculations; neg if date is before 1970 */
  69.  
  70. - (id)addTimeInterval:(NSTimeInterval)seconds;
  71.     /* Returns an auto-released date; 
  72.     may return a date from a different representation */
  73.  
  74. - (NSDate *)earlierDate:(NSDate *)anotherDate;
  75. - (NSDate *)laterDate:(NSDate *)anotherDate;
  76. - (NSComparisonResult)compare:(NSDate *)other;
  77.  
  78. - (BOOL)isEqualToDate:(NSDate *)otherDate;
  79.  
  80. + (NSTimeInterval)timeIntervalSinceReferenceDate;
  81.     
  82. @end
  83.  
  84. /***************    Creation of basic dates        ***************/
  85.  
  86. @interface NSDate (NSDateCreation)
  87.  
  88. + (id)allocWithZone:(NSZone *)zone;
  89.     /* Create an uninitialized instance of a concrete date;
  90.     When called with NSDate, substitutes a concrete class that guarantees a wide dynamic range, but as accurate as possible for dates close to boot time for use in timers;
  91.     +alloc can also be used to that effect */
  92.  
  93. + (id)date;
  94.     /* now! */
  95.     
  96. + (id)dateWithTimeIntervalSinceNow:(NSTimeInterval)secs;
  97.     /* Creates a new autoreleased date before or after now */
  98.     
  99. + (id)dateWithTimeIntervalSinceReferenceDate:(NSTimeInterval)secs;
  100.     /* Creates a new autoreleased date before or after the absolute reference date */
  101.  
  102. + (id)dateWithTimeIntervalSince1970:(NSTimeInterval)secs;
  103.     /* creates a date given the POSIX delta */
  104.     
  105. + (id)distantFuture;
  106.     /* a date far far into the future */
  107.     
  108. + (id)distantPast;
  109.     /* a date far far in the past */
  110.     
  111. - (id)initWithTimeIntervalSinceReferenceDate:(NSTimeInterval)secsToBeAdded;
  112.     /* Designated initializer: MUST BE IMPLEMENTED BY CONCRETE CLASSES;
  113.     Creates a new date before or after the absolute reference date */
  114.  
  115. - (id)init;
  116.     /*  returns the date for now */
  117.  
  118. - (id)initWithTimeInterval:(NSTimeInterval)secsToBeAdded sinceDate:(NSDate *)anotherDate;
  119.     /* initializes a date given an offset from anotherDate;
  120.     Conversion between two dates using the same representation should be exact;
  121.     offset may be <0 (date is before anotherDate); */
  122.  
  123. - (id)initWithTimeIntervalSinceNow:(NSTimeInterval)secsToBeAddedToNow;
  124.     /* initializes a date given an offset from now;
  125.     offset may be <0 (date is before now); */
  126.  
  127. @end
  128.  
  129. /*******    Conveniences to deal with Western calendar    *******/
  130.  
  131. @interface NSDate (NSCalendarDateExtras)
  132.  
  133. - (id)initWithString:(NSString *)description;
  134.     // uses standard format string, only.
  135.  
  136. - (NSCalendarDate *)dateWithCalendarFormat:(NSString *)format timeZone:(NSTimeZone *)aTimeZone;
  137.  
  138. - (NSString *)descriptionWithLocale:(NSDictionary *)locale;
  139.     /* use the default time zone and locale's NSTimeDateFormatString */
  140.  
  141. - (NSString *)descriptionWithCalendarFormat:(NSString *)format timeZone:(NSTimeZone *)aTimeZone locale:(NSDictionary *)locale;
  142. @end
  143.  
  144. #if !defined(STRICT_OPENSTEP)
  145.  
  146. @interface NSDate (NSNaturalLangage)
  147. + dateWithNaturalLanguageString:(NSString *)string;
  148. + dateWithNaturalLanguageString:(NSString *)string locale:(NSDictionary *)dict;
  149.    // Handles natural (human) language date and time expressions
  150.    // such as "Mar 12, 1995" and "last tuesday", where the language elements
  151.    // are parameterized by the locale.  If locale is nil, NSUserDefaults is used.
  152. @end
  153.  
  154. #endif /* !STRICT_OPENSTEP */
  155.  
  156. /************ Time Zone Abstract Class *********************************/
  157.  
  158. /* An abstract TimeZone class.  TimeZones provide "locality" to universal
  159. dates.  Since many areas provide Daylight Savings Time in one form or
  160. another, we consider a NSTimeZone to represent the geo-political area,
  161. and a NSTimeZoneDetail as a specialization that contains the details of
  162. abbreviation and offset.
  163.    Private concrete classes are provided that supply
  164.       - unnamed offset style timezones, e.g. +0900
  165.       - unchanging named timezones "GMT" +0000
  166.       - varying timezones, e.g. PST/PDT, derived from underlying
  167.       system resources
  168.    The initialization method will substitute a private concrete instance.
  169.  */
  170.  
  171. @class NSTimeZoneDetail;
  172.  
  173. @interface NSTimeZone: NSObject <NSCopying, NSCoding>
  174.  
  175. // The default time zone is established via TZFILE environment variable
  176. // which should name a path for tzfile(5) information 
  177. // (/etc/zoneinfo/localtime is the default if TZFILE is not set)
  178. + (void)setDefaultTimeZone:(NSTimeZone *)aTimeZone;
  179. + (NSTimeZoneDetail *)defaultTimeZone;
  180.  
  181. // provides a defaultTimeZone look-alike except that when decoding
  182. // it replaces itself with the current defaultTimeZone
  183. // this is useful for NSCalendarDates that have a particular
  184. // format but wish to always use the current (local) timezone.
  185. + (NSTimeZone *)localTimeZone;
  186.  
  187. + (NSDictionary *)abbreviationDictionary;
  188.    // maps abbreviation to regionName (which is currently a file name)
  189.    // e.g. { PST = US/Pacific; PDT = US/Pacific; MDT = US/Mountain; ...}
  190.    // keys may be used to obtain timeZones via timeZoneWithName
  191.    // values may also be used to obtain timezones via timeZoneWithRegionName
  192.  
  193. + (NSArray *)timeZoneArray;
  194.    // supplies all regionNames grouped as subarrays per geographical region
  195.    // e.g. ( ... ( US/Pacific, US/Pacific-New, Canada/Pacific), ...)
  196.  
  197. + (NSTimeZone *)timeZoneWithName:(NSString *)aTimeZoneName;
  198.    // provide timezone with this timeZoneName
  199.    // currently, timeZoneName names a file in tzfile(5) format
  200.  
  201. + (NSTimeZoneDetail *)timeZoneWithAbbreviation:(NSString *)abbreviation;
  202.    // provide timezone with this abbreviation (if in abbreviationDictionary)
  203.    
  204. + (NSTimeZone *)timeZoneForSecondsFromGMT:(int)seconds;
  205.    // yield an unnamed timezone for this offset
  206.  
  207. - (NSString *)timeZoneName;
  208.  
  209. - (NSArray *)timeZoneDetailArray;
  210. - (NSTimeZoneDetail *)timeZoneDetailForDate:(NSDate *)date;
  211.  
  212. @end
  213.  
  214. @interface NSTimeZoneDetail : NSTimeZone
  215. - (int)timeZoneSecondsFromGMT;
  216. - (NSString *)timeZoneAbbreviation;
  217. - (BOOL)isDaylightSavingTimeZone;
  218. @end
  219.  
  220. /***************    Calendar Utilities        ***************/
  221.  
  222.  
  223.     /* Calendar formatting: (codes identical to strftime()):
  224.         %% encode a '%' character
  225.     %a abbreviated weekday name
  226.     %A full weekday name
  227.     %b abbreviated month name
  228.     %B full month name
  229.     %c time and date using 
  230.     %d day of the month as a decimal number (01-31)
  231.     %H hour based on a 24-hour clock as a decimal number (00-23)
  232.     %I hour based on a 12-hour clock as a decimal number (01-12)
  233.     %j day of the year as a decimal number (001-366)
  234.     %m month as a decimal number (01-12)
  235.     %M minute as a decimal number (00-59)
  236.     %p AM/PM designation associated with a 12-hour clock
  237.     %S second as a decimal number (00-61)
  238.     %w weekday as a decimal number (0-6), where Sunday is 0
  239.     %x date using the NSDateFormatString representation for the locale
  240.     %X time using the NSTimeFormatString representation for the locale
  241.     %y year without century (00-99)
  242.     %Y year with century (e.g. 1990)
  243.     %Z time zone detail name
  244.       and additionally:
  245.     %e day of the month as a decimal (1-31) (same as %d, but no leading 0)
  246.           %z timezone offset in hours & minutes from GMT (HHMM)
  247.     
  248.       The locale information may be supplied in the form of a dictionary,
  249.       where the keys and canonical contents are:
  250.           {
  251.         NSDateFormatString = "%a %b %d %Y";
  252.         NSTimeDateFormatString = "%a %b %d %H:%M:%S %Z %Y";
  253.         NSTimeFormatString = "%H:%M:%S %Z";
  254.         NSAMPMDesignation = ( AM, PM );
  255.         NSMonthNameArray = ( January, February, March, April, May,
  256.             June, July, August, September, October, November,
  257.         December );
  258.         NSShortMonthNameArray = ( Jan, Feb, Mar, Apr, May, Jun, Jul,
  259.             Aug, Sep, Oct, Nov, Dec );
  260.         NSWeekDayNameArray = ( Sunday, Monday, Tuesday, Wednesday,
  261.              Thursday, Friday, Saturday );
  262.         NSShortWeekDayNameArray = ( Sun, Mon, Tue, Wed, Thu, Fri, Sat );
  263.     }
  264.       These keys and values are available implicitly from NSUserDefaults
  265.  
  266.       The following additional information is also used when parsing natural language
  267.       date expresssions, and is supplied in language contexts:
  268.       {
  269.             NSHourNameDesignations = ( ( 0, midnight ), ( 12, noon, lunch ), ( 10, morning), ( 14, afternoon), ( 19, dinner) );
  270.             NSYearMonthWeekDesignations = ( year, month, week );
  271.             NSEarlierTimeDesignations = ( prior, last, past, ago );
  272.             NSLaterTimeDesignations = ( next );
  273.             NSThisDayDesignations = ( today );
  274.             NSNextDayDesignations = ( tomorrow );
  275.             NSNextNextDayDesignations = ( nextday );
  276.             NSPriorDayDesignations = ( yesterday );
  277.             NSDateTimeOrdering = MDYH;
  278.        }
  279.        NSDateTimeOrdering determines how to use ambiguous numbers, in this example,
  280.        value MDYH treats "2/3/4 10" as Month 2, Day 3, Year 4 and Hour 10.  However,
  281.        if fewer numbers are specified than are needed, the numbers are prioritized to satisfy
  282.        Day, Month, and Year.  If other elements are still missing, they are supplied
  283.        as follows:
  284.         day = today's day
  285.         month = today's month
  286.         year = today's year
  287.         hour = 12
  288.         minute = 0
  289.         second = 0
  290.         timezone = the default time zone
  291.     So for example, with "MDYH", "12" yields the 12th day of this month, this year,
  292.     "2 12" yields Feb 12 this year.  With value "YMDH", "12" still yields the
  293.     12 day of this month and "2 12" yields Feb 12.  With "DMYH" however, "2 12"
  294.     would yield Dec 12.
  295.    */
  296.    
  297.  
  298. @interface NSCalendarDate : NSDate {
  299.     unsigned        refCount;
  300.     NSTimeInterval     _timeIntervalSinceReferenceDate;
  301.     NSTimeZoneDetail     *_timeZone;
  302.     NSString         *_formatString;
  303.     void        *_reserved;
  304. }
  305.  
  306. + (id)dateWithYear:(int)year month:(unsigned)month day:(unsigned)day hour:(unsigned)hour minute:(unsigned)minute second:(unsigned)second timeZone:(NSTimeZone *)aTimeZone;
  307.  
  308. + (id)dateWithString:(NSString *)description calendarFormat:(NSString *)format;
  309.  
  310. + (id)dateWithString:(NSString *)description calendarFormat:(NSString *)format locale:(NSDictionary *)dict;
  311.  
  312. + (id)calendarDate;
  313.  
  314. - (id)initWithYear:(int)year month:(unsigned)month day:(unsigned)day hour:(unsigned)hour minute:(unsigned)minute second:(unsigned)second timeZone:(NSTimeZone *)aTimeZone;
  315.     /* e.g. [[NSDate alloc] initWithYear:1789 month:7 day:14 hour:12 minute:0 second:0 timeZone:[NSTimeZone timeZoneDetailForKey:@"MST"]]; 
  316.     raises if no timezone supplied */
  317.     
  318. - (id)initWithString:(NSString *)description;
  319. - (id)initWithString:(NSString *)description calendarFormat:(NSString *)format;
  320.      /* uses canonical (Western) style month and day names */
  321.  
  322. - (id)initWithString:(NSString *)description calendarFormat:(NSString *)format locale:(NSDictionary *)dict;
  323.     /* Uses dictionary provided month and day names for input resolution. */
  324.  
  325. - (NSTimeZoneDetail *)timeZoneDetail;
  326. - (void)setTimeZone:(NSTimeZone *)aTimeZone;
  327.     // picks the correct detail
  328.  
  329. - (NSString *)calendarFormat;
  330. - (void)setCalendarFormat:(NSString *)format;
  331.     /* default: %Y-%m-%d %H:%M:%S %z for an international style header */
  332.  
  333. - (int)yearOfCommonEra;    /* 1 C.E. (AD) is the base */
  334. - (int)monthOfYear;    /* 1 - 12 */
  335. - (int)dayOfMonth;    /* 1 - 31 */
  336. - (int)dayOfWeek;    /* 0 - 6 */
  337. - (int)dayOfYear;    /* 1 - 366 */
  338. - (int)dayOfCommonEra;    /* 1 - ????? */
  339. - (int)hourOfDay;    /* 0 - 23 */
  340. - (int)minuteOfHour;    /* 0 - 59 */
  341. - (int)secondOfMinute;    /* 0 - 59 */
  342.  
  343. - (NSCalendarDate *)dateByAddingYears:(int)year months:(int)month days:(int)day hours:(int)hour minutes:(int)minute seconds:(int)second;
  344.     /* All offsets may be negative; autoreleased result.
  345.        Will attempt to retain class
  346.        Adding 1 day across a timezone change will keep the same hour/minute/second offset (e.g. 2:30pm will stay 2:30pm)  */
  347.  
  348. - (void)years:(int *)yp months:(int *)mop days:(int *)dp hours:(int *)hp minutes:(int *)mip seconds:(int *)sp sinceDate:(NSCalendarDate *)date;
  349.     // compute offsets from date to target.  Missing pointers hold correspoding unit to 0.
  350.     // Thus, by supplying the years and seconds pointer, one can compute the difference
  351.     // in terms of years and seconds; by supplying years and days, the difference in years and
  352.     // days.
  353.     // If date is earlier than target, results are positive.
  354.     
  355. - (NSString *)description;
  356.     /* uses calendarFormat and timeZone, canonical locale */
  357.  
  358. - (NSString *)descriptionWithLocale:(NSDictionary *)locale;
  359.     /* uses calendarFormat and timeZone, supplied locale */
  360.  
  361. - (NSString *)descriptionWithCalendarFormat:(NSString *)format;
  362.     /* uses supplied format, canonical locale */
  363.  
  364. - (NSString *)descriptionWithCalendarFormat:(NSString *)format locale:(NSDictionary *)locale;
  365.     /* uses supplied format and locale */
  366. @end
  367.  
  368.