1use crate::components::FastComponents;
4use chrono::{DateTime, Local};
5
6#[derive(Debug, Clone)]
11pub struct ReferenceWithTimezone {
12 pub instant: DateTime<Local>,
14 pub timezone_offset: Option<i32>,
18}
19
20impl ReferenceWithTimezone {
21 pub fn new(instant: DateTime<Local>, timezone_offset: Option<i32>) -> Self {
23 Self {
24 instant,
25 timezone_offset,
26 }
27 }
28
29 pub fn now() -> Self {
31 Self {
32 instant: Local::now(),
33 timezone_offset: None,
34 }
35 }
36
37 pub fn get_timezone_offset(&self) -> i32 {
39 self.timezone_offset
40 .unwrap_or_else(|| -self.instant.offset().utc_minus_local() / 60)
41 }
42
43 pub fn get_system_timezone_adjustment(
45 &self,
46 date: Option<DateTime<Local>>,
47 override_offset: Option<i32>,
48 ) -> i32 {
49 let date = date.unwrap_or_else(Local::now);
50 let current_offset = -date.offset().utc_minus_local() / 60;
51 let target_offset = override_offset.unwrap_or_else(|| self.get_timezone_offset());
52 current_offset - target_offset
53 }
54}
55
56#[derive(Debug, Clone)]
58pub struct ParsedResult {
59 pub ref_date: DateTime<Local>,
61 pub index: usize,
63 pub end_index: usize,
65 pub text: String,
67 pub start: FastComponents,
69 pub end: Option<FastComponents>,
71}
72
73impl ParsedResult {
74 pub fn new(
76 reference: &ReferenceWithTimezone,
77 index: usize,
78 text: impl Into<String>,
79 start: FastComponents,
80 end: Option<FastComponents>,
81 ) -> Self {
82 let text = text.into();
83 let end_index = index + text.len();
84 Self {
85 ref_date: reference.instant,
86 index,
87 end_index,
88 text,
89 start,
90 end,
91 }
92 }
93
94 pub fn date(&self, reference: &ReferenceWithTimezone) -> Option<DateTime<Local>> {
96 self.start.to_datetime(reference)
97 }
98}