[pbs-devel] [PATCH proxmox 4/5] time: posix: add epoch_to_rfc2822

Lukas Wagner l.wagner at proxmox.com
Tue Dec 5 13:11:41 CET 2023


This is the format used in the 'Date' header in mails.

Signed-off-by: Lukas Wagner <l.wagner at proxmox.com>
---
 proxmox-time/src/posix.rs | 16 ++++++++++++++++
 1 file changed, 16 insertions(+)

diff --git a/proxmox-time/src/posix.rs b/proxmox-time/src/posix.rs
index 9c8002a..96bc88e 100644
--- a/proxmox-time/src/posix.rs
+++ b/proxmox-time/src/posix.rs
@@ -371,6 +371,15 @@ fn parse_rfc3339_do(input_str: &str) -> Result<i64, Error> {
     Ok(epoch)
 }
 
+/// Convert Unix epoch into RFC2822 local time with TZ
+pub fn epoch_to_rfc2822(epoch: i64) -> Result<String, Error> {
+    let localtime = localtime(epoch)?;
+    let locale = Locale::new(libc::LC_ALL, Locale::C)?;
+    let rfc2822_date = strftime_l("%a, %d %b %Y %T %z", &localtime, &locale)?;
+
+    Ok(rfc2822_date)
+}
+
 #[test]
 fn test_leap_seconds() {
     let convert_reconvert = |epoch| {
@@ -490,3 +499,10 @@ fn test_strftime_l() {
 
     assert_eq!(formatted, "Tue, 29 Dec 2020 17:30:00 +0000");
 }
+
+#[test]
+fn test_epoch_to_rfc2822() {
+    let epoch = 1609263000;
+    let formatted = epoch_to_rfc2822(epoch).unwrap();
+    assert_eq!(formatted, "Tue, 29 Dec 2020 18:30:00 +0100");
+}
-- 
2.39.2





More information about the pbs-devel mailing list