-
Notifications
You must be signed in to change notification settings - Fork 0
utils::time
Do Le Long An edited this page Jan 9, 2023
·
2 revisions
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.
-
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 twoDatetime
members: start and end. Subtracting twoDatetime
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 withPeriod
above. However, aDuration
object can be extracted from aPeriod
object by callingPeriod::duration()
.
- Create a
Datetime
object for the current instant by callingutils::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 aDatetime
object. Format guide. - Subtract two Datetime objects to get a
Period
object, or use the Java-like static methodDatetime::between()
. (1) - Can also create new
Period
object using thePeriod(Datetime datetime1, Datetime datetime2)
constructor. - Get the duration of a Period object by calling
Period::duration()
. - Create independent
Duration
object by calling its constructor (seeDuration.h
). -
Datetime
objects can be modified by adding and subtracting them withDuration
objects. -
Duration
objects can be added and subtracted with otherDuration
objects. For subtraction, the absolute value of the result is always taken, so thatDuration
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, checkTemporal.h
andTemporal.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 😄