diff options
author | gotmanov <gotmanov@yandex-team.ru> | 2022-02-10 16:49:26 +0300 |
---|---|---|
committer | Daniil Cherednik <dcherednik@yandex-team.ru> | 2022-02-10 16:49:26 +0300 |
commit | b3c1a086292c74ed88830693f15a96ddeadc6b9a (patch) | |
tree | 5d5cb817648f650d76cf1076100726fd9b8448e8 /library | |
parent | 243edeedfd2c5152b5c7812e0af471e1bb0232e8 (diff) | |
download | ydb-b3c1a086292c74ed88830693f15a96ddeadc6b9a.tar.gz |
Restoring authorship annotation for <gotmanov@yandex-team.ru>. Commit 2 of 2.
Diffstat (limited to 'library')
-rw-r--r-- | library/cpp/codecs/float_huffman.h | 2 | ||||
-rw-r--r-- | library/cpp/codecs/ut/float_huffman_ut.cpp | 38 | ||||
-rw-r--r-- | library/cpp/enumbitset/enumbitset.h | 88 | ||||
-rw-r--r-- | library/cpp/json/writer/ya.make | 4 | ||||
-rw-r--r-- | library/cpp/protobuf/util/is_equal.cpp | 6 | ||||
-rw-r--r-- | library/cpp/protobuf/util/is_equal.h | 6 | ||||
-rw-r--r-- | library/cpp/protobuf/util/is_equal_ut.cpp | 84 | ||||
-rw-r--r-- | library/cpp/protobuf/util/traits.h | 14 | ||||
-rw-r--r-- | library/cpp/timezone_conversion/civil-inl.h | 122 | ||||
-rw-r--r-- | library/cpp/timezone_conversion/civil.cpp | 102 | ||||
-rw-r--r-- | library/cpp/timezone_conversion/civil.h | 132 | ||||
-rw-r--r-- | library/cpp/timezone_conversion/ut/civil_ut.cpp | 86 | ||||
-rw-r--r-- | library/cpp/timezone_conversion/ya.make | 4 |
13 files changed, 344 insertions, 344 deletions
diff --git a/library/cpp/codecs/float_huffman.h b/library/cpp/codecs/float_huffman.h index e94a778115..786a8eae1d 100644 --- a/library/cpp/codecs/float_huffman.h +++ b/library/cpp/codecs/float_huffman.h @@ -47,4 +47,4 @@ namespace NCodecs::NFloatHuff { }; TVector<float> Decode(TStringBuf data, size_t sizeHint = 0); -} +} diff --git a/library/cpp/codecs/ut/float_huffman_ut.cpp b/library/cpp/codecs/ut/float_huffman_ut.cpp index 55e578ca03..3156fb1f46 100644 --- a/library/cpp/codecs/ut/float_huffman_ut.cpp +++ b/library/cpp/codecs/ut/float_huffman_ut.cpp @@ -136,11 +136,11 @@ Y_UNIT_TEST_SUITE(FloatHuffmanTest) { static const size_t CodedSize = Y_ARRAY_SIZE(CodedFactors); static const TStringBuf CodedFactorsBuf(reinterpret_cast<const char*>(CodedFactors), CodedSize); - void FillWithGarbage(float* factors, size_t count) { - void* data = static_cast<void*>(factors); - memset(data, 0xAA, sizeof(float) * count); - } - + void FillWithGarbage(float* factors, size_t count) { + void* data = static_cast<void*>(factors); + memset(data, 0xAA, sizeof(float) * count); + } + // Helper for dumping compressed values void PrintCompressed(const TVector<ui8>& codedFactors) { for (size_t i = 0; i < codedFactors.size(); ++i) { @@ -184,7 +184,7 @@ Y_UNIT_TEST_SUITE(FloatHuffmanTest) { Y_UNIT_TEST(TestDecompressInParts) { float factors[FactorCount]; - FillWithGarbage(factors, FactorCount); + FillWithGarbage(factors, FactorCount); fh::TDecoder decoder(CodedFactorsBuf); const size_t firstPack = 100; // unpack first pack @@ -198,26 +198,26 @@ Y_UNIT_TEST_SUITE(FloatHuffmanTest) { } Y_UNIT_TEST(TestSkip) { - float factors[FactorCount]; - FillWithGarbage(factors, FactorCount); + float factors[FactorCount]; + FillWithGarbage(factors, FactorCount); fh::TDecoder decoder(CodedFactorsBuf); - const size_t firstPack = 100; - // unpack first pack + const size_t firstPack = 100; + // unpack first pack UNIT_ASSERT_VALUES_EQUAL(decoder.Decode({factors, firstPack}), firstPack); - // skip some factors - const size_t skipCount = 60; + // skip some factors + const size_t skipCount = 60; UNIT_ASSERT_VALUES_EQUAL(decoder.Skip(skipCount / 2), skipCount / 2); - // unpack all, except some factors in the end + // unpack all, except some factors in the end const auto toDecode = FactorCount - firstPack - skipCount; UNIT_ASSERT_VALUES_EQUAL(decoder.Decode({factors + firstPack, toDecode}), toDecode); UNIT_ASSERT_VALUES_EQUAL(decoder.Skip(skipCount / 2), skipCount / 2); for (size_t i = 0; i < FactorCount - skipCount; ++i) { - size_t correctedI = i < firstPack ? i : i + skipCount / 2; - UNIT_ASSERT_VALUES_EQUAL(factors[i], Factors[correctedI]); - } - //PrintDecompressed(factors); - } - + size_t correctedI = i < firstPack ? i : i + skipCount / 2; + UNIT_ASSERT_VALUES_EQUAL(factors[i], Factors[correctedI]); + } + //PrintDecompressed(factors); + } + Y_UNIT_TEST(TestDecompressForgedData) { // this coredumps without end-of-coded-stream check, see SEARCH-1156 for details TString brokenBase64Encoded = diff --git a/library/cpp/enumbitset/enumbitset.h b/library/cpp/enumbitset/enumbitset.h index 178dd497ce..41864c3a04 100644 --- a/library/cpp/enumbitset/enumbitset.h +++ b/library/cpp/enumbitset/enumbitset.h @@ -11,12 +11,12 @@ // Stack memory bitmask for TEnum values [begin, end). // @end value is not included in the mask and is not necessarily defined as enum value. // For example: enum EType { A, B, C } ==> TEnumBitSet<EType, A, C + 1> -template <typename TEnum, int mbegin, int mend> +template <typename TEnum, int mbegin, int mend> class TEnumBitSet: private TBitMap<mend - mbegin> { public: - static const int BeginIndex = mbegin; - static const int EndIndex = mend; - static const size_t BitsetSize = EndIndex - BeginIndex; + static const int BeginIndex = mbegin; + static const int EndIndex = mend; + static const size_t BitsetSize = EndIndex - BeginIndex; typedef TBitMap<BitsetSize> TParent; typedef TEnumBitSet<TEnum, mbegin, mend> TThis; @@ -57,7 +57,7 @@ public: } static bool IsValid(TEnum c) { - return int(c) >= BeginIndex && int(c) < EndIndex; + return int(c) >= BeginIndex && int(c) < EndIndex; } bool Test(TEnum c) const { @@ -260,7 +260,7 @@ public: //serialization to/from stream void Save(IOutputStream* buffer) const { ::Save(buffer, (ui32)Count()); - for (TEnum bit : *this) { + for (TEnum bit : *this) { ::Save(buffer, (ui32)bit); } } @@ -271,7 +271,7 @@ public: ui32 sz = 0; ::Load(buffer, sz); - for (ui32 t = 0; t < sz; t++) { + for (ui32 t = 0; t < sz; t++) { ui32 bit = 0; ::Load(buffer, bit); @@ -350,18 +350,18 @@ public: class TIterator { public: - TIterator(TEnum value, const TThis* bitmap) noexcept - : Value(static_cast<int>(value)) + TIterator(TEnum value, const TThis* bitmap) noexcept + : Value(static_cast<int>(value)) + , BitMap(bitmap) + { + } + + TIterator(const TThis* bitmap) noexcept + : Value(EndIndex) , BitMap(bitmap) { } - TIterator(const TThis* bitmap) noexcept - : Value(EndIndex) - , BitMap(bitmap) - { - } - TEnum operator*() const noexcept { Y_ASSERT(Value < EndIndex); return static_cast<TEnum>(Value); @@ -373,12 +373,12 @@ public: TIterator& operator++() noexcept { Y_ASSERT(Value < EndIndex); - TEnum res; - if (BitMap->FindNext(static_cast<TEnum>(Value), res)) { - Value = static_cast<int>(res); - } else { - Value = EndIndex; - } + TEnum res; + if (BitMap->FindNext(static_cast<TEnum>(Value), res)) { + Value = static_cast<int>(res); + } else { + Value = EndIndex; + } return *this; } @@ -389,12 +389,12 @@ public: }; TIterator begin() const { - TEnum res; - return FindFirst(res) ? TIterator(res, this) : TIterator(this); + TEnum res; + return FindFirst(res) ? TIterator(res, this) : TIterator(this); } TIterator end() const { - return TIterator(this); + return TIterator(this); } private: @@ -410,26 +410,26 @@ private: bool HasAll(TEnum c) const { return Test(c); } - - bool FindFirst(TEnum& result) const { - // finds first set item in bitset (or End if bitset is empty) - const int index = int(this->FirstNonZeroBit()) + BeginIndex; - if (index < EndIndex) { - result = static_cast<TEnum>(index); - return true; - } - return false; - } - - bool FindNext(TEnum current, TEnum& result) const { - // finds first set item in bitset (or End if bitset is empty) - const int index = int(this->NextNonZeroBit(int(current) - BeginIndex)) + BeginIndex; - if (index < EndIndex) { - result = static_cast<TEnum>(index); - return true; - } - return false; - } + + bool FindFirst(TEnum& result) const { + // finds first set item in bitset (or End if bitset is empty) + const int index = int(this->FirstNonZeroBit()) + BeginIndex; + if (index < EndIndex) { + result = static_cast<TEnum>(index); + return true; + } + return false; + } + + bool FindNext(TEnum current, TEnum& result) const { + // finds first set item in bitset (or End if bitset is empty) + const int index = int(this->NextNonZeroBit(int(current) - BeginIndex)) + BeginIndex; + if (index < EndIndex) { + result = static_cast<TEnum>(index); + return true; + } + return false; + } }; template <typename TEnum, TEnum mbegin, int mend> diff --git a/library/cpp/json/writer/ya.make b/library/cpp/json/writer/ya.make index 5bbef2d3f5..3989ff3504 100644 --- a/library/cpp/json/writer/ya.make +++ b/library/cpp/json/writer/ya.make @@ -15,6 +15,6 @@ SRCS( json.cpp ) -GENERATE_ENUM_SERIALIZATION(json_value.h) - +GENERATE_ENUM_SERIALIZATION(json_value.h) + END() diff --git a/library/cpp/protobuf/util/is_equal.cpp b/library/cpp/protobuf/util/is_equal.cpp index 0cc14774e3..227408006e 100644 --- a/library/cpp/protobuf/util/is_equal.cpp +++ b/library/cpp/protobuf/util/is_equal.cpp @@ -158,6 +158,6 @@ namespace NProtoBuf { bool IsEqualFieldDefault(const Message& m1, const Message& m2, const FieldDescriptor& field) { return IsEqualFieldImpl<true>(m1, m2, field, nullptr); - } - -} + } + +} diff --git a/library/cpp/protobuf/util/is_equal.h b/library/cpp/protobuf/util/is_equal.h index 97de691767..13c0aae63d 100644 --- a/library/cpp/protobuf/util/is_equal.h +++ b/library/cpp/protobuf/util/is_equal.h @@ -5,13 +5,13 @@ namespace google { namespace protobuf { class Message; - class FieldDescriptor; + class FieldDescriptor; } } namespace NProtoBuf { using ::google::protobuf::FieldDescriptor; - using ::google::protobuf::Message; + using ::google::protobuf::Message; } namespace NProtoBuf { @@ -27,7 +27,7 @@ namespace NProtoBuf { // Non-strict version: optional field without explicit value is compared // using its default value. bool IsEqualDefault(const Message& m1, const Message& m2); - + bool IsEqualFieldDefault(const Message& m1, const Message& m2, const FieldDescriptor& field); } diff --git a/library/cpp/protobuf/util/is_equal_ut.cpp b/library/cpp/protobuf/util/is_equal_ut.cpp index c370b26e8d..3ca4c90dd5 100644 --- a/library/cpp/protobuf/util/is_equal_ut.cpp +++ b/library/cpp/protobuf/util/is_equal_ut.cpp @@ -4,19 +4,19 @@ #include <library/cpp/testing/unittest/registar.h> #include <google/protobuf/descriptor.h> - + Y_UNIT_TEST_SUITE(ProtobufIsEqual) { - const ::google::protobuf::Descriptor* Descr = TSampleForIsEqual::descriptor(); - const ::google::protobuf::FieldDescriptor* NameDescr = Descr->field(0); - const ::google::protobuf::FieldDescriptor* InnerDescr = Descr->field(1); - + const ::google::protobuf::Descriptor* Descr = TSampleForIsEqual::descriptor(); + const ::google::protobuf::FieldDescriptor* NameDescr = Descr->field(0); + const ::google::protobuf::FieldDescriptor* InnerDescr = Descr->field(1); + Y_UNIT_TEST(CheckDescriptors) { - UNIT_ASSERT(Descr); - UNIT_ASSERT(NameDescr); - UNIT_ASSERT_VALUES_EQUAL(NameDescr->name(), "Name"); - UNIT_ASSERT_VALUES_EQUAL(InnerDescr->name(), "Inner"); - } - + UNIT_ASSERT(Descr); + UNIT_ASSERT(NameDescr); + UNIT_ASSERT_VALUES_EQUAL(NameDescr->name(), "Name"); + UNIT_ASSERT_VALUES_EQUAL(InnerDescr->name(), "Inner"); + } + Y_UNIT_TEST(IsEqual1) { TSampleForIsEqual a; TSampleForIsEqual b; @@ -29,8 +29,8 @@ Y_UNIT_TEST_SUITE(ProtobufIsEqual) { bool equal = NProtoBuf::IsEqual(a, b, &path); UNIT_ASSERT(!equal); UNIT_ASSERT_VALUES_EQUAL("Name", path); - - UNIT_ASSERT(!NProtoBuf::IsEqualField(a, b, *NameDescr)); + + UNIT_ASSERT(!NProtoBuf::IsEqualField(a, b, *NameDescr)); } Y_UNIT_TEST(IsEqual2) { @@ -45,35 +45,35 @@ Y_UNIT_TEST_SUITE(ProtobufIsEqual) { bool equal = NProtoBuf::IsEqual(a, b, &path); UNIT_ASSERT(!equal); UNIT_ASSERT_VALUES_EQUAL("Inner/Brbrbr", path); - - bool equalField = NProtoBuf::IsEqualField(a, b, *InnerDescr); - UNIT_ASSERT(!equalField); + + bool equalField = NProtoBuf::IsEqualField(a, b, *InnerDescr); + UNIT_ASSERT(!equalField); } Y_UNIT_TEST(IsEqual3) { - TSampleForIsEqual a; - TSampleForIsEqual b; - - a.SetName("aaa"); - a.MutableInner()->SetBrbrbr("bbb"); - - b.SetName("aaa"); - b.MutableInner()->SetBrbrbr("bbb"); - - TString path; - - UNIT_ASSERT(NProtoBuf::IsEqual(a, b)); - UNIT_ASSERT(NProtoBuf::IsEqualField(a, b, *NameDescr)); - UNIT_ASSERT(NProtoBuf::IsEqualField(a, b, *InnerDescr)); - - b.MutableInner()->SetBrbrbr("ccc"); - UNIT_ASSERT(!NProtoBuf::IsEqual(a, b)); - UNIT_ASSERT(!NProtoBuf::IsEqualField(a, b, *InnerDescr)); - - b.SetName("ccc"); - UNIT_ASSERT(!NProtoBuf::IsEqualField(a, b, *NameDescr)); - } - + TSampleForIsEqual a; + TSampleForIsEqual b; + + a.SetName("aaa"); + a.MutableInner()->SetBrbrbr("bbb"); + + b.SetName("aaa"); + b.MutableInner()->SetBrbrbr("bbb"); + + TString path; + + UNIT_ASSERT(NProtoBuf::IsEqual(a, b)); + UNIT_ASSERT(NProtoBuf::IsEqualField(a, b, *NameDescr)); + UNIT_ASSERT(NProtoBuf::IsEqualField(a, b, *InnerDescr)); + + b.MutableInner()->SetBrbrbr("ccc"); + UNIT_ASSERT(!NProtoBuf::IsEqual(a, b)); + UNIT_ASSERT(!NProtoBuf::IsEqualField(a, b, *InnerDescr)); + + b.SetName("ccc"); + UNIT_ASSERT(!NProtoBuf::IsEqualField(a, b, *NameDescr)); + } + Y_UNIT_TEST(IsEqualDefault) { TSampleForIsEqual a; TSampleForIsEqual b; @@ -81,8 +81,8 @@ Y_UNIT_TEST_SUITE(ProtobufIsEqual) { a.SetName(""); UNIT_ASSERT(NProtoBuf::IsEqualDefault(a, b)); UNIT_ASSERT(!NProtoBuf::IsEqual(a, b)); - - UNIT_ASSERT(!NProtoBuf::IsEqualField(a, b, *NameDescr)); - UNIT_ASSERT(NProtoBuf::IsEqualFieldDefault(a, b, *NameDescr)); + + UNIT_ASSERT(!NProtoBuf::IsEqualField(a, b, *NameDescr)); + UNIT_ASSERT(NProtoBuf::IsEqualFieldDefault(a, b, *NameDescr)); } } diff --git a/library/cpp/protobuf/util/traits.h b/library/cpp/protobuf/util/traits.h index e2d75e4a90..50f036d0ea 100644 --- a/library/cpp/protobuf/util/traits.h +++ b/library/cpp/protobuf/util/traits.h @@ -44,8 +44,8 @@ namespace NProtoBuf { Y_ASSERT(fd); \ return fd->default_value_##method(); \ } \ - }; - + }; + DECLARE_CPPTYPE_DEFAULT(FieldDescriptor::CppType::CPPTYPE_INT32, int32); DECLARE_CPPTYPE_DEFAULT(FieldDescriptor::CppType::CPPTYPE_INT64, int64); DECLARE_CPPTYPE_DEFAULT(FieldDescriptor::CppType::CPPTYPE_UINT32, uint32); @@ -55,9 +55,9 @@ namespace NProtoBuf { DECLARE_CPPTYPE_DEFAULT(FieldDescriptor::CppType::CPPTYPE_BOOL, bool); DECLARE_CPPTYPE_DEFAULT(FieldDescriptor::CppType::CPPTYPE_ENUM, enum); DECLARE_CPPTYPE_DEFAULT(FieldDescriptor::CppType::CPPTYPE_STRING, string); - -#undef DECLARE_CPPTYPE_DEFAULT - + +#undef DECLARE_CPPTYPE_DEFAULT + // getters/setters of field with specified CppType template <FieldDescriptor::CppType cpptype> struct TCppTypeTraits : TCppTypeTraitsBase { @@ -190,7 +190,7 @@ namespace NProtoBuf { Y_ASSERT(index == 0); TBaseTraits::Set(msg, field, value); } - + static inline void Add(Message& msg, const FieldDescriptor* field, T value) { TBaseTraits::Set(msg, field, value); } @@ -220,7 +220,7 @@ namespace NProtoBuf { static inline void Set(Message& msg, const FieldDescriptor* field, T value, size_t index = 0) { TBaseTraits::SetRepeated(msg, field, index, value); } - + static inline void Add(Message& msg, const FieldDescriptor* field, T value) { TBaseTraits::AddRepeated(msg, field, value); } diff --git a/library/cpp/timezone_conversion/civil-inl.h b/library/cpp/timezone_conversion/civil-inl.h index 5181c97b7d..32afbccb64 100644 --- a/library/cpp/timezone_conversion/civil-inl.h +++ b/library/cpp/timezone_conversion/civil-inl.h @@ -1,63 +1,63 @@ -#pragma once - -#include "civil.h" - -namespace NDatetime { - namespace NDetail { - template <typename T> - struct TGetCivilUnit; - - template <> - struct TGetCivilUnit<TCivilSecond> { - static constexpr ECivilUnit Value = ECivilUnit::Second; - }; - template <> - struct TGetCivilUnit<TCivilMinute> { - static constexpr ECivilUnit Value = ECivilUnit::Minute; - }; - template <> - struct TGetCivilUnit<TCivilHour> { - static constexpr ECivilUnit Value = ECivilUnit::Hour; - }; - template <> - struct TGetCivilUnit<TCivilDay> { - static constexpr ECivilUnit Value = ECivilUnit::Day; - }; - template <> - struct TGetCivilUnit<TCivilMonth> { - static constexpr ECivilUnit Value = ECivilUnit::Month; - }; - template <> - struct TGetCivilUnit<TCivilYear> { - static constexpr ECivilUnit Value = ECivilUnit::Year; - }; - - template <ECivilUnit Unit> - struct TGetCivilTime; - - template <> - struct TGetCivilTime<ECivilUnit::Second> { - using TResult = TCivilSecond; - }; - template <> - struct TGetCivilTime<ECivilUnit::Minute> { - using TResult = TCivilMinute; - }; - template <> - struct TGetCivilTime<ECivilUnit::Hour> { - using TResult = TCivilHour; - }; - template <> - struct TGetCivilTime<ECivilUnit::Day> { - using TResult = TCivilDay; - }; - template <> - struct TGetCivilTime<ECivilUnit::Month> { - using TResult = TCivilMonth; - }; - template <> - struct TGetCivilTime<ECivilUnit::Year> { - using TResult = TCivilYear; - }; +#pragma once + +#include "civil.h" + +namespace NDatetime { + namespace NDetail { + template <typename T> + struct TGetCivilUnit; + + template <> + struct TGetCivilUnit<TCivilSecond> { + static constexpr ECivilUnit Value = ECivilUnit::Second; + }; + template <> + struct TGetCivilUnit<TCivilMinute> { + static constexpr ECivilUnit Value = ECivilUnit::Minute; + }; + template <> + struct TGetCivilUnit<TCivilHour> { + static constexpr ECivilUnit Value = ECivilUnit::Hour; + }; + template <> + struct TGetCivilUnit<TCivilDay> { + static constexpr ECivilUnit Value = ECivilUnit::Day; + }; + template <> + struct TGetCivilUnit<TCivilMonth> { + static constexpr ECivilUnit Value = ECivilUnit::Month; + }; + template <> + struct TGetCivilUnit<TCivilYear> { + static constexpr ECivilUnit Value = ECivilUnit::Year; + }; + + template <ECivilUnit Unit> + struct TGetCivilTime; + + template <> + struct TGetCivilTime<ECivilUnit::Second> { + using TResult = TCivilSecond; + }; + template <> + struct TGetCivilTime<ECivilUnit::Minute> { + using TResult = TCivilMinute; + }; + template <> + struct TGetCivilTime<ECivilUnit::Hour> { + using TResult = TCivilHour; + }; + template <> + struct TGetCivilTime<ECivilUnit::Day> { + using TResult = TCivilDay; + }; + template <> + struct TGetCivilTime<ECivilUnit::Month> { + using TResult = TCivilMonth; + }; + template <> + struct TGetCivilTime<ECivilUnit::Year> { + using TResult = TCivilYear; + }; } } diff --git a/library/cpp/timezone_conversion/civil.cpp b/library/cpp/timezone_conversion/civil.cpp index c712f88748..5986318b9a 100644 --- a/library/cpp/timezone_conversion/civil.cpp +++ b/library/cpp/timezone_conversion/civil.cpp @@ -122,57 +122,57 @@ namespace NDatetime { return Calc<TCivilSecond>(tp, diff); } - TCivilSecond AddCivil(const TCivilSecond& tp, TCivilDiff diff) { - switch (diff.Unit) { - case ECivilUnit::Second: { - return AddSeconds(tp, diff.Value); - } - case ECivilUnit::Minute: { - return AddMinutes(tp, diff.Value); - } - case ECivilUnit::Hour: { - return AddHours(tp, diff.Value); - } - case ECivilUnit::Day: { - return AddDays(tp, diff.Value); - } - case ECivilUnit::Month: { - return AddMonths(tp, diff.Value); - } - case ECivilUnit::Year: { - return AddYears(tp, diff.Value); - } - default: { - ythrow yexception() << "Unexpected civil unit value " << static_cast<int>(diff.Unit); - } - } - } - - TCivilDiff GetCivilDiff(const TCivilSecond& tpX, const TCivilSecond& tpY, ECivilUnit unit) { - switch (unit) { - case ECivilUnit::Second: { - return {tpX - tpY, unit}; - } - case ECivilUnit::Minute: { - return {static_cast<TCivilMinute>(tpX) - static_cast<TCivilMinute>(tpY), unit}; - } - case ECivilUnit::Hour: { - return {static_cast<TCivilHour>(tpX) - static_cast<TCivilHour>(tpY), unit}; - } - case ECivilUnit::Day: { - return {static_cast<TCivilDay>(tpX) - static_cast<TCivilDay>(tpY), unit}; - } - case ECivilUnit::Month: { - return {static_cast<TCivilMonth>(tpX) - static_cast<TCivilMonth>(tpY), unit}; - } - case ECivilUnit::Year: { - return {static_cast<TCivilYear>(tpX) - static_cast<TCivilYear>(tpY), unit}; - } - default: { - ythrow yexception() << "Unexpected civil unit value " << static_cast<int>(unit); - } - } - } + TCivilSecond AddCivil(const TCivilSecond& tp, TCivilDiff diff) { + switch (diff.Unit) { + case ECivilUnit::Second: { + return AddSeconds(tp, diff.Value); + } + case ECivilUnit::Minute: { + return AddMinutes(tp, diff.Value); + } + case ECivilUnit::Hour: { + return AddHours(tp, diff.Value); + } + case ECivilUnit::Day: { + return AddDays(tp, diff.Value); + } + case ECivilUnit::Month: { + return AddMonths(tp, diff.Value); + } + case ECivilUnit::Year: { + return AddYears(tp, diff.Value); + } + default: { + ythrow yexception() << "Unexpected civil unit value " << static_cast<int>(diff.Unit); + } + } + } + + TCivilDiff GetCivilDiff(const TCivilSecond& tpX, const TCivilSecond& tpY, ECivilUnit unit) { + switch (unit) { + case ECivilUnit::Second: { + return {tpX - tpY, unit}; + } + case ECivilUnit::Minute: { + return {static_cast<TCivilMinute>(tpX) - static_cast<TCivilMinute>(tpY), unit}; + } + case ECivilUnit::Hour: { + return {static_cast<TCivilHour>(tpX) - static_cast<TCivilHour>(tpY), unit}; + } + case ECivilUnit::Day: { + return {static_cast<TCivilDay>(tpX) - static_cast<TCivilDay>(tpY), unit}; + } + case ECivilUnit::Month: { + return {static_cast<TCivilMonth>(tpX) - static_cast<TCivilMonth>(tpY), unit}; + } + case ECivilUnit::Year: { + return {static_cast<TCivilYear>(tpX) - static_cast<TCivilYear>(tpY), unit}; + } + default: { + ythrow yexception() << "Unexpected civil unit value " << static_cast<int>(unit); + } + } + } } template <> diff --git a/library/cpp/timezone_conversion/civil.h b/library/cpp/timezone_conversion/civil.h index 51983e73b2..0e95b807ed 100644 --- a/library/cpp/timezone_conversion/civil.h +++ b/library/cpp/timezone_conversion/civil.h @@ -43,53 +43,53 @@ namespace NDatetime { using TYear = cctz::year_t; using TMonth = cctz::detail::month_t; - enum class ECivilUnit : int { - Second = 0, - Minute = 1, - Hour = 2, - Day = 3, - Month = 4, - Year = 5 - }; - - namespace NDetail { - template <typename T> - struct TGetCivilUnit; - - template <ECivilUnit Unit> - struct TGetCivilTime; + enum class ECivilUnit : int { + Second = 0, + Minute = 1, + Hour = 2, + Day = 3, + Month = 4, + Year = 5 + }; + + namespace NDetail { + template <typename T> + struct TGetCivilUnit; + + template <ECivilUnit Unit> + struct TGetCivilTime; + } + + template <typename T> + CONSTEXPR_M ECivilUnit GetCivilUnit(const T& = {}) { + return NDetail::TGetCivilUnit<T>::Value; } - - template <typename T> - CONSTEXPR_M ECivilUnit GetCivilUnit(const T& = {}) { - return NDetail::TGetCivilUnit<T>::Value; - } - - template <ECivilUnit Unit> - using TCivilTime = typename NDetail::TGetCivilTime<Unit>::TResult; - + + template <ECivilUnit Unit> + using TCivilTime = typename NDetail::TGetCivilTime<Unit>::TResult; + + /** + * Class with variable unit diff. + */ + struct TCivilDiff { + TDiff Value = 0; + ECivilUnit Unit = ECivilUnit::Second; + + TCivilDiff() = default; + TCivilDiff(TDiff value, ECivilUnit unit) + : Value(value) + , Unit(unit) + { + } + + /** + * Straightfoward implementation of operators <, == and unit conversions + * can be potentially misleading (e.g. 1 month == 30 days?); + * we leave it to user to implement it properly for each application. + */ + }; + /** - * Class with variable unit diff. - */ - struct TCivilDiff { - TDiff Value = 0; - ECivilUnit Unit = ECivilUnit::Second; - - TCivilDiff() = default; - TCivilDiff(TDiff value, ECivilUnit unit) - : Value(value) - , Unit(unit) - { - } - - /** - * Straightfoward implementation of operators <, == and unit conversions - * can be potentially misleading (e.g. 1 month == 30 days?); - * we leave it to user to implement it properly for each application. - */ - }; - - /** * 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 @@ -150,7 +150,7 @@ namespace NDatetime { * @return the calculated T type */ template <typename T, typename S> - inline T Calc(const S& tp, TDiff diff) { + inline T Calc(const S& tp, TDiff diff) { return T(tp) + diff; } @@ -166,24 +166,24 @@ namespace NDatetime { TCivilSecond AddMinutes(const TCivilSecond& tp, TDiff diff); TCivilSecond AddSeconds(const TCivilSecond& tp, TDiff diff); - /** Method to add TCivilDiff - * @param[in] tp is a timepoint with which calc will be - * @param[in] diff is quantity of which will be added to the tp - * @return the calculated TCivilSecond object - */ - TCivilSecond AddCivil(const TCivilSecond& tp, TCivilDiff diff); - - /** Method to subtract to civil dates/times and get TCivilDiff. - * First casts to unit, then subtracts; - * e.g. GetCivilDiff(2017-10-01, 2017-09-30, Month) = 1. - * - * @param[in] tpX is a timepoint - * @param[in] tpY is a timepoint to subtract from tpX - * @param[in] unit is a civil time unit to use in subtraction - * @return the calculated diff as TCivilDiff object - */ - TCivilDiff GetCivilDiff(const TCivilSecond& tpX, const TCivilSecond& tpY, ECivilUnit unit); - + /** Method to add TCivilDiff + * @param[in] tp is a timepoint with which calc will be + * @param[in] diff is quantity of which will be added to the tp + * @return the calculated TCivilSecond object + */ + TCivilSecond AddCivil(const TCivilSecond& tp, TCivilDiff diff); + + /** Method to subtract to civil dates/times and get TCivilDiff. + * First casts to unit, then subtracts; + * e.g. GetCivilDiff(2017-10-01, 2017-09-30, Month) = 1. + * + * @param[in] tpX is a timepoint + * @param[in] tpY is a timepoint to subtract from tpX + * @param[in] unit is a civil time unit to use in subtraction + * @return the calculated diff as TCivilDiff object + */ + 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: @@ -333,6 +333,6 @@ namespace NDatetime { } } -#include "civil-inl.h" - +#include "civil-inl.h" + #undef CONSTEXPR_M diff --git a/library/cpp/timezone_conversion/ut/civil_ut.cpp b/library/cpp/timezone_conversion/ut/civil_ut.cpp index a4e45879af..a21bd4bd7d 100644 --- a/library/cpp/timezone_conversion/ut/civil_ut.cpp +++ b/library/cpp/timezone_conversion/ut/civil_ut.cpp @@ -4,17 +4,17 @@ #include <util/stream/str.h> -namespace NDatetime { +namespace NDatetime { inline bool operator==(const NDatetime::TCivilDiff& x, const NDatetime::TCivilDiff& y) { - return x.Unit == y.Unit && x.Value == y.Value; - } + return x.Unit == y.Unit && x.Value == y.Value; + } +} + +template <> +inline void Out<NDatetime::TCivilDiff>(IOutputStream& out, const NDatetime::TCivilDiff& diff) { + out << "(" << diff.Value << "," << diff.Unit << ")"; } - -template <> -inline void Out<NDatetime::TCivilDiff>(IOutputStream& out, const NDatetime::TCivilDiff& diff) { - out << "(" << diff.Value << "," << diff.Unit << ")"; -} - + Y_UNIT_TEST_SUITE(DateTime) { Y_UNIT_TEST(Calc) { NDatetime::TCivilSecond s(2017, 2, 1, 10, 12, 9); @@ -83,40 +83,40 @@ Y_UNIT_TEST_SUITE(DateTime) { UNIT_ASSERT_VALUES_EQUAL(NDatetime::WeekdayOnTheWeek(d, NDatetime::TWeekday::friday), NDatetime::TCivilDay(2013, 1, 4)); } Y_UNIT_TEST(CivilUnit) { - using namespace NDatetime; - - UNIT_ASSERT_VALUES_EQUAL(GetCivilUnit<TCivilMonth>(), ECivilUnit::Month); - UNIT_ASSERT_VALUES_EQUAL(GetCivilUnit(TCivilHour{}), ECivilUnit::Hour); - - UNIT_ASSERT_VALUES_EQUAL(TCivilTime<ECivilUnit::Day>(2017, 1, 11), TCivilDay(2017, 1, 11)); - - NDatetime::TCivilSecond s(2017, 2, 1, 10, 12, 9); - - UNIT_ASSERT_VALUES_EQUAL( - NDatetime::AddCivil(s, TCivilDiff{2, ECivilUnit::Day}), - NDatetime::AddDays(s, 2)); - UNIT_ASSERT_VALUES_EQUAL( - NDatetime::AddCivil(s, TCivilDiff{-2, ECivilUnit::Month}), - NDatetime::AddMonths(s, -2)); - UNIT_ASSERT_VALUES_EQUAL( - NDatetime::AddCivil(s, TCivilDiff{-55, ECivilUnit::Year}), - NDatetime::AddYears(s, -55)); - UNIT_ASSERT_VALUES_EQUAL( - NDatetime::AddCivil(s, TCivilDiff{40, ECivilUnit::Hour}), - NDatetime::AddHours(s, 40)); - - UNIT_ASSERT_VALUES_EQUAL( - GetCivilDiff(TCivilSecond(2017, 10), TCivilSecond(2017, 7), ECivilUnit::Month), - TCivilDiff(3, ECivilUnit::Month)); - - UNIT_ASSERT_VALUES_EQUAL( - GetCivilDiff(TCivilSecond(2017, 10, 1), TCivilSecond(2017, 9, 30), ECivilUnit::Month), - TCivilDiff(1, ECivilUnit::Month)); - - UNIT_ASSERT_VALUES_EQUAL( - GetCivilDiff(TCivilSecond(2017, 10, 1), TCivilSecond(2017, 9, 31), ECivilUnit::Month), - TCivilDiff(0, ECivilUnit::Month)); - } + using namespace NDatetime; + + UNIT_ASSERT_VALUES_EQUAL(GetCivilUnit<TCivilMonth>(), ECivilUnit::Month); + UNIT_ASSERT_VALUES_EQUAL(GetCivilUnit(TCivilHour{}), ECivilUnit::Hour); + + UNIT_ASSERT_VALUES_EQUAL(TCivilTime<ECivilUnit::Day>(2017, 1, 11), TCivilDay(2017, 1, 11)); + + NDatetime::TCivilSecond s(2017, 2, 1, 10, 12, 9); + + UNIT_ASSERT_VALUES_EQUAL( + NDatetime::AddCivil(s, TCivilDiff{2, ECivilUnit::Day}), + NDatetime::AddDays(s, 2)); + UNIT_ASSERT_VALUES_EQUAL( + NDatetime::AddCivil(s, TCivilDiff{-2, ECivilUnit::Month}), + NDatetime::AddMonths(s, -2)); + UNIT_ASSERT_VALUES_EQUAL( + NDatetime::AddCivil(s, TCivilDiff{-55, ECivilUnit::Year}), + NDatetime::AddYears(s, -55)); + UNIT_ASSERT_VALUES_EQUAL( + NDatetime::AddCivil(s, TCivilDiff{40, ECivilUnit::Hour}), + NDatetime::AddHours(s, 40)); + + UNIT_ASSERT_VALUES_EQUAL( + GetCivilDiff(TCivilSecond(2017, 10), TCivilSecond(2017, 7), ECivilUnit::Month), + TCivilDiff(3, ECivilUnit::Month)); + + UNIT_ASSERT_VALUES_EQUAL( + GetCivilDiff(TCivilSecond(2017, 10, 1), TCivilSecond(2017, 9, 30), ECivilUnit::Month), + TCivilDiff(1, ECivilUnit::Month)); + + UNIT_ASSERT_VALUES_EQUAL( + GetCivilDiff(TCivilSecond(2017, 10, 1), TCivilSecond(2017, 9, 31), ECivilUnit::Month), + TCivilDiff(0, ECivilUnit::Month)); + } Y_UNIT_TEST(TestYearWeekNmb) { diff --git a/library/cpp/timezone_conversion/ya.make b/library/cpp/timezone_conversion/ya.make index b0412c6932..f99ebe73ee 100644 --- a/library/cpp/timezone_conversion/ya.make +++ b/library/cpp/timezone_conversion/ya.make @@ -15,8 +15,8 @@ SRCS( civil.cpp ) -GENERATE_ENUM_SERIALIZATION(civil.h) - +GENERATE_ENUM_SERIALIZATION(civil.h) + END() RECURSE_FOR_TESTS(ut) |