feat: v0.1.2, add now
This commit is contained in:
1
.gitignore
vendored
1
.gitignore
vendored
@@ -1,3 +1,4 @@
|
||||
.idea/
|
||||
# ---> Rust
|
||||
# Generated by Cargo
|
||||
# will have compiled files and executables
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
[package]
|
||||
name = "simpledateformat"
|
||||
version = "0.1.1"
|
||||
version = "0.1.2"
|
||||
authors = ["Hatter Jiang <jht5945@gmail.com>"]
|
||||
edition = "2018"
|
||||
description = "SimpleDateFormat.java style like date format"
|
||||
|
||||
25
src/lib.rs
25
src/lib.rs
@@ -1,4 +1,6 @@
|
||||
#[macro_use] extern crate quick_error;
|
||||
#[macro_use]
|
||||
extern crate quick_error;
|
||||
|
||||
use chrono::prelude::*;
|
||||
use std::{
|
||||
convert::TryFrom,
|
||||
@@ -79,6 +81,14 @@ pub fn format_human(d: Duration) -> String {
|
||||
return_ret(ret)
|
||||
}
|
||||
|
||||
pub fn get_local_now() -> DateTime<Local> {
|
||||
Local::now()
|
||||
}
|
||||
|
||||
pub fn get_utc_now() -> DateTime<Utc> {
|
||||
Utc::now()
|
||||
}
|
||||
|
||||
/// Format struct, parse format and format date
|
||||
#[derive(Debug)]
|
||||
pub struct SimpleDateFormat {
|
||||
@@ -86,7 +96,6 @@ pub struct SimpleDateFormat {
|
||||
}
|
||||
|
||||
impl SimpleDateFormat {
|
||||
|
||||
/// Create format from string
|
||||
pub fn new(f: &str) -> Result<SimpleDateFormat, ParseError> {
|
||||
fmt(f)
|
||||
@@ -94,7 +103,15 @@ impl SimpleDateFormat {
|
||||
|
||||
/// Create format from string with out error
|
||||
pub fn new_with_out_err(f: &str) -> SimpleDateFormat {
|
||||
Self::new(f).unwrap_or_else(|_| Self{ parts: vec![] })
|
||||
Self::new(f).unwrap_or_else(|_| Self { parts: vec![] })
|
||||
}
|
||||
|
||||
pub fn format_local_now(&self) -> String {
|
||||
self.format(&Local::now())
|
||||
}
|
||||
|
||||
pub fn format_utc_now(&self) -> String {
|
||||
self.format(&Utc::now())
|
||||
}
|
||||
|
||||
// Format date
|
||||
@@ -179,7 +196,7 @@ pub fn fmt(f: &str) -> Result<SimpleDateFormat, ParseError> {
|
||||
}
|
||||
}
|
||||
|
||||
Ok(SimpleDateFormat{ parts })
|
||||
Ok(SimpleDateFormat { parts })
|
||||
}
|
||||
|
||||
impl TryFrom<&str> for SimpleDateFormat {
|
||||
|
||||
Reference in New Issue
Block a user