datetime.cpp 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. #include "datetime.h"
  2. #include <yql/essentials/minikql/mkql_type_ops.h>
  3. namespace NYql::DateTime {
  4. bool DoAddMonths(TTMStorage& storage, i64 months, const NUdf::IDateBuilder& builder) {
  5. i64 newMonth = months + storage.Month;
  6. storage.Year += (newMonth - 1) / 12;
  7. newMonth = 1 + (newMonth - 1) % 12;
  8. if (newMonth <= 0) {
  9. storage.Year--;
  10. newMonth += 12;
  11. }
  12. storage.Month = newMonth;
  13. bool isLeap = NKikimr::NMiniKQL::IsLeapYear(storage.Year);
  14. ui32 monthLength = NKikimr::NMiniKQL::GetMonthLength(storage.Month, isLeap);
  15. storage.Day = std::min(monthLength, storage.Day);
  16. return storage.Validate(builder);
  17. }
  18. bool DoAddYears(TTMStorage& storage, i64 years, const NUdf::IDateBuilder& builder) {
  19. storage.Year += years;
  20. if (storage.Month == 2 && storage.Day == 29) {
  21. bool isLeap = NKikimr::NMiniKQL::IsLeapYear(storage.Year);
  22. if (!isLeap) {
  23. storage.Day--;
  24. }
  25. }
  26. return storage.Validate(builder);
  27. }
  28. TInstant DoAddMonths(TInstant current, i64 months, const NUdf::IDateBuilder& builder) {
  29. TTMStorage storage;
  30. storage.FromTimestamp(builder, current.GetValue());
  31. if (!DoAddMonths(storage, months, builder)) {
  32. ythrow yexception() << "Shift error " << current.ToIsoStringLocal() << " by " << months << " months";
  33. }
  34. return TInstant::FromValue(storage.ToTimestamp(builder));
  35. }
  36. TInstant DoAddYears(TInstant current, i64 years, const NUdf::IDateBuilder& builder) {
  37. TTMStorage storage;
  38. storage.FromTimestamp(builder, current.GetValue());
  39. if (!DoAddYears(storage, years, builder)) {
  40. ythrow yexception() << "Shift error " << current.ToIsoStringLocal() << " by " << years << " years";
  41. }
  42. return TInstant::FromValue(storage.ToTimestamp(builder));
  43. }
  44. }