Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- use std::fmt;
- use std::fmt::Write;
- use chrono::prelude::*;
- pub struct Time {
- pub format: String,
- }
- impl fmt::Display for Time {
- fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
- write!(f, "{}", Time::now(&self))
- }
- }
- impl Time {
- fn now(&self) -> String {
- Local::now().format(&self.format).to_string()
- }
- fn from_format(format: String) -> Time {
- Time { format: format }
- }
- }
- fn main() {
- let valid_format = Time::from_format("%A, %Y-%m-%d, %H:%M:%S".to_string());
- let invalid_format = Time::from_format("%DAG@#$%#^$@#%DR".to_string());
- println!("{}", valid_format); // works
- println!("{}", invalid_format); // panic
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement