date_time_facet.hpp 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. //
  2. // Copyright (c) 2009-2011 Artyom Beilis (Tonkikh)
  3. //
  4. // Distributed under the Boost Software License, Version 1.0.
  5. // https://www.boost.org/LICENSE_1_0.txt
  6. #ifndef BOOST_LOCALE_DATE_TIME_FACET_HPP_INCLUDED
  7. #define BOOST_LOCALE_DATE_TIME_FACET_HPP_INCLUDED
  8. #include <boost/locale/config.hpp>
  9. #include <boost/cstdint.hpp>
  10. #include <locale>
  11. #ifdef BOOST_MSVC
  12. # pragma warning(push)
  13. # pragma warning(disable : 4275 4251 4231 4660)
  14. #endif
  15. namespace boost { namespace locale {
  16. /// \brief Namespace that contains various types for manipulation with dates
  17. namespace period {
  18. /// \brief This namespace holds a enum of various period types like era, year, month, etc..
  19. namespace marks {
  20. /// \brief the type that defines a flag that holds a period identifier
  21. enum period_mark {
  22. invalid, ///< Special invalid value, should not be used directly
  23. era, ///< Era i.e. AC, BC in Gregorian and Julian calendar, range [0,1]
  24. year, ///< Year, it is calendar specific, for example 2011 in Gregorian calendar.
  25. extended_year, ///< Extended year for Gregorian/Julian calendars, where 1 BC == 0, 2 BC == -1.
  26. month, ///< The month of year, calendar specific, in Gregorian [0..11]
  27. day, ///< The day of month, calendar specific, in Gregorian [1..31]
  28. day_of_year, ///< The number of day in year, starting from 1, in Gregorian [1..366]
  29. day_of_week, ///< Day of week, Sunday=1, Monday=2,..., Saturday=7.
  30. ///< Note that updating this value respects local day of week, so for example,
  31. ///< If first day of week is Monday and the current day is Tuesday then setting
  32. ///< the value to Sunday (1) would forward the date by 5 days forward and not backward
  33. ///< by two days as it could be expected if the numbers were taken as is.
  34. day_of_week_in_month, ///< Original number of the day of the week in month. For example 1st Sunday,
  35. ///< 2nd Sunday, etc. in Gregorian [1..5]
  36. day_of_week_local, ///< Local day of week, for example in France Monday is 1, in US Sunday is 1, [1..7]
  37. hour, ///< 24 clock hour [0..23]
  38. hour_12, ///< 12 clock hour [0..11]
  39. am_pm, ///< am or pm marker [0..1]
  40. minute, ///< minute [0..59]
  41. second, ///< second [0..59]
  42. week_of_year, ///< The week number in the year
  43. week_of_month, ///< The week number within current month
  44. first_day_of_week, ///< First day of week, constant, for example Sunday in US = 1, Monday in France = 2
  45. };
  46. } // namespace marks
  47. /// \brief This class holds a type that represents certain period of time like
  48. /// year, hour, second and so on.
  49. ///
  50. /// It can be created from either marks::period_mark type or by using shortcuts in period
  51. /// namespace - calling functions like period::year(), period::hour() and so on.
  52. ///
  53. /// Basically it represents the same object as enum marks::period_mark but allows to
  54. /// provide save operator overloading that would not collide with casing of enum to
  55. /// numeric values.
  56. class period_type {
  57. public:
  58. /// Create a period of specific type, default is invalid.
  59. period_type(marks::period_mark m = marks::invalid) : mark_(m) {}
  60. /// Get the value of marks::period_mark it was created with.
  61. marks::period_mark mark() const { return mark_; }
  62. /// Check if two periods are the same
  63. bool operator==(const period_type& other) const { return mark() == other.mark(); }
  64. /// Check if two periods are different
  65. bool operator!=(const period_type& other) const { return mark() != other.mark(); }
  66. private:
  67. marks::period_mark mark_;
  68. };
  69. } // namespace period
  70. /// Structure that define POSIX time, seconds and milliseconds
  71. /// since Jan 1, 1970, 00:00 not including leap seconds.
  72. struct posix_time {
  73. int64_t seconds; ///< Seconds since epoch
  74. uint32_t nanoseconds; ///< Nanoseconds resolution
  75. };
  76. /// This class defines generic calendar class, it is used by date_time and calendar
  77. /// objects internally. It is less useful for end users, but it is build for localization
  78. /// backend implementation
  79. class BOOST_LOCALE_DECL abstract_calendar {
  80. public:
  81. /// Type that defines how to fetch the value
  82. enum value_type {
  83. absolute_minimum, ///< Absolute possible minimum for the value, for example for day is 1
  84. actual_minimum, ///< Actual minimal value for this period.
  85. greatest_minimum, ///< Maximal minimum value that can be for this period
  86. current, ///< Current value of this period
  87. least_maximum, ///< The last maximal value for this period, For example for Gregorian calendar
  88. ///< day it is 28
  89. actual_maximum, ///< Actual maximum, for it can be 28, 29, 30, 31 for day according to current month
  90. absolute_maximum, ///< Maximal value, for Gregorian day it would be 31.
  91. };
  92. /// A way to update the value
  93. enum update_type {
  94. move, ///< Change the value up or down effecting others for example 1990-12-31 + 1 day = 1991-01-01
  95. roll, ///< Change the value up or down not effecting others for example 1990-12-31 + 1 day = 1990-12-01
  96. };
  97. /// Information about calendar
  98. enum calendar_option_type {
  99. is_gregorian, ///< Check if the calendar is Gregorian
  100. is_dst ///< Check if the current time is in daylight time savings
  101. };
  102. /// Make a polymorphic copy of the calendar
  103. virtual abstract_calendar* clone() const = 0;
  104. /// Set specific \a value for period \a p, note not all values are settable.
  105. ///
  106. /// After calling set_value you may want to call normalize() function to make sure
  107. /// all periods are updated, if you set several fields that are part of a single
  108. /// date/time representation you should call set_value several times and then
  109. /// call normalize().
  110. ///
  111. /// If normalize() is not called after set_value, the behavior is undefined
  112. virtual void set_value(period::marks::period_mark m, int value) = 0;
  113. /// Recalculate all periods after setting them, should be called after use of set_value() function.
  114. virtual void normalize() = 0;
  115. /// Get specific value for period \a p according to a value_type \a v
  116. virtual int get_value(period::marks::period_mark m, value_type v) const = 0;
  117. /// Set current time point
  118. virtual void set_time(const posix_time& p) = 0;
  119. /// Get current time point
  120. virtual posix_time get_time() const = 0;
  121. /// Get current time since epoch in milliseconds
  122. virtual double get_time_ms() const = 0;
  123. /// Set option for calendar, for future use
  124. virtual void set_option(calendar_option_type opt, int v) = 0;
  125. /// Get option for calendar, currently only check if it is Gregorian calendar
  126. virtual int get_option(calendar_option_type opt) const = 0;
  127. /// Adjust period's \a p value by \a difference items using a update_type \a u.
  128. /// Note: not all values are adjustable
  129. virtual void adjust_value(period::marks::period_mark m, update_type u, int difference) = 0;
  130. /// Calculate the difference between this calendar and \a other in \a p units
  131. virtual int difference(const abstract_calendar& other, period::marks::period_mark m) const = 0;
  132. /// Set time zone, empty - use system
  133. virtual void set_timezone(const std::string& tz) = 0;
  134. /// Get current time zone, empty - system one
  135. virtual std::string get_timezone() const = 0;
  136. /// Check of two calendars have same rules
  137. virtual bool same(const abstract_calendar* other) const = 0;
  138. virtual ~abstract_calendar();
  139. };
  140. /// \brief the facet that generates calendar for specific locale
  141. class BOOST_LOCALE_DECL calendar_facet : public std::locale::facet {
  142. public:
  143. /// Basic constructor
  144. calendar_facet(size_t refs = 0) : std::locale::facet(refs) {}
  145. ~calendar_facet();
  146. /// Create a new calendar that points to current point of time.
  147. virtual abstract_calendar* create_calendar() const = 0;
  148. /// Locale id (needed to work with std::locale)
  149. static std::locale::id id;
  150. };
  151. }} // namespace boost::locale
  152. #ifdef BOOST_MSVC
  153. # pragma warning(pop)
  154. #endif
  155. #endif