Skip to content

Commit 85539d6

Browse files
committed
Last commit for the -t STAMP option #8
1 parent 3f7a2dc commit 85539d6

File tree

2 files changed

+99
-33
lines changed

2 files changed

+99
-33
lines changed

Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
[package]
22
name = "touch"
33
version = "0.1.0"
4-
authors = ["srbdev <[email protected]>"]
4+
authors = ["srbdev"]
55
license = "GPLv3+"
66
description = "A Rust implementation of the touch command (for Windows)"
77
readme = "README.md"

src/main.rs

+98-32
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,9 @@ struct Cli {
3939
files: Vec<PathBuf>,
4040
}
4141

42-
fn parse_seconds(stamp: &String) -> u8 {
42+
fn parse_seconds(stamp: &String) -> u32 {
4343
let tokens: Vec<&str> = stamp.split(".").collect();
44-
let mut secs_u8 = 0;
44+
let mut secs_u32: u32 = 0;
4545

4646
if tokens.len() > 1 {
4747
let mut secs = tokens[1].to_string();
@@ -50,55 +50,105 @@ fn parse_seconds(stamp: &String) -> u8 {
5050
secs.push_str("0");
5151
}
5252

53-
secs_u8 = match u8::from_str(secs.as_str()) {
53+
secs_u32 = match u32::from_str(secs.as_str()) {
5454
Ok(s) => s,
5555
Err(_) => 0,
5656
};
5757

58-
if secs_u8 > 59 {
59-
secs_u8 = 0;
58+
if secs_u32 > 59 {
59+
secs_u32 = 0;
6060
}
6161
}
6262

63-
return secs_u8;
63+
return secs_u32;
6464
}
6565

66-
fn parse_minutes(stamp: &String) -> u8 {
66+
fn parse_minutes(stamp: &String) -> u32 {
6767
let tokens: Vec<&str> = stamp.split(".").collect();
6868
let mins = tokens[0];
69-
let mut mins_u8 = 0;
69+
let mut mins_u32: u32 = 0;
7070

7171
if let Some((i, _)) = mins.char_indices().rev().nth(1) {
7272
let mins_str = &mins[i..];
7373

74-
mins_u8 = match u8::from_str(mins_str) {
74+
mins_u32 = match u32::from_str(mins_str) {
7575
Ok(s) => s,
7676
Err(_) => 0,
7777
};
7878

79-
if mins_u8 > 59 {
80-
mins_u8 = 0;
79+
if mins_u32 > 59 {
80+
mins_u32 = 0;
8181
}
8282
}
8383

84-
return mins_u8;
84+
return mins_u32;
8585
}
8686

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;
89114
}
90115

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;
93143
}
94144

95-
fn parse_month(stamp: &String) -> u8 {
145+
fn parse_month(stamp: &String) -> u32 {
96146
let tokens: Vec<&str> = stamp.split(".").collect();
97147
let month = tokens[0];
98-
let mut month_u8 = 1;
148+
let mut month_u32: u32 = 1;
99149

100150
if month.len() < 8 {
101-
return month_u8;
151+
return month_u32;
102152
}
103153

104154
if let Some((i, _)) = month.char_indices().nth(month.len() - 6) {
@@ -107,18 +157,18 @@ fn parse_month(stamp: &String) -> u8 {
107157
if let Some((j, _)) = some_str.char_indices().rev().nth(1) {
108158
let month_str = &some_str[j..];
109159

110-
month_u8 = match u8::from_str(month_str) {
160+
month_u32 = match u32::from_str(month_str) {
111161
Ok(s) => s,
112162
Err(_) => 1,
113163
};
114164

115-
if month_u8 > 12 {
116-
month_u8 = 1;
165+
if month_u32 > 12 {
166+
month_u32 = 1;
117167
}
118168
}
119169
}
120170

121-
return month_u8;
171+
return month_u32;
122172
}
123173

124174
fn parse_year(stamp: &String) -> i32 {
@@ -149,14 +199,15 @@ fn parse_year(stamp: &String) -> i32 {
149199
}
150200

151201
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);
160211
}
161212

162213

@@ -258,13 +309,28 @@ mod tests {
258309

259310
#[test]
260311
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")));
261319
assert_eq!(0, parse_hours(&String::from("test")));
262320
assert_eq!(0, parse_hours(&String::from("test.test")));
263321
assert_eq!(0, parse_hours(&String::from("")));
264322
}
265323

266324
#[test]
267325
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")));
268334
assert_eq!(1, parse_day(&String::from("test")));
269335
assert_eq!(1, parse_day(&String::from("test.test")));
270336
assert_eq!(1, parse_day(&String::from("")));
@@ -276,7 +342,7 @@ mod tests {
276342
assert_eq!(2, parse_month(&String::from("201302010000.00")));
277343
assert_eq!(12, parse_month(&String::from("1612010000.00")));
278344
assert_eq!(11, parse_month(&String::from("11010000.00")));
279-
// TODO: what's the convention here
345+
// TODO: what's the convention here?
280346
assert_eq!(1, parse_month(&String::from("200013010000.00")));
281347
assert_eq!(1, parse_month(&String::from("qwertyuiop")));
282348
assert_eq!(1, parse_month(&String::from("test")));

0 commit comments

Comments
 (0)