home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / octa21fs.zip / octave / octave-2.1.23 / liboctave / oct-time.h < prev    next >
C/C++ Source or Header  |  2000-01-15  |  6KB  |  305 lines

  1. /*
  2.  
  3. Copyright (C) 1999 John W. Eaton
  4.  
  5. This file is part of Octave.
  6.  
  7. Octave is free software; you can redistribute it and/or modify it
  8. under the terms of the GNU General Public License as published by the
  9. Free Software Foundation; either version 2, or (at your option) any
  10. later version.
  11.  
  12. Octave is distributed in the hope that it will be useful, but WITHOUT
  13. ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  14. FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  15. for more details.
  16.  
  17. You should have received a copy of the GNU General Public License
  18. along with Octave; see the file COPYING.  If not, write to the Free
  19. Software Foundation, 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
  20.  
  21. */
  22.  
  23. #if !defined (octave_time_h)
  24. #define octave_time_h 1
  25.  
  26. #include <cmath>
  27.  
  28. #include <string>
  29.  
  30. #include "systime.h"
  31.  
  32. class octave_base_tm;
  33.  
  34. class
  35. octave_time
  36. {
  37. public:
  38.  
  39.   octave_time (void)
  40.     : ot_unix_time (0), ot_usec (0) { stamp (); }
  41.  
  42.   octave_time (time_t t)
  43.     : ot_unix_time (t), ot_usec (0) { }
  44.  
  45.   octave_time (double d)
  46.     : ot_unix_time (static_cast<time_t> (d)), ot_usec (0)
  47.   {
  48.     double ip;
  49.     ot_usec = static_cast<int> (modf (d, &ip) * 1e6);
  50.   }
  51.  
  52.   octave_time (const octave_base_tm& tm);
  53.  
  54.   octave_time (const octave_time& ot)
  55.     : ot_unix_time (ot.ot_unix_time), ot_usec (ot.ot_usec) { }
  56.  
  57.   octave_time& operator = (const octave_time& ot)
  58.   {
  59.     if (this != &ot)
  60.       {
  61.     ot_unix_time = ot.ot_unix_time;
  62.     ot_usec = ot.ot_usec;
  63.       }
  64.  
  65.     return *this;
  66.   }
  67.  
  68.   ~octave_time (void) { }
  69.  
  70.   void stamp (void);
  71.  
  72.   operator double () const { return ot_unix_time + ot_usec / 1e6; }
  73.  
  74.   operator time_t () const { return ot_unix_time; }
  75.  
  76.   time_t unix_time (void) const { return ot_unix_time; }
  77.  
  78.   int usec (void) const { return ot_usec; }
  79.  
  80.   string ctime (void) const;
  81.  
  82. private:
  83.  
  84.   // Seconds since the epoch.
  85.   time_t ot_unix_time;
  86.  
  87.   // Additional microseconds.
  88.   int ot_usec;
  89. };
  90.  
  91. inline bool
  92. operator == (const octave_time& t1, const octave_time& t2)
  93. {
  94.   return (t1.unix_time () == t2.unix_time () && t1.usec () == t2.usec ());
  95. }
  96.  
  97. inline bool
  98. operator != (const octave_time& t1, const octave_time& t2)
  99. {
  100.   return ! (t1 == t2);
  101. }
  102.  
  103. inline bool
  104. operator < (const octave_time& t1, const octave_time& t2)
  105. {
  106.   if (t1.unix_time () < t2.unix_time ())
  107.     return true;
  108.   else if (t1.unix_time () > t2.unix_time ())
  109.     return false;
  110.   else if (t1.usec () < t2.usec ())
  111.     return true;
  112.   else
  113.     return false;
  114. }
  115.  
  116. inline bool
  117. operator <= (const octave_time& t1, const octave_time& t2)
  118. {
  119.   return (t1 < t2 || t1 == t2);
  120. }
  121.  
  122. inline bool
  123. operator > (const octave_time& t1, const octave_time& t2)
  124. {
  125.   if (t1.unix_time () > t2.unix_time ())
  126.     return true;
  127.   else if (t1.unix_time () < t2.unix_time ())
  128.     return false;
  129.   else if (t1.usec () > t2.usec ())
  130.     return true;
  131.   else
  132.     return false;
  133. }
  134.  
  135. inline bool
  136. operator >= (const octave_time& t1, const octave_time& t2)
  137. {
  138.   return (t1 > t2 || t1 == t2);
  139. }
  140.  
  141. class
  142. octave_base_tm
  143. {
  144. public:
  145.  
  146.   octave_base_tm (void)
  147.     : tm_usec (0), tm_sec (0), tm_min (0), tm_hour (0),
  148.       tm_mday (0), tm_mon (0), tm_year (0), tm_wday (0),
  149.       tm_yday (0), tm_isdst (0), tm_zone ("unknown")
  150.   { }
  151.  
  152.   octave_base_tm (const octave_base_tm& tm)
  153.     : tm_usec (tm.tm_usec), tm_sec (tm.tm_sec), tm_min (tm.tm_min),
  154.       tm_hour (tm.tm_hour), tm_mday (tm.tm_mday), tm_mon (tm.tm_mon),
  155.       tm_year (tm.tm_year), tm_wday (tm.tm_wday), tm_yday (tm.tm_yday),
  156.       tm_isdst (tm.tm_isdst), tm_zone (tm.tm_zone)
  157.   { }
  158.  
  159.   octave_base_tm& operator = (const octave_base_tm& tm)
  160.   {
  161.     if (this != &tm)
  162.       {
  163.     tm_usec = tm.tm_usec;
  164.     tm_sec = tm.tm_sec;
  165.     tm_min = tm.tm_min;
  166.     tm_hour = tm.tm_hour;
  167.     tm_mday = tm.tm_mday;
  168.     tm_mon = tm.tm_mon;
  169.     tm_year = tm.tm_year;
  170.     tm_wday = tm.tm_wday;
  171.     tm_yday = tm.tm_yday;
  172.     tm_isdst = tm.tm_isdst;
  173.     tm_zone = tm.tm_zone;
  174.       }
  175.  
  176.     return *this;
  177.   }
  178.  
  179.   virtual ~octave_base_tm (void) { }
  180.  
  181.   int usec (void) const { return tm_usec; }
  182.   int sec (void) const { return tm_sec; }
  183.   int min (void) const { return tm_min; }
  184.   int hour (void) const { return tm_hour; }
  185.   int mday (void) const { return tm_mday; }
  186.   int mon (void) const { return tm_mon; }
  187.   int year (void) const { return tm_year; }
  188.   int wday (void) const { return tm_wday; }
  189.   int yday (void) const { return tm_yday; }
  190.   int isdst (void) const { return tm_isdst; }
  191.   string zone (void) const { return tm_zone; }
  192.  
  193.   octave_base_tm& usec (int v);
  194.   octave_base_tm& sec (int v);
  195.   octave_base_tm& min (int v);
  196.   octave_base_tm& hour (int v);
  197.   octave_base_tm& mday (int v);
  198.   octave_base_tm& mon (int v);
  199.   octave_base_tm& year (int v);
  200.   octave_base_tm& wday (int v);
  201.   octave_base_tm& yday (int v);
  202.   octave_base_tm& isdst (int v);
  203.   octave_base_tm& zone (const string& s);
  204.  
  205.   string strftime (const string& fmt) const;
  206.  
  207.   string asctime (void) const { return strftime ("%a %b %d %H:%M:%S %Y\n"); }
  208.  
  209. protected:
  210.  
  211.   // Microseconds after the second (0, 999999).
  212.   int tm_usec;
  213.  
  214.   // Seconds after the minute (0, 61).
  215.   int tm_sec;
  216.  
  217.   // Minutes after the hour (0, 59).
  218.   int tm_min;
  219.  
  220.   // Hours since midnight (0, 23).
  221.   int tm_hour;
  222.  
  223.   // Day of the month (1, 31).
  224.   int tm_mday;
  225.  
  226.   // Months since January (0, 11).
  227.   int tm_mon;
  228.  
  229.   // Years since 1900.
  230.   int tm_year;
  231.  
  232.   // Days since Sunday (0, 6).
  233.   int tm_wday;
  234.  
  235.   // Days since January 1 (0, 365).
  236.   int tm_yday;
  237.  
  238.   // Daylight Savings Time flag.
  239.   int tm_isdst;
  240.  
  241.   // Time zone.
  242.   string tm_zone;
  243.  
  244.   void init (void *p);
  245. };
  246.  
  247. class
  248. octave_localtime : public octave_base_tm
  249. {
  250. public:
  251.  
  252.   octave_localtime (void)
  253.     : octave_base_tm () { init (octave_time ()); }
  254.  
  255.   octave_localtime (const octave_time& ot)
  256.     : octave_base_tm () { init (ot); }
  257.  
  258.   octave_localtime (const octave_localtime& t)
  259.     : octave_base_tm (t) { }
  260.  
  261.   octave_localtime& operator = (const octave_localtime& t)
  262.   {
  263.     octave_base_tm::operator = (t);
  264.     return *this;
  265.   }
  266.  
  267.   ~octave_localtime (void) { }
  268.  
  269. private:
  270.  
  271.   void init (const octave_time& ot);
  272. };
  273.  
  274. class
  275. octave_gmtime : public octave_base_tm
  276. {
  277. public:
  278.  
  279.   octave_gmtime (void)
  280.     : octave_base_tm () { init (octave_time ()); }
  281.  
  282.   octave_gmtime (const octave_time& ot)
  283.     : octave_base_tm () { init (ot); }
  284.  
  285.   octave_gmtime& operator = (const octave_gmtime& t)
  286.   {
  287.     octave_base_tm::operator = (t);
  288.     return *this;
  289.   }
  290.  
  291.   ~octave_gmtime (void) { }
  292.  
  293. private:
  294.  
  295.   void init (const octave_time& ot);
  296. };
  297.  
  298. #endif
  299.  
  300. /*
  301. ;;; Local Variables: ***
  302. ;;; mode: C++ ***
  303. ;;; End: ***
  304. */
  305.