home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / ool.zip / OOL / source / xtime.cpp < prev    next >
C/C++ Source or Header  |  1997-03-18  |  4KB  |  180 lines

  1. #include "xtime.h"
  2. #include "xstring.h"
  3. #include "time.h"
  4. #include "xdate.h"
  5.  
  6.  
  7. /*@ XTime::operator ==(const XTime &ti)
  8. @group operators
  9. @remarks Compare two XTimes
  10. */
  11.  
  12. /*@ XTime::operator !=(const XTime &ti)
  13. @group operators
  14. @remarks Compare two XTimes
  15. */
  16.  
  17.  
  18. /*@ XTime::operator =(const XTime &ti)
  19. @group operators
  20. @remarks Copy the current time to another XTime instance
  21. */
  22.  
  23.  
  24. /*@ XTime :: XTime(UCHAR hours, UCHAR minutes, UCHAR seconds)
  25. @group constructors/desructors
  26. @remarks Construct a XTime, if no parameters are used all values are zero, call GetCurrentTime() to get the current time.
  27. @parameters: UCHAR hours<BR>
  28.             UCHAR minutes<BR>
  29.             UCHAR seconds
  30. */
  31. XTime :: XTime(UCHAR hours, UCHAR minutes, UCHAR seconds)
  32. {
  33.     t.hours = hours;
  34.     t.minutes = minutes;
  35.     t.seconds = seconds;
  36.     t.dayLightSave = FALSE;
  37. }
  38.  
  39.  
  40. /*@ XTime::Compare(const XTime * ti)
  41. @group misc
  42. @remarks Compares the time stored with another time
  43. @parameters: XTime * time
  44. @returns  <t '°' c=2>
  45.                 °SHORT result      °-1 the time stored is earlier<BR>
  46.                             0 the times are equal<BR>
  47.                             1 the time stored is later<BR>
  48.             </t>
  49. */
  50. SHORT XTime::Compare(const XTime * ti) const
  51. {
  52.     if (t.hours < ti->t.hours)
  53.         return -1;
  54.     if (t.hours > ti->t.hours)
  55.         return 1;
  56.     if (t.minutes < ti->t.minutes)
  57.         return -1;
  58.     if (t.minutes > ti->t.minutes)
  59.         return 1;
  60.     if (t.seconds < ti->t.seconds)
  61.         return -1;
  62.     return (t.seconds > ti->t.seconds ? 1 : 0);
  63. }
  64.  
  65.  
  66. /*@ XTime::GetCurrentTime(void)
  67. @group misc
  68. @remarks Query the current time.
  69. */
  70. void XTime::GetCurrentTime(void)
  71. {
  72.     tm *ti;
  73.     time_t ltime;
  74.  
  75.     time(<ime);
  76.     ti = localtime(<ime);
  77.  
  78.     t.hours = ti->tm_hour;
  79.     t.minutes = ti->tm_min;
  80.     t.seconds = ti->tm_sec;
  81.     t.dayLightSave = ti->tm_isdst;
  82. }
  83.  
  84.  
  85. /*@ XTime::Format(XString * buffer, const char *format)
  86. @group misc
  87. @remarks Print the date formated in a string
  88. @parameters: XString * buffer    buffer which will hold data<BR>
  89.             char * format       string which holds the format to use, see strftime() for valid format attributes
  90. */
  91. void XTime::Format(XString * buffer, const char *format)
  92. {
  93.     tm  td;
  94.  
  95.     td.tm_sec = t.seconds;
  96.     td.tm_min = t.minutes;
  97.     td.tm_hour = t.hours;
  98.     td.tm_mday = 0;
  99.     td.tm_mon = 0;
  100.     td.tm_year = 0;
  101.     td.tm_wday = 0;
  102.     td.tm_yday = 0;
  103.     td.tm_isdst = t.dayLightSave;
  104.  
  105.     size_t l = strftime(buffer->GetBuffer(512), 512, format, &td);
  106.  
  107.     buffer->ReleaseBuffer(l);
  108. }
  109.  
  110.  
  111. XDate :: XDate(UCHAR day, UCHAR month, SHORT year, UCHAR weekD, USHORT dayOfYear)
  112. {
  113.     d.days = day;
  114.     d.months = month;
  115.     d.years = year;
  116.     d.weekday = weekD;
  117.     d.dayOfYear = dayOfYear;
  118. }
  119.  
  120.  
  121. SHORT XDate::Compare(const XDate * date) const
  122. {
  123.     if (d.years < date->d.years)
  124.         return -1;
  125.     if (d.years > date->d.years)
  126.         return 1;
  127.     if (d.months < date->d.months)
  128.         return -1;
  129.     if (d.months > date->d.months)
  130.         return 1;
  131.     if (d.days < date->d.days)
  132.         return -1;
  133.     return (d.days > date->d.days ? 1 : 0);
  134. }
  135.  
  136.  
  137. /*@ XDate::GetCurrentDate(void)
  138. @group misc
  139. @remarks Query the current date from the system
  140. */
  141. void XDate::GetCurrentDate(void)
  142. {
  143.     tm *t;
  144.     time_t ltime;
  145.  
  146.     time(<ime);
  147.     t = localtime(<ime);
  148.     d.days = t->tm_mday;
  149.     d.months = t->tm_mon;
  150.     d.years = t->tm_year + 1900;
  151.     d.weekday = t->tm_wday;
  152.     d.dayOfYear = t->tm_yday;
  153. }
  154.  
  155.  
  156. /*@ XDate::Format(XString * buffer, const char *format)
  157. @group misc
  158. @remarks Print the date into a string
  159. @parameters XString * buffer        string which get the date<BR>
  160.                 char * format            format, see strftime() for details
  161. */
  162. void XDate::Format(XString * buffer, const char *format)
  163. {
  164.     tm  td;
  165.  
  166.     td.tm_sec = 0;
  167.     td.tm_min = 0;
  168.     td.tm_hour = 0;
  169.     td.tm_mday = d.days;
  170.     td.tm_mon = d.months;
  171.     td.tm_year = d.years - 1900;
  172.     td.tm_wday = d.weekday;
  173.     td.tm_yday = d.dayOfYear;
  174.     td.tm_isdst = 0;
  175.  
  176.     size_t l = strftime(buffer->GetBuffer(512), 512, format, &td);
  177.  
  178.     buffer->ReleaseBuffer(l);
  179. }
  180.