Chris@16
|
1 #ifndef DATE_CLOCK_DEVICE_HPP___
|
Chris@16
|
2 #define DATE_CLOCK_DEVICE_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 #include "boost/date_time/c_time.hpp"
|
Chris@16
|
13
|
Chris@16
|
14
|
Chris@16
|
15 namespace boost {
|
Chris@16
|
16 namespace date_time {
|
Chris@16
|
17
|
Chris@16
|
18 //! A clock providing day level services based on C time_t capabilities
|
Chris@16
|
19 /*! This clock uses Posix interfaces as its implementation and hence
|
Chris@16
|
20 * uses the timezone settings of the operating system. Incorrect
|
Chris@16
|
21 * user settings will result in incorrect results for the calls
|
Chris@16
|
22 * to local_day.
|
Chris@16
|
23 */
|
Chris@16
|
24 template<class date_type>
|
Chris@16
|
25 class day_clock
|
Chris@16
|
26 {
|
Chris@16
|
27 public:
|
Chris@16
|
28 typedef typename date_type::ymd_type ymd_type;
|
Chris@16
|
29 //! Get the local day as a date type
|
Chris@16
|
30 static date_type local_day()
|
Chris@16
|
31 {
|
Chris@16
|
32 return date_type(local_day_ymd());
|
Chris@16
|
33 }
|
Chris@16
|
34 //! Get the local day as a ymd_type
|
Chris@16
|
35 static typename date_type::ymd_type local_day_ymd()
|
Chris@16
|
36 {
|
Chris@16
|
37 ::std::tm result;
|
Chris@16
|
38 ::std::tm* curr = get_local_time(result);
|
Chris@101
|
39 return ymd_type(static_cast<unsigned short>(curr->tm_year + 1900),
|
Chris@101
|
40 static_cast<unsigned short>(curr->tm_mon + 1),
|
Chris@101
|
41 static_cast<unsigned short>(curr->tm_mday));
|
Chris@16
|
42 }
|
Chris@16
|
43 //! Get the current day in universal date as a ymd_type
|
Chris@16
|
44 static typename date_type::ymd_type universal_day_ymd()
|
Chris@16
|
45 {
|
Chris@16
|
46 ::std::tm result;
|
Chris@16
|
47 ::std::tm* curr = get_universal_time(result);
|
Chris@101
|
48 return ymd_type(static_cast<unsigned short>(curr->tm_year + 1900),
|
Chris@101
|
49 static_cast<unsigned short>(curr->tm_mon + 1),
|
Chris@101
|
50 static_cast<unsigned short>(curr->tm_mday));
|
Chris@16
|
51 }
|
Chris@16
|
52 //! Get the UTC day as a date type
|
Chris@16
|
53 static date_type universal_day()
|
Chris@16
|
54 {
|
Chris@16
|
55 return date_type(universal_day_ymd());
|
Chris@16
|
56 }
|
Chris@16
|
57
|
Chris@16
|
58 private:
|
Chris@16
|
59 static ::std::tm* get_local_time(std::tm& result)
|
Chris@16
|
60 {
|
Chris@16
|
61 ::std::time_t t;
|
Chris@16
|
62 ::std::time(&t);
|
Chris@16
|
63 return c_time::localtime(&t, &result);
|
Chris@16
|
64 }
|
Chris@16
|
65 static ::std::tm* get_universal_time(std::tm& result)
|
Chris@16
|
66 {
|
Chris@16
|
67 ::std::time_t t;
|
Chris@16
|
68 ::std::time(&t);
|
Chris@16
|
69 return c_time::gmtime(&t, &result);
|
Chris@16
|
70 }
|
Chris@16
|
71
|
Chris@16
|
72 };
|
Chris@16
|
73
|
Chris@16
|
74 } } //namespace date_time
|
Chris@16
|
75
|
Chris@16
|
76
|
Chris@16
|
77 #endif
|