time_clock.hpp 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. #ifndef DATE_TIME_TIME_CLOCK_HPP___
  2. #define DATE_TIME_TIME_CLOCK_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. /*! @file time_clock.hpp
  11. This file contains the interface for clock devices.
  12. */
  13. #include "boost/date_time/c_time.hpp"
  14. #include "boost/shared_ptr.hpp"
  15. namespace boost {
  16. namespace date_time {
  17. //! A clock providing time level services based on C time_t capabilities
  18. /*! This clock provides resolution to the 1 second level
  19. */
  20. template<class time_type>
  21. class second_clock
  22. {
  23. public:
  24. typedef typename time_type::date_type date_type;
  25. typedef typename time_type::time_duration_type time_duration_type;
  26. static time_type local_time()
  27. {
  28. ::std::time_t t;
  29. ::std::time(&t);
  30. ::std::tm curr, *curr_ptr;
  31. //curr_ptr = ::std::localtime(&t);
  32. curr_ptr = c_time::localtime(&t, &curr);
  33. return create_time(curr_ptr);
  34. }
  35. //! Get the current day in universal date as a ymd_type
  36. static time_type universal_time()
  37. {
  38. ::std::time_t t;
  39. ::std::time(&t);
  40. ::std::tm curr, *curr_ptr;
  41. //curr_ptr = ::std::gmtime(&t);
  42. curr_ptr = c_time::gmtime(&t, &curr);
  43. return create_time(curr_ptr);
  44. }
  45. template<class time_zone_type>
  46. static time_type local_time(boost::shared_ptr<time_zone_type> tz_ptr)
  47. {
  48. typedef typename time_type::utc_time_type utc_time_type;
  49. utc_time_type utc_time = second_clock<utc_time_type>::universal_time();
  50. return time_type(utc_time, tz_ptr);
  51. }
  52. private:
  53. static time_type create_time(::std::tm* current)
  54. {
  55. date_type d(static_cast<unsigned short>(current->tm_year + 1900),
  56. static_cast<unsigned short>(current->tm_mon + 1),
  57. static_cast<unsigned short>(current->tm_mday));
  58. time_duration_type td(current->tm_hour,
  59. current->tm_min,
  60. current->tm_sec);
  61. return time_type(d,td);
  62. }
  63. };
  64. } } //namespace date_time
  65. #endif