From 7eca5520d7ffc4f59b623807ec0c7fe0c3f6b2ef Mon Sep 17 00:00:00 2001 From: Hatter Jiang Date: Thu, 28 May 2020 08:20:13 +0800 Subject: [PATCH] simpledateformat init version --- Cargo.toml | 16 ++++++++++++++++ README.md | 6 +++++- src/lib.rs | 51 +++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 72 insertions(+), 1 deletion(-) create mode 100644 Cargo.toml create mode 100644 src/lib.rs diff --git a/Cargo.toml b/Cargo.toml new file mode 100644 index 0000000..88ed464 --- /dev/null +++ b/Cargo.toml @@ -0,0 +1,16 @@ +[package] +name = "simpledateformat" +version = "0.0.0" +authors = ["Hatter Jiang "] +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" diff --git a/README.md b/README.md index 6e24493..135588d 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,7 @@ # simpledateformat -SimpleDateFormat.java style like date format \ No newline at end of file +SimpleDateFormat.java style like date format + + +Working in progress ... + diff --git a/src/lib.rs b/src/lib.rs new file mode 100644 index 0000000..e85f222 --- /dev/null +++ b/src/lib.rs @@ -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, +} + +impl SimpleDateFormat { + + fn format_local(&self, date_time: &DateTime) -> 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 { + 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); +}