| 54 | } |
| 55 | |
| 56 | pub fn get_rx_timestamp(rx_info: &[gw::UplinkRxInfo]) -> SystemTime { |
| 57 | let conf = config::get(); |
| 58 | let rx_timestamp_max_drift = conf.gateway.rx_timestamp_max_drift; |
| 59 | |
| 60 | // First search for time_since_gps_epoch. |
| 61 | for rxi in rx_info { |
| 62 | if let Some(gps_time) = &rxi.time_since_gps_epoch |
| 63 | && let Ok(ts) = chrono::Duration::from_std(Duration::new( |
| 64 | gps_time.seconds as u64, |
| 65 | gps_time.nanos as u32, |
| 66 | )) |
| 67 | { |
| 68 | return ts.to_date_time().into(); |
| 69 | } |
| 70 | } |
| 71 | |
| 72 | // Then search for time. |
| 73 | for rxi in rx_info { |
| 74 | if let Some(ts) = &rxi.gw_time { |
| 75 | let ts: Result<DateTime<Utc>> = (*ts).try_into().map_err(anyhow::Error::msg); |
| 76 | if let Ok(ts) = ts { |
| 77 | let mut delta = Utc::now() - ts; |
| 78 | if delta < TimeDelta::default() { |
| 79 | delta = -delta; |
| 80 | } |
| 81 | let delta = delta.to_std().unwrap_or_default(); |
| 82 | if delta < rx_timestamp_max_drift { |
| 83 | return ts.into(); |
| 84 | } |
| 85 | } |
| 86 | } |
| 87 | } |
| 88 | |
| 89 | // last resort use systemtime of NS |
| 90 | SystemTime::now() |
| 91 | } |
| 92 | |
| 93 | pub fn get_rx_timestamp_chrono(rx_info: &[gw::UplinkRxInfo]) -> DateTime<Utc> { |
| 94 | let conf = config::get(); |