Chris@16
|
1 #ifndef DATE_TIME_TIME_CLOCK_HPP___
|
Chris@16
|
2 #define DATE_TIME_TIME_CLOCK_HPP___
|
Chris@16
|
3
|
Chris@16
|
4 /* Copyright (c) 2002,2003,2005 CrystalClear Software, Inc.
|
Chris@16
|
5 * Use, modification and distribution is subject to the
|
Chris@16
|
6 * Boost Software License, Version 1.0. (See accompanying
|
Chris@16
|
7 * file LICENSE_1_0.txt or http://www.boost.org/LICENSE_1_0.txt)
|
Chris@16
|
8 * Author: Jeff Garland, Bart Garst
|
Chris@101
|
9 * $Date$
|
Chris@16
|
10 */
|
Chris@16
|
11
|
Chris@16
|
12 /*! @file time_clock.hpp
|
Chris@16
|
13 This file contains the interface for clock devices.
|
Chris@16
|
14 */
|
Chris@16
|
15
|
Chris@16
|
16 #include "boost/date_time/c_time.hpp"
|
Chris@16
|
17 #include "boost/shared_ptr.hpp"
|
Chris@16
|
18
|
Chris@16
|
19 namespace boost {
|
Chris@16
|
20 namespace date_time {
|
Chris@16
|
21
|
Chris@16
|
22
|
Chris@16
|
23 //! A clock providing time level services based on C time_t capabilities
|
Chris@16
|
24 /*! This clock provides resolution to the 1 second level
|
Chris@16
|
25 */
|
Chris@16
|
26 template<class time_type>
|
Chris@16
|
27 class second_clock
|
Chris@16
|
28 {
|
Chris@16
|
29 public:
|
Chris@16
|
30 typedef typename time_type::date_type date_type;
|
Chris@16
|
31 typedef typename time_type::time_duration_type time_duration_type;
|
Chris@16
|
32
|
Chris@16
|
33 static time_type local_time()
|
Chris@16
|
34 {
|
Chris@16
|
35 ::std::time_t t;
|
Chris@16
|
36 ::std::time(&t);
|
Chris@16
|
37 ::std::tm curr, *curr_ptr;
|
Chris@16
|
38 //curr_ptr = ::std::localtime(&t);
|
Chris@16
|
39 curr_ptr = c_time::localtime(&t, &curr);
|
Chris@16
|
40 return create_time(curr_ptr);
|
Chris@16
|
41 }
|
Chris@16
|
42
|
Chris@16
|
43
|
Chris@16
|
44 //! Get the current day in universal date as a ymd_type
|
Chris@16
|
45 static time_type universal_time()
|
Chris@16
|
46 {
|
Chris@16
|
47
|
Chris@16
|
48 ::std::time_t t;
|
Chris@16
|
49 ::std::time(&t);
|
Chris@16
|
50 ::std::tm curr, *curr_ptr;
|
Chris@16
|
51 //curr_ptr = ::std::gmtime(&t);
|
Chris@16
|
52 curr_ptr = c_time::gmtime(&t, &curr);
|
Chris@16
|
53 return create_time(curr_ptr);
|
Chris@16
|
54 }
|
Chris@16
|
55
|
Chris@16
|
56 template<class time_zone_type>
|
Chris@16
|
57 static time_type local_time(boost::shared_ptr<time_zone_type> tz_ptr)
|
Chris@16
|
58 {
|
Chris@16
|
59 typedef typename time_type::utc_time_type utc_time_type;
|
Chris@16
|
60 utc_time_type utc_time = second_clock<utc_time_type>::universal_time();
|
Chris@16
|
61 return time_type(utc_time, tz_ptr);
|
Chris@16
|
62 }
|
Chris@16
|
63
|
Chris@16
|
64
|
Chris@16
|
65 private:
|
Chris@16
|
66 static time_type create_time(::std::tm* current)
|
Chris@16
|
67 {
|
Chris@16
|
68 date_type d(static_cast<unsigned short>(current->tm_year + 1900),
|
Chris@16
|
69 static_cast<unsigned short>(current->tm_mon + 1),
|
Chris@16
|
70 static_cast<unsigned short>(current->tm_mday));
|
Chris@16
|
71 time_duration_type td(current->tm_hour,
|
Chris@16
|
72 current->tm_min,
|
Chris@16
|
73 current->tm_sec);
|
Chris@16
|
74 return time_type(d,td);
|
Chris@16
|
75 }
|
Chris@16
|
76
|
Chris@16
|
77 };
|
Chris@16
|
78
|
Chris@16
|
79
|
Chris@16
|
80 } } //namespace date_time
|
Chris@16
|
81
|
Chris@16
|
82
|
Chris@16
|
83 #endif
|