Skip to content

Commit

Permalink
chore: Upgrade deprecated timezone code.
Browse files Browse the repository at this point in the history
This doesn't actually add any new places for code to panic, since the deprecated functions all `unwrap`'d internally.
  • Loading branch information
kmeisthax committed Nov 22, 2022
1 parent bc3f6f2 commit 0aec23b
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 9 deletions.
27 changes: 20 additions & 7 deletions core/src/avm2/globals/date.rs
Original file line number Diff line number Diff line change
Expand Up @@ -212,8 +212,7 @@ impl<'builder, 'activation_a, 'gc, 'gc_context, T: TimeZone>

if let LocalResult::Single(Some(result)) = current
.timezone()
.ymd_opt(year, (month + 1) as u32, 1)
.and_hms_opt(0, 0, 0)
.with_ymd_and_hms(year, (month + 1) as u32, 1, 0, 0, 0)
.map(|date| date.checked_add_signed(duration))
{
Some(result.with_timezone(&Utc))
Expand Down Expand Up @@ -255,7 +254,13 @@ pub fn instance_init<'gc>(
// We need a starting value to adjust from.
date.set_date_time(
activation.context.gc_context,
Some(timezone.ymd(0, 1, 1).and_hms(0, 0, 0).into()),
Some(
timezone
.with_ymd_and_hms(0, 1, 1, 0, 0, 0)
.single()
.expect("Unambiguous epoch time when constructing Date")
.into(),
),
);

DateAdjustment::new(activation, &timezone)
Expand Down Expand Up @@ -347,7 +352,10 @@ pub fn set_time<'gc>(
.unwrap_or(&Value::Undefined)
.coerce_to_number(activation)?;
if new_time.is_finite() {
let time = Utc.timestamp_millis(new_time as i64);
let time = Utc
.timestamp_millis_opt(new_time as i64)
.single()
.expect("Unambiguous timestamp for current time zone");
this.set_date_time(activation.context.gc_context, Some(time));
return Ok((time.timestamp_millis() as f64).into());
} else {
Expand Down Expand Up @@ -919,7 +927,11 @@ pub fn utc<'gc>(
.second(args.get(5))?
.millisecond(args.get(6))?
.map_year(|year| if year < 100.0 { year + 1900.0 } else { year })
.calculate(Utc.ymd(0, 1, 1).and_hms(0, 0, 0));
.calculate(
Utc.with_ymd_and_hms(0, 1, 1, 0, 0, 0)
.single()
.expect("Unambiguous UTC time conversions"),
);
let millis = if let Some(date) = date {
date.timestamp_millis() as f64
} else {
Expand Down Expand Up @@ -1286,8 +1298,9 @@ pub fn parse<'gc>(
if let Some(timestamp) = final_time.calculate(
new_timezone
.unwrap_or(timezone)
.ymd(0, 1, 1)
.and_hms(0, 0, 0),
.with_ymd_and_hms(0, 1, 1, 0, 0, 0)
.single()
.expect("Unambiguous starting time when converting parsed dates into local timezone"),
) {
Ok((timestamp.timestamp_millis() as f64).into())
} else {
Expand Down
8 changes: 6 additions & 2 deletions core/src/locale.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,19 @@ const MOCK_TIME: bool = cfg!(any(test, feature = "deterministic"));

pub fn get_current_date_time() -> DateTime<Utc> {
if MOCK_TIME {
get_timezone().ymd(2001, 2, 3).and_hms(4, 5, 6).into()
get_timezone()
.with_ymd_and_hms(2001, 2, 3, 4, 5, 6)
.single()
.expect("Unambiguous mock time")
.into()
} else {
Utc::now()
}
}

pub fn get_timezone() -> FixedOffset {
if MOCK_TIME {
FixedOffset::east(20700)
FixedOffset::east_opt(20700).expect("Unambiguous mock timezone")
} else {
Local::now().offset().fix()
}
Expand Down

0 comments on commit 0aec23b

Please sign in to comment.