include: co/time.h.
#epoch time
The EPOCH is a specific time 1970-01-01 00:00:00 UTC
, and the epoch time is the time since the EPOCH.
#epoch::ms
int64 ms();
- Return milliseconds since EPOCH.
#epoch::us
int64 us();
- Return microseconds since EPOCH.
#monotonic time
Monotonic time is a monotonic increasing time, it is implemented as the time since last reboot of system on most platforms. It is generally used for timing and is more stable than system time.
#now::ms
int64 ms();
- Returns a monotonically increasing timestamp in milliseconds.
- On mac platform, if the system does not support
CLOCK_MONOTONIC
,epoch::ms()
will be used instead.
#now::us
int64 us();
-
Returns a monotonically increasing timestamp in microseconds.
-
On mac platform, if the system does not support
CLOCK_MONOTONIC
,epoch::us()
will be used instead. -
Example
int64 beg = now::us();
int64 end = now::us();
LOG << "time used: "<< (end-beg) <<" us";
#Time string (now::str)
// fm: time output format
fastring str(const char* fm="%Y-%m-%d %H:%M:%S");
-
This function returns the string form of the current system time in the specified format. It is implemented based on
strftime
. -
Example
fastring s = now::str(); // "2021-07-07 17:07:07"
fastring s = now::str("%Y"); // "2021"
#sleep
#sleep::ms
void ms(uint32 n);
- Sleep for n milliseconds.
#sleep::sec
void sec(uint32 n);
-
Sleep for n seconds.
-
Example
sleep::ms(10); // sleep for 10 milliseconds
sleep::sec(1); // sleep for 1 second
#co::Timer
co::Timer is a simple timer based on monotonic time.
Timer was added to namespace co
since v3.0.1.
#constructor
Timer();
- Set the start time of the timer, and start timing when the object is created.
#ms
int64 ms() const;
- Return milliseconds since start of the timing.
#us
int64 us() const;
- Return microseconds since start of the timing.
#restart
void restart();
- Restart the timer.
#Example
co::Timer t;
sleep::ms(10);
int64 us = t.us();
t.restart();
sleep::ms(20);
int64 ms = t.ms();