simpledateformat init version

This commit is contained in:
2020-05-28 08:20:13 +08:00
parent 136cf75a48
commit 7eca5520d7
3 changed files with 72 additions and 1 deletions

16
Cargo.toml Normal file
View File

@@ -0,0 +1,16 @@
[package]
name = "simpledateformat"
version = "0.0.0"
authors = ["Hatter Jiang <jht5945@gmail.com>"]
edition = "2018"
description = "SimpleDateFormat.java style like date format"
keywords = ["DateFormat", "SimpleDateFormat"]
readme = "README.md"
repository = "https://git.hatter.ink/hatter/simpledateformat"
license = "MIT"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
quick-error = "1.2.3"
chrono = "0.4.11"

View File

@@ -1,3 +1,7 @@
# simpledateformat # simpledateformat
SimpleDateFormat.java style like date format SimpleDateFormat.java style like date format
Working in progress ...

51
src/lib.rs Normal file
View File

@@ -0,0 +1,51 @@
#[macro_use] extern crate quick_error;
use chrono::{ Local,prelude::*, };
quick_error! {
#[derive(Debug)]
pub enum ParseError {
Format(m: String) {
display("Format error: {}", m)
}
}
}
enum SimpleDateFormatPart {
Year,
Month,
Day,
Hour,
Minute,
Second,
Literal(String),
}
pub struct SimpleDateFormat {
parts: Vec<SimpleDateFormatPart>,
}
impl SimpleDateFormat {
fn format_local(&self, date_time: &DateTime<Local>) -> String {
let mut ret = String::with_capacity(512);
ret.push_str(&format!("{}", date_time.year()));
ret.push('-');
ret.push_str(&format!("{}", date_time.month()));
ret.push('-');
ret.push_str(&format!("{}", date_time.day()));
ret
}
}
pub fn fmt(f: &str) -> Result<SimpleDateFormat, ParseError> {
Ok(SimpleDateFormat{ parts: vec![] })
// Err(ParseError::Format(f.into()))
}
#[test]
fn it_works() {
println!("test output: {}", fmt("").unwrap().format_local(&Local::now()));
assert_eq!(2 + 2, 4);
}