Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor Local #1072

Merged
merged 4 commits into from
May 26, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
129 changes: 47 additions & 82 deletions src/offset/local/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,19 @@ use rkyv::{Archive, Deserialize, Serialize};

use super::fixed::FixedOffset;
use super::{LocalResult, TimeZone};
use crate::naive::{NaiveDate, NaiveDateTime};
use crate::naive::{NaiveDate, NaiveDateTime, NaiveTime};
#[allow(deprecated)]
use crate::{Date, DateTime};
use crate::Date;
use crate::{DateTime, Utc};

#[cfg(unix)]
#[path = "unix.rs"]
mod inner;

#[cfg(windows)]
#[path = "windows.rs"]
mod inner;

// we don't want `stub.rs` when the target_os is not wasi or emscripten
// as we use js-sys to get the date instead
#[cfg(all(
not(unix),
not(windows),
Expand All @@ -23,16 +30,37 @@ use crate::{Date, DateTime};
not(any(target_os = "emscripten", target_os = "wasi"))
))
))]
#[path = "stub.rs"]
mod inner;
mod inner {
use crate::{FixedOffset, LocalResult, NaiveDateTime};

#[cfg(unix)]
#[path = "unix.rs"]
mod inner;
pub(super) fn offset_from_utc_datetime(_utc_time: &NaiveDateTime) -> LocalResult<FixedOffset> {
LocalResult::Single(FixedOffset::east_opt(0).unwrap())
}

#[cfg(windows)]
#[path = "windows.rs"]
mod inner;
pub(super) fn offset_from_local_datetime(
_local_time: &NaiveDateTime,
) -> LocalResult<FixedOffset> {
LocalResult::Single(FixedOffset::east_opt(0).unwrap())
}
}

#[cfg(all(
target_arch = "wasm32",
feature = "wasmbind",
not(any(target_os = "emscripten", target_os = "wasi"))
))]
mod inner {
use crate::{FixedOffset, LocalResult, NaiveDateTime};

pub(super) fn offset_from_utc_datetime(_utc: &NaiveDateTime) -> LocalResult<FixedOffset> {
let offset = js_sys::Date::new_0().get_timezone_offset();
LocalResult::Single(FixedOffset::west_opt((offset as i32) * 60).unwrap())
}

pub(super) fn offset_from_local_datetime(local: &NaiveDateTime) -> LocalResult<FixedOffset> {
offset_from_utc_datetime(local)
}
}

#[cfg(unix)]
mod tz_info;
Expand Down Expand Up @@ -74,7 +102,7 @@ impl Local {
)))]
#[must_use]
pub fn now() -> DateTime<Local> {
inner::now()
Utc::now().with_timezone(&Local)
}

/// Returns a `DateTime` which corresponds to the current date and time.
Expand Down Expand Up @@ -103,87 +131,24 @@ impl TimeZone for Local {
Local
}

// they are easier to define in terms of the finished date and time unlike other offsets
#[allow(deprecated)]
fn offset_from_local_date(&self, local: &NaiveDate) -> LocalResult<FixedOffset> {
self.from_local_date(local).map(|date| *date.offset())
// Get the offset at local midnight.
self.offset_from_local_datetime(&local.and_time(NaiveTime::MIN))
}

fn offset_from_local_datetime(&self, local: &NaiveDateTime) -> LocalResult<FixedOffset> {
self.from_local_datetime(local).map(|datetime| *datetime.offset())
inner::offset_from_local_datetime(local)
}

#[allow(deprecated)]
fn offset_from_utc_date(&self, utc: &NaiveDate) -> FixedOffset {
*self.from_utc_date(utc).offset()
// Get the offset at midnight.
self.offset_from_utc_datetime(&utc.and_time(NaiveTime::MIN))
}

fn offset_from_utc_datetime(&self, utc: &NaiveDateTime) -> FixedOffset {
*self.from_utc_datetime(utc).offset()
}

// override them for avoiding redundant works
#[allow(deprecated)]
fn from_local_date(&self, local: &NaiveDate) -> LocalResult<Date<Local>> {
// this sounds very strange, but required for keeping `TimeZone::ymd` sane.
// in the other words, we use the offset at the local midnight
// but keep the actual date unaltered (much like `FixedOffset`).
let midnight = self.from_local_datetime(&local.and_hms_opt(0, 0, 0).unwrap());
midnight.map(|datetime| Date::from_utc(*local, *datetime.offset()))
}

#[cfg(all(
target_arch = "wasm32",
feature = "wasmbind",
not(any(target_os = "emscripten", target_os = "wasi"))
))]
fn from_local_datetime(&self, local: &NaiveDateTime) -> LocalResult<DateTime<Local>> {
let mut local = local.clone();
// Get the offset from the js runtime
let offset =
FixedOffset::west_opt((js_sys::Date::new_0().get_timezone_offset() as i32) * 60)
.unwrap();
local -= crate::Duration::seconds(offset.local_minus_utc() as i64);
LocalResult::Single(DateTime::from_utc(local, offset))
}

#[cfg(not(all(
target_arch = "wasm32",
feature = "wasmbind",
not(any(target_os = "emscripten", target_os = "wasi"))
)))]
fn from_local_datetime(&self, local: &NaiveDateTime) -> LocalResult<DateTime<Local>> {
inner::naive_to_local(local, true)
}

#[allow(deprecated)]
fn from_utc_date(&self, utc: &NaiveDate) -> Date<Local> {
let midnight = self.from_utc_datetime(&utc.and_hms_opt(0, 0, 0).unwrap());
Date::from_utc(*utc, *midnight.offset())
}

#[cfg(all(
target_arch = "wasm32",
feature = "wasmbind",
not(any(target_os = "emscripten", target_os = "wasi"))
))]
fn from_utc_datetime(&self, utc: &NaiveDateTime) -> DateTime<Local> {
// Get the offset from the js runtime
let offset =
FixedOffset::west_opt((js_sys::Date::new_0().get_timezone_offset() as i32) * 60)
.unwrap();
DateTime::from_utc(*utc, offset)
}

#[cfg(not(all(
target_arch = "wasm32",
feature = "wasmbind",
not(any(target_os = "emscripten", target_os = "wasi"))
)))]
fn from_utc_datetime(&self, utc: &NaiveDateTime) -> DateTime<Local> {
// this is OK to unwrap as getting local time from a UTC
// timestamp is never ambiguous
inner::naive_to_local(utc, false).unwrap()
inner::offset_from_utc_datetime(utc).unwrap()
}
}

Expand Down
Loading