File: timepoint.cc

package info (click to toggle)
c%2B%2B-annotations 12.2.0-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 13,044 kB
  • sloc: cpp: 24,337; makefile: 1,517; ansic: 165; sh: 121; perl: 90
file content (46 lines) | stat: -rw-r--r-- 1,378 bytes parent folder | download | duplicates (4)
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
    #include <iostream>
    #include <chrono>
    #include <iomanip>      // put_time

    using namespace std;

    struct days: public chrono::hours
    {
        days(size_t count)
        :
            chrono::hours(count * 24)
        {}
    };

    int main()
    {
            // get the current time
        chrono::time_point<chrono::system_clock>
                                timePoint{chrono::system_clock::now()};

            // convert it to a std::time_t:
        time_t time = chrono::system_clock::to_time_t(timePoint);

            // show the time in seconds since the epoch:
        cout << time << '\n';

            // get a std::tm value:
        tm tmValue{*localtime(&time)};

            // display the time:
        cout << put_time(&tmValue, "current time: %c") << '\n';

            // display the time in rfc2822 format:
        cout << put_time(&tmValue, "rfs2822 format: %a, %e %b %Y %T %z")
                                                                    << '\n';

            // modifiy 'timePoint':
        timePoint += days(7);

            // convert timePoint's value to std::time_t:
        time = chrono::system_clock::to_time_t(timePoint);

            // display the gmtime, directly using gmtime's return value:
        cout << put_time(gmtime(&time),
                        "gmtime, one week from now:  %c %z") << '\n';
    }