home *** CD-ROM | disk | FTP | other *** search
/ Nebula 1995 August / NEBULA.mdf / SourceCode / MiscKit1.2.6 / Palettes / MiscCalendarPalette / MiscCalendarView.subproj / SimpleDate.m < prev   
Encoding:
Text File  |  1994-05-30  |  3.9 KB  |  245 lines

  1. /******************************************************************************
  2.  *      $Log$
  3.  ******************************************************************************/
  4. static char *ident = "$Id$";
  5. static int __hack(int x)
  6. {if(x)return x;else return __hack((int)*ident);}
  7. /*****************************************************************************/
  8.  
  9. #import <libc.h>
  10.  
  11. #import "SimpleDate.h"
  12.  
  13. static int days[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
  14. static char *months[] = {"", "January", "February", "March", "April", \
  15.              "May", "June", "July", "August", "September", \
  16.              "October", "November", "December"};
  17.  
  18. @interface SimpleDate (Private)
  19.  
  20. - _setToday;
  21. - (BOOL)_isValidYear:(int)year month:(int)month day:(int)day;
  22. - (BOOL)_isLeapYear:(int)year;
  23.  
  24. @end
  25.  
  26. @implementation SimpleDate
  27.  
  28. - init
  29. {
  30.     [super init];
  31.  
  32.     [self _setToday];
  33.  
  34.     return self;
  35. }
  36.  
  37. - (int)day
  38. {
  39.     return day;
  40. }
  41.  
  42. - (int)month
  43. {
  44.     return month;
  45. }
  46.  
  47. - (int)year
  48. {
  49.     return year;
  50. }
  51.  
  52. - setYear:(int)aYear month:(int)aMonth day:(int)aDay
  53. {
  54.     if (![self _isValidYear:aYear month:aMonth day:aDay])
  55.       return nil;
  56.  
  57.     year = aYear;
  58.     month = aMonth;
  59.     day = aDay;
  60.  
  61.     return self;
  62. }
  63.  
  64. - (int)numberOfDaysInMonth
  65. {
  66.     if ([self _isLeapYear:year] && month == 2)
  67.       return 29;
  68.  
  69.     return days[month];
  70. }
  71.  
  72. - (int)startDayOfMonth
  73. {
  74.     struct tm date;
  75.     time_t time;
  76.  
  77.     date.tm_sec = 0;
  78.     date.tm_min = 0;
  79.     date.tm_hour = 0;
  80.     date.tm_mday = 1;
  81.     date.tm_mon = month - 1;
  82.     date.tm_year = year - 1900;
  83.     date.tm_wday = 0;
  84.     date.tm_yday = 0;
  85.     date.tm_isdst = 0;
  86.     date.tm_gmtoff = 0;
  87.     date.tm_zone = "";
  88.  
  89.     time = mktime(&date);
  90.  
  91.     date = *localtime(&time);
  92.  
  93.     time -= date.tm_gmtoff;
  94.  
  95.     date = *localtime(&time);
  96.  
  97.     return date.tm_wday;
  98. }
  99.     
  100. - incrementMonth
  101. {
  102.     if (month == 12)
  103.       month = 1;
  104.     else
  105.       month++;
  106.  
  107.     if (day > days[month])
  108.       day = days[month];
  109.  
  110.     return self;
  111. }
  112.  
  113. - decrementMonth
  114. {
  115.     if (month == 1)
  116.       month = 12;
  117.     else
  118.       month--;
  119.  
  120.     if (day > days[month])
  121.       day = days[month];
  122.  
  123.     return self;
  124. }
  125.  
  126. - incrementYear
  127. {
  128.     if (year+1 == 2038)
  129.       return nil;
  130.  
  131.     if ([self _isLeapYear:year] && ![self _isLeapYear:year+1] &&
  132.     month == 2 && day == 29)
  133.       day = 28;
  134.  
  135.     year++;
  136.  
  137.     return self;
  138. }
  139.  
  140. - decrementYear
  141. {
  142.     if (year == 1970)
  143.       return nil;
  144.  
  145.     if ([self _isLeapYear:year] && ![self _isLeapYear:year-1] &&
  146.     month == 2 && day == 29)
  147.       day = 28;
  148.  
  149.     year--;
  150.  
  151.     return self;
  152. }
  153.  
  154. - (const char *)monthStringValue
  155. {
  156.     return months[month];
  157. }
  158.  
  159. - (const char *)dateStringValue
  160. {
  161.     sprintf(dateString, "%s %d, %d", months[month], day, year);
  162.  
  163.     return dateString;
  164. }
  165.  
  166. - read:(NXTypedStream *)aStream
  167. {
  168.     [super read:aStream];
  169.  
  170.     NXReadTypes(aStream, "iii", &day, &month, &year);
  171.  
  172.     return self;
  173. }
  174.  
  175.  
  176. - write:(NXTypedStream *)aStream
  177. {
  178.     [super write:aStream];
  179.  
  180.     NXWriteTypes(aStream, "iii", &day, &month, &year);
  181.  
  182.     return self;
  183. }
  184.  
  185. @end
  186.  
  187. @implementation SimpleDate (Private)
  188.  
  189. - _setToday
  190. {
  191.     struct tm    *tmstruct;
  192.     time_t    now;
  193.  
  194.     now = time(0);
  195.  
  196.     /* Get the tm structure for the time right now */
  197.  
  198.     tmstruct = localtime(&now);
  199.  
  200.     /* Grab the year, month and day info from the tm struct */
  201.  
  202.     year = tmstruct->tm_year + 1900;
  203.     month = tmstruct->tm_mon + 1;
  204.     day = tmstruct->tm_mday;
  205.  
  206.     return self;
  207. }
  208.  
  209. - (BOOL)_isValidYear:(int)aYear month:(int)aMonth day:(int)aDay
  210. {
  211.     /* Is it a valid year? We do not accept any date before */
  212.     /* January 1, 1970 */
  213.  
  214.     if (aYear < 1970)
  215.       return NO;
  216.  
  217.     /* Is it a valid month? */
  218.  
  219.     if (aMonth < 1  ||  aMonth > 12)
  220.       return NO;
  221.  
  222.     /* Is it a valid day? */
  223.     if (aDay < 1)
  224.       return NO;
  225.  
  226.     /* Is it Feburary on a leap year? */
  227.  
  228.     if (aDay > (((month==2) && [self _isLeapYear:aYear]) ? 29 : days[aMonth]))
  229.       return NO;
  230.  
  231.     /* It is valid! */
  232.  
  233.     return YES;
  234. }
  235.  
  236. - (BOOL)_isLeapYear:(int)aYear
  237. {
  238.     if (((aYear % 4) == 0 && ((aYear % 100) != 0)) || (aYear % 400) == 0)
  239.       return YES;
  240.  
  241.     return NO;
  242. }
  243.  
  244. @end
  245.