simpledateformat init version
This commit is contained in:
16
Cargo.toml
Normal file
16
Cargo.toml
Normal 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"
|
||||||
@@ -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
51
src/lib.rs
Normal 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);
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user