核心语义
基于左闭右开区间语义[start, end):
start:本次时间间隔的起始点(包含),也是本次版次的最小时间点end:下次版次的最小时间点(不包含),即本次区间的上界
结构体定义
/// 时间区间 [start, end)#[derive(Copy, Clone, Debug, PartialEq)]pubstructTimeRange{start:DateTime,end:DateTime,}构造器方法
注意:除
from_start_to_now和from_now_to_end外,所有构造器均为const函数,可在编译时求值。
new(start, end)—const
创建区间[start, end)。
pubconstfnnew(start:DateTime,end:DateTime)->Self{debug_assert!(start.lt(end),"TimeRange::new: start must be < end");Self{start,end}}示例:
letr=TimeRange::new(today_start,tomorrow_start);// 包含:today_start ≤ t < tomorrow_startfrom_start_to_now(start)— 非const
构造从起始时间到现在的区间[start, now + 1ms],包含 now。
pubfnfrom_start_to_now(start:DateTime)->Self{Self::new(start,DateTime::now().ms(1))}示例:
letr=TimeRange::from_start_to_now(last_sync);// 包含:last_sync ≤ t ≤ nowfrom_now_to_end(end)— 非const
构造从现在到指定时间的区间[now, end)。
pubfnfrom_now_to_end(end:DateTime)->Self{Self::new(DateTime::now(),end)}示例:
letr=TimeRange::from_now_to_end(deadline);// 包含:now ≤ t < deadlinepoint(at)—const
构造仅包含单个时间点的区间[at, at+1ms)。
pubconstfnpoint(at:DateTime)->Self{Self::new(at,at.ms(1))}示例:
letr=TimeRange::point(snapshot_time);// 仅包含:t = snapshot_timefrom_start_duration(start, duration)—const
从起始时间和时长创建区间。
pubconstfnfrom_start_duration(start:DateTime,duration:Duration)->Self{Self::new(start,start.add(duration))}示例:
letr=TimeRange::from_start_duration(start,Duration::from_hours(2));// 包含:[start, start + 2h)from_end_backward(end, duration)—const
从结束时间和时长向前创建区间[end - duration, end)。
pubconstfnfrom_end_backward(end:DateTime,duration:Duration)->Self{Self::new(end.sub(duration),end)}示例:
letr=TimeRange::from_end_backward(end,Duration::from_hours(2));// 包含:[end - 2h, end)查询方法
所有查询方法均为
const函数。
implTimeRange{/// 获取起始时间点(包含)pubconstfnstart(self)->DateTime{self.start}/// 获取结束时间点(不包含)pubconstfnend(self)->DateTime{self.end}/// 计算区间跨度(时长)pubconstfnduration(self)->Duration{self.end.diff(self.start)}/// 将区间转换为 (start, end) 元组pubconstfnto_parts(self)->(DateTime,DateTime){(self.start,self.end)}}判断方法
所有判断方法均为
const函数。
implTimeRange{/// 判断某个时间点是否在区间内pubconstfncontains(self,t:DateTime)->bool{self.start.le(t)&&t.lt(self.end)}/// 判断是否包含另一个区间pubconstfncontains_range(self,other:Self)->bool{self.start.le(other.start)&&other.end.le(self.end)}/// 判断两个区间是否相邻pubconstfnis_adjacent_to(self,other:Self)->bool{self.end.eq(other.start)||other.end.eq(self.start)}/// 合并相邻区间(必须首尾相接,不支持重叠)pubconstfnmerge(self,other:Self)->Option<Self>{ifself.end.eq(other.start){Some(TimeRange::new(self.start,other.end))}elseifself.start.eq(other.end){Some(TimeRange::new(other.start,self.end))}else{None}}}类型转换
所有
From/Into实现均非const(Rust 稳定版暂不支持consttrait)。
implFrom<(DateTime,DateTime)>forTimeRange{fnfrom((start,end):(DateTime,DateTime))->Self{Self::new(start,end)}}implFrom<TimeRange>for(DateTime,DateTime){fnfrom(range:TimeRange)->Self{(range.start,range.end)}}implFrom<Range<DateTime>>forTimeRange{fnfrom(range:Range<DateTime>)->Self{Self::new(range.start,range.end)}}implFrom<TimeRange>forRange<DateTime>{fnfrom(range:TimeRange)->Self{range.start..range.end}}implFrom<[DateTime;2]>forTimeRange{fnfrom([start,end]:[DateTime;2])->Self{Self::new(start,end)}}implFrom<TimeRange>for[DateTime;2]{fnfrom(range:TimeRange)->Self{[range.start,range.end]}}使用示例
letlast_sync=DateTime::from_ymd(2026,8,18);letnow=DateTime::now();// 构造区间letrange=TimeRange::from_start_to_now(last_sync);// [last_sync, now + 1ms]// 检查包含关系assert!(range.contains(last_sync));// 起点包含assert!(range.contains(now));// now 包含assert!(!range.contains(now.ms(1)));// now + 1ms 不包含(右边界)// 区间跨度letduration=range.duration();// 相邻与合并letr1=TimeRange::new(t1,t2);letr2=TimeRange::new(t2,t3);ifletSome(merged)=r1.merge(r2){// 合并后的区间覆盖 [t1, t3)}const 函数汇总
| 方法分类 | 方法名 | 是否const |
|---|---|---|
| 构造器 | new | ✅ |
point | ✅ | |
from_start_duration | ✅ | |
from_end_backward | ✅ | |
from_start_to_now | ❌(依赖DateTime::now()) | |
from_now_to_end | ❌(依赖DateTime::now()) | |
| 查询 | start | ✅ |
end | ✅ | |
duration | ✅ | |
to_parts | ✅ | |
| 判断 | contains | ✅ |
contains_range | ✅ | |
is_adjacent_to | ✅ | |
merge | ✅ | |
| 类型转换 | 所有From/Into | ❌(Rust 稳定版限制) |
设计要点
| 概念 | 说明 |
|---|---|
start | 区间起始时间点(包含),本次版次的最小时间点 |
end | 区间结束时间点(不包含),下次版次的最小时间点 |
| 有效区间 | [start, end),即start ≤ t < end |
| 存储形式 | 自定义结构体TimeRange,包含start和end两个字段 |
| 包含 now | from_start_to_now(start)使用now.ms(1)作为结束边界,使now被包含 |
| 相邻合并 | end == other.start或other.end == start,仅相邻可合并,重叠不可合并 |
| const 支持 | 除涉及now()的构造器和类型转换外,所有方法均为const |
一句话总结:
start是本次版次的最小时间点(包含),end是下次版次的最小时间点(不包含)。区间[start, end)使用from_start_to_now(start)包含now时,end = now.ms(1)。