diff options
author | Jeremy Lin <[email protected]> | 2020-12-11 22:47:54 -0800 |
---|---|---|
committer | Jeremy Lin <[email protected]> | 2020-12-13 19:49:22 -0800 |
commit | 455a23361f9071b7c16048ce320ecfc26adccc2d (patch) | |
tree | edd62880226564e78dcc6decf1cb67031211237f /src/mail.rs | |
parent | 219a9d9f5ed9a374f2b0f7ad039bf57394b1852c (diff) | |
download | vaultwarden-455a23361f9071b7c16048ce320ecfc26adccc2d.tar.gz vaultwarden-455a23361f9071b7c16048ce320ecfc26adccc2d.zip |
Clean up datetime output and code
* For clarity, add `UTC` suffix for datetimes in the `Diagnostics` admin tab.
* Format datetimes in the local timezone in the `Users` admin tab.
* Refactor some datetime code and add doc comments.
Diffstat (limited to 'src/mail.rs')
-rw-r--r-- | src/mail.rs | 22 |
1 files changed, 3 insertions, 19 deletions
diff --git a/src/mail.rs b/src/mail.rs index 7419169a..641d9127 100644 --- a/src/mail.rs +++ b/src/mail.rs @@ -1,7 +1,6 @@ -use std::{env, str::FromStr}; +use std::{str::FromStr}; use chrono::{DateTime, Local}; -use chrono_tz::Tz; use percent_encoding::{percent_encode, NON_ALPHANUMERIC}; use lettre::{ @@ -107,22 +106,6 @@ fn get_template(template_name: &str, data: &serde_json::Value) -> Result<(String Ok((subject, body)) } -pub fn format_datetime(dt: &DateTime<Local>) -> String { - let fmt = "%A, %B %_d, %Y at %r %Z"; - - // With a DateTime<Local>, `%Z` formats as the time zone's UTC offset - // (e.g., `+00:00`). If the `TZ` environment variable is set, try to - // format as a time zone abbreviation instead (e.g., `UTC`). - if let Ok(tz) = env::var("TZ") { - if let Ok(tz) = tz.parse::<Tz>() { - return dt.with_timezone(&tz).format(fmt).to_string(); - } - } - - // Otherwise, fall back to just displaying the UTC offset. - dt.format(fmt).to_string() -} - pub fn send_password_hint(address: &str, hint: Option<String>) -> EmptyResult { let template_name = if hint.is_some() { "email/pw_hint_some" @@ -257,13 +240,14 @@ pub fn send_new_device_logged_in(address: &str, ip: &str, dt: &DateTime<Local>, use crate::util::upcase_first; let device = upcase_first(device); + let fmt = "%A, %B %_d, %Y at %r %Z"; let (subject, body_html, body_text) = get_text( "email/new_device_logged_in", json!({ "url": CONFIG.domain(), "ip": ip, "device": device, - "datetime": format_datetime(dt), + "datetime": crate::util::format_datetime_local(dt, fmt), }), )?; |