Boost.UI
User Interface Boost library
datetime.hpp
Go to the documentation of this file.
1 // Copyright (c) 2017 Kolya Kosenko
2 
3 // Distributed under the Boost Software License, Version 1.0.
4 // See http://www.boost.org/LICENSE_1_0.txt
5 
7 
8 #ifndef BOOST_UI_DATETIME_HPP
9 #define BOOST_UI_DATETIME_HPP
10 
11 #ifdef DOXYGEN
12 #define BOOST_UI_USE_DATE_TIME
13 #define BOOST_UI_USE_CHRONO
14 #endif
15 
16 #include <boost/ui/config.hpp>
17 
18 #ifdef BOOST_HAS_PRAGMA_ONCE
19 #pragma once
20 #endif
21 
22 #include <boost/ui/widget.hpp>
23 #include <boost/cstdint.hpp>
24 
25 #ifdef BOOST_UI_USE_DATE_TIME
26 #include <boost/date_time/gregorian/greg_date.hpp>
27 #include <boost/date_time/posix_time/posix_time.hpp>
28 #endif
29 
30 #ifdef BOOST_UI_USE_CHRONO
31 #include <boost/chrono.hpp>
32 #endif
33 
34 #ifndef BOOST_NO_CXX11_HDR_CHRONO
35 #include <chrono>
36 #endif
37 
38 #include <ctime>
39 
40 namespace boost {
41 namespace ui {
42 
46 
47 class BOOST_UI_DECL date_picker : public widget
48 {
49 public:
50  date_picker() {}
51 
53  explicit date_picker(widget& parent)
54  { create(parent); }
55  date_picker& create(widget& parent);
57 
60  std::time_t get_time_t() const;
61 
64  date_picker& set_time_t(std::time_t t);
65 
68  std::tm get_tm() const;
69 
72  date_picker& set_tm(const std::tm& t);
73 
74 #ifdef BOOST_UI_USE_DATE_TIME
75  boost::gregorian::date gregorian_date() const
79  {
80  int year = 0, month = 0, day = 0;
81  ymd(year, month, day);
82  return boost::gregorian::date(year, month, day);
83  }
84 
88  date_picker& gregorian_date(const boost::gregorian::date& d)
89  {
90  set_ymd(d.year(), d.month(), d.day());
91  return *this;
92  }
93 #endif
94 
95 #ifndef BOOST_NO_CXX11_HDR_CHRONO
96  std::chrono::system_clock::time_point system_clock_time_point() const
99  {
100  return std::chrono::system_clock::from_time_t(get_time_t());
101  }
102 
105  date_picker& system_clock_time_point(const std::chrono::system_clock::time_point& d)
106  {
107  set_time_t(std::chrono::system_clock::to_time_t(d));
108  return *this;
109  }
110 #endif
111 
112 #ifdef BOOST_UI_USE_CHRONO
113  boost::chrono::system_clock::time_point boost_system_clock_time_point() const
117  {
118  return boost::chrono::system_clock::from_time_t(get_time_t());
119  }
120 
124  date_picker& system_clock_time_point(const boost::chrono::system_clock::time_point& d)
125  {
126  set_time_t(boost::chrono::system_clock::to_time_t(d));
127  return *this;
128  }
129 #endif
130 
132  BOOST_UI_DETAIL_HANDLER(change, date_picker);
133 
134 #ifndef DOXYGEN
135 private:
136  void ymd(int& year, int& month, int& day) const;
137  void set_ymd(int year, int month, int day);
138 
139  void on_change_raw(const boost::function<void()>& handler);
140 
141  class detail_impl;
142  detail_impl* get_impl();
143  const detail_impl* get_impl() const;
144 #endif
145 };
146 
150 
151 class BOOST_UI_DECL time_picker : public widget
152 {
153 public:
154  time_picker() {}
155 
157  explicit time_picker(widget& parent)
158  { create(parent); }
159  time_picker& create(widget& parent);
161 
164  std::time_t get_time_t() const;
165 
168  time_picker& set_time_t(std::time_t t);
169 
172  std::tm get_tm() const;
173 
176  time_picker& set_tm(const std::tm& t);
177 
178 #ifdef BOOST_UI_USE_DATE_TIME
179  boost::posix_time::time_duration posix_time_duration() const
183  {
184  int hours = 0, minutes = 0, seconds = 0;
185  get_time_raw(&hours, &minutes, &seconds);
186  return boost::posix_time::time_duration(hours, minutes, seconds);
187  }
188 
192  time_picker& posix_time_duration(const boost::posix_time::time_duration& d)
193  {
194  set_time(d.hours(), d.minutes(), d.seconds());
195  return *this;
196  }
197 #endif
198 
199 #ifdef BOOST_UI_USE_CHRONO
200  template <class Rep, class Period>
204  boost::chrono::duration<Rep, Period> boost_chrono_duration() const
205  {
206  return boost::chrono::seconds(get_seconds());
207  }
208 
212  template <class Rep, class Period>
213  time_picker& chrono_duration(const boost::chrono::duration<Rep, Period>& d)
214  {
215  set_seconds(boost::chrono::duration_cast<boost::chrono::seconds>(d).count());
216  return *this;
217  }
218 #endif
219 
220 #ifndef BOOST_NO_CXX11_HDR_CHRONO
221  template <class Rep, class Period>
224  std::chrono::duration<Rep, Period> chrono_duration() const
225  {
226  return std::chrono::seconds(get_seconds());
227  }
228 
231  template <class Rep, class Period>
232  time_picker& chrono_duration(const std::chrono::duration<Rep, Period>& d)
233  {
234  set_seconds(std::chrono::duration_cast<std::chrono::seconds>(d).count());
235  return *this;
236  }
237 #endif
238 
240  template <class T>
241  void get_time(T hours_ptr, T minutes_ptr, T seconds_ptr)
242  {
243  int hours = 0;
244  int minutes = 0;
245  int seconds = 0;
246  get_time_raw(&hours, &minutes, &seconds);
247  if ( hours_ptr )
248  *hours_ptr = hours;
249  if ( minutes_ptr )
250  *minutes_ptr = minutes;
251  if ( seconds_ptr )
252  *seconds_ptr = seconds;
253  }
254 
257  time_picker& set_time(int hours, int minutes, int seconds);
258 
260  BOOST_UI_DETAIL_HANDLER(change, time_picker);
261 
262 private:
263  void get_time_raw(int* hours_ptr, int* minutes_ptr, int* seconds_ptr) const;
264  int get_seconds() const
265  {
266  int hours = 0, minutes = 0, seconds = 0;
267  get_time_raw(&hours, &minutes, &seconds);
268  return (hours * 60 + minutes) * 60 + seconds;
269  }
270  void set_seconds(boost::intmax_t seconds)
271  {
272  set_time(static_cast<int>(seconds / 3600), (seconds / 60) % 60, seconds % 60);
273  }
274 
275  void on_change_raw(const boost::function<void()>& handler);
276 
277  class detail_impl;
278  detail_impl* get_impl();
279  const detail_impl* get_impl() const;
280 };
281 
282 } // namespace ui
283 } // namespace boost
284 
285 #endif // BOOST_UI_DATETIME_HPP
boost::ui::time_picker::boost_chrono_duration
boost::chrono::duration< Rep, Period > boost_chrono_duration() const
Returns boost::chrono::duration.
Definition: datetime.hpp:204
boost::ui::date_picker::date_picker
date_picker(widget &parent)
Creates widget.
Definition: datetime.hpp:53
config.hpp
Configuration options.
widget.hpp
Widget class.
boost::ui::time_picker::time_picker
time_picker(widget &parent)
Creates widget.
Definition: datetime.hpp:157
boost
Boost C++ libraries namespace.
Definition: window.hpp:19
boost::ui::time_picker::get_time
void get_time(T hours_ptr, T minutes_ptr, T seconds_ptr)
Returns time.
Definition: datetime.hpp:241
boost::ui::date_picker
Date picker widget.
Definition: datetime.hpp:47
boost::ui::time_picker::chrono_duration
std::chrono::duration< Rep, Period > chrono_duration() const
Returns std::chrono::duration.
Definition: datetime.hpp:224
boost::ui::date_picker::gregorian_date
date_picker & gregorian_date(const boost::gregorian::date &d)
Sets boost::gregorian::date.
Definition: datetime.hpp:88
boost::ui::time_picker::chrono_duration
time_picker & chrono_duration(const std::chrono::duration< Rep, Period > &d)
Sets std::chrono::duration.
Definition: datetime.hpp:232
boost::ui::time_picker
Time picker widget.
Definition: datetime.hpp:151
boost::ui::time_picker::posix_time_duration
time_picker & posix_time_duration(const boost::posix_time::time_duration &d)
Sets boost::posix_time::time_duration.
Definition: datetime.hpp:192
boost::ui::date_picker::system_clock_time_point
date_picker & system_clock_time_point(const std::chrono::system_clock::time_point &d)
Sets std::chrono::system_clock::time_point.
Definition: datetime.hpp:105
boost::ui::widget
Base class for all widgets.
Definition: widget.hpp:45
boost::ui::date_picker::system_clock_time_point
date_picker & system_clock_time_point(const boost::chrono::system_clock::time_point &d)
Sets boost::chrono::system_clock::time_point.
Definition: datetime.hpp:124
boost::ui::time_picker::chrono_duration
time_picker & chrono_duration(const boost::chrono::duration< Rep, Period > &d)
Sets boost::chrono::duration.
Definition: datetime.hpp:213