尧图网站设计 尧图网站设计YAOTU DESIGN
ARTICLE DETAIL

资讯详情

深耕网站设计与一线实操的经验洞察。

【A11】时间区间(Time Range)

【A11】时间区间(Time Range) 核心语义基于左闭右开区间语义[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}}示例letrTimeRange::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))}示例letrTimeRange::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)}示例letrTimeRange::from_now_to_end(deadline);// 包含now ≤ t deadlinepoint(at)—const构造仅包含单个时间点的区间[at, at1ms)。pubconstfnpoint(at:DateTime)-Self{Self::new(at,at.ms(1))}示例letrTimeRange::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))}示例letrTimeRange::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)}示例letrTimeRange::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)-OptionSelf{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实现均非constRust 稳定版暂不支持consttrait。implFrom(DateTime,DateTime)forTimeRange{fnfrom((start,end):(DateTime,DateTime))-Self{Self::new(start,end)}}implFromTimeRangefor(DateTime,DateTime){fnfrom(range:TimeRange)-Self{(range.start,range.end)}}implFromRangeDateTimeforTimeRange{fnfrom(range:RangeDateTime)-Self{Self::new(range.start,range.end)}}implFromTimeRangeforRangeDateTime{fnfrom(range:TimeRange)-Self{range.start..range.end}}implFrom[DateTime;2]forTimeRange{fnfrom([start,end]:[DateTime;2])-Self{Self::new(start,end)}}implFromTimeRangefor[DateTime;2]{fnfrom(range:TimeRange)-Self{[range.start,range.end]}}使用示例letlast_syncDateTime::from_ymd(2026,8,18);letnowDateTime::now();// 构造区间letrangeTimeRange::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 不包含右边界// 区间跨度letdurationrange.duration();// 相邻与合并letr1TimeRange::new(t1,t2);letr2TimeRange::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两个字段包含 nowfrom_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)。
返回列表