Skip to content

Commit 77e22ad

Browse files
committed
ffi
1 parent 619d7c6 commit 77e22ad

File tree

9 files changed

+146
-14
lines changed

9 files changed

+146
-14
lines changed

ffi/capi/bindings/c/TimeZoneInfo.h

+7
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

ffi/capi/bindings/cpp/icu4x/TimeZoneInfo.d.hpp

+8
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

ffi/capi/bindings/cpp/icu4x/TimeZoneInfo.hpp

+21
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

ffi/capi/bindings/dart/TimeZoneInfo.g.dart

+37
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

ffi/capi/bindings/js/TimeZoneInfo.d.ts

+7
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

ffi/capi/bindings/js/TimeZoneInfo.mjs

+25
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

ffi/capi/src/timezone.rs

+21-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ pub mod ffi {
1010
use core::fmt::Write;
1111
use icu_timezone::TimeZoneBcp47Id;
1212

13-
use crate::errors::ffi::TimeZoneInvalidOffsetError;
13+
use crate::{datetime::ffi::IsoDateTime, errors::ffi::TimeZoneInvalidOffsetError};
1414

1515
#[diplomat::opaque]
1616
#[diplomat::rust_link(icu::timezone::TimeZoneInfo, Struct)]
@@ -245,6 +245,26 @@ pub mod ffi {
245245
pub fn is_daylight_time(&self) -> Option<bool> {
246246
Some(self.0.zone_variant? == icu_timezone::ZoneVariant::daylight())
247247
}
248+
249+
/// Sets the `local_time` field.
250+
#[diplomat::rust_link(icu::timezone::TimeZoneInfo::local_time, StructField)]
251+
pub fn set_local_time(&mut self, datetime: &IsoDateTime) {
252+
self.0.local_time = Some((datetime.0.date, datetime.0.time.clone()));
253+
}
254+
255+
/// Clears the `local_time` field.
256+
#[diplomat::rust_link(icu::timezone::TimeZoneInfo::local_time, StructField)]
257+
pub fn clear_local_time(&mut self) {
258+
self.0.local_time.take();
259+
}
260+
261+
/// Returns a copy of the `local_time` field.
262+
#[diplomat::rust_link(icu::timezone::TimeZoneInfo::local_time, StructField, compact)]
263+
pub fn get_local_time(&self) -> Option<Box<IsoDateTime>> {
264+
self.0
265+
.local_time
266+
.map(|(date, time)| Box::new(IsoDateTime(icu_calendar::DateTime { date, time })))
267+
}
248268
}
249269
}
250270

ffi/capi/tests/missing_apis.txt

+3
Original file line numberDiff line numberDiff line change
@@ -225,3 +225,6 @@ icu::timezone::WindowsTimeZoneMapperBorrowed#Struct
225225
icu::timezone::WindowsTimeZoneMapperBorrowed::new#FnInStruct
226226
icu::timezone::WindowsTimeZoneMapperBorrowed::windows_tz_to_bcp47_id#FnInStruct
227227
icu::timezone::WindowsTimeZoneMapperBorrowed::windows_tz_to_bcp47_id_with_region#FnInStruct
228+
icu::timezone::ZoneOffsetCalculator#Struct
229+
icu::timezone::ZoneOffsetCalculator::compute_offsets_from_time_zone#FnInStruct
230+
icu::timezone::ZoneOffsetCalculator::new#FnInStruct

tutorials/cpp/datetime.cpp

+17-13
Original file line numberDiff line numberDiff line change
@@ -62,20 +62,7 @@ int main() {
6262
return 1;
6363
}
6464

65-
std::unique_ptr<TimeZoneInfo> time_zone = TimeZoneInfo::unknown();
66-
time_zone->try_set_offset_str("-05:00").ok().value();
67-
int32_t offset = time_zone->offset_seconds().value();
68-
if (offset != -18000) {
69-
std::cout << "GMT offset doesn't parse" << std::endl;
70-
return 1;
71-
}
7265
std::unique_ptr<TimeZoneIdMapper> mapper = TimeZoneIdMapper::create(*dp.get()).ok().value();
73-
time_zone->set_iana_time_zone_id(*mapper.get(), "america/chicago");
74-
std::string time_zone_id_return = time_zone->time_zone_id();
75-
if (time_zone_id_return != "uschi") {
76-
std::cout << "Time zone ID does not roundtrip: " << time_zone_id_return << std::endl;
77-
return 1;
78-
}
7966
std::string normalized_iana_id = mapper->normalize_iana("America/CHICAGO").ok().value().value();
8067
if (normalized_iana_id != "America/Chicago") {
8168
std::cout << "Time zone ID does not normalize: " << normalized_iana_id << std::endl;
@@ -98,6 +85,21 @@ int main() {
9885
return 1;
9986
}
10087

88+
std::unique_ptr<TimeZoneInfo> time_zone = TimeZoneInfo::unknown();
89+
time_zone->try_set_offset_str("-05:00").ok().value();
90+
int32_t offset = time_zone->offset_seconds().value();
91+
if (offset != -18000) {
92+
std::cout << "GMT offset doesn't parse" << std::endl;
93+
return 1;
94+
}
95+
time_zone->set_iana_time_zone_id(*mapper.get(), "america/chicago");
96+
std::string time_zone_id_return = time_zone->time_zone_id();
97+
if (time_zone_id_return != "uschi") {
98+
std::cout << "Time zone ID does not roundtrip: " << time_zone_id_return << std::endl;
99+
return 1;
100+
}
101+
time_zone->set_local_time(*date.get());
102+
101103
std::unique_ptr<GregorianZonedDateTimeFormatter> gzdtf = GregorianZonedDateTimeFormatter::create_with_length(*dp.get(), *locale.get(), DateTimeLength::Long).ok().value();
102104
out = gzdtf->format_iso_datetime_with_custom_time_zone(*date.get(), *time_zone.get());
103105
std::cout << "Formatted value is " << out << std::endl;
@@ -106,6 +108,8 @@ int main() {
106108
return 1;
107109
}
108110

111+
time_zone->set_local_time(*any_date->to_iso().get());
112+
109113
std::unique_ptr<ZonedDateTimeFormatter> zdtf = ZonedDateTimeFormatter::create_with_length(*dp.get(), *locale.get(), DateTimeLength::Long).ok().value();
110114
out = zdtf->format_datetime_with_custom_time_zone(*any_date.get(), *time_zone.get()).ok().value();
111115
std::cout << "Formatted value is " << out << std::endl;

0 commit comments

Comments
 (0)