Advertisement
Guest User

Untitled

a guest
Jul 23rd, 2019
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.67 KB | None | 0 0
  1. use std::fmt;
  2. use std::fmt::Write;
  3. use chrono::prelude::*;
  4.  
  5. pub struct Time {
  6. pub format: String,
  7. }
  8.  
  9. impl fmt::Display for Time {
  10. fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
  11. write!(f, "{}", Time::now(&self))
  12. }
  13. }
  14.  
  15. impl Time {
  16. fn now(&self) -> String {
  17. Local::now().format(&self.format).to_string()
  18. }
  19. fn from_format(format: String) -> Time {
  20. Time { format: format }
  21. }
  22. }
  23.  
  24. fn main() {
  25. let valid_format = Time::from_format("%A, %Y-%m-%d, %H:%M:%S".to_string());
  26. let invalid_format = Time::from_format("%DAG@#$%#^$@#%DR".to_string());
  27. println!("{}", valid_format); // works
  28. println!("{}", invalid_format); // panic
  29. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement