"clock" object

with std::chrono, how could I make a "clock" object that keeps system time?
There is nothing like a 'clock' object. Hoever there is a time_point. See:

http://www.cplusplus.com/reference/chrono/time_point/?kw=time_point


You can get this time_point from e.g. the system clock. See:

http://www.cplusplus.com/reference/chrono/system_clock/
http://www.cplusplus.com/reference/chrono/system_clock/now/
I suppose you can keep an object of type time_point if you like. But it is not required to get the time as you can call that method directly. It already keeps system time for you, or more precisely, the now method fetches the current value from the OS or hardware or whatever it uses.
Last edited on
what's the formula to convert the type_point to a ledgiable time? could someone show an example?
If you want legible time, IMO you're better off using time() et al.

1
2
3
4
5
6
7
8
9
10
#include <iostream>
#include <ctime>

int main()
{
	std::time_t tt;
	time(&tt);

	std::cout << "Current time = " << std::ctime(&tt);
}

Last edited on
See the examples in one of the links above. Particularly the last link (now) shows how to get the time_t value.
Actually, I want to do all sorts of applicable tests et al.
Actually, I want to do all sorts of applicable tests et al.
I'm not sure what that means.

strftime(...) is the most flexible function to show all aspects of the time. See:

http://www.cplusplus.com/reference/ctime/strftime/?kw=strftime
FI. C++20 includes Calender support. See https://en.cppreference.com/w/cpp/chrono#Calendar

and is already available as a 3rd party library at https://github.com/HowardHinnant/date

See also http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2018/p0355r7.html
Here is a bit of code someone gave me for timing a block of code. Maybe you'll find it useful.

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
#include<iostream>
#include<chrono>
#include<limits>


class Timer {

public:

    Timer() {
        m_start_time = std::chrono::high_resolution_clock::now();

    }

    ~Timer() {

        stop();
    }

    void stop() {

        auto end_time = std::chrono::high_resolution_clock::now();
        auto start    = std::chrono::time_point_cast<std::chrono::microseconds>(m_start_time).time_since_epoch().count();
        auto end      = std::chrono::time_point_cast<std::chrono::microseconds>(end_time).time_since_epoch().count();

        auto duration = end - start;
        double ms     = duration * 0.001;

        std::cout << duration << "us (" << ms << "ms)\n";
    }

private:
    std::chrono::time_point<std::chrono::high_resolution_clock> m_start_time; 
};



int main () {

    {
        Timer test; // Just create and object and you can find out long that 
                    // takes to run on your machine.

        //This below is nonsense just to give the computer something to do.

        std::string a;
        for(int i = 0; i != 1000; ++i)
            a += "a";
        std::cout << a.size() << '\t' << a.substr(0,500) << '\n';

    }
    return 0;
}
what's the formula to convert the type_point to a ledgiable time? could someone show an example?


you need a bunch of casts to make it printable. The way c++ does dates and times is a mix of things from C/ 1980s clunk and modern overengineered designs. The C clunk is simple and it works, but its also not objects which you asked for. The C++ stuff has objects, too many of them, but ironically to get a decent print of them you have to ... cast them ... back into the C stuff! It is an unholy mess, IMHO. There are likely 3rd party tools that can do this cleanly for you as OOP, if you don't want to build your own.

In short, you would think there was an object that would have month/day/year/hours/min/sec/decimalsubsecond fields of some sort. You would be wrong, as far as I know, there isnt one. You can put everything you need to know for general purpose (eg all but high res performance stuff) use into one 64 bit integer and methods to extract the fields, but that was too simple I guess. (I think that is what the C style stuff does, but again, they did not wrap to objects).
Last edited on
month/day/year/hours/min/sec/decimalsubsecond fields of some sort. You would be wrong, as far as I know, there isnt one.


C++20 - re my previous post.
Try this.
It's adapted from cppreference and can be run in a separate terminal instance or window while other thinks are being done.
If that's what you want then the next step is a class and consideration to the time display and display frequency.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <iostream>
#include <iomanip>
#include <chrono>

int main()
{
    using namespace std::literals;
    
    while(true)
    {
        auto t_c = std::chrono::system_clock::to_time_t
        ( std::chrono::system_clock::now() );
        std::cout
        << "It is now: "
        << std::put_time(std::localtime(&t_c), "%F %T") << '\n';
    }
    
    return 0;
}

Last edited on
Topic archived. No new replies allowed.