1 | #ifndef DATE_TIME_TIME_RESOLUTION_TRAITS_HPP |
---|
2 | #define DATE_TIME_TIME_RESOLUTION_TRAITS_HPP |
---|
3 | |
---|
4 | /* Copyright (c) 2002,2003 CrystalClear Software, Inc. |
---|
5 | * Use, modification and distribution is subject to the |
---|
6 | * Boost Software License, Version 1.0. (See accompanying |
---|
7 | * file LICENSE-1.0 or http://www.boost.org/LICENSE-1.0) |
---|
8 | * Author: Jeff Garland, Bart Garst |
---|
9 | * $Date: 2007/05/25 19:58:16 $ |
---|
10 | */ |
---|
11 | |
---|
12 | |
---|
13 | #include "boost/date_time/time_defs.hpp" |
---|
14 | #include "boost/date_time/int_adapter.hpp" |
---|
15 | #include "boost/cstdint.hpp" |
---|
16 | |
---|
17 | namespace boost { |
---|
18 | namespace date_time { |
---|
19 | |
---|
20 | //! Simple function to calculate absolute value of a numeric type |
---|
21 | template <typename T> |
---|
22 | // JDG [7/6/02 made a template], |
---|
23 | // moved here from time_duration.hpp 2003-Sept-4. |
---|
24 | inline T absolute_value(T x) |
---|
25 | { |
---|
26 | return x < 0 ? -x : x; |
---|
27 | } |
---|
28 | |
---|
29 | //! traits struct for time_resolution_traits implementation type |
---|
30 | struct time_resolution_traits_bi32_impl { |
---|
31 | typedef boost::int32_t int_type; |
---|
32 | typedef boost::int32_t impl_type; |
---|
33 | static int_type as_number(impl_type i){ return i;} |
---|
34 | //! Used to determine if implemented type is int_adapter or int |
---|
35 | static bool is_adapted() { return false;} |
---|
36 | }; |
---|
37 | //! traits struct for time_resolution_traits implementation type |
---|
38 | struct time_resolution_traits_adapted32_impl { |
---|
39 | typedef boost::int32_t int_type; |
---|
40 | typedef boost::date_time::int_adapter<boost::int32_t> impl_type; |
---|
41 | static int_type as_number(impl_type i){ return i.as_number();} |
---|
42 | //! Used to determine if implemented type is int_adapter or int |
---|
43 | static bool is_adapted() { return true;} |
---|
44 | }; |
---|
45 | //! traits struct for time_resolution_traits implementation type |
---|
46 | struct time_resolution_traits_bi64_impl { |
---|
47 | typedef boost::int64_t int_type; |
---|
48 | typedef boost::int64_t impl_type; |
---|
49 | static int_type as_number(impl_type i){ return i;} |
---|
50 | //! Used to determine if implemented type is int_adapter or int |
---|
51 | static bool is_adapted() { return false;} |
---|
52 | }; |
---|
53 | //! traits struct for time_resolution_traits implementation type |
---|
54 | struct time_resolution_traits_adapted64_impl { |
---|
55 | typedef boost::int64_t int_type; |
---|
56 | typedef boost::date_time::int_adapter<boost::int64_t> impl_type; |
---|
57 | static int_type as_number(impl_type i){ return i.as_number();} |
---|
58 | //! Used to determine if implemented type is int_adapter or int |
---|
59 | static bool is_adapted() { return true;} |
---|
60 | }; |
---|
61 | |
---|
62 | template<typename frac_sec_type, |
---|
63 | time_resolutions res, |
---|
64 | #if (defined(BOOST_MSVC) && (_MSC_VER < 1300)) |
---|
65 | boost::int64_t resolution_adjust, |
---|
66 | #else |
---|
67 | typename frac_sec_type::int_type resolution_adjust, |
---|
68 | #endif |
---|
69 | unsigned short frac_digits, |
---|
70 | typename v_type = boost::int32_t > |
---|
71 | class time_resolution_traits { |
---|
72 | public: |
---|
73 | typedef typename frac_sec_type::int_type fractional_seconds_type; |
---|
74 | typedef typename frac_sec_type::int_type tick_type; |
---|
75 | typedef typename frac_sec_type::impl_type impl_type; |
---|
76 | typedef v_type day_type; |
---|
77 | typedef v_type hour_type; |
---|
78 | typedef v_type min_type; |
---|
79 | typedef v_type sec_type; |
---|
80 | |
---|
81 | // bring in function from frac_sec_type traits structs |
---|
82 | static typename frac_sec_type::int_type as_number(typename frac_sec_type::impl_type i) |
---|
83 | { |
---|
84 | return frac_sec_type::as_number(i); |
---|
85 | } |
---|
86 | static bool is_adapted() |
---|
87 | { |
---|
88 | return frac_sec_type::is_adapted(); |
---|
89 | } |
---|
90 | |
---|
91 | //Would like this to be frac_sec_type, but some compilers complain |
---|
92 | BOOST_STATIC_CONSTANT(int, ticks_per_second = resolution_adjust); |
---|
93 | // static const boost::int32_t ticks_per_second = resolution_adjust; |
---|
94 | |
---|
95 | static time_resolutions resolution() |
---|
96 | { |
---|
97 | return res; |
---|
98 | } |
---|
99 | static unsigned short num_fractional_digits() |
---|
100 | { |
---|
101 | return frac_digits; |
---|
102 | } |
---|
103 | static fractional_seconds_type res_adjust() |
---|
104 | { |
---|
105 | return resolution_adjust; |
---|
106 | } |
---|
107 | //! Any negative argument results in a negative tick_count |
---|
108 | static tick_type to_tick_count(hour_type hours, |
---|
109 | min_type minutes, |
---|
110 | sec_type seconds, |
---|
111 | fractional_seconds_type fs) |
---|
112 | { |
---|
113 | if(hours < 0 || minutes < 0 || seconds < 0 || fs < 0) |
---|
114 | { |
---|
115 | hours = absolute_value(hours); |
---|
116 | minutes = absolute_value(minutes); |
---|
117 | seconds = absolute_value(seconds); |
---|
118 | fs = absolute_value(fs); |
---|
119 | return (((((fractional_seconds_type(hours)*3600) |
---|
120 | + (fractional_seconds_type(minutes)*60) |
---|
121 | + seconds)*res_adjust()) + fs) * -1); |
---|
122 | } |
---|
123 | |
---|
124 | return (((fractional_seconds_type(hours)*3600) |
---|
125 | + (fractional_seconds_type(minutes)*60) |
---|
126 | + seconds)*res_adjust()) + fs; |
---|
127 | } |
---|
128 | |
---|
129 | }; |
---|
130 | |
---|
131 | typedef time_resolution_traits<time_resolution_traits_adapted32_impl, milli, 1000, 3 > milli_res; |
---|
132 | typedef time_resolution_traits<time_resolution_traits_adapted64_impl, micro, 1000000, 6 > micro_res; |
---|
133 | typedef time_resolution_traits<time_resolution_traits_adapted64_impl, nano, 1000000000, 9 > nano_res; |
---|
134 | |
---|
135 | |
---|
136 | } } //namespace date_time |
---|
137 | |
---|
138 | |
---|
139 | |
---|
140 | #endif |
---|