Skip to content

Commit d200c92

Browse files
committed
replace chrono with a simpler custom DateTime
1 parent aea3bad commit d200c92

File tree

4 files changed

+80
-15
lines changed

4 files changed

+80
-15
lines changed

Cargo.toml

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,14 @@ description = "FTP client for Rust"
88
readme = "README.md"
99
license = "Apache-2.0/MIT"
1010
keywords = ["ftp"]
11+
edition = "2021"
1112
categories = ["network-programming"]
1213

1314
[badges]
1415
travis-ci = { repository = "mattnenterprise/rust-ftp" }
1516

1617
[lib]
17-
name ="ftp"
18+
name = "ftp"
1819
path = "src/lib.rs"
1920

2021
[features]
@@ -25,11 +26,11 @@ debug_print = []
2526
[dependencies]
2627
lazy_static = "1"
2728
regex = "1"
28-
chrono = "0.4"
2929
openssl = { version = "0.10", optional = true }
3030
native-tls = { version = "0.2", optional = true }
3131

32-
33-
3432
[package.metadata.docs.rs]
3533
rustc-args = ["--cfg", "openssl"]
34+
35+
[dev-dependencies]
36+
chrono = "0.4"

src/ftp.rs

Lines changed: 44 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ use super::{
77
};
88

99
use {
10-
chrono::{offset::TimeZone, DateTime, Utc},
1110
regex::Regex,
1211
std::{
1312
borrow::Cow,
@@ -47,6 +46,40 @@ pub struct FtpStream {
4746
ssl_cfg: Option<SslContext>,
4847
}
4948

49+
pub struct DateTime {
50+
pub year: u32,
51+
pub month: u32,
52+
pub day: u32,
53+
pub hour: u32,
54+
pub minute: u32,
55+
pub second: u32,
56+
}
57+
impl DateTime {
58+
pub fn timestamp(&self) -> u32 {
59+
let days_asof_m = [31_u16, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365];
60+
let yyear = self.year - 1;
61+
let countleap = ((yyear / 4) - (yyear / 100) + (yyear / 400))
62+
- ((1970 / 4) - (1970 / 100) + (1970 / 400));
63+
64+
let m = if self.month > 1 {
65+
let days_per_month = ((self.year % 4 == 0
66+
&& ((self.year % 100 != 0) || self.year % 400 == 0))
67+
&& self.month > 2
68+
|| self.month == 2 && self.day >= 29) as u16;
69+
(days_asof_m[(self.month - 2) as usize] + days_per_month) as u32 * 86400
70+
} else {
71+
0
72+
};
73+
(self.year - 1970) * 365 * 86400
74+
+ (countleap * 86400)
75+
+ self.second
76+
+ (self.hour * 3600)
77+
+ (self.minute * 60)
78+
+ ((self.day - 1) * 86400)
79+
+ m
80+
}
81+
}
82+
5083
impl FtpStream {
5184
/// Creates an FTP Stream and returns the welcome message
5285
#[cfg(not(any(feature = "openssl", feature = "native-tls")))]
@@ -641,14 +674,14 @@ impl FtpStream {
641674

642675
/// Retrieves the modification time of the file at `pathname` if it exists.
643676
/// In case the file does not exist `None` is returned.
644-
pub fn mdtm(&mut self, pathname: &str) -> crate::Result<Option<DateTime<Utc>>> {
677+
pub fn mdtm(&mut self, pathname: &str) -> crate::Result<Option<DateTime>> {
645678
self.write_str(format!("MDTM {}\r\n", pathname))?;
646679
let Line(_, content) = self.read_response(status::FILE)?;
647680

648681
match MDTM_RE.captures(&content) {
649682
Some(caps) => {
650683
let (year, month, day) = (
651-
caps[1].parse::<i32>().unwrap(),
684+
caps[1].parse::<u32>().unwrap(),
652685
caps[2].parse::<u32>().unwrap(),
653686
caps[3].parse::<u32>().unwrap(),
654687
);
@@ -657,9 +690,14 @@ impl FtpStream {
657690
caps[5].parse::<u32>().unwrap(),
658691
caps[6].parse::<u32>().unwrap(),
659692
);
660-
Ok(Some(
661-
Utc.ymd(year, month, day).and_hms(hour, minute, second),
662-
))
693+
Ok(Some(DateTime {
694+
year,
695+
month,
696+
day,
697+
hour,
698+
minute,
699+
second,
700+
}))
663701
}
664702
None => Ok(None),
665703
}

src/lib.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,8 @@
2828
//! For better security it's the good practice to switch to the secure mode
2929
//! before authentication.
3030
//!
31-
#![cfg_attr(feature = "openssl",
31+
#![cfg_attr(
32+
feature = "openssl",
3233
doc = r##"
3334
## FTPS Usage
3435
@@ -49,7 +50,8 @@ let _ = ftp_stream.quit();
4950
```
5051
"##
5152
)]
52-
#![cfg_attr(feature = "native-ssl",
53+
#![cfg_attr(
54+
feature = "native-ssl",
5355
doc = r##"
5456
## FTPS Usage
5557
@@ -72,7 +74,6 @@ let _ = ftp_stream.quit();
7274
)]
7375
#[macro_use]
7476
extern crate lazy_static;
75-
extern crate chrono;
7677
extern crate regex;
7778

7879
#[cfg(feature = "native-tls")]
@@ -85,7 +86,7 @@ mod ftp;
8586
pub mod status;
8687
pub mod types;
8788

88-
pub use self::ftp::FtpStream;
89+
pub use self::ftp::{DateTime, FtpStream};
8990
pub use self::types::FtpError;
9091

9192
/// A shorthand for a Result whose error type is always an FtpError.

src/types.rs

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,8 +140,33 @@ impl std::error::Error for FtpError {
140140

141141
#[cfg(test)]
142142
mod tests {
143-
144143
use super::*;
144+
use crate::DateTime;
145+
use chrono::TimeZone;
146+
147+
#[test]
148+
fn test_datetime() {
149+
let year: u32 = 2024;
150+
let month: u32 = 3;
151+
let day: u32 = 28;
152+
let hour: u32 = 13;
153+
let minute: u32 = 33;
154+
let second: u32 = 59;
155+
156+
let dt = DateTime {
157+
year,
158+
month,
159+
day,
160+
hour,
161+
minute,
162+
second,
163+
};
164+
let chronos_dt = chrono::Utc
165+
.ymd(year as i32, month, day)
166+
.and_hms(hour, minute, second);
167+
168+
assert_eq!(dt.timestamp(), chronos_dt.timestamp() as u32);
169+
}
145170

146171
#[test]
147172
fn error_str() {

0 commit comments

Comments
 (0)