safer handling of Local DateTime objects
This commit is contained in:
		
							parent
							
								
									4dc14db293
								
							
						
					
					
						commit
						ed0c277c4b
					
				
					 1 changed files with 13 additions and 5 deletions
				
			
		|  | @ -7,9 +7,17 @@ pub(crate) mod repo; | ||||||
| pub(crate) mod tree; | pub(crate) mod tree; | ||||||
| 
 | 
 | ||||||
| use chrono::offset::{Local, TimeZone}; | use chrono::offset::{Local, TimeZone}; | ||||||
| use chrono::DateTime; | use chrono::{DateTime, NaiveDateTime}; | ||||||
| use git2::{Commit, Signature}; | use git2::{Commit, Signature}; | ||||||
| 
 | 
 | ||||||
|  | fn min_date() -> DateTime<Local> { | ||||||
|  |     Local.from_utc_datetime(&NaiveDateTime::MIN) | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | fn timestamp_with_fallback(timestamp: i64) -> DateTime<Local> { | ||||||
|  |     Local.timestamp_opt(timestamp, 0).earliest().unwrap_or(min_date()) | ||||||
|  | } | ||||||
|  | 
 | ||||||
| #[derive(Clone, Debug)] | #[derive(Clone, Debug)] | ||||||
| pub struct SocialSignature { | pub struct SocialSignature { | ||||||
|     pub when: DateTime<Local>, |     pub when: DateTime<Local>, | ||||||
|  | @ -20,7 +28,7 @@ pub struct SocialSignature { | ||||||
| impl Default for SocialSignature { | impl Default for SocialSignature { | ||||||
|     fn default() -> Self { |     fn default() -> Self { | ||||||
|         Self { |         Self { | ||||||
|             when: Local.timestamp(0, 0), |             when: min_date(), | ||||||
|             name: "".to_string(), |             name: "".to_string(), | ||||||
|             email: "".to_string(), |             email: "".to_string(), | ||||||
|         } |         } | ||||||
|  | @ -53,7 +61,7 @@ impl Default for SocialCommit { | ||||||
|     fn default() -> Self { |     fn default() -> Self { | ||||||
|         Self { |         Self { | ||||||
|             id: "".to_string(), |             id: "".to_string(), | ||||||
|             time: Local.timestamp(0, 0), |             time: min_date(), | ||||||
|             message: "".to_string(), |             message: "".to_string(), | ||||||
|             author: SocialSignature::default(), |             author: SocialSignature::default(), | ||||||
|             committer: SocialSignature::default(), |             committer: SocialSignature::default(), | ||||||
|  | @ -68,7 +76,7 @@ impl From<Signature<'_>> for SocialSignature { | ||||||
|         Self { |         Self { | ||||||
|             name: s.name().unwrap_or("").to_owned(), |             name: s.name().unwrap_or("").to_owned(), | ||||||
|             email: s.email().unwrap_or("").to_owned(), |             email: s.email().unwrap_or("").to_owned(), | ||||||
|             when: Local.timestamp(s.when().seconds(), 0), |             when: timestamp_with_fallback(s.when().seconds()), | ||||||
|         } |         } | ||||||
|     } |     } | ||||||
| } | } | ||||||
|  | @ -77,7 +85,7 @@ impl From<Commit<'_>> for SocialCommit { | ||||||
|     fn from(c: Commit<'_>) -> Self { |     fn from(c: Commit<'_>) -> Self { | ||||||
|         Self { |         Self { | ||||||
|             id: format!("{}", c.id()), |             id: format!("{}", c.id()), | ||||||
|             time: Local.timestamp(c.time().seconds(), 0), |             time: timestamp_with_fallback(c.time().seconds()), | ||||||
|             message: c.message().unwrap_or("").to_owned(), |             message: c.message().unwrap_or("").to_owned(), | ||||||
|             author: c.author().into(), |             author: c.author().into(), | ||||||
|             committer: c.committer().into(), |             committer: c.committer().into(), | ||||||
|  |  | ||||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue