Skip to content

Commit

Permalink
refactor TimeKeeper to use std::chrono
Browse files Browse the repository at this point in the history
  • Loading branch information
jwmelto authored and macsforme committed Aug 21, 2019
1 parent 2f7f5fa commit aa19b4e
Show file tree
Hide file tree
Showing 3 changed files with 106 additions and 258 deletions.
109 changes: 39 additions & 70 deletions include/TimeKeeper.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,14 @@
* operator+=() allows a time in seconds to be added to a TimeKeeper.
*/

#ifndef BZF_TIME_KEEPER_H
#define BZF_TIME_KEEPER_H
#pragma once
#ifndef BZF_TIME_KEEPER_H
#define BZF_TIME_KEEPER_H

#include "common.h"

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


Expand All @@ -38,54 +40,45 @@
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;
}
TimeKeeper() = default;
using Seconds_t = std::chrono::duration<double>;

explicit TimeKeeper(Seconds_t secs);

operator bool() const;

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

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

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

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

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

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

static const TimeKeeper& getNullTime();

/** 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 const char *timestamp();

static void localTime( int &day);

Expand All @@ -96,75 +89,51 @@ class TimeKeeper

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

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

protected:
// not that we expect to subclass, but prefer separating methods from members
void now();

private:
double seconds;
static TimeKeeper currentTime;
static TimeKeeper tickTime;
static TimeKeeper sunExplodeTime;
static TimeKeeper sunGenesisTime;
static TimeKeeper nullTime;
static TimeKeeper startTime;
std::chrono::time_point<std::chrono::steady_clock, Seconds_t> lastTime; // floating seconds
};

//
// 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
inline double TimeKeeper::operator-(const TimeKeeper& t) const
{
return seconds - t.seconds;
return (lastTime - t.lastTime).count();
}

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

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

inline double TimeKeeper::getSeconds(void) const
inline double TimeKeeper::getSeconds() const
{
return seconds;
return lastTime.time_since_epoch().count();
}


Expand Down
8 changes: 4 additions & 4 deletions src/bzfs/bzfs.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -794,13 +794,13 @@ void startCountdown ( int delay, float limit, int playerID )
matchBegins = "Match begins now!";
else
{
TimeKeeper::convertTime(countdownDelay, timeArray);
TimeKeeper::convertTime(TimeKeeper::Seconds_t(countdownDelay), timeArray);
std::string countdowntime = TimeKeeper::printTime(timeArray);
matchBegins = TextUtils::format("Match begins in about %s", countdowntime.c_str());
}
sendMessage(ServerPlayer, AllPlayers, matchBegins.c_str());

TimeKeeper::convertTime(clOptions->timeLimit, timeArray);
TimeKeeper::convertTime(TimeKeeper::Seconds_t(clOptions->timeLimit), timeArray);
std::string timelimit = TimeKeeper::printTime(timeArray);
matchBegins = TextUtils::format("Match duration is %s", timelimit.c_str());
sendMessage(ServerPlayer, AllPlayers, matchBegins.c_str());
Expand Down Expand Up @@ -2230,7 +2230,7 @@ void AddPlayer(int playerIndex, GameKeeper::Player *playerData)
if (duration < 365.0f * 24 * 3600)
{
long int timeArray[4];
TimeKeeper::convertTime(duration, timeArray);
TimeKeeper::convertTime(TimeKeeper::Seconds_t(duration), timeArray);
std::string bantime = TimeKeeper::printTime(timeArray);
rejectionMessage += bantime;
rejectionMessage += " remaining";
Expand Down Expand Up @@ -5925,7 +5925,7 @@ static void doStuffOnPlayer(GameKeeper::Player &playerData)
if (duration < 365.0f * 24 * 3600)
{
long int timeArray[4];
TimeKeeper::convertTime(duration, timeArray);
TimeKeeper::convertTime(TimeKeeper::Seconds_t(duration), timeArray);
std::string bantime = TimeKeeper::printTime(timeArray);
reason += bantime;
reason += " remaining";
Expand Down
Loading

0 comments on commit aa19b4e

Please sign in to comment.