File: TimeKeeper.h

package info (click to toggle)
bzflag 2.0.13.20080902-1
  • links: PTS
  • area: main
  • in suites: lenny
  • size: 27,564 kB
  • ctags: 34,716
  • sloc: cpp: 139,842; ansic: 14,510; sh: 10,715; makefile: 2,454; perl: 477; php: 428; python: 345; objc: 243; xml: 24
file content (172 lines) | stat: -rw-r--r-- 4,663 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
/* bzflag
 * Copyright (c) 1993 - 2008 Tim Riker
 *
 * This package is free software;  you can redistribute it and/or
 * modify it under the terms of the license found in the file
 * named COPYING that should have accompanied this file.
 *
 * THIS PACKAGE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
 * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
 */

/**
 * TimeKeeper:
 *	Standard way to keep track of time in game.
 *
 * Generally, only the difference between TimeKeeper's is useful.
 * operator-() computes the difference in seconds as a float and
 * correctly handles wraparound.
 * operator+=() allows a time in seconds to be added to a TimeKeeper.
 */

#ifndef	BZF_TIME_KEEPER_H
#define	BZF_TIME_KEEPER_H

#include "common.h"

/* system interface headers */
#include <string>


/** TimeKeeper keeps time.  It's useful to determine how much time has
 * elapsed from some other point in time.  Use getCurrent() to return a
 * timekeeper object set to the current time.  You can then use subsequent
 * calls to getCurrent and subtract the second from the first to get an
 * elapsed float time value.
 */
class TimeKeeper {
public:
  TimeKeeper();
  TimeKeeper(const TimeKeeper&);
  ~TimeKeeper();
  TimeKeeper&		operator=(const TimeKeeper&);

  double		operator-(const TimeKeeper&) const;
  bool			operator<=(const TimeKeeper&) const;
  TimeKeeper&		operator+=(double);
  TimeKeeper&		operator+=(const TimeKeeper&);

	// make a TimeKeeper with seconds = NULL act like unset
	// Fixme: must this be defined here? didn't work for me outside the class
	inline operator void*()
	{
		if (seconds > 0.0)
			return this;
		else
			return NULL;
	}

  /** returns how many seconds have elapsed since epoch, Jan 1, 1970 */
  double       getSeconds(void) const;

  /** returns a timekeeper representing the current time */
  static const TimeKeeper&	getCurrent(void);

  /** returns a timekeeper representing the time of program execution */
  static const TimeKeeper&	getStartTime(void);

  /** sets the time to the current time (recalculates) */
  static void			setTick(void);
  /** returns a timekeeper that is updated periodically via setTick */
  static const TimeKeeper&	getTick(void); // const

  /** returns a timekeeper representing +Inf */
  static const TimeKeeper&	getSunExplodeTime(void);
  /** returns a timekeeper representing -Inf */
  static const TimeKeeper&	getSunGenesisTime(void);
  /** returns a timekeeper representing an unset timekeeper */
  static const TimeKeeper&	getNullTime(void);


  /** returns the local time */
  static void localTime(int *year = NULL, int *month = NULL, int* day = NULL, int* hour = NULL, int* min = NULL, int* sec = NULL, bool* dst = NULL);

  /** returns a string of the local time */
  static const char		*timestamp(void);

  static void localTime( int &day);

  /** converts a time difference into an array of integers
      representing days, hours, minutes, seconds */
  static void			convertTime(double raw, long int convertedTimes[]);
  /** prints an integer-array time difference in human-readable form */
  static const std::string	printTime(long int timeValue[]);
  /** prints an float time difference in human-readable form */
  static const std::string	printTime(double diff);

  /** sleep for a given number of floating point seconds */
  static void			sleep(double secs); //const

private:
  double		seconds;
  static TimeKeeper	currentTime;
  static TimeKeeper	tickTime;
  static TimeKeeper	sunExplodeTime;
  static TimeKeeper	sunGenesisTime;
  static TimeKeeper	nullTime;
  static TimeKeeper	startTime;
};

//
// TimeKeeper
//

inline TimeKeeper::TimeKeeper() : seconds(0.0)
{
  // do nothing
}

inline TimeKeeper::TimeKeeper(const TimeKeeper& t) :
  seconds(t.seconds)
{
  // do nothing
}

inline TimeKeeper::~TimeKeeper()
{
  // do nothing
}

inline TimeKeeper&	TimeKeeper::operator=(const TimeKeeper& t)
{
  seconds = t.seconds;
  return *this;
}

inline double		TimeKeeper::operator-(const TimeKeeper& t) const
{
  return seconds - t.seconds;
}

inline TimeKeeper&	TimeKeeper::operator+=(double dt)
{
  seconds += dt;
  return *this;
}
inline TimeKeeper&	TimeKeeper::operator+=(const TimeKeeper& t)
{
  seconds += t.seconds;
  return *this;
}

inline bool		TimeKeeper::operator<=(const TimeKeeper& t) const
{
  return seconds <= t.seconds;
}

inline double		TimeKeeper::getSeconds(void) const
{
  return seconds;
}


#endif // BZF_TIME_KEEPER_H

// Local Variables: ***
// mode: C++ ***
// tab-width: 8 ***
// c-basic-offset: 2 ***
// indent-tabs-mode: t ***
// End: ***
// ex: shiftwidth=2 tabstop=8