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