@@ -633,24 +633,10 @@ inline bool CastToDatetimeV2::from_string_strict_mode(const StringRef& str,
633633 " invalid timezone name '{}'" , std::string {start, ptr});
634634 }
635635 // convert tz
636- cctz::civil_second cs {res.year (), res.month (), res.day (),
637- res.hour (), res.minute (), res.second ()};
638-
639- if constexpr (type == DataTimeCastEnumType::DATE_TIME) {
640- // if not timestamptz, the given time is in local_time_zone
641- SET_PARAMS_RET_FALSE_IFN (
642- local_time_zone != nullptr ,
643- " local time zone required for datetime string without timezone" );
644- auto given = cctz::convert (cs, parsed_tz);
645- auto local = cctz::convert (given, *local_time_zone);
646- res.unchecked_set_time_unit <TimeUnit::YEAR>((uint32_t )local.year ());
647- res.unchecked_set_time_unit <TimeUnit::MONTH>((uint32_t )local.month ());
648- res.unchecked_set_time_unit <TimeUnit::DAY>((uint32_t )local.day ());
649- res.unchecked_set_time_unit <TimeUnit::HOUR>((uint32_t )local.hour ());
650- res.unchecked_set_time_unit <TimeUnit::MINUTE>((uint32_t )local.minute ());
651- res.unchecked_set_time_unit <TimeUnit::SECOND>((uint32_t )local.second ());
652- } else {
653- // if timestamptz, the given time is in UTC
636+ if constexpr (type == DataTimeCastEnumType::TIMESTAMP_TZ) {
637+ // timestamptz: the given time is in UTC
638+ cctz::civil_second cs {res.year (), res.month (), res.day (),
639+ res.hour (), res.minute (), res.second ()};
654640 auto given = cctz::convert (cs, parsed_tz);
655641 auto utc = cctz::convert (given, cctz::utc_time_zone ());
656642 res.unchecked_set_time_unit <TimeUnit::YEAR>((uint32_t )utc.year ());
@@ -659,6 +645,8 @@ inline bool CastToDatetimeV2::from_string_strict_mode(const StringRef& str,
659645 res.unchecked_set_time_unit <TimeUnit::HOUR>((uint32_t )utc.hour ());
660646 res.unchecked_set_time_unit <TimeUnit::MINUTE>((uint32_t )utc.minute ());
661647 res.unchecked_set_time_unit <TimeUnit::SECOND>((uint32_t )utc.second ());
648+ } else {
649+ SET_PARAMS_RET_FALSE_IFN (false , " timezone is not allowed for datetime string" );
662650 }
663651 SET_PARAMS_RET_FALSE_IFN (res.year () <= 9999 , " datetime year {} out of range [0, 9999]" ,
664652 res.year ());
@@ -913,24 +901,10 @@ inline bool CastToDatetimeV2::from_string_non_strict_mode_impl(
913901 }
914902
915903 // convert tz
916- cctz::civil_second cs {res.year (), res.month (), res.day (),
917- res.hour (), res.minute (), res.second ()};
918-
919- if constexpr (type == DataTimeCastEnumType::DATE_TIME) {
920- // if not timestamptz, the given time is in local_time_zone
921- SET_PARAMS_RET_FALSE_IFN (
922- local_time_zone != nullptr ,
923- " local time zone required for datetime string without timezone" );
924- auto given = cctz::convert (cs, parsed_tz);
925- auto local = cctz::convert (given, *local_time_zone);
926- res.unchecked_set_time_unit <TimeUnit::YEAR>((uint32_t )local.year ());
927- res.unchecked_set_time_unit <TimeUnit::MONTH>((uint32_t )local.month ());
928- res.unchecked_set_time_unit <TimeUnit::DAY>((uint32_t )local.day ());
929- res.unchecked_set_time_unit <TimeUnit::HOUR>((uint32_t )local.hour ());
930- res.unchecked_set_time_unit <TimeUnit::MINUTE>((uint32_t )local.minute ());
931- res.unchecked_set_time_unit <TimeUnit::SECOND>((uint32_t )local.second ());
932- } else {
933- // if timestamptz, the given time is in UTC
904+ if constexpr (type == DataTimeCastEnumType::TIMESTAMP_TZ) {
905+ // timestamptz: the given time is in UTC
906+ cctz::civil_second cs {res.year (), res.month (), res.day (),
907+ res.hour (), res.minute (), res.second ()};
934908 auto given = cctz::convert (cs, parsed_tz);
935909 auto utc = cctz::convert (given, cctz::utc_time_zone ());
936910 res.unchecked_set_time_unit <TimeUnit::YEAR>((uint32_t )utc.year ());
@@ -939,6 +913,8 @@ inline bool CastToDatetimeV2::from_string_non_strict_mode_impl(
939913 res.unchecked_set_time_unit <TimeUnit::HOUR>((uint32_t )utc.hour ());
940914 res.unchecked_set_time_unit <TimeUnit::MINUTE>((uint32_t )utc.minute ());
941915 res.unchecked_set_time_unit <TimeUnit::SECOND>((uint32_t )utc.second ());
916+ } else {
917+ SET_PARAMS_RET_FALSE_IFN (false , " timezone is not allowed for datetime string" );
942918 }
943919 SET_PARAMS_RET_FALSE_IFN (res.year () <= 9999 , " datetime year {} out of range [0, 9999]" ,
944920 res.year ());
0 commit comments