date_clock_device.hpp 2.2 KB

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