File: io_ex5.cpp

package info (click to toggle)
boost1.90 1.90.0-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 593,120 kB
  • sloc: cpp: 4,190,908; xml: 196,648; python: 34,618; ansic: 23,145; asm: 5,468; sh: 3,774; makefile: 1,161; perl: 1,020; sql: 728; ruby: 676; yacc: 478; java: 77; lisp: 24; csh: 6
file content (90 lines) | stat: -rw-r--r-- 2,492 bytes parent folder | download | duplicates (20)
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
//  io_ex1.cpp  ----------------------------------------------------------//

//  Copyright 2010 Howard Hinnant
//  Copyright 2010 Vicente J. Botet Escriba

//  Distributed under the Boost Software License, Version 1.0.
//  See http://www.boost.org/LICENSE_1_0.txt

/*
This code was adapted by Vicente J. Botet Escriba from Hinnant's html documentation.
Many thanks to Howard for making his code available under the Boost license.

*/

#include <boost/chrono/chrono_io.hpp>
#include <ostream>
#include <iostream>

// format duration as [-]d/hh::mm::ss.cc
template <class CharT, class Traits, class Rep, class Period>
std::basic_ostream<CharT, Traits>&
display(std::basic_ostream<CharT, Traits>& os,
        boost::chrono::duration<Rep, Period> d)
{
    using std::cout;
    using namespace boost;
    using namespace boost::chrono;

    typedef duration<long long, ratio<86400> > days;
    typedef duration<long long, centi> centiseconds;

    // if negative, print negative sign and negate
    if (d < duration<Rep, Period>(0))
    {
        d = -d;
        os << '-';
    }
    // round d to nearest centiseconds, to even on tie
    centiseconds cs = duration_cast<centiseconds>(d);
    if (d - cs > milliseconds(5)
        || (d - cs == milliseconds(5) && cs.count() & 1))
        ++cs;
    // separate seconds from centiseconds
    seconds s = duration_cast<seconds>(cs);
    cs -= s;
    // separate minutes from seconds
    minutes m = duration_cast<minutes>(s);
    s -= m;
    // separate hours from minutes
    hours h = duration_cast<hours>(m);
    m -= h;
    // separate days from hours
    days dy = duration_cast<days>(h);
    h -= dy;
    // print d/hh:mm:ss.cc
    os << dy.count() << '/';
    if (h < hours(10))
        os << '0';
    os << h.count() << ':';
    if (m < minutes(10))
        os << '0';
    os << m.count() << ':';
    if (s < seconds(10))
        os << '0';
    os << s.count() << '.';
    if (cs < centiseconds(10))
        os << '0';
    os << cs.count();
    return os;
}

int main()
{
    using std::cout;
    using namespace boost;
    using namespace boost::chrono;

#ifdef BOOST_CHRONO_HAS_CLOCK_STEADY
    display(cout, steady_clock::now().time_since_epoch()
                  + duration<long, mega>(1)) << '\n';
#endif
    display(cout, -milliseconds(6)) << '\n';
    display(cout, duration<long, mega>(1)) << '\n';
    display(cout, -duration<long, mega>(1)) << '\n';
}

//~ 12/06:03:22.95
//~ -0/00:00:00.01
//~ 11/13:46:40.00
//~ -11/13:46:40.00