From f59e02cf251d981b5d786ecdd9a9258a79b3abe7 Mon Sep 17 00:00:00 2001 From: Antonio Scandurra Date: Thu, 2 Sep 2021 19:15:05 +0200 Subject: [PATCH] Use macOS API to retrieve the local timezone The `time` crate currently doesn't have a reliable way to get that. In the future, `NSSystemTimeZoneDidChangeNotification` could be used to keep the cached timezone up-to-date. Co-Authored-By: Max Brunsfeld --- Cargo.lock | 1 + gpui/Cargo.toml | 1 + gpui/src/platform.rs | 3 +++ gpui/src/platform/mac/platform.rs | 9 +++++++++ gpui/src/platform/test.rs | 5 +++++ zed/Cargo.toml | 2 +- zed/src/chat_panel.rs | 15 ++++++++++----- 7 files changed, 30 insertions(+), 6 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index fdb5a30352589b0fe0664c648b721c38d631a2df..89169c0efd64af3fea51c014a8e096f5c4c492c5 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2187,6 +2187,7 @@ dependencies = [ "simplelog", "smallvec", "smol", + "time 0.3.2", "tiny-skia", "tree-sitter", "usvg", diff --git a/gpui/Cargo.toml b/gpui/Cargo.toml index b6b29c39e2541cad4837f68f217ba58910786d95..11a855cd40c087c4aed6fc278adfbdb7ded92952 100644 --- a/gpui/Cargo.toml +++ b/gpui/Cargo.toml @@ -27,6 +27,7 @@ serde = { version = "1.0.125", features = ["derive"] } serde_json = "1.0.64" smallvec = { version = "1.6", features = ["union"] } smol = "1.2" +time = { version = "0.3" } tiny-skia = "0.5" tree-sitter = "0.19" usvg = "0.14" diff --git a/gpui/src/platform.rs b/gpui/src/platform.rs index 21679ae877bb2dab58006bfccf15fe7f54bcfefd..b58ad381a29f9804ea999f70278bd6db1521dde5 100644 --- a/gpui/src/platform.rs +++ b/gpui/src/platform.rs @@ -26,6 +26,7 @@ use std::{ rc::Rc, sync::Arc, }; +use time::UtcOffset; pub trait Platform: Send + Sync { fn dispatcher(&self) -> Arc; @@ -49,6 +50,8 @@ pub trait Platform: Send + Sync { fn read_credentials(&self, url: &str) -> Option<(String, Vec)>; fn set_cursor_style(&self, style: CursorStyle); + + fn local_timezone(&self) -> UtcOffset; } pub(crate) trait ForegroundPlatform { diff --git a/gpui/src/platform/mac/platform.rs b/gpui/src/platform/mac/platform.rs index 861984a24761ccc1dbe2fe92601383d3c44d8e88..4d81fe964a0b80584527a7cce9936a7b133a4fab 100644 --- a/gpui/src/platform/mac/platform.rs +++ b/gpui/src/platform/mac/platform.rs @@ -42,6 +42,7 @@ use std::{ slice, str, sync::Arc, }; +use time::UtcOffset; const MAC_PLATFORM_IVAR: &'static str = "platform"; static mut APP_CLASS: *const Class = ptr::null(); @@ -558,6 +559,14 @@ impl platform::Platform for MacPlatform { let _: () = msg_send![cursor, set]; } } + + fn local_timezone(&self) -> UtcOffset { + unsafe { + let local_timezone: id = msg_send![class!(NSTimeZone), localTimeZone]; + let seconds_from_gmt: NSInteger = msg_send![local_timezone, secondsFromGMT]; + UtcOffset::from_whole_seconds(seconds_from_gmt.try_into().unwrap()).unwrap() + } + } } unsafe fn get_foreground_platform(object: &mut Object) -> &MacForegroundPlatform { diff --git a/gpui/src/platform/test.rs b/gpui/src/platform/test.rs index 3295fb63822eba6fc36324826e58e27929439cc6..004b1f94a9ba86b4ce4aad710828b42bcae73023 100644 --- a/gpui/src/platform/test.rs +++ b/gpui/src/platform/test.rs @@ -9,6 +9,7 @@ use std::{ rc::Rc, sync::Arc, }; +use time::UtcOffset; pub struct Platform { dispatcher: Arc, @@ -136,6 +137,10 @@ impl super::Platform for Platform { fn set_cursor_style(&self, style: CursorStyle) { *self.cursor.lock() = style; } + + fn local_timezone(&self) -> UtcOffset { + UtcOffset::UTC + } } impl Window { diff --git a/zed/Cargo.toml b/zed/Cargo.toml index b77ed8caff0bd7c27310870b88aad5112206fefd..17314ebd6542473e0ed7d36c098df251ea7af07d 100644 --- a/zed/Cargo.toml +++ b/zed/Cargo.toml @@ -49,7 +49,7 @@ smallvec = { version = "1.6", features = ["union"] } smol = "1.2.5" surf = "2.2" tempdir = { version = "0.3.7", optional = true } -time = { version = "0.3", features = ["local-offset"] } +time = { version = "0.3" } tiny_http = "0.8" toml = "0.5" tree-sitter = "0.19.5" diff --git a/zed/src/chat_panel.rs b/zed/src/chat_panel.rs index 6edac94b2ec61b6cb6ec1ef35eb8dadd3998270b..6b52f7edfd970d781e798959d2af91925fe090f0 100644 --- a/zed/src/chat_panel.rs +++ b/zed/src/chat_panel.rs @@ -25,6 +25,7 @@ pub struct ChatPanel { input_editor: ViewHandle, channel_select: ViewHandle