Skip to content

utils::time

Do Le Long An edited this page Jan 9, 2023 · 2 revisions

Brief description

This utils::time namespace contains 4 classes implemented as a thin wrapper for the C ctime library (docs on cppreference / docs on cplusplus.com). Its goal is to emulate Java's time package (java.time) in behavior, but simpler and easier to use comparing to Java java.time and C++ std::chrono for the purpose of this project.

Class added:

  • Temporal: A generic class for timekeeping, has overloaded arithmetic operators and all other low-level memory management facilities (Rule of 5).
  • Datetime: A class representing a specific point in time,
  • Period: A class that contains two Datetime members: start and end. Subtracting two Datetime objects automatically returns an object of this class. Used to represent a start date and end date (a period in time, duh 😆).
  • Duration: A class representing an AMOUNT of time (>= 0). Not to be confused with Period above. However, a Duration object can be extracted from a Period object by calling Period::duration().

Main functionalities:

  • Create a Datetime object for the current instant by calling utils::time::Datetime(utils::time::Datetime::now()).
  • Alternatively, use Datetime(const std::string &time_str, const char *fmt) constructor to create an object for a specific point in time using a string with a given format. Format guide.
  • Use Datetime::strf(const char *fmt) to get a string representation of a Datetime object. Format guide.
  • Subtract two Datetime objects to get a Period object, or use the Java-like static method Datetime::between(). (1)
  • Can also create new Period object using the Period(Datetime datetime1, Datetime datetime2) constructor.
  • Get the duration of a Period object by calling Period::duration().
  • Create independent Duration object by calling its constructor (see Duration.h).
  • Datetime objects can be modified by adding and subtracting them with Duration objects.
  • Duration objects can be added and subtracted with other Duration objects. For subtraction, the absolute value of the result is always taken, so that Duration is always greater or equal to 0. (2)
  • Duration objects can be multiplied or divided with unsigned int to scale them up or down.
  • All object of parent class Temporal can be added and subtracted together as if they are integers, unless overriden by its child class operator overload (case (1) and (2)).
  • All object of parent class Temporal can be compared with each others as if they are integers, e.g. using <, >, ==, !=, <=, >= operators (meaning I have overloaded all of them for Temporal, check Temporal.h and Temporal.cpp for details). and more... to be explained in future documentations, I'm spent for this Sprint 🥺 Remind me to do it later, or one of you can help me write it 😄
Clone this wiki locally