@@ -39,9 +39,9 @@ struct Cli {
39
39
files : Vec < PathBuf > ,
40
40
}
41
41
42
- fn parse_seconds ( stamp : & String ) -> u8 {
42
+ fn parse_seconds ( stamp : & String ) -> u32 {
43
43
let tokens: Vec < & str > = stamp. split ( "." ) . collect ( ) ;
44
- let mut secs_u8 = 0 ;
44
+ let mut secs_u32 : u32 = 0 ;
45
45
46
46
if tokens. len ( ) > 1 {
47
47
let mut secs = tokens[ 1 ] . to_string ( ) ;
@@ -50,55 +50,105 @@ fn parse_seconds(stamp: &String) -> u8 {
50
50
secs. push_str ( "0" ) ;
51
51
}
52
52
53
- secs_u8 = match u8 :: from_str ( secs. as_str ( ) ) {
53
+ secs_u32 = match u32 :: from_str ( secs. as_str ( ) ) {
54
54
Ok ( s) => s,
55
55
Err ( _) => 0 ,
56
56
} ;
57
57
58
- if secs_u8 > 59 {
59
- secs_u8 = 0 ;
58
+ if secs_u32 > 59 {
59
+ secs_u32 = 0 ;
60
60
}
61
61
}
62
62
63
- return secs_u8 ;
63
+ return secs_u32 ;
64
64
}
65
65
66
- fn parse_minutes ( stamp : & String ) -> u8 {
66
+ fn parse_minutes ( stamp : & String ) -> u32 {
67
67
let tokens: Vec < & str > = stamp. split ( "." ) . collect ( ) ;
68
68
let mins = tokens[ 0 ] ;
69
- let mut mins_u8 = 0 ;
69
+ let mut mins_u32 : u32 = 0 ;
70
70
71
71
if let Some ( ( i, _) ) = mins. char_indices ( ) . rev ( ) . nth ( 1 ) {
72
72
let mins_str = & mins[ i..] ;
73
73
74
- mins_u8 = match u8 :: from_str ( mins_str) {
74
+ mins_u32 = match u32 :: from_str ( mins_str) {
75
75
Ok ( s) => s,
76
76
Err ( _) => 0 ,
77
77
} ;
78
78
79
- if mins_u8 > 59 {
80
- mins_u8 = 0 ;
79
+ if mins_u32 > 59 {
80
+ mins_u32 = 0 ;
81
81
}
82
82
}
83
83
84
- return mins_u8 ;
84
+ return mins_u32 ;
85
85
}
86
86
87
- fn parse_hours ( _stamp : & String ) -> u8 {
88
- return 0 ;
87
+ fn parse_hours ( stamp : & String ) -> u32 {
88
+ let tokens: Vec < & str > = stamp. split ( "." ) . collect ( ) ;
89
+ let hours = tokens[ 0 ] ;
90
+ let mut hours_u32: u32 = 0 ;
91
+
92
+ if hours. len ( ) < 8 {
93
+ return hours_u32;
94
+ }
95
+
96
+ if let Some ( ( i, _) ) = hours. char_indices ( ) . nth ( hours. len ( ) - 2 ) {
97
+ let some_str = & hours[ ..i] ;
98
+
99
+ if let Some ( ( j, _) ) = some_str. char_indices ( ) . rev ( ) . nth ( 1 ) {
100
+ let hours_str = & some_str[ j..] ;
101
+
102
+ hours_u32 = match u32:: from_str ( hours_str) {
103
+ Ok ( s) => s,
104
+ Err ( _) => 0 ,
105
+ } ;
106
+
107
+ if hours_u32 > 23 {
108
+ hours_u32 = 0 ;
109
+ }
110
+ }
111
+ }
112
+
113
+ return hours_u32;
89
114
}
90
115
91
- fn parse_day ( _stamp : & String ) -> u8 {
92
- return 1 ;
116
+ fn parse_day ( stamp : & String ) -> u32 {
117
+ let tokens: Vec < & str > = stamp. split ( "." ) . collect ( ) ;
118
+ let day = tokens[ 0 ] ;
119
+ let mut day_u32: u32 = 1 ;
120
+
121
+ if day. len ( ) < 8 {
122
+ return day_u32;
123
+ }
124
+
125
+ if let Some ( ( i, _) ) = day. char_indices ( ) . nth ( day. len ( ) - 4 ) {
126
+ let some_str = & day[ ..i] ;
127
+
128
+ if let Some ( ( j, _) ) = some_str. char_indices ( ) . rev ( ) . nth ( 1 ) {
129
+ let day_str = & some_str[ j..] ;
130
+
131
+ day_u32 = match u32:: from_str ( day_str) {
132
+ Ok ( s) => s,
133
+ Err ( _) => 1 ,
134
+ } ;
135
+
136
+ if day_u32 > 31 {
137
+ day_u32 = 1 ;
138
+ }
139
+ }
140
+ }
141
+
142
+ return day_u32;
93
143
}
94
144
95
- fn parse_month ( stamp : & String ) -> u8 {
145
+ fn parse_month ( stamp : & String ) -> u32 {
96
146
let tokens: Vec < & str > = stamp. split ( "." ) . collect ( ) ;
97
147
let month = tokens[ 0 ] ;
98
- let mut month_u8 = 1 ;
148
+ let mut month_u32 : u32 = 1 ;
99
149
100
150
if month. len ( ) < 8 {
101
- return month_u8 ;
151
+ return month_u32 ;
102
152
}
103
153
104
154
if let Some ( ( i, _) ) = month. char_indices ( ) . nth ( month. len ( ) - 6 ) {
@@ -107,18 +157,18 @@ fn parse_month(stamp: &String) -> u8 {
107
157
if let Some ( ( j, _) ) = some_str. char_indices ( ) . rev ( ) . nth ( 1 ) {
108
158
let month_str = & some_str[ j..] ;
109
159
110
- month_u8 = match u8 :: from_str ( month_str) {
160
+ month_u32 = match u32 :: from_str ( month_str) {
111
161
Ok ( s) => s,
112
162
Err ( _) => 1 ,
113
163
} ;
114
164
115
- if month_u8 > 12 {
116
- month_u8 = 1 ;
165
+ if month_u32 > 12 {
166
+ month_u32 = 1 ;
117
167
}
118
168
}
119
169
}
120
170
121
- return month_u8 ;
171
+ return month_u32 ;
122
172
}
123
173
124
174
fn parse_year ( stamp : & String ) -> i32 {
@@ -149,14 +199,15 @@ fn parse_year(stamp: &String) -> i32 {
149
199
}
150
200
151
201
pub fn parse_tstamp ( stamp : & String ) -> FileTime {
152
- let _year = parse_year ( & stamp) ;
153
- let _month = parse_month ( & stamp) ;
154
- let _day = parse_day ( & stamp) ;
155
- let _hour = parse_hours ( & stamp) ;
156
- let _minutes = parse_minutes ( & stamp) ;
157
- let _seconds = parse_seconds ( & stamp) ;
158
-
159
- return FileTime :: now ( ) ;
202
+ let year = parse_year ( & stamp) ;
203
+ let month = parse_month ( & stamp) ;
204
+ let day = parse_day ( & stamp) ;
205
+ let hour = parse_hours ( & stamp) ;
206
+ let minutes = parse_minutes ( & stamp) ;
207
+ let seconds = parse_seconds ( & stamp) ;
208
+
209
+ let dt: DateTime < Local > = Local . ymd ( year, month, day) . and_hms ( hour, minutes, seconds) ;
210
+ return FileTime :: from_unix_time ( dt. timestamp ( ) , 0 ) ;
160
211
}
161
212
162
213
@@ -258,13 +309,28 @@ mod tests {
258
309
259
310
#[ test]
260
311
fn test_parse_hours ( ) {
312
+ assert_eq ! ( 1 , parse_hours( & String :: from( "200001010100.00" ) ) ) ;
313
+ assert_eq ! ( 2 , parse_hours( & String :: from( "201302020200.00" ) ) ) ;
314
+ assert_eq ! ( 15 , parse_hours( & String :: from( "1612151500.00" ) ) ) ;
315
+ assert_eq ! ( 22 , parse_hours( & String :: from( "11242200.00" ) ) ) ;
316
+ // TODO: what's the convention here?
317
+ assert_eq ! ( 0 , parse_hours( & String :: from( "200013322400.00" ) ) ) ;
318
+ assert_eq ! ( 0 , parse_hours( & String :: from( "qwertyuiop" ) ) ) ;
261
319
assert_eq ! ( 0 , parse_hours( & String :: from( "test" ) ) ) ;
262
320
assert_eq ! ( 0 , parse_hours( & String :: from( "test.test" ) ) ) ;
263
321
assert_eq ! ( 0 , parse_hours( & String :: from( "" ) ) ) ;
264
322
}
265
323
266
324
#[ test]
267
325
fn test_parse_day ( ) {
326
+ assert_eq ! ( 1 , parse_day( & String :: from( "200001010000.00" ) ) ) ;
327
+ assert_eq ! ( 2 , parse_day( & String :: from( "201302020000.00" ) ) ) ;
328
+ assert_eq ! ( 15 , parse_day( & String :: from( "1612150000.00" ) ) ) ;
329
+ assert_eq ! ( 24 , parse_day( & String :: from( "11240000.00" ) ) ) ;
330
+ // TODO: what's the convention here?
331
+ // TODO: know what the upper limit is depending on the month
332
+ assert_eq ! ( 1 , parse_day( & String :: from( "200013320000.00" ) ) ) ;
333
+ assert_eq ! ( 1 , parse_day( & String :: from( "qwertyuiop" ) ) ) ;
268
334
assert_eq ! ( 1 , parse_day( & String :: from( "test" ) ) ) ;
269
335
assert_eq ! ( 1 , parse_day( & String :: from( "test.test" ) ) ) ;
270
336
assert_eq ! ( 1 , parse_day( & String :: from( "" ) ) ) ;
@@ -276,7 +342,7 @@ mod tests {
276
342
assert_eq ! ( 2 , parse_month( & String :: from( "201302010000.00" ) ) ) ;
277
343
assert_eq ! ( 12 , parse_month( & String :: from( "1612010000.00" ) ) ) ;
278
344
assert_eq ! ( 11 , parse_month( & String :: from( "11010000.00" ) ) ) ;
279
- // TODO: what's the convention here
345
+ // TODO: what's the convention here?
280
346
assert_eq ! ( 1 , parse_month( & String :: from( "200013010000.00" ) ) ) ;
281
347
assert_eq ! ( 1 , parse_month( & String :: from( "qwertyuiop" ) ) ) ;
282
348
assert_eq ! ( 1 , parse_month( & String :: from( "test" ) ) ) ;
0 commit comments