home *** CD-ROM | disk | FTP | other *** search
/ Nebula 1994 June / NEBULA_SE.ISO / SourceCode / MiscKit / Temp / MiscTimeAdditions / Time_Stuff / Time.m < prev    next >
Encoding:
Text File  |  1993-07-19  |  8.8 KB  |  561 lines

  1.  
  2. #import "Time.h"
  3. #import <string.h>
  4. #import <sys/time.h>
  5.  
  6. #define SIZE_OF_TIME_STRING 27
  7. #define SIZE_OF_MNAME 10
  8. #define SIZE_OF_MABRNAME 4
  9. #define SIZE_OF_DNAME 10
  10. #define SIZE_OF_DABRNAME 4
  11.  
  12. @implementation Time
  13.  
  14. //--------------------------------
  15.  
  16. - free
  17. {
  18.     int i;
  19.     if( timeString )
  20.         NXZoneFree([ self zone ], timeString );
  21.     if( timesPtr )
  22.         NXZoneFree( [self zone], timesPtr );
  23.     for(i=0;i<12;i++){
  24.         NXZoneFree( [self zone], mNames[i] );
  25.         NXZoneFree( [self zone], mAbrNames[i] );
  26.     }
  27.     for(i=0;i<7;i++){
  28.         NXZoneFree( [self zone], dNames[i] );
  29.         NXZoneFree( [self zone], dAbrNames[i] );
  30.     }
  31.     return [super free];
  32. }
  33.  
  34. - init
  35. {
  36.     [super init];
  37.     timesPtr = NULL;
  38.     myTime = 0;
  39.     [self _syncTimesStruct];
  40.     [self initNames];
  41.     return self;
  42. }
  43.  
  44. - initWithCurrentTime
  45. {
  46.     [self init];
  47.     [self setToCurrentTime];
  48.     return self;
  49. }
  50.  
  51. - initNames
  52. {
  53.     int i;
  54.     for(i=0;i<12;i++){
  55.         mNames[i] = NXZoneMalloc( [self zone], SIZE_OF_MNAME * 12 );
  56.         mAbrNames[i] = NXZoneMalloc( [self zone], SIZE_OF_MABRNAME * 12 );
  57.     }
  58.  
  59.     for(i=0;i<7;i++){
  60.         dNames[i] = NXZoneMalloc( [self zone], SIZE_OF_DNAME * 7 );
  61.         dAbrNames[i] = NXZoneMalloc( [self zone], SIZE_OF_DABRNAME * 7 );
  62.     }
  63.  
  64.     mNames[0] =    "January";    mAbrNames[0] =    "Jan";
  65.     mNames[1] =    "Febuary";    mAbrNames[1] =    "Feb";
  66.     mNames[2] =    "March";    mAbrNames[2] =    "Mar";
  67.     mNames[3] =    "April";    mAbrNames[3] =    "Apr";
  68.     mNames[4] =    "May";        mAbrNames[4] =    "May";
  69.     mNames[5] =    "June";        mAbrNames[5] =    "Jun";
  70.     mNames[6] =    "July";        mAbrNames[6] =    "Jul";
  71.     mNames[7] =    "August";    mAbrNames[7] =    "Aug";
  72.     mNames[8] =    "September";    mAbrNames[8] =    "Sep";
  73.     mNames[9] =    "October";    mAbrNames[9] =    "Oct";
  74.     mNames[10] =    "November";    mAbrNames[10] =    "Nov";
  75.     mNames[11] =    "December";    mAbrNames[11] =    "Dec";
  76.  
  77.     dNames[0] =    "Sunday";    dAbrNames[0] =     "Sun";
  78.     dNames[1] =    "Monday";    dAbrNames[1] =     "Mon";
  79.     dNames[2] =    "Tuesday";    dAbrNames[2] =     "Tue";
  80.     dNames[3] =    "Wednesday";    dAbrNames[3] =     "Wed";
  81.     dNames[4] =    "Thursday";    dAbrNames[4] =     "Thu";
  82.     dNames[5] =    "Friday";    dAbrNames[5] =     "Fri";
  83.     dNames[6] =    "Saturday";    dAbrNames[6] =     "Sat";
  84.  
  85.     return self;
  86. }
  87.  
  88. //--------------------------------
  89.  
  90. - (BOOL) abrMode
  91. {
  92.     return theAbrMode;
  93. }
  94.  
  95. - (BOOL) setAbrMode: (BOOL)mode
  96. {
  97.     return (theAbrMode = mode);
  98. }
  99. - (long) indexOfDayName: (const char *)dayName
  100. {
  101.     [self notImplemented:_cmd];
  102.     return 0;
  103. }
  104.  
  105. - (const char *) nameOfDay: (long)dayIndex
  106. {
  107.     if((dayIndex>=1)&&(dayIndex<=7)){
  108.         if([self abrMode])
  109.             return dAbrNames[dayIndex-1];
  110.         return dNames[dayIndex-1];
  111.     }
  112.     return NULL;
  113. }
  114.  
  115. - (const char *) nameOfDay
  116. {
  117.     return [self nameOfDay:[self dayOfWeek]];
  118. }
  119.  
  120. - (long) indexOfMonthName: (const char *)monthName
  121. {
  122.     [self notImplemented:_cmd];
  123.     return 0;
  124. }
  125.  
  126. - (const char *) nameOfMonth: (long)monthIndex
  127. {
  128.     if((monthIndex>=1)&&(monthIndex<=12)){
  129.         if([self abrMode])
  130.             return mAbrNames[monthIndex-1];
  131.         return mNames[monthIndex-1];
  132.     }
  133.     return mNames[monthIndex];
  134. }
  135.  
  136. - (const char *) nameOfMonth
  137. {
  138.     return [self nameOfMonth:[self month]];
  139. }
  140.  
  141. - (const char *) timeString
  142. {
  143.     char *tmpPtr;
  144.     if( !timeString )
  145.         timeString = NXZoneMalloc( [self zone], SIZE_OF_TIME_STRING );
  146.     tmpPtr = ctime( &myTime );
  147.     bcopy( tmpPtr, timeString, strlen( tmpPtr ));
  148.     return timeString;
  149. }
  150.  
  151. - (const char *) timeZone
  152. {
  153.     if( timesPtr )
  154.         return timesPtr->tm_zone;
  155.     return NULL;
  156. }
  157.  
  158. //--------------------------------
  159.  
  160. - (const time_t *)getTime_t
  161. {
  162.     return &myTime;
  163. }
  164.  
  165. - (BOOL) leapYear: (long)year
  166. {
  167.     return (((year % 4) == 0) && ((year % 400) != 0))?YES:NO;
  168. }
  169.  
  170. - (BOOL) leapYear
  171. {
  172.     return([self leapYear:[self year]]);
  173. }
  174.  
  175. - (long) daysInYear: (long)year
  176. {
  177.     if([self leapYear])
  178.         return 366;
  179.     return 355;
  180. }
  181.  
  182. - (long) daysInYear
  183. {
  184.     return [self daysInYear:[self year]];
  185. }
  186.  
  187. - (long) daysInMonth: (long)monthIndex forYear: (long)year
  188. {
  189. /*30*/    if((monthIndex==4)||
  190.        (monthIndex==6)||
  191.        (monthIndex==9)||
  192.        (monthIndex==11))    return 30;     
  193. /*31*/    if((monthIndex==1)||
  194.        (monthIndex==3)||
  195.        (monthIndex==5)||
  196.        (monthIndex==7)||
  197.        (monthIndex==8)||
  198.        (monthIndex==12))    return 31;
  199. /*28*/    if((monthIndex==2)&&![self leapYear:year])
  200.                 return 28;
  201. /*29*/    else            return 29;
  202.     return 0;
  203. }
  204.  
  205. - (long) daysInMonth: (long)monthIndex
  206. {
  207.     return [self daysInMonth:monthIndex forYear:[self year]];
  208. }
  209.  
  210. - (long) daysInMonth
  211. {
  212.     return [self daysInMonth:[self month] forYear:[self year]];
  213. }
  214.  
  215. - (long) dayOfMonth
  216. {
  217.     if( timesPtr )
  218.         return timesPtr->tm_mday + 1;
  219.     else
  220.         return 0;
  221. }
  222.  
  223. - (long) dayOfWeek
  224. {
  225.     if( timesPtr )
  226.         return timesPtr->tm_wday + 1;
  227.     else
  228.         return 0;
  229. }
  230.  
  231. - (long) weekOfMonth
  232. {
  233.     if( timesPtr )
  234.         return (timesPtr->tm_mday / 7) + 1;
  235.     return 0;
  236. }
  237.  
  238. - (long) dayOfYear
  239. {
  240.     return [self day];
  241. }
  242.  
  243. - (long) year
  244. {
  245.     if( timesPtr )
  246.         return timesPtr->tm_year + 1900;
  247.     return 0;
  248. }
  249.  
  250. - (long) month
  251. {
  252.     if( timesPtr )
  253.         return timesPtr->tm_mon + 1;
  254.     return 0;
  255. }
  256.  
  257. - (long) week
  258. {
  259.     if( timesPtr )
  260.         return (timesPtr->tm_yday / 7) + 1;
  261.     return 0;
  262. }
  263.  
  264. - (long) day
  265. {
  266.     if( timesPtr )
  267.         return timesPtr->tm_yday + 1;
  268.     return 0;
  269. }
  270.  
  271. - (long) hour
  272. {
  273.     if( timesPtr )
  274.         return timesPtr->tm_hour;
  275.     return 0;
  276. }
  277.  
  278. - (long) minute
  279. {
  280.     if( timesPtr )
  281.         return timesPtr->tm_min;
  282.     return 0;
  283. }
  284.  
  285. - (long) second
  286. {
  287.     if( timesPtr )
  288.         return timesPtr->tm_sec;
  289.     return 0;
  290. }
  291.  
  292. - (long) microSecond
  293. {
  294.     [self notImplemented:_cmd];
  295.     return 0;
  296. }
  297.  
  298. //--------------------------------
  299.  
  300. - _syncTimesStruct
  301. {
  302.     struct tm *tmpTimesPtr;
  303.     
  304.     tmpTimesPtr = localtime( &myTime);
  305.  
  306.     if( !timesPtr )
  307.         timesPtr = NXZoneMalloc( [self zone],
  308.             sizeof( struct tm ) );
  309.     
  310.     if( timesPtr )
  311.         *timesPtr = *tmpTimesPtr;
  312.  
  313.     return self;
  314. }
  315.  
  316. - resetTimeFromTM
  317. {
  318.     myTime = mktime( timesPtr );
  319.     return self;
  320. }
  321.  
  322. - setTime_t:(time_t)num
  323. {
  324.     myTime = num;
  325.     [self _syncTimesStruct];
  326.     return self;
  327. }
  328.  
  329. - setToCurrentTime
  330. {
  331.     struct timezone tzp;
  332.     struct timeval curTime;
  333.  
  334.     if( gettimeofday( &curTime, &tzp) == -1 )
  335.     {
  336.         /* call failed */
  337.         return self;
  338.     }
  339.     [self setTime_t:curTime.tv_sec];
  340.     return self;
  341. }
  342.  
  343. - setTime:(Time *)aTimeObj
  344. {
  345.     if( timesPtr )
  346.     {
  347.         myTime = *[aTimeObj getTime_t];
  348.         [self resetTimeFromTM];
  349.     }
  350.     return self;
  351. }
  352.  
  353. - setYears:(long)num
  354. {
  355.     if( timesPtr )
  356.     {
  357.         timesPtr->tm_year = num - 1900;
  358.         [self resetTimeFromTM];
  359.     }
  360.     return self;
  361. }
  362.  
  363. - setMonths:(long)num
  364. {
  365.     if( timesPtr )
  366.     {
  367.         timesPtr->tm_mon = num - 1;
  368.         [self resetTimeFromTM];
  369.     }
  370.     return self;
  371. }
  372.  
  373. - setWeeks:(long)num
  374. {
  375.     [self setDays:(num-1)*7+[self dayOfWeek]];
  376.     return self;
  377. }
  378.  
  379. /*
  380. ????    tm_yday and tm_wday are ignored with mktime()
  381.     reset mon to 0 then set mday to num
  382. --->    Use the _sync.. method and just use seconds
  383.     (above method don't work?)
  384. */
  385. - setDays:(long)num
  386. {
  387.     if( timesPtr )
  388.     {
  389.         myTime = myTime + (num - [self day])*86400;
  390.         [self _syncTimesStruct];
  391.     }
  392.     return self;
  393. }
  394.  
  395. - setHours:(long)num
  396. {
  397.     if( timesPtr )
  398.     {
  399.         timesPtr->tm_hour = num;
  400.         [self resetTimeFromTM];
  401.     }
  402.     return self;
  403. }
  404.  
  405. - setMinutes:(long)num
  406. {
  407.     if( timesPtr )
  408.     {
  409.         timesPtr->tm_min = num;
  410.         [self resetTimeFromTM];
  411.     }
  412.     return self;
  413. }
  414.  
  415. - setSeconds: (long)numSeconds microSeconds: (long)numMicroSeconds
  416. {
  417.     if( timesPtr )
  418.     {
  419.         timesPtr->tm_sec = numSeconds;
  420.         [self resetTimeFromTM];
  421.     }
  422.     return self;
  423. }
  424.  
  425. - setSeconds: (long)numSeconds
  426. {
  427.     [self setSeconds:numSeconds microSeconds:0];
  428.     return self;
  429. }
  430.  
  431. - setMicroSeconds:(long)num
  432. {
  433.     [self notImplemented:_cmd];
  434.     return self;
  435. }
  436.  
  437. //--------------------------------
  438.  
  439. - addTime: (Time *)aTimeObj
  440. {
  441.     [self setTime_t:*[self getTime_t] + *[aTimeObj getTime_t]];
  442.     return self;
  443. }
  444.  
  445. - addTime_t: (time_t)num
  446. {
  447.     [self setTime_t:*[self getTime_t] + num];
  448.     return self;
  449. }
  450.  
  451. - addYears: (unsigned)num
  452. {
  453.     [self setYears:[self year] + num];
  454.     return self;
  455. }
  456.  
  457. - addMonths: (unsigned)num
  458. {
  459.     [self setMonths:[self month] + num];
  460.     return self;
  461. }
  462.  
  463. - addWeeks: (unsigned)num
  464. {
  465.     [self addDays:num*7];
  466.     return self;
  467. }
  468.  
  469. - addDays: (unsigned)num
  470. {
  471.     [self setDays:[self day] + num];
  472.     return self;
  473. }
  474.  
  475. - addHours: (unsigned)num
  476. {
  477.     [self setHours:[self hour] + num];
  478.     return self;
  479. }
  480.  
  481. - addMinutes: (unsigned)num
  482. {
  483.     [self setMinutes:[self minute] + num];
  484.     return self;
  485. }
  486.  
  487. - addSeconds: (unsigned)num
  488. {
  489.     [self setSeconds:[self second] + num];
  490.     return self;
  491. }
  492.  
  493. - addMicroSeconds: (unsigned)num
  494. {
  495.     [self setMicroSeconds:[self microSecond] + num];
  496.     return self;
  497. }
  498.  
  499. //--------------------------------
  500.  
  501. - subtractTime: (Time *)aTimeObj
  502. {
  503.     [self setTime_t:*[self getTime_t] - *[aTimeObj getTime_t]];
  504.     return self;
  505. }
  506.  
  507. - subtractTime_t: (time_t)num
  508. {
  509.     [self setTime_t:*[self getTime_t] - num];
  510.     return self;
  511. }
  512.  
  513. - subtractYears: (unsigned)num
  514. {
  515.     [self setYears:[self year] - num];
  516.     return self;
  517. }
  518.  
  519. - subtractMonths: (unsigned)num
  520. {
  521.     [self setMonths:[self month] - num];
  522.     return self;
  523. }
  524.  
  525. - subtractWeeks: (unsigned)num
  526. {
  527.     [self subtractDays:num*7];
  528.     return self;
  529. }
  530.  
  531. - subtractDays: (unsigned)num
  532. {
  533.     [self setDays:[self day] - num];
  534.     return self;
  535. }
  536.  
  537. - subtractHours: (unsigned)num
  538. {
  539.     [self setHours:[self hour] - num];
  540.     return self;
  541. }
  542.  
  543. - subtractMinutes: (unsigned)num
  544. {
  545.     [self setMinutes:[self minute] - num];
  546.     return self;
  547. }
  548.  
  549. - subtractSeconds: (unsigned)num
  550. {
  551.     [self setSeconds:[self second] - num];
  552.     return self;
  553. }
  554.  
  555. - subtractMicroSeconds: (unsigned)num
  556. {
  557.     [self setMicroSeconds:[self microSecond] - num];
  558.     return self;
  559. }
  560.  
  561. @end