xtime.h 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. /*
  2. ** Command & Conquer Renegade(tm)
  3. ** Copyright 2025 Electronic Arts Inc.
  4. **
  5. ** This program is free software: you can redistribute it and/or modify
  6. ** it under the terms of the GNU General Public License as published by
  7. ** the Free Software Foundation, either version 3 of the License, or
  8. ** (at your option) any later version.
  9. **
  10. ** This program is distributed in the hope that it will be useful,
  11. ** but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. ** GNU General Public License for more details.
  14. **
  15. ** You should have received a copy of the GNU General Public License
  16. ** along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. /****************************************************************************\
  19. xtime Neal Kettler
  20. This is version 2 of the Wtime library (now xtime). It now supports
  21. time storage from the year 0 to well after the sun
  22. will have gone supernova (OK, OK I admit it'll break in the
  23. year 5 Million.)
  24. The call to update the current time will break in 2038.
  25. Hopefully by then somebody will replace the lame time()
  26. function :-)
  27. \****************************************************************************/
  28. #ifndef XTIME_HEADER
  29. #define XTIME_HEADER
  30. #include <stdio.h>
  31. #include <stdlib.h>
  32. #include <assert.h>
  33. #include <sys/types.h>
  34. #ifndef _WINDOWS
  35. #include <unistd.h>
  36. #include <netinet/in.h>
  37. #include <sys/time.h>
  38. #else
  39. #include <sys/timeb.h>
  40. #include <winsock.h>
  41. #endif
  42. #include <time.h>
  43. #include <string.h>
  44. #include "wstypes.h"
  45. class Xtime
  46. {
  47. public:
  48. Xtime(); // init to system time
  49. Xtime( Xtime &other );
  50. Xtime( time_t other ); // 1970-2038
  51. ~Xtime();
  52. void addSeconds(sint32 seconds);
  53. bit8 getTime(int &month, int &mday, int &year, int &hour, int &minute,
  54. int &second) const;
  55. bit8 setTime(int month, int mday, int year, int hour, int minute,
  56. int second);
  57. void update(); // Update members sec & usec to system time
  58. // This will break after 2038
  59. /********
  60. void PrintTime(FILE *out) const;
  61. void PrintTime(char *out) const;
  62. void PrintDate(FILE *out) const;
  63. void PrintDate(char *out) const;
  64. **********/
  65. sint32 getDay(void) const; // Get days since year 0
  66. sint32 getMsec(void) const; // Get milliseconds into the day
  67. void setDay(sint32 day);
  68. void setMsec(sint32 msec);
  69. void set(sint32 newday, sint32 newmsec);
  70. bit8 ParseDate(char *in);
  71. bit8 FormatTime(char *out, char *format);
  72. bit8 getTimeval(struct timeval &tv);
  73. // All of these may return -1 if the time is invalid
  74. int getSecond(void) const; // Second (0-60) (60 is for a leap second)
  75. int getMinute(void) const; // Minute (0-59)
  76. int getHour(void) const; // Hour (0-23)
  77. int getMDay(void) const; // Day of Month (1-31)
  78. int getWDay(void) const; // Day of Week (1-7)
  79. int getYDay(void) const; // Day of Year (1-366) (366 = leap yr)
  80. int getMonth(void) const; // Month (1-12)
  81. int getYWeek(void) const; // Week of Year (1-53)
  82. int getYear(void) const; // Year (e.g. 1997)
  83. // Modify the time components. Return FALSE if fail
  84. bit8 setSecond(sint32 sec);
  85. bit8 setMinute(sint32 min);
  86. bit8 setHour(sint32 hour);
  87. bit8 setYear(sint32 year);
  88. bit8 setMonth(sint32 month);
  89. bit8 setMDay(sint32 mday);
  90. void normalize(void); // move msec overflows to the day
  91. // Compare two times
  92. int compare(const Xtime &other) const;
  93. // comparisons
  94. bit8 operator == ( const Xtime &other ) const;
  95. bit8 operator != ( const Xtime &other ) const;
  96. bit8 operator < ( const Xtime &other ) const;
  97. bit8 operator > ( const Xtime &other ) const;
  98. bit8 operator <= ( const Xtime &other ) const;
  99. bit8 operator >= ( const Xtime &other ) const;
  100. // assignments
  101. Xtime &operator = (const Xtime &other);
  102. Xtime &operator = (const time_t other);
  103. // signed
  104. Xtime &operator += (const Xtime &other);
  105. Xtime &operator -= (const Xtime &other);
  106. Xtime operator + (Xtime &other);
  107. Xtime operator - (Xtime &other);
  108. Xtime &operator += (const time_t other);
  109. Xtime &operator -= (const time_t other);
  110. Xtime operator + (time_t other);
  111. Xtime operator - (time_t other);
  112. protected:
  113. sint32 day_; // days since Jan 1, 0
  114. sint32 msec_; // milliseconds (thousandths of a sec)
  115. };
  116. #endif