@@ -7,7 +7,6 @@ use super::{
77} ;
88
99use {
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+
5083impl 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 }
0 commit comments