4 // Fraunhofer Institute for Open Communication Systems (FOKUS)
6 // The contents of this file are subject to the Fraunhofer FOKUS Public License
7 // Version 1.0 (the "License"); you may not use this file except in compliance
8 // with the License. You may obtain a copy of the License at
9 // http://senf.berlios.de/license.html
11 // The Fraunhofer FOKUS Public License Version 1.0 is based on,
12 // but modifies the Mozilla Public License Version 1.1.
13 // See the full license text for the amendments.
15 // Software distributed under the License is distributed on an "AS IS" basis,
16 // WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
17 // for the specific language governing rights and limitations under the License.
19 // The Original Code is Fraunhofer FOKUS code.
21 // The Initial Developer of the Original Code is Fraunhofer-Gesellschaft e.V.
22 // (registered association), Hansastraße 27 c, 80686 Munich, Germany.
23 // All Rights Reserved.
26 // Stefan Bund <g0dil@berlios.de>
29 \brief ClockService inline non-template implementation */
33 #include <boost/date_time/posix_time/posix_time_types.hpp>
34 #include <senf/Utils/Exception.hh>
35 #include <senf/Utils/ClockTypeMacros.hh>
37 #define prefix_ inline
38 //-/////////////////////////////////////////////////////////////////////////////////////////////////
40 //-/////////////////////////////////////////////////////////////////////////////////////////////////
43 prefix_ senf::ClockService::clock_type senf::ClockService::now()
46 if (clock_gettime(CLOCK_MONOTONIC, &spec) < 0)
47 SENF_THROW_SYSTEM_EXCEPTION("clock_gettime()");
48 return SENF_INT2CLOCKTYPE(spec.tv_sec * 1000000000LL + spec.tv_nsec);
51 //-/////////////////////////////////////////////////////////////////////////////////////////////////
54 namespace senf { namespace scheduler { ClockService::clock_type now(); } }
56 prefix_ senf::ClockService::clock_type senf::ClockService::clock_m(abstime_type time)
58 if (scheduler::now() - baseClock_ > clock_type(1000000000ll))
60 boost::posix_time::time_duration delta (time - baseAbstime_);
61 return baseClock_ + clock_type( delta.ticks() )
62 * clock_type( 1000000000UL / boost::posix_time::time_duration::ticks_per_second() );
65 prefix_ senf::ClockService::abstime_type senf::ClockService::abstime_m(clock_type clock)
68 return abstime_type();
69 if (scheduler::now() - baseClock_ > clock_type(1000000000ll))
71 #ifdef BOOST_DATE_TIME_POSIX_TIME_STD_CONFIG
72 return baseAbstime_ + boost::posix_time::nanoseconds(clock-baseClock_);
74 return baseAbstime_ + boost::posix_time::microseconds((clock.value() - baseClock_.value() + 500 )/1000);
78 prefix_ senf::ClockService::ClockService()
83 prefix_ void senf::ClockService::restart_m()
85 baseAbstime_ = boost::posix_time::microsec_clock::universal_time();
91 prefix_ senf::ClockService::abstime_type senf::ClockService::abstime(clock_type clock)
93 return instance().abstime_m(clock);
96 prefix_ senf::ClockService::reltime_type senf::ClockService::reltime(clock_type clock)
98 #ifdef BOOST_DATE_TIME_POSIX_TIME_STD_CONFIG
99 return boost::posix_time::nanoseconds(clock);
101 return boost::posix_time::microseconds((clock.value() + 500)/1000);
105 prefix_ senf::ClockService::clock_type senf::ClockService::clock(abstime_type time)
107 return instance().clock_m(time);
110 prefix_ senf::ClockService::clock_type senf::ClockService::from_time_t(time_t const & time)
112 return clock( boost::posix_time::from_time_t(time) );
115 prefix_ senf::ClockService::clock_type senf::ClockService::nanoseconds(int64_type v)
117 return SENF_INT2CLOCKTYPE(v);
120 prefix_ senf::ClockService::clock_type senf::ClockService::microseconds(int64_type v)
122 return SENF_INT2CLOCKTYPE(v * SENF_CLOCKTYPEVAL(nanoseconds(1000)));
125 prefix_ senf::ClockService::clock_type senf::ClockService::milliseconds(int64_type v)
127 return SENF_INT2CLOCKTYPE(v * SENF_CLOCKTYPEVAL(microseconds(1000)));
130 prefix_ senf::ClockService::clock_type senf::ClockService::seconds(int64_type v)
132 return SENF_INT2CLOCKTYPE(v * SENF_CLOCKTYPEVAL(milliseconds(1000)));
135 prefix_ senf::ClockService::clock_type senf::ClockService::minutes(int64_type v)
137 return SENF_INT2CLOCKTYPE(v * SENF_CLOCKTYPEVAL(seconds(60)));
140 prefix_ senf::ClockService::clock_type senf::ClockService::hours(int64_type v)
142 return SENF_INT2CLOCKTYPE(v * SENF_CLOCKTYPEVAL(minutes(60)));
145 prefix_ senf::ClockService::clock_type senf::ClockService::days(int64_type v)
147 return SENF_INT2CLOCKTYPE(v * SENF_CLOCKTYPEVAL(hours(24)));
150 prefix_ senf::ClockService::int64_type senf::ClockService::in_nanoseconds(clock_type v)
152 return SENF_CLOCKTYPEVAL(v);
155 prefix_ senf::ClockService::int64_type senf::ClockService::in_microseconds(clock_type v)
157 return (SENF_CLOCKTYPEVAL(v) / SENF_CLOCKTYPEVAL(nanoseconds(1000)));
160 prefix_ senf::ClockService::int64_type senf::ClockService::in_milliseconds(clock_type v)
162 return (SENF_CLOCKTYPEVAL(v) / SENF_CLOCKTYPEVAL(microseconds(1000)));
165 prefix_ senf::ClockService::int64_type senf::ClockService::in_seconds(clock_type v)
167 return (SENF_CLOCKTYPEVAL(v) / SENF_CLOCKTYPEVAL(milliseconds(1000)));
170 prefix_ senf::ClockService::int64_type senf::ClockService::in_minutes(clock_type v)
172 return (SENF_CLOCKTYPEVAL(v) / SENF_CLOCKTYPEVAL(seconds(60)));
175 prefix_ senf::ClockService::int64_type senf::ClockService::in_hours(clock_type v)
177 return (SENF_CLOCKTYPEVAL(v) / SENF_CLOCKTYPEVAL(minutes(60))) ;
180 prefix_ senf::ClockService::int64_type senf::ClockService::in_days(clock_type v)
182 return (SENF_CLOCKTYPEVAL(v) / SENF_CLOCKTYPEVAL(hours(24))) ;
185 prefix_ senf::ClockService::clock_type senf::ClockService::from_timeval(timeval const & time)
187 return from_time_t(time.tv_sec) + microseconds(time.tv_usec);
190 prefix_ void senf::ClockService::restart()
192 instance().restart_m();
195 //-/////////////////////////////////////////////////////////////////////////////////////////////////
202 // comment-column: 40
203 // c-file-style: "senf"
204 // indent-tabs-mode: nil
205 // ispell-local-dictionary: "american"
206 // compile-command: "scons -u test"