Richard Boegli's CnC_Generals_Zero_Hour Fork WIP
This is documentation of Richard Boegil's Zero Hour Fork
 
Loading...
Searching...
No Matches
xtime.h
Go to the documentation of this file.
1/*
2** Command & Conquer Generals Zero Hour(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/****************************************************************************\
20xtime Neal Kettler
21
22This is version 2 of the Wtime library (now xtime). It now supports
23time storage from the year 0 to well after the sun
24will have gone supernova (OK, OK I admit it'll break in the
25year 5 Million.)
26
27The call to update the current time will break in 2038.
28Hopefully by then somebody will replace the lame time()
29function :-)
30\****************************************************************************/
31
32#ifndef XTIME_HEADER
33#define XTIME_HEADER
34
35
36#include <stdio.h>
37#include <stdlib.h>
38#include <assert.h>
39#include <sys/types.h>
40
41#ifndef _WINDOWS
42#include <unistd.h>
43#include <netinet/in.h>
44#include <sys/time.h>
45#else
46#include <sys/timeb.h>
47#include <winsock.h>
48#endif
49
50#include <time.h>
51#include <string.h>
52
53#include "wstypes.h"
54
55class Xtime
56{
57 public:
58
59 Xtime(); // init to system time
60 Xtime( Xtime &other );
61 Xtime( time_t other ); // 1970-2038
62 ~Xtime();
63
64 void addSeconds(sint32 seconds);
65
66 bit8 getTime(int &month, int &mday, int &year, int &hour, int &minute,
67 int &second) const;
68
69 bit8 setTime(int month, int mday, int year, int hour, int minute,
70 int second);
71
72 void update(); // Update members sec & usec to system time
73 // This will break after 2038
74
75/********
76 void PrintTime(FILE *out) const;
77 void PrintTime(char *out) const;
78 void PrintDate(FILE *out) const;
79 void PrintDate(char *out) const;
80**********/
81
82 sint32 getDay(void) const; // Get days since year 0
83 sint32 getMsec(void) const; // Get milliseconds into the day
84
85 void setDay(sint32 day);
86 void setMsec(sint32 msec);
87
88 void set(sint32 newday, sint32 newmsec);
89 bit8 ParseDate(char *in);
90 bit8 FormatTime(char *out, char *format);
91
92 bit8 getTimeval(struct timeval &tv);
93
94 // All of these may return -1 if the time is invalid
95 int getSecond(void) const; // Second (0-60) (60 is for a leap second)
96 int getMinute(void) const; // Minute (0-59)
97 int getHour(void) const; // Hour (0-23)
98 int getMDay(void) const; // Day of Month (1-31)
99 int getWDay(void) const; // Day of Week (1-7)
100 int getYDay(void) const; // Day of Year (1-366) (366 = leap yr)
101 int getMonth(void) const; // Month (1-12)
102 int getYWeek(void) const; // Week of Year (1-53)
103 int getYear(void) const; // Year (e.g. 1997)
104
105 // Modify the time components. Return FALSE if fail
106 bit8 setSecond(sint32 sec);
108 bit8 setHour(sint32 hour);
109 bit8 setYear(sint32 year);
110 bit8 setMonth(sint32 month);
111 bit8 setMDay(sint32 mday);
112
113 void normalize(void); // move msec overflows to the day
114
115 // Compare two times
116 int compare(const Xtime &other) const;
117
118 // comparisons
119 bit8 operator == ( const Xtime &other ) const;
120 bit8 operator != ( const Xtime &other ) const;
121 bit8 operator < ( const Xtime &other ) const;
122 bit8 operator > ( const Xtime &other ) const;
123 bit8 operator <= ( const Xtime &other ) const;
124 bit8 operator >= ( const Xtime &other ) const;
125
126 // assignments
127 Xtime &operator = (const Xtime &other);
128 Xtime &operator = (const time_t other);
129
130 // signed
131 Xtime &operator += (const Xtime &other);
132 Xtime &operator -= (const Xtime &other);
133 Xtime operator + (Xtime &other);
134 Xtime operator - (Xtime &other);
135
136 Xtime &operator += (const time_t other);
137 Xtime &operator -= (const time_t other);
138 Xtime operator + (time_t other);
139 Xtime operator - (time_t other);
140
141 protected:
142 sint32 day_; // days since Jan 1, 0
143 sint32 msec_; // milliseconds (thousandths of a sec)
144};
145
146#endif
#define min(x, y)
Definition BaseType.h:101
char bit8
Definition wstypes.h:61
signed long sint32
Definition bittype.h:51
Xtime operator+(Xtime &other)
Definition xtime.cpp:964
bit8 setSecond(sint32 sec)
Definition xtime.cpp:785
int getYear(void) const
Definition xtime.cpp:774
Xtime & operator-=(const Xtime &other)
Definition xtime.cpp:949
void normalize(void)
Definition xtime.cpp:268
bit8 operator==(const Xtime &other) const
Definition xtime.cpp:886
Xtime & operator=(const Xtime &other)
Definition xtime.cpp:972
bit8 operator>(const Xtime &other) const
Definition xtime.cpp:913
bit8 operator<(const Xtime &other) const
Definition xtime.cpp:904
int getMDay(void) const
Definition xtime.cpp:759
bit8 getTimeval(struct timeval &tv)
Definition xtime.cpp:661
int getWDay(void) const
void setDay(sint32 day)
Definition xtime.cpp:638
int compare(const Xtime &other) const
Definition xtime.cpp:870
sint32 msec_
Definition xtime.h:143
~Xtime()
Definition xtime.cpp:247
int getMonth(void) const
Definition xtime.cpp:766
bit8 setHour(sint32 hour)
Definition xtime.cpp:807
sint32 getDay(void) const
Definition xtime.cpp:626
void addSeconds(sint32 seconds)
Definition xtime.cpp:254
Xtime()
Definition xtime.cpp:222
int getHour(void) const
Definition xtime.cpp:753
int getMinute(void) const
Definition xtime.cpp:747
Xtime & operator+=(const Xtime &other)
Definition xtime.cpp:941
int getYDay(void) const
Definition xtime.cpp:689
void set(sint32 newday, sint32 newmsec)
Definition xtime.cpp:650
int getYWeek(void) const
bit8 setYear(sint32 year)
Definition xtime.cpp:819
bit8 FormatTime(char *out, char *format)
Definition xtime.cpp:315
sint32 getMsec(void) const
Definition xtime.cpp:632
void update()
Definition xtime.cpp:284
sint32 day_
Definition xtime.h:142
bit8 getTime(int &month, int &mday, int &year, int &hour, int &minute, int &second) const
Definition xtime.cpp:704
int getSecond(void) const
Definition xtime.cpp:741
void setMsec(sint32 msec)
Definition xtime.cpp:644
bit8 setTime(int month, int mday, int year, int hour, int minute, int second)
Definition xtime.cpp:680
bit8 setMinute(sint32 min)
Definition xtime.cpp:796
bit8 operator<=(const Xtime &other) const
Definition xtime.cpp:922
Xtime operator-(Xtime &other)
Definition xtime.cpp:957
bit8 setMonth(sint32 month)
Definition xtime.cpp:834
bit8 ParseDate(char *in)
bit8 operator!=(const Xtime &other) const
Definition xtime.cpp:895
bit8 setMDay(sint32 mday)
Definition xtime.cpp:850
bit8 operator>=(const Xtime &other) const
Definition xtime.cpp:931
long time_t
Definition wolapi.h:436