wtime.h 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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. wtime Neal Kettler
  20. \****************************************************************************/
  21. #ifndef WTIME_HEADER
  22. #define WTIME_HEADER
  23. #include <stdio.h>
  24. #include <stdlib.h>
  25. #include <assert.h>
  26. #include <sys/types.h>
  27. #ifndef _WINDOWS
  28. #include <unistd.h>
  29. #include <netinet/in.h>
  30. #include <sys/time.h>
  31. #else
  32. #include <sys/timeb.h>
  33. #include <winsock.h>
  34. #endif
  35. #include <time.h>
  36. #include <string.h>
  37. #include "wlib/wstypes.h"
  38. class Wtime
  39. {
  40. public:
  41. enum
  42. {
  43. POSITIVE=0,
  44. NEGATIVE=1
  45. };
  46. Wtime(); // init to system time
  47. Wtime( Wtime &other );
  48. Wtime( uint32 other );
  49. ~Wtime();
  50. void Update(); // Update members sec & usec to system time
  51. void PrintTime(FILE *out) const;
  52. void PrintTime(char *out) const;
  53. void PrintDate(FILE *out) const;
  54. void PrintDate(char *out) const;
  55. uint32 GetSec(void) const; // Get member variable 'sec'
  56. uint32 GetUsec(void) const; // Get member variable 'usec'
  57. void SetSec(uint32 newsec);
  58. void SetUsec(uint32 newusec);
  59. void Set(uint32 newsec,uint32 newusec);
  60. bit8 ParseDate(char *in);
  61. bit8 FormatTime(char *out, char *format);
  62. struct timeval *GetTimeval(void);
  63. void GetTimevalMT(struct timeval &tv);
  64. uint32 GetSecond(void) const; // Second (0- 60) (60 is for a leap second)
  65. uint32 GetMinute(void) const; // Minute (0 - 59)
  66. uint32 GetHour(void) const; // Hour (0-23)
  67. uint32 GetMDay(void) const; // Day of Month (1-31)
  68. uint32 GetWDay(void) const; // Day of Week (1-7)
  69. uint32 GetYDay(void) const; // Day of Year (1-366)
  70. uint32 GetMonth(void) const; // Month (1-12)
  71. uint32 GetYWeek(void) const; // Week of Year (1-53)
  72. uint32 GetYear(void) const; // Year (e.g. 1997)
  73. bit8 GetSign(void) const; // 0 = pos 1 = neg
  74. int Compare(const Wtime &other) const;
  75. // comparisons
  76. bit8 operator == ( const Wtime &other ) const;
  77. bit8 operator != ( const Wtime &other ) const;
  78. bit8 operator < ( const Wtime &other ) const;
  79. bit8 operator > ( const Wtime &other ) const;
  80. bit8 operator <= ( const Wtime &other ) const;
  81. bit8 operator >= ( const Wtime &other ) const;
  82. // assignments
  83. Wtime &operator = (const Wtime &other);
  84. Wtime &operator = (const uint32 other);
  85. // math
  86. // signed
  87. void SignedAdd(const Wtime &other);
  88. void SignedSubtract(const Wtime &other);
  89. // unsigned
  90. Wtime &operator += (const Wtime &other);
  91. Wtime &operator -= (const Wtime &other);
  92. Wtime operator + (Wtime &other);
  93. Wtime operator - (Wtime &other);
  94. Wtime &operator += (const uint32 other);
  95. Wtime &operator -= (const uint32 other);
  96. Wtime operator + (uint32 other);
  97. Wtime operator - (uint32 other);
  98. protected:
  99. uint32 sec; // seconds since Jan 1, 1970
  100. uint32 usec; // microseconds (millionths of a second)
  101. bit8 sign; // for time differences 0 = pos 1 = neg
  102. };
  103. #endif