diff options
author | petrk <petrk@yandex-team.ru> | 2022-02-10 16:47:26 +0300 |
---|---|---|
committer | Daniil Cherednik <dcherednik@yandex-team.ru> | 2022-02-10 16:47:26 +0300 |
commit | af66956edf116b93d5a07894ccb61dd4447d0b34 (patch) | |
tree | 7b4cde47a2b3630f5a3048855949c4d46b893fc0 /library/cpp/timezone_conversion/civil.h | |
parent | dc5feacd15e76abe98d23fe0d4ea3c02cb8de60f (diff) | |
download | ydb-af66956edf116b93d5a07894ccb61dd4447d0b34.tar.gz |
Restoring authorship annotation for <petrk@yandex-team.ru>. Commit 1 of 2.
Diffstat (limited to 'library/cpp/timezone_conversion/civil.h')
-rw-r--r-- | library/cpp/timezone_conversion/civil.h | 404 |
1 files changed, 202 insertions, 202 deletions
diff --git a/library/cpp/timezone_conversion/civil.h b/library/cpp/timezone_conversion/civil.h index 0e95b807ed..b5917dad52 100644 --- a/library/cpp/timezone_conversion/civil.h +++ b/library/cpp/timezone_conversion/civil.h @@ -1,48 +1,48 @@ -#pragma once - -#include <util/datetime/base.h> - +#pragma once + +#include <util/datetime/base.h> + #include <contrib/libs/cctz/include/cctz/civil_time.h> #include <contrib/libs/cctz/include/cctz/time_zone.h> - -#include <util/generic/strbuf.h> -#include <util/generic/string.h> -#include <util/generic/yexception.h> - -#if __clang__ && __cpp_constexpr >= 201304 -#define CONSTEXPR_M constexpr -#else -#define CONSTEXPR_M inline -#endif - -namespace NDatetime { - /** Exception class which throws when time zone is not valid - */ - class TInvalidTimezone: public yexception { - }; - - using TSystemClock = std::chrono::system_clock; - using TTimePoint = std::chrono::time_point<TSystemClock>; - - /* - * An opaque class representing past, present, and future rules of - * mapping between absolute and civil times in a given region. - * It is very lightweight and may be passed by value. - */ - using TTimeZone = cctz::time_zone; - - using TCivilYear = cctz::civil_year; - using TCivilMonth = cctz::civil_month; - using TCivilDay = cctz::civil_day; - using TCivilHour = cctz::civil_hour; - using TCivilMinute = cctz::civil_minute; - using TCivilSecond = cctz::civil_second; - using TWeekday = cctz::weekday; - using TDiff = cctz::diff_t; - - using TYear = cctz::year_t; - using TMonth = cctz::detail::month_t; - + +#include <util/generic/strbuf.h> +#include <util/generic/string.h> +#include <util/generic/yexception.h> + +#if __clang__ && __cpp_constexpr >= 201304 +#define CONSTEXPR_M constexpr +#else +#define CONSTEXPR_M inline +#endif + +namespace NDatetime { + /** Exception class which throws when time zone is not valid + */ + class TInvalidTimezone: public yexception { + }; + + using TSystemClock = std::chrono::system_clock; + using TTimePoint = std::chrono::time_point<TSystemClock>; + + /* + * An opaque class representing past, present, and future rules of + * mapping between absolute and civil times in a given region. + * It is very lightweight and may be passed by value. + */ + using TTimeZone = cctz::time_zone; + + using TCivilYear = cctz::civil_year; + using TCivilMonth = cctz::civil_month; + using TCivilDay = cctz::civil_day; + using TCivilHour = cctz::civil_hour; + using TCivilMinute = cctz::civil_minute; + using TCivilSecond = cctz::civil_second; + using TWeekday = cctz::weekday; + using TDiff = cctz::diff_t; + + using TYear = cctz::year_t; + using TMonth = cctz::detail::month_t; + enum class ECivilUnit : int { Second = 0, Minute = 1, @@ -68,7 +68,7 @@ namespace NDatetime { template <ECivilUnit Unit> using TCivilTime = typename NDetail::TGetCivilTime<Unit>::TResult; - /** + /** * Class with variable unit diff. */ struct TCivilDiff { @@ -90,16 +90,16 @@ namespace NDatetime { }; /** - * Gets the time zone by an IANA name. - * @param name A name in IANA format (e.g., "Europe/Moscow"). - * @note After you request a time zone for the first time, it is cached - * in a (thread-safe) cache, so subsequent requests to the - * same time zone should be extremely fast. - * @throw TInvalidTimezone if the name is invalid. - * @see http://www.iana.org/time-zones - */ - TTimeZone GetTimeZone(TStringBuf name); - + * Gets the time zone by an IANA name. + * @param name A name in IANA format (e.g., "Europe/Moscow"). + * @note After you request a time zone for the first time, it is cached + * in a (thread-safe) cache, so subsequent requests to the + * same time zone should be extremely fast. + * @throw TInvalidTimezone if the name is invalid. + * @see http://www.iana.org/time-zones + */ + TTimeZone GetTimeZone(TStringBuf name); + /** * Returns a time zone that is a fixed offset (seconds east) from UTC. * Note: If the absolute value of the offset is greater than 24 hours @@ -118,42 +118,42 @@ namespace NDatetime { return cctz::convert(cctz::convert(src, from), to); } - /** Convert absolute time to civil time by rules from timezone. - * @param[in] absTime is an absolute time which is used to convert - * @param[in] tz is a loaded timezone - * @return a civil time + /** Convert absolute time to civil time by rules from timezone. + * @param[in] absTime is an absolute time which is used to convert + * @param[in] tz is a loaded timezone + * @return a civil time * * Note: This function doesn't work properly for dates before 1 Jan 1970! - */ - TCivilSecond Convert(const TInstant& absTime, const TTimeZone& tz); - - /** Convert absolute time to civil time by rules from timezone which will be loaded. - * @throw InvalidTimezone if the name is invalid. - * @param[in] absTime is an absolute time which is used to convert - * @param[in] tzName is a timezone name which will be loaded - * @return a civil time + */ + TCivilSecond Convert(const TInstant& absTime, const TTimeZone& tz); + + /** Convert absolute time to civil time by rules from timezone which will be loaded. + * @throw InvalidTimezone if the name is invalid. + * @param[in] absTime is an absolute time which is used to convert + * @param[in] tzName is a timezone name which will be loaded + * @return a civil time * * Note: This function doesn't work properly for dates before 1 Jan 1970! - */ - TCivilSecond Convert(const TInstant& absTime, TStringBuf tzName); - - /** Convert a civil time to absolute by using rules from timezone + */ + TCivilSecond Convert(const TInstant& absTime, TStringBuf tzName); + + /** Convert a civil time to absolute by using rules from timezone * * Note: This function doesn't work properly for dates before 1 Jan 1970! - */ - TInstant Convert(const TCivilSecond& s, const TTimeZone& tz); - - /** Just to simply calculations between dates/times. - * NDatetime::Calc<TCivilDay>(TCivilSecond(2001, 1, 1, 10, 10, 10), 5); // returns TCivilDay(2001, 1, 6); + */ + TInstant Convert(const TCivilSecond& s, const TTimeZone& tz); + + /** Just to simply calculations between dates/times. + * NDatetime::Calc<TCivilDay>(TCivilSecond(2001, 1, 1, 10, 10, 10), 5); // returns TCivilDay(2001, 1, 6); * @param[in] tp is a timepoint with which calc will be - * @param[in] diff is quantity of which will be added (of type T) to the tp - * @return the calculated T type - */ - template <typename T, typename S> + * @param[in] diff is quantity of which will be added (of type T) to the tp + * @return the calculated T type + */ + template <typename T, typename S> inline T Calc(const S& tp, TDiff diff) { - return T(tp) + diff; - } - + return T(tp) + diff; + } + /** Non-template methods for adding dates/times. * @param[in] tp is a timepoint with which calc will be * @param[in] diff is quantity of which will be added to the tp @@ -184,124 +184,124 @@ namespace NDatetime { */ TCivilDiff GetCivilDiff(const TCivilSecond& tpX, const TCivilSecond& tpY, ECivilUnit unit); - /** Formats the given TimePoint in the given TTimeZone according to - * the provided format string. Uses strftime()-like formatting options, - * with the following extensions: - * - * - %Ez - RFC3339-compatible numeric time zone (+hh:mm or -hh:mm) - * - %E#S - Seconds with # digits of fractional precision - * - %E*S - Seconds with full fractional precision (a literal '*') - * - %E#f - Fractional seconds with # digits of precision - * - %E*f - Fractional seconds with full precision (a literal '*') - * - %E4Y - Four-character years (-999 ... -001, 0000, 0001 ... 9999) - * - * Note that %E0S behaves like %S, and %E0f produces no characters. In - * contrast %E*f always produces at least one digit, which may be '0'. - * - * Note that %Y produces as many characters as it takes to fully render the - * year. A year outside of [-999:9999] when formatted with %E4Y will produce - * more than four characters, just like %Y. - * - * Tip: Format strings should include the UTC offset (e.g., %z or %Ez) so that - * the resultng string uniquely identifies an absolute time. - * - * Example: - * NDatetime::TTimeZone lax = NDatetime::GetTimeZone("America/Los_Angeles"); - * NDatetime::TCivilSecond tp(2013, 1, 2, 3, 4, 5); - * TString f = NDatetime::Format("%H:%M:%S", tp, lax); // "03:04:05" - * TString f = NDatetime::Format("%H:%M:%E3S", tp, lax); //"03:04:05.000" - */ - template <typename TP> - TString Format(TStringBuf fmt, const TP& tp, const TTimeZone& tz) { + /** Formats the given TimePoint in the given TTimeZone according to + * the provided format string. Uses strftime()-like formatting options, + * with the following extensions: + * + * - %Ez - RFC3339-compatible numeric time zone (+hh:mm or -hh:mm) + * - %E#S - Seconds with # digits of fractional precision + * - %E*S - Seconds with full fractional precision (a literal '*') + * - %E#f - Fractional seconds with # digits of precision + * - %E*f - Fractional seconds with full precision (a literal '*') + * - %E4Y - Four-character years (-999 ... -001, 0000, 0001 ... 9999) + * + * Note that %E0S behaves like %S, and %E0f produces no characters. In + * contrast %E*f always produces at least one digit, which may be '0'. + * + * Note that %Y produces as many characters as it takes to fully render the + * year. A year outside of [-999:9999] when formatted with %E4Y will produce + * more than four characters, just like %Y. + * + * Tip: Format strings should include the UTC offset (e.g., %z or %Ez) so that + * the resultng string uniquely identifies an absolute time. + * + * Example: + * NDatetime::TTimeZone lax = NDatetime::GetTimeZone("America/Los_Angeles"); + * NDatetime::TCivilSecond tp(2013, 1, 2, 3, 4, 5); + * TString f = NDatetime::Format("%H:%M:%S", tp, lax); // "03:04:05" + * TString f = NDatetime::Format("%H:%M:%E3S", tp, lax); //"03:04:05.000" + */ + template <typename TP> + TString Format(TStringBuf fmt, const TP& tp, const TTimeZone& tz) { return TString(cctz::format(static_cast<std::string>(fmt), TTimePoint(cctz::convert(tp, tz)), tz)); - } - - /** Returns the weekday by day. - * @param[in] day is a given day - * @return a weekday (enum) - */ - CONSTEXPR_M TWeekday GetWeekday(const TCivilDay& day) noexcept { - return cctz::get_weekday(day); - } - - /** Returns the TCivilDay that strictly follows or precedes the given - * civil_day, and that falls on the given weekday. - * @code - For example, given: - - August 2015 - Su Mo Tu We Th Fr Sa - 1 - 2 3 4 5 6 7 8 - 9 10 11 12 13 14 15 - 16 17 18 19 20 21 22 - 23 24 25 26 27 28 29 - 30 31 - - TCivilDay a(2015, 8, 13); // GetWeekday(a) == TWeekday::thursday - TCivilDay b = NextWeekday(a, TWeekday::thursday); // b = 2015-08-20 - TCivilDay c = PrevWeekday(a, TWeekday::thursday); // c = 2015-08-06 - TCivilDay d = NearestWeekday(a, TWeekday::thursday); // d = 2015-08-13 - - TCivilDay e = ... - // Gets the following Thursday if e is not already Thursday - TCivilDay thurs1 = PrevWeekday(e, TWeekday::thursday) + 7; - // Gets the previous Thursday if e is not already Thursday - TCivilDay thurs2 = NextWeekday(e, TWeekday::thursday) - 7; - * @endcode - * @see PrevWeekday() - * @see NearestWeekday() - * @param[in] cd is a current day - * @param[in] wd is a weekday which wanetd for find on next week - * @return a civil day on weekday next week - */ - CONSTEXPR_M TCivilDay NextWeekday(const TCivilDay& cd, TWeekday wd) noexcept { - return cctz::next_weekday(cd, wd); - } - - /** Returns prev weekday. See the description of NextWeekday(). - * @see NextWeekday() - * @see NearestWeekday() - * @param[in] cd is a current day - * @param[in] wd is a weekday which is looking for (backward) - * @return a first occurence of the given weekday (backward) - */ - CONSTEXPR_M TCivilDay PrevWeekday(const TCivilDay& cd, TWeekday wd) noexcept { - return cctz::prev_weekday(cd, wd); - } - - /** Find a nearest occurence of the given weekday forward (could be current day). - * @see NextWeekday() - * @param[in] cd is a current day - * @param[in] wd is a weekday which is looking for (current day or later) - * @return first occurence (including current day) of the given weekday - */ - CONSTEXPR_M TCivilDay NearestWeekday(const TCivilDay& cd, TWeekday wd) noexcept { - return get_weekday(cd) != wd ? next_weekday(cd, wd) : cd; - } - - /** Find the date of the given weekday within the given week. - * @param[in] cd is a current day - * @param[in] wd is a requested week day - * @return day within a week of a given day - */ - CONSTEXPR_M TCivilDay WeekdayOnTheWeek(const TCivilDay& cd, TWeekday wd) noexcept { - const auto d = get_weekday(cd); - if (d == wd) - return cd; - - return d < wd ? NextWeekday(cd, wd) : PrevWeekday(cd, wd); - } - - /** Returns an absolute day of year by given day. - */ - CONSTEXPR_M int GetYearDay(const TCivilDay& cd) noexcept { - return cctz::get_yearday(cd); - } - - CONSTEXPR_M int DaysPerMonth(TYear year, TMonth month) noexcept { - return cctz::detail::impl::days_per_month(year, month); - } + } + + /** Returns the weekday by day. + * @param[in] day is a given day + * @return a weekday (enum) + */ + CONSTEXPR_M TWeekday GetWeekday(const TCivilDay& day) noexcept { + return cctz::get_weekday(day); + } + + /** Returns the TCivilDay that strictly follows or precedes the given + * civil_day, and that falls on the given weekday. + * @code + For example, given: + + August 2015 + Su Mo Tu We Th Fr Sa + 1 + 2 3 4 5 6 7 8 + 9 10 11 12 13 14 15 + 16 17 18 19 20 21 22 + 23 24 25 26 27 28 29 + 30 31 + + TCivilDay a(2015, 8, 13); // GetWeekday(a) == TWeekday::thursday + TCivilDay b = NextWeekday(a, TWeekday::thursday); // b = 2015-08-20 + TCivilDay c = PrevWeekday(a, TWeekday::thursday); // c = 2015-08-06 + TCivilDay d = NearestWeekday(a, TWeekday::thursday); // d = 2015-08-13 + + TCivilDay e = ... + // Gets the following Thursday if e is not already Thursday + TCivilDay thurs1 = PrevWeekday(e, TWeekday::thursday) + 7; + // Gets the previous Thursday if e is not already Thursday + TCivilDay thurs2 = NextWeekday(e, TWeekday::thursday) - 7; + * @endcode + * @see PrevWeekday() + * @see NearestWeekday() + * @param[in] cd is a current day + * @param[in] wd is a weekday which wanetd for find on next week + * @return a civil day on weekday next week + */ + CONSTEXPR_M TCivilDay NextWeekday(const TCivilDay& cd, TWeekday wd) noexcept { + return cctz::next_weekday(cd, wd); + } + + /** Returns prev weekday. See the description of NextWeekday(). + * @see NextWeekday() + * @see NearestWeekday() + * @param[in] cd is a current day + * @param[in] wd is a weekday which is looking for (backward) + * @return a first occurence of the given weekday (backward) + */ + CONSTEXPR_M TCivilDay PrevWeekday(const TCivilDay& cd, TWeekday wd) noexcept { + return cctz::prev_weekday(cd, wd); + } + + /** Find a nearest occurence of the given weekday forward (could be current day). + * @see NextWeekday() + * @param[in] cd is a current day + * @param[in] wd is a weekday which is looking for (current day or later) + * @return first occurence (including current day) of the given weekday + */ + CONSTEXPR_M TCivilDay NearestWeekday(const TCivilDay& cd, TWeekday wd) noexcept { + return get_weekday(cd) != wd ? next_weekday(cd, wd) : cd; + } + + /** Find the date of the given weekday within the given week. + * @param[in] cd is a current day + * @param[in] wd is a requested week day + * @return day within a week of a given day + */ + CONSTEXPR_M TCivilDay WeekdayOnTheWeek(const TCivilDay& cd, TWeekday wd) noexcept { + const auto d = get_weekday(cd); + if (d == wd) + return cd; + + return d < wd ? NextWeekday(cd, wd) : PrevWeekday(cd, wd); + } + + /** Returns an absolute day of year by given day. + */ + CONSTEXPR_M int GetYearDay(const TCivilDay& cd) noexcept { + return cctz::get_yearday(cd); + } + + CONSTEXPR_M int DaysPerMonth(TYear year, TMonth month) noexcept { + return cctz::detail::impl::days_per_month(year, month); + } CONSTEXPR_M int DaysPerYear(TYear year, TMonth month) noexcept { return cctz::detail::impl::days_per_year(year, month); @@ -331,8 +331,8 @@ namespace NDatetime { return (daysCount + static_cast<int>(jan1) - 1) / 7 + (jan1 == cctz::weekday::monday || jan1 == cctz::weekday::tuesday || jan1 == cctz::weekday::wednesday); } -} - +} + #include "civil-inl.h" -#undef CONSTEXPR_M +#undef CONSTEXPR_M |