in sql_utils/public/functions/date_time_util.cc [3803:3851]
absl::Status TruncateTime(const TimeValue& time, DateTimestampPart part,
TimeValue* output) {
if (!time.IsValid()) {
return MakeEvalError() << "Invalid time value: " << time.DebugString();
}
switch (part) {
case HOUR:
*output = TimeValue::FromHMSAndNanos(time.Hour(), 0, 0, 0);
break;
case MINUTE:
*output = TimeValue::FromHMSAndNanos(time.Hour(), time.Minute(), 0, 0);
break;
case SECOND:
*output = TimeValue::FromHMSAndNanos(time.Hour(), time.Minute(),
time.Second(), 0);
break;
case MILLISECOND:
*output = TimeValue::FromHMSAndNanos(
time.Hour(), time.Minute(), time.Second(),
time.Nanoseconds() / powers_of_ten[6] * powers_of_ten[6]);
break;
case MICROSECOND:
*output = TimeValue::FromHMSAndNanos(
time.Hour(), time.Minute(), time.Second(),
time.Nanoseconds() / powers_of_ten[3] * powers_of_ten[3]);
break;
case NANOSECOND:
*output = time;
break;
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_TRUNC";
default:
return MakeEvalError()
<< "Unexpected DateTimestampPart " << DateTimestampPart_Name(part)
<< " for TIME_TRUNC";
}
return absl::OkStatus();
}