1#![allow(non_camel_case_types)]
2#![allow(non_snake_case)]
3#![allow(non_upper_case_globals)]
4#![allow(dead_code)]
5
6use time;
7use std::time::{self as std_time, SystemTime};
8
9pub mod errcode;
10pub mod tsidallocator;
11pub use tsidallocator::TsIdAllocator;
12
13pub mod tsmap;
14pub use tsmap::TsHashMap;
15pub mod tsqueue;
16pub use tsqueue::TsDequeue;
17
18pub mod indexring;
19pub mod rawstring;
20pub mod atomicqueue;
21pub use atomicqueue::AtomicDequeue;
22
23pub mod spin_lock;
24pub use spin_lock::spin_lock_t;
25
26pub mod sched;
27pub mod bitmap;
28pub use bitmap::bitmap_t;
29
30pub mod ringbuf;
31pub use ringbuf::ring_buffer_t;
32pub use ringbuf::ts_ring_buffer_t;
33
34pub mod uuid;
35pub use uuid::uuid_t;
36
37pub type rsm_time_t = time::OffsetDateTime;
38
39pub const UNIX_EPOCH_STRING:&str = "1970-1-1 00:00:00.000";
40#[inline(always)]
41pub fn get_now_usec64() -> u64 {
42 match SystemTime::UNIX_EPOCH.elapsed() {
43 Err(_) => 0,
44 Ok(d) => d.as_micros() as u64,
45 }
46}
47#[inline(always)]
48pub fn get_time_from_usec(usec: u64) -> SystemTime {
49 match SystemTime::UNIX_EPOCH.checked_add(std_time::Duration::from_micros(usec)) {
50 None => std_time::SystemTime::UNIX_EPOCH,
51 Some(t) => t,
52 }
53}
54const INVALID_MONTH:u16=0xFFFF;
55
56fn get_month(day_in_year:u16,leap_year:bool)->(u16,u16) {
57 const days_per_mon1:[u16;12]=[31,28,31,30,31,30,31,31,30,31,30,31];
58 const days_per_mon2:[u16;12]=[31,29,31,30,31,30,31,31,30,31,30,31];
59 let days=if leap_year {&days_per_mon2} else {&days_per_mon1};
60 let mut cuml = 0;
61 for i in 0..12 {
62 cuml+=days[i];
63 if day_in_year<cuml {
64 return ((i+1) as u16,day_in_year+1 -(cuml-days[i]));
65 }
66
67 }
68 return (INVALID_MONTH,0);
69}
70pub fn is_leap_year(year:u64)->bool {
71 if ((year % 4 ==0) && (year % 100!=0)) || (year % 400==0) {
72 return true
73 } else {
74 return false;
75 }
76}
77fn get_years_of_days(days:u64)->u64 {
78 if days % 365 >58 {
79 return days/365
80 } else {
81 days/365+1
82 }
83}
84pub fn ceiling(a:u64,b:u64)->u64 {
85 if a % b==0 {
86 return a/b
87 } else {
88 return a/b+1
89 }
90}
91fn get_years_since_1970(days:u64)->(u64,u64) {
93 let mut years = get_years_of_days(days);
94 let leap_years = (years+2)/4 - (70+years)/100+(370+years)/400;
95
96 years = (days-leap_years)/365;
97 return (years,years*365+leap_years);
98}
99const SECS_PER_DAY:u64=24*3600;
100#[derive(Clone)]
101pub struct datetime_t {
102 systime:std_time::SystemTime,
103 year:u32,
104 mon:u8,
105 day:u8,
106 hour:u8,
107 min:u8,
108 sec:u8,
109 msec:u16,
110}
111
112impl datetime_t {
113 pub fn new()->Self {
114 return datetime_t{
115 systime:std_time::UNIX_EPOCH,
116 year:1970,
117 mon:1,
118 day:1,
119 hour:0,
120 min:0,
121 sec:0,
122 msec:0,
123 }
124 }
125
126 pub fn get_year(&self)->u32 {
127 self.year
128 }
129 pub fn get_mon_in_year(&self)->u8 {
130 self.mon
131 }
132 pub fn get_day_in_mon(&self)->u8 {
133 self.day
134 }
135
136 pub fn get_hour_in_day(&self)->u8 {
137 self.hour
138 }
139 pub fn get_min_in_hour(&self)->u8 {
140 self.min
141 }
142 pub fn get_secs_in_min(&self)->u8 {
143 self.sec
144 }
145 pub fn get_day_in_year(&self)->u16 {
146 const days_per_mon1:[u16;12]=[31,28,31,30,31,30,31,31,30,31,30,31];
147 const days_per_mon2:[u16;12]=[31,29,31,30,31,30,31,31,30,31,30,31];
148 let pCount = if is_leap_year(self.year as u64) {
149 &days_per_mon2
150 } else {
151 &days_per_mon1
152 };
153 let mut days_in_year = 0;
154 for i in 0..self.mon as usize {
155 days_in_year+=pCount[i];
156 }
157 return days_in_year+self.day as u16;
158 }
159
160 pub fn prev_ndays(&self,days:u64)->Self {
161 let prev_time=match self.systime.checked_sub(std_time::Duration::from_secs(SECS_PER_DAY*days)) {
162 Some(d)=>d,
163 None=>self.systime.clone(),
164 };
165
166 return get_datetime_from_std(&prev_time)
167 }
168
169 pub fn prev_secs(&self,secs:u64)->Self {
170 let prev_time=match self.systime.checked_sub(std_time::Duration::from_secs(secs)) {
171 Some(d)=>d,
172 None=>self.systime.clone(),
173 };
174
175 return get_datetime_from_std(&prev_time)
176 }
177
178 pub fn to_usecs(&self)->u64 {
179 match self.systime.duration_since(std_time::UNIX_EPOCH) {
180 Err(_) => return 0,
181 Ok(d) => d.as_micros() as u64,
182 }
183 }
184}
185
186
187pub fn get_datetime_from_std(dt:&std_time::SystemTime) ->datetime_t {
188 let mut offset = 0;
189 if let Ok(local) = time::UtcOffset::current_local_offset() {
190 offset=local.whole_seconds();
191 }
192
193 let dur = (dt.duration_since(std_time::UNIX_EPOCH).unwrap().as_millis() as i64 + (offset as i64)*1000) as u64 ;
194 if dur==0 {
195 return datetime_t::new();
196 }
197 let total_days = dur/(SECS_PER_DAY*1000);
198
199 let (years,days_elapsed)=get_years_since_1970(total_days);
200 let days_in_year= total_days-days_elapsed;
201 let leap_year = is_leap_year(1970+years);
202 let (mon,day_in_mon) = get_month(days_in_year as u16, leap_year);
203 let mut msec = dur % (SECS_PER_DAY*1000);
204 let hour = msec /(3600*1000);
205 msec-=hour*3600*1000;
206 let min = msec / (60*1000);
207 msec-=min*60*1000;
208 let sec=msec/1000;
209 msec = msec %1000;
210 return datetime_t{
211 systime:dt.clone(),
212 year:(years+1970) as u32,
213 mon:mon as u8,
214 day:day_in_mon as u8,
215 hour:hour as u8,
216 min:min as u8,
217 sec:sec as u8,
218 msec:msec as u16,
219 }
220
221
222}
223
224pub fn format_datetime(dt:&std_time::SystemTime) ->String {
225 let t = get_datetime_from_std(dt);
226 format!("{:04}-{:02}-{:02} {:02}:{:02}:{:02}.{:03}",
227 t.year,t.mon,t.day,t.hour,t.min,t.sec,t.msec)
228}
229
230pub fn format_datetime2(dt:&std_time::SystemTime) ->String {
231 let tm = time::OffsetDateTime::from(*dt);
232 format!("{}",tm)
233}
234
235pub fn format_datetime_golang(dt:&std_time::SystemTime) ->String {
236 let t = get_datetime_from_std(dt);
237 format!("{:04}-{:02}-{:02}T{:02}:{:02}:{:02}Z",
238 t.year,t.mon,t.day,t.hour,t.min,t.sec)
239}