getBusinessDate static method
营业日归属:按日切线 cutoffTime 把结账时刻 checkoutTime 归属到
所属营业日的锚点时刻。
规则:checkoutTime 晚于等于当天 cutoffTime("HH:mm:ss")时归属
当天,否则归属前一天;返回值为归属日的 cutoffTime 时刻(而非日界
00:00),调用方可直接取其年月日作为营业日、或整体作为营业时间戳。
未提供 cutoffTime 时原样返回 checkoutTime(无日切概念)。
cutoffTime 必须是三段数字(0 <= hour <= 23、0 <= minute <= 59、
0 <= second <= 59),否则抛 ArgumentError——不静默回退原值,
调用方应 try-catch 或预先校验配置。
提供了 cutoffTime 时,checkoutTime 若为 UTC 时间会先按本地时区
折算(日切线语义是本地墙钟时间);未提供时原样返回、不做折算。
能力吸收自 saas_pos 收银端营业日归属逻辑并按本库基线重写。
example:
// 日切线 04:00:10 月 12 日凌晨 3 点的结账归属 10 月 11 日
DateTimeUtils.getBusinessDate(
checkoutTime: DateTime(2026, 10, 12, 3),
cutoffTime: '04:00:00',
); // DateTime(2026, 10, 11, 4, 0, 0)
Implementation
static DateTime getBusinessDate({
required DateTime checkoutTime,
String? cutoffTime,
}) {
if (cutoffTime == null) return checkoutTime;
// UTC 输入按本地时区折算后再比对年月日——日切线语义是"本地墙钟时间"
// 的 04:00,直接拿 UTC 的 year/month/day 比对会在时区偏移下归错营业日。
final checkout = checkoutTime.toLocal();
final parts = cutoffTime.split(':');
if (parts.length != 3) {
throw ArgumentError.value(
cutoffTime, 'cutoffTime', '必须是 "HH:mm:ss" 三段格式');
}
final h = int.tryParse(parts[0]);
final m = int.tryParse(parts[1]);
final s = int.tryParse(parts[2]);
if (h == null ||
m == null ||
s == null ||
h < 0 ||
h > 23 ||
m < 0 ||
m > 59 ||
s < 0 ||
s > 59) {
throw ArgumentError.value(cutoffTime, 'cutoffTime', '段值非法');
}
final cutoffToday =
DateTime(checkout.year, checkout.month, checkout.day, h, m, s);
if (!checkout.isBefore(cutoffToday)) {
return cutoffToday;
}
return previousDay(cutoffToday);
}