home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / snip9707.zip / LOCTM.CPP < prev    next >
Text File  |  1997-07-05  |  1KB  |  72 lines

  1. // +++Date last modified: 05-Jul-1997
  2.  
  3. //
  4. // loctm.cpp
  5. // C++ class wrapper for ANSI-C struct tm
  6. //
  7. // written by David Nugent
  8. //
  9. // This code is public domain. Use for any purpose is unrestricted.
  10. //
  11.  
  12. # include "loctm.h"
  13.  
  14. char loc_tm::timech = ':';
  15. char loc_tm::datech = '/';
  16.  
  17. int loc_tm::datefmt = loc_tm::d_IntlShort;
  18. int loc_tm::timefmt = loc_tm::t_International;
  19.  
  20.  
  21. loc_tm::loc_tm (time_t t)
  22. {
  23.     if (t == 0)
  24.         t = time(0);
  25.     *(tm *)this = *localtime(&t);
  26. }
  27.  
  28. loc_tm::loc_tm (tm const & t)
  29. {
  30.     *((tm *)this) = t;
  31. }
  32.  
  33. loc_tm &
  34. loc_tm::operator= (tm const & t)
  35. {
  36.     *((tm *)this) = t;
  37.     return *this;
  38. }
  39.  
  40. int
  41. loc_tm::compare (loc_tm const & t) const
  42. {
  43.     return compare ((time_t)t);
  44. }
  45.  
  46. int
  47. loc_tm::compare (time_t const tt) const
  48. {
  49.     time_t tx = (time_t)*this;
  50.     return (tx == tt) ? 0 : (tx > tt) ? 1 : -1;
  51. }
  52.  
  53. loc_tm::operator time_t (void) const
  54. {
  55.     return mktime ((tm *)this);
  56. }
  57.  
  58. int
  59. loc_tm::is_valid (void) const
  60. {
  61.     static int maxd[] =
  62.     {
  63.         31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31
  64.     };
  65.  
  66.     return (tm_year > 0 &&
  67.             tm_mon >= 0 && tm_mon < 12 &&
  68.             tm_mday > 0 && tm_mday <= maxd[tm_mon] &&
  69.             tm_wday < 7 && tm_yday < 367 &&
  70.             tm_sec < 60 && tm_min < 60 && tm_hour < 24);
  71. }
  72.