in sql_utils/public/functions/date_time_util.cc [3747:3792]
absl::Status DiffTimes(const TimeValue& time1, const TimeValue& time2,
DateTimestampPart part, int64_t* output) {
if (!time1.IsValid()) {
return MakeEvalError() << "Invalid time value: " << time1.DebugString();
}
if (!time2.IsValid()) {
return MakeEvalError() << "Invalid time value: " << time2.DebugString();
}
const absl::CivilSecond civil_time_1(1970, 1, 1, time1.Hour(), time1.Minute(),
time1.Second());
const absl::CivilSecond civil_time_2(1970, 1, 1, time2.Hour(), time2.Minute(),
time2.Second());
switch (part) {
case HOUR:
case MINUTE:
case SECOND:
case MILLISECOND:
case MICROSECOND:
case NANOSECOND:
return DiffWithPartsSmallerThanDay(
civil_time_1, time1.Nanoseconds(), civil_time_2, time2.Nanoseconds(),
part,
[]() -> absl::Status {
SQL_RET_CHECK_FAIL() << "TIME_DIFF should never have overflow error";
} /* nano_overflow_error_maker */,
output);
case YEAR:
case MONTH:
case DAY:
case DAYOFWEEK:
case DAYOFYEAR:
case QUARTER:
case DATE:
case WEEK:
case DATETIME:
case TIME:
return MakeEvalError()
<< "Unsupported DateTimestampPart " << DateTimestampPart_Name(part)
<< " for TIME_DIFF";
default:
return MakeEvalError()
<< "Unexpected DateTimestampPart " << DateTimestampPart_Name(part)
<< " for TIME_DIFF";
}
}