format month
This commit is contained in:
39
src/lib.rs
39
src/lib.rs
@@ -17,11 +17,10 @@ quick_error! {
|
|||||||
enum SimpleDateFormatPart {
|
enum SimpleDateFormatPart {
|
||||||
YearLower(usize),
|
YearLower(usize),
|
||||||
YearUpper(usize),
|
YearUpper(usize),
|
||||||
MonthLower(usize),
|
Month(usize),
|
||||||
MonthUpper(usize),
|
|
||||||
Day,
|
Day,
|
||||||
Hour,
|
Hour,
|
||||||
Minute,
|
Minute(usize),
|
||||||
Second,
|
Second,
|
||||||
LiteralChar(char),
|
LiteralChar(char),
|
||||||
Literal(String),
|
Literal(String),
|
||||||
@@ -42,7 +41,12 @@ impl SimpleDateFormat {
|
|||||||
SimpleDateFormatPart::LiteralChar(c) => ret.push(*c),
|
SimpleDateFormatPart::LiteralChar(c) => ret.push(*c),
|
||||||
SimpleDateFormatPart::Literal(s) => ret.push_str(s),
|
SimpleDateFormatPart::Literal(s) => ret.push_str(s),
|
||||||
SimpleDateFormatPart::YearLower(cnt) => ret.push_str(&format_str(date_time.year(), *cnt)),
|
SimpleDateFormatPart::YearLower(cnt) => ret.push_str(&format_str(date_time.year(), *cnt)),
|
||||||
SimpleDateFormatPart::MonthLower(cnt) => ret.push_str(&format_str(date_time.month() as i32, *cnt)),
|
SimpleDateFormatPart::Month(cnt) => if *cnt <= 2 {
|
||||||
|
ret.push_str(&format_str(date_time.month() as i32, *cnt))
|
||||||
|
} else {
|
||||||
|
ret.push_str(format_month(date_time.month(), *cnt));
|
||||||
|
},
|
||||||
|
SimpleDateFormatPart::Minute(cnt) => ret.push_str(&format_str(date_time.minute() as i32, *cnt)),
|
||||||
_ => (),
|
_ => (),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -74,11 +78,11 @@ pub fn fmt(f: &str) -> Result<SimpleDateFormat, ParseError> {
|
|||||||
} else {
|
} else {
|
||||||
is_in_quotation_mark = true;
|
is_in_quotation_mark = true;
|
||||||
},
|
},
|
||||||
',' | '.' | ':' | '-' | ' ' => parts.push(SimpleDateFormatPart::LiteralChar(c)),
|
',' | '.' | ':' | '-' | ' ' | '/' => parts.push(SimpleDateFormatPart::LiteralChar(c)),
|
||||||
'y' => parts.push(SimpleDateFormatPart::YearLower(get_all_chars(c, &mut chars))),
|
'y' => parts.push(SimpleDateFormatPart::YearLower(get_all_chars(c, &mut chars))),
|
||||||
'Y' => parts.push(SimpleDateFormatPart::YearUpper(get_all_chars(c, &mut chars))),
|
'Y' => parts.push(SimpleDateFormatPart::YearUpper(get_all_chars(c, &mut chars))),
|
||||||
'm' => parts.push(SimpleDateFormatPart::MonthLower(get_all_chars(c, &mut chars))),
|
'M' => parts.push(SimpleDateFormatPart::Month(get_all_chars(c, &mut chars))),
|
||||||
'M' => parts.push(SimpleDateFormatPart::MonthUpper(get_all_chars(c, &mut chars))),
|
'm' => parts.push(SimpleDateFormatPart::Minute(get_all_chars(c, &mut chars))),
|
||||||
_ => (),
|
_ => (),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -87,6 +91,25 @@ pub fn fmt(f: &str) -> Result<SimpleDateFormat, ParseError> {
|
|||||||
// Err(ParseError::Format(f.into()))
|
// Err(ParseError::Format(f.into()))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn format_month(n: u32, cnt: usize) -> &'static str {
|
||||||
|
let is_short = cnt == 3;
|
||||||
|
match n {
|
||||||
|
1 => if is_short { "Jan" } else { "January" },
|
||||||
|
2 => if is_short { "Feb" } else { "February" },
|
||||||
|
3 => if is_short { "Mar" } else { "March" },
|
||||||
|
4 => if is_short { "Apr" } else { "April" },
|
||||||
|
5 => if is_short { "May" } else { "May" },
|
||||||
|
6 => if is_short { "Jun" } else { "June" },
|
||||||
|
7 => if is_short { "Jul" } else { "July" },
|
||||||
|
8 => if is_short { "Aug" } else { "August" },
|
||||||
|
9 => if is_short { "Sep" } else { "September" },
|
||||||
|
10 => if is_short { "Oct" } else { "October" },
|
||||||
|
11 => if is_short { "Nov" } else { "November" },
|
||||||
|
12 => if is_short { "Dec" } else { "December" },
|
||||||
|
_ => "ERR!UNKNOWN MONTH",
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
fn format_str(n: i32, cnt: usize) -> String {
|
fn format_str(n: i32, cnt: usize) -> String {
|
||||||
let ret = format!("{}", n);
|
let ret = format!("{}", n);
|
||||||
if cnt > ret.len() {
|
if cnt > ret.len() {
|
||||||
@@ -114,7 +137,7 @@ fn it_works() {
|
|||||||
// println!("test output: {}", fmt("").unwrap().format_local(&Local::now()));
|
// println!("test output: {}", fmt("").unwrap().format_local(&Local::now()));
|
||||||
|
|
||||||
println!("{:?}", fmt("y yy-mm 'mm '''"));
|
println!("{:?}", fmt("y yy-mm 'mm '''"));
|
||||||
println!("{:?}", fmt("y yy-mm'(-'m')' '[mm]'").unwrap().format_local(&Local::now()));
|
println!("{:?}", fmt("y yy-MM mm'(-'m')' MM MMM MMMMM '[mm]'").unwrap().format_local(&Local::now()));
|
||||||
|
|
||||||
assert_eq!(2 + 2, 4);
|
assert_eq!(2 + 2, 4);
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user