i18n_next/format/api.rs
1//! `tn` / `tn_as` / `td` / `td_as` / `tdr` / `tdr_with` / `language_name` on [`I18n`] and
2//! [`View`], and the implicit-instance forms the macros use (DESIGN 4.2, 4.4, 5.3, 8b.14).
3
4use super::data::{EN, LOCALES};
5use super::{DateOptions, Loc, Number, NumberOptions, RelativeOptions, Timestamp, Zone};
6use crate::i18n::{I18n, Snapshot, View};
7use crate::scope::{self, Code};
8use std::fmt;
9
10impl Snapshot {
11 /// The data locale and options of a number in `locale` (the active one when `None`):
12 /// the named format `name` by the locale code, else (also when it is missing) the
13 /// defaults. The flag says a name was asked for and not found.
14 fn number_setup(
15 &self,
16 locale: Option<&str>,
17 name: Option<&str>,
18 ) -> (&'static Loc, &NumberOptions, bool) {
19 let loc = locale.map_or(self.number, |c| self.number_of(c));
20 let named = name.map(|n| self.meta.named_number(locale.unwrap_or(&self.locale), n));
21 match named {
22 Some(Some(o)) => (loc, o, false),
23 Some(None) => (loc, &NumberOptions::DEFAULT, true),
24 None => (loc, &NumberOptions::DEFAULT, false),
25 }
26 }
27}
28
29impl Snapshot {
30 /// The data locale and options of a date in `locale` (the active one when `None`), as
31 /// [`Snapshot::number_setup`]; the zone is the format's own, else the instance's.
32 pub(crate) fn date_setup(
33 &self,
34 locale: Option<&str>,
35 name: Option<&str>,
36 ) -> (&'static Loc, &DateOptions, &Zone, bool) {
37 let loc = locale.map_or(self.number, |c| self.number_of(c));
38 let named = name.map(|n| self.meta.named_date(locale.unwrap_or(&self.locale), n));
39 let (o, miss) = match named {
40 Some(Some(o)) => (o, false),
41 Some(None) => (&DateOptions::DEFAULT, true),
42 None => (&DateOptions::DEFAULT, false),
43 };
44 (
45 loc,
46 o,
47 o.zone.as_ref().unwrap_or_else(|| self.meta.zone()),
48 miss,
49 )
50 }
51}
52
53/// JS `warnMissingFormat`: a named format that no locale of the chain of truncations has.
54#[cold]
55fn missing(name: &str, locale: &str) {
56 crate::trace::missing_format("number", name, locale);
57}
58
59#[cold]
60fn missing_date(name: &str, locale: &str) {
61 crate::trace::missing_format("datetime", name, locale);
62}
63
64/// The system clock in milliseconds.
65pub(crate) fn system_now() -> Option<i64> {
66 #[cfg(not(all(target_arch = "wasm32", target_os = "unknown")))]
67 {
68 Timestamp::from(std::time::SystemTime::now()).millis()
69 }
70 #[cfg(all(target_arch = "wasm32", target_os = "unknown", feature = "tz"))]
71 {
72 Timestamp::from(js_sys::Date::now()).millis()
73 }
74 #[cfg(all(target_arch = "wasm32", target_os = "unknown", not(feature = "tz")))]
75 {
76 None
77 }
78}
79
80/// A clock of [`Builder::clock`](crate::Builder::clock): milliseconds since 1970.
81#[derive(Clone)]
82pub(crate) struct Clock(pub(crate) std::sync::Arc<dyn Fn() -> i64 + Send + Sync>);
83
84impl fmt::Debug for Clock {
85 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
86 f.write_str("Clock")
87 }
88}
89
90impl I18n {
91 /// Formats `n` like `Intl.NumberFormat` in the active locale (its `iso`, else its code),
92 /// with the default options (JS `tn(value)`): `1234.5` is `1,234.5` in `en`,
93 /// `1.234,5` in `de`, `١٬٢٣٤٫٥` in `ar-EG`. One allocation: the result. Feature
94 /// `format`; the locales come from `I18N_NEXT_LOCALES` at build time (a locale left out
95 /// formats as its nearest selected parent, else `en`).
96 ///
97 /// ```
98 /// use i18n_next::I18n;
99 ///
100 /// let i18n = I18n::builder().iso("ar", "ar-EG").locale("ar").build()?;
101 /// assert_eq!(i18n.tn(1234.5), "١٬٢٣٤٫٥");
102 /// assert_eq!(i18n.with_locale("en").tn(1234.5), "1,234.5");
103 /// assert_eq!(i18n.with_locale("de").tn(-0.0), "-0");
104 /// # Ok::<(), i18n_next::Error>(())
105 /// ```
106 #[must_use]
107 pub fn tn(&self, n: impl Into<Number>) -> String {
108 self.number_string(None, n.into(), None)
109 }
110
111 /// Formats `n` with the named format `name` of the active locale (JS `tn(value, name)`,
112 /// [`Builder::formats`](crate::Builder::formats) or `numberFormats` of `i18n.json`),
113 /// looked up by the locale code, then shorter (`zh-Hant-TW` → `zh-Hant` → `zh`). A name
114 /// nowhere to be found warns (debug builds, or `tracing`) and formats with the defaults,
115 /// as JS does.
116 ///
117 /// ```
118 /// use i18n_next::I18n;
119 ///
120 /// let i18n = I18n::builder()
121 /// .formats(r#"{"en": {"usd": {"style": "currency", "currency": "USD"},
122 /// "short": {"notation": "compact"},
123 /// "pct": {"style": "percent", "maximumFractionDigits": 1}}}"#)
124 /// .build()?;
125 /// assert_eq!(i18n.tn_as(1234.5, "usd"), "$1,234.50");
126 /// assert_eq!(i18n.tn_as(1_234_567, "short"), "1.2M");
127 /// assert_eq!(i18n.tn_as(0.1234, "pct"), "12.3%");
128 /// # Ok::<(), i18n_next::Error>(())
129 /// ```
130 #[must_use]
131 pub fn tn_as(&self, n: impl Into<Number>, name: &str) -> String {
132 self.number_string(None, n.into(), Some(name))
133 }
134
135 pub(crate) fn number_string(
136 &self,
137 locale: Option<&str>,
138 n: Number,
139 name: Option<&str>,
140 ) -> String {
141 let (out, miss) = self.inner.read(|s| {
142 let (loc, o, miss) = s.number_setup(locale, name);
143 (super::to_string(n, o, loc), miss.then(|| s.locale.clone()))
144 });
145 if let (Some(active), Some(name)) = (miss, name) {
146 missing(name, locale.unwrap_or(&active));
147 }
148 out
149 }
150
151 /// Writes `n` (named format `name`) in `locale` (the active one when `None`) to `out`.
152 pub(crate) fn write_number(
153 &self,
154 locale: Option<&str>,
155 n: Number,
156 name: Option<&str>,
157 out: &mut dyn fmt::Write,
158 ) -> fmt::Result {
159 let (r, miss) = self.inner.read(|s| {
160 let (loc, o, miss) = s.number_setup(locale, name);
161 (super::write(n, o, loc, out), miss.then(|| s.locale.clone()))
162 });
163 if let (Some(active), Some(name)) = (miss, name) {
164 missing(name, locale.unwrap_or(&active));
165 }
166 r
167 }
168
169 /// Formats `t` like `Intl.DateTimeFormat` in the active locale (its `iso`, else its code)
170 /// with the default options (JS `td(value)`): year, month and day, numeric
171 /// (`1/5/2024` in `en`, `5.1.2024` in `de`, `٥/١/٢٠٢٤` in `ar-EG`), in the zone of
172 /// [`Builder::time_zone`](crate::Builder::time_zone) (else the system's). An invalid
173 /// date is `Invalid Date`. One allocation: the result. Feature `format`; Gregorian only.
174 ///
175 /// ```
176 /// use i18n_next::I18n;
177 ///
178 /// let i18n = I18n::builder().time_zone("UTC").iso("ar", "ar-EG").build()?;
179 /// let at = 1_704_428_640_000i64; // 2024-01-05 04:24 UTC
180 /// assert_eq!(i18n.td(at), "1/5/2024");
181 /// assert_eq!(i18n.with_locale("de").td(at), "5.1.2024");
182 /// assert_eq!(i18n.with_locale("ar").td(at), "٥/١/٢٠٢٤");
183 /// assert_eq!(i18n.td(f64::NAN), "Invalid Date");
184 /// # Ok::<(), i18n_next::Error>(())
185 /// ```
186 #[must_use]
187 pub fn td(&self, t: impl Into<Timestamp>) -> String {
188 self.date_string(None, t.into(), None)
189 }
190
191 /// Takes the IANA zone `iana` as the system zone dates show in, as a front end knows it
192 /// (the Tauri plugin passes the WebView's `Intl.DateTimeFormat().resolvedOptions()
193 /// .timeZone` with its first `meta`, DESIGN 5.4). It is taken only when the builder set no
194 /// [`time_zone`](crate::Builder::time_zone), the system zone was not looked up yet (a
195 /// date already shown keeps its zone) and the zone is known; `true` when taken. A taken
196 /// zone is sent to the front end with the payload meta as `timeZone`. Feature `format`.
197 ///
198 /// ```
199 /// use i18n_next::I18n;
200 ///
201 /// let i18n = I18n::builder().build()?;
202 /// assert!(!i18n.set_system_time_zone("Mars/Olympus"));
203 /// assert!(i18n.set_system_time_zone("Asia/Tokyo"));
204 /// assert_eq!(i18n.td(1_704_481_200_000i64), "1/6/2024"); // 2024-01-05 19:00 UTC
205 /// assert!(!i18n.set_system_time_zone("UTC")); // once
206 /// assert!(!I18n::builder().time_zone("UTC").build()?.set_system_time_zone("Asia/Tokyo"));
207 /// # Ok::<(), i18n_next::Error>(())
208 /// ```
209 #[must_use]
210 pub fn set_system_time_zone(&self, iana: &str) -> bool {
211 self.inner.read(|s| s.meta.set_system_zone(iana))
212 }
213
214 /// Formats `t` with the named date format `name` of the active locale (JS `td(value,
215 /// name)`, [`Builder::datetime_formats`](crate::Builder::datetime_formats) or
216 /// `datetimeFormats` of `i18n.json`), looked up by the locale code, then shorter
217 /// (`zh-Hant-TW` → `zh-Hant` → `zh`). A name nowhere to be found warns (debug builds,
218 /// or `tracing`) and formats with the defaults, as JS does.
219 ///
220 /// ```
221 /// use i18n_next::I18n;
222 ///
223 /// let i18n = I18n::builder()
224 /// .time_zone("UTC")
225 /// .datetime_formats(r#"{"en": {"long": {"dateStyle": "full", "timeStyle": "short"},
226 /// "clock": {"hour": "2-digit", "minute": "2-digit", "hour12": false},
227 /// "berlin": {"timeStyle": "short", "timeZone": "Europe/Berlin"}}}"#)
228 /// .build()?;
229 /// let at = 1_704_428_640_000i64; // 2024-01-05 04:24 UTC
230 /// assert_eq!(i18n.td_as(at, "long"), "Friday, January 5, 2024 at 4:24 AM");
231 /// assert_eq!(i18n.td_as(at, "clock"), "04:24");
232 /// assert_eq!(i18n.td_as(at, "berlin"), "5:24 AM");
233 /// # Ok::<(), i18n_next::Error>(())
234 /// ```
235 #[must_use]
236 pub fn td_as(&self, t: impl Into<Timestamp>, name: &str) -> String {
237 self.date_string(None, t.into(), Some(name))
238 }
239
240 /// `t` relative to now (JS `tdr(value)`, `Intl.RelativeTimeFormat` with the default
241 /// options): the largest unit that fits at least once (a year is 365 days, a month 30),
242 /// rounded down: `3 days ago`, `in 2 hours`, `0 seconds ago` under a second in the past.
243 /// Now is [`Builder::clock`](crate::Builder::clock), else the system clock. An invalid
244 /// date (or no clock, or a clock outside a JS `Date`'s ±8.64e15 ms) is `Invalid Date`.
245 ///
246 /// ```
247 /// use i18n_next::I18n;
248 ///
249 /// let now = 1_704_428_640_000i64;
250 /// let i18n = I18n::builder().clock(move || now).build()?;
251 /// assert_eq!(i18n.tdr(now - 3 * 86_400_000), "3 days ago");
252 /// assert_eq!(i18n.tdr(now + 7_200_000), "in 2 hours");
253 /// assert_eq!(i18n.with_locale("ru").tdr(now - 5 * 60_000), "5 минут назад");
254 /// # Ok::<(), i18n_next::Error>(())
255 /// ```
256 #[must_use]
257 pub fn tdr(&self, t: impl Into<Timestamp>) -> String {
258 self.relative_string(None, t.into(), RelativeOptions::default())
259 }
260
261 /// [`I18n::tdr`] with `Intl.RelativeTimeFormat` options (JS `tdr(value, options)`).
262 ///
263 /// ```
264 /// use i18n_next::{I18n, RelativeOptions};
265 ///
266 /// let now = 1_704_428_640_000i64;
267 /// let i18n = I18n::builder().clock(move || now).build()?;
268 /// let auto = RelativeOptions::default().auto();
269 /// assert_eq!(i18n.tdr_with(now - 86_400_000, auto), "yesterday");
270 /// assert_eq!(i18n.tdr_with(now - 3 * 3_600_000, RelativeOptions::default().short()), "3 hr. ago");
271 /// # Ok::<(), i18n_next::Error>(())
272 /// ```
273 #[must_use]
274 pub fn tdr_with(&self, t: impl Into<Timestamp>, options: RelativeOptions) -> String {
275 self.relative_string(None, t.into(), options)
276 }
277
278 pub(crate) fn date_string(
279 &self,
280 locale: Option<&str>,
281 t: Timestamp,
282 name: Option<&str>,
283 ) -> String {
284 let (out, miss) = self.inner.read(|s| {
285 let (loc, o, zone, miss) = s.date_setup(locale, name);
286 (
287 super::string_of(|w| super::date::write(t, o, loc, zone, w)),
288 miss.then(|| s.locale.clone()),
289 )
290 });
291 if let (Some(active), Some(name)) = (miss, name) {
292 missing_date(name, locale.unwrap_or(&active));
293 }
294 out
295 }
296
297 /// Writes `t` (named format `name`) in `locale` (the active one when `None`) to `out`.
298 pub(crate) fn write_date(
299 &self,
300 locale: Option<&str>,
301 t: Timestamp,
302 name: Option<&str>,
303 out: &mut dyn fmt::Write,
304 ) -> fmt::Result {
305 let (r, miss) = self.inner.read(|s| {
306 let (loc, o, zone, miss) = s.date_setup(locale, name);
307 (
308 super::date::write(t, o, loc, zone, out),
309 miss.then(|| s.locale.clone()),
310 )
311 });
312 if let (Some(active), Some(name)) = (miss, name) {
313 missing_date(name, locale.unwrap_or(&active));
314 }
315 r
316 }
317
318 pub(crate) fn relative_string(
319 &self,
320 locale: Option<&str>,
321 t: Timestamp,
322 o: RelativeOptions,
323 ) -> String {
324 super::string_of(|w| self.write_relative(locale, t, o, w))
325 }
326
327 /// Writes `t` relative to the clock in `locale` (the active one when `None`) to `out`.
328 pub(crate) fn write_relative(
329 &self,
330 locale: Option<&str>,
331 t: Timestamp,
332 o: RelativeOptions,
333 out: &mut dyn fmt::Write,
334 ) -> fmt::Result {
335 self.inner.read(|s| {
336 let loc = locale.map_or(s.number, |c| s.number_of(c));
337 match (t.millis(), s.meta.now()) {
338 (Some(at), Some(now)) => super::relative::write(at, now, o, loc, out),
339 _ => out.write_str("Invalid Date"),
340 }
341 })
342 }
343
344 /// The locale's own name (CLDR, 8b.14) of a locale code, by its `iso` (else the code):
345 /// `de` → `Deutsch`, `ru` → `русский`, `pt-PT` → `português europeu`; a regional tag
346 /// without its own data names its language (`de-AT` → `Österreichisches Deutsch`,
347 /// `en-US` → `English`). `None` for a tag with no CLDR data in this build.
348 ///
349 /// ```
350 /// use i18n_next::I18n;
351 ///
352 /// let i18n = I18n::builder().iso("ua", "uk-UA").build()?;
353 /// assert_eq!(i18n.language_name("de"), Some("Deutsch"));
354 /// assert_eq!(i18n.language_name("ua"), Some("українська"));
355 /// assert_eq!(i18n.language_name("xx"), None);
356 /// # Ok::<(), i18n_next::Error>(())
357 /// ```
358 #[must_use]
359 pub fn language_name(&self, code: &str) -> Option<&'static str> {
360 self.inner
361 .read(|s| super::locale::autonym(s.meta.tag(code)))
362 }
363}
364
365impl View {
366 /// [`I18n::td`] in this view's locale.
367 #[must_use]
368 pub fn td(&self, t: impl Into<Timestamp>) -> String {
369 self.i18n
370 .date_string(self.locale.as_deref(), t.into(), None)
371 }
372
373 /// [`I18n::td_as`] in this view's locale: the name is looked up by this locale's code.
374 #[must_use]
375 pub fn td_as(&self, t: impl Into<Timestamp>, name: &str) -> String {
376 self.i18n
377 .date_string(self.locale.as_deref(), t.into(), Some(name))
378 }
379
380 /// [`I18n::tdr`] in this view's locale.
381 #[must_use]
382 pub fn tdr(&self, t: impl Into<Timestamp>) -> String {
383 self.i18n
384 .relative_string(self.locale.as_deref(), t.into(), RelativeOptions::default())
385 }
386
387 /// [`I18n::tdr_with`] in this view's locale.
388 #[must_use]
389 pub fn tdr_with(&self, t: impl Into<Timestamp>, options: RelativeOptions) -> String {
390 self.i18n
391 .relative_string(self.locale.as_deref(), t.into(), options)
392 }
393
394 /// [`I18n::tn`] in this view's locale.
395 #[must_use]
396 pub fn tn(&self, n: impl Into<Number>) -> String {
397 self.i18n
398 .number_string(self.locale.as_deref(), n.into(), None)
399 }
400
401 /// [`I18n::tn_as`] in this view's locale: the name is looked up by this locale's code.
402 #[must_use]
403 pub fn tn_as(&self, n: impl Into<Number>, name: &str) -> String {
404 self.i18n
405 .number_string(self.locale.as_deref(), n.into(), Some(name))
406 }
407}
408
409/// Writes `n` on `i18n` (else the nearest scope's instance, else the global one) in `locale`
410/// (else the scope's, else the active one). Without any instance: `en` and the defaults.
411pub(crate) fn write_in(
412 i18n: Option<&I18n>,
413 locale: Option<&str>,
414 n: Number,
415 name: Option<&str>,
416 out: &mut dyn fmt::Write,
417) -> fmt::Result {
418 if let Some(i) = i18n {
419 return i.write_number(locale, n, name, out);
420 }
421 let (scoped, scope_locale, ..) = scope::current(true, locale.is_none(), false);
422 let locale = locale.or(scope_locale.as_ref().map(Code::as_str));
423 match scoped.as_ref().or(scope::global_instance()) {
424 Some(i) => i.write_number(locale, n, name, out),
425 None => super::write(n, &NumberOptions::DEFAULT, &LOCALES[EN], out),
426 }
427}
428
429/// [`I18n::tn`] on the implicit instance: the nearest [`scope`](crate::scope())'s, else the
430/// global one, in the scope's locale (else the active one); without either, `en`. What
431/// [`tn!`](crate::tn!) calls.
432///
433/// ```
434/// use i18n_next::{I18n, scope, tn};
435///
436/// let i18n = I18n::builder().build()?;
437/// scope().instance(i18n).locale("de").run(|| {
438/// assert_eq!(tn!(1234.5), "1.234,5");
439/// });
440/// # Ok::<(), i18n_next::Error>(())
441/// ```
442#[must_use]
443pub fn tn(n: impl Into<Number>) -> String {
444 scoped_string(n.into(), None)
445}
446
447/// [`I18n::tn_as`] on the implicit instance (see [`tn()`]).
448#[must_use]
449pub fn tn_as(n: impl Into<Number>, name: &str) -> String {
450 scoped_string(n.into(), Some(name))
451}
452
453fn scoped_string(n: Number, name: Option<&str>) -> String {
454 let (scoped, locale, ..) = scope::current(true, true, false);
455 let locale = locale.as_ref().map(Code::as_str);
456 match scoped.as_ref().or(scope::global_instance()) {
457 Some(i) => i.number_string(locale, n, name),
458 None => super::to_string(n, &NumberOptions::DEFAULT, &LOCALES[EN]),
459 }
460}
461
462/// A date or a relative date to show later: what [`Arg::date`](crate::Arg::date),
463/// [`Arg::date_as`](crate::Arg::date_as), [`Arg::relative`](crate::Arg::relative) and
464/// [`Text::date`](crate::Text::date) hold. Its formatter is a pointer set by those
465/// constructors, so an app that never shows a date links none of the date code.
466#[derive(Clone)]
467pub struct DateArg<'a> {
468 at: Timestamp,
469 format: Option<std::borrow::Cow<'a, str>>,
470 relative: bool,
471 write: WriteWhen,
472}
473
474type WriteWhen = fn(Option<&I18n>, Option<&str>, &DateArg<'_>, &mut dyn fmt::Write) -> fmt::Result;
475
476impl<'a> DateArg<'a> {
477 pub(crate) fn date(at: Timestamp, format: Option<std::borrow::Cow<'a, str>>) -> Self {
478 DateArg {
479 at,
480 format,
481 relative: false,
482 write: write_when_in,
483 }
484 }
485
486 pub(crate) fn relative(at: Timestamp) -> Self {
487 DateArg {
488 at,
489 format: None,
490 relative: true,
491 write: write_when_in,
492 }
493 }
494
495 pub(crate) fn is_relative(&self) -> bool {
496 self.relative
497 }
498
499 /// Writes it on `i18n` (else the nearest scope's instance, else the global one) in
500 /// `locale` (else the scope's, else the active one).
501 pub(crate) fn write_in(
502 &self,
503 i18n: Option<&I18n>,
504 locale: Option<&str>,
505 out: &mut dyn fmt::Write,
506 ) -> fmt::Result {
507 (self.write)(i18n, locale, self, out)
508 }
509}
510
511impl fmt::Debug for DateArg<'_> {
512 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
513 let mut d = f.debug_struct("DateArg");
514 d.field("at", &self.at);
515 if let Some(name) = &self.format {
516 d.field("format", name);
517 }
518 if self.relative {
519 d.field("relative", &true);
520 }
521 d.finish()
522 }
523}
524
525impl PartialEq for DateArg<'_> {
526 fn eq(&self, other: &Self) -> bool {
527 (self.at, self.relative, &self.format) == (other.at, other.relative, &other.format)
528 }
529}
530
531/// [`DateArg::write_in`] for every date argument.
532fn write_when_in(
533 i18n: Option<&I18n>,
534 locale: Option<&str>,
535 d: &DateArg<'_>,
536 out: &mut dyn fmt::Write,
537) -> fmt::Result {
538 let write = |i: &I18n, locale: Option<&str>, out: &mut dyn fmt::Write| {
539 if d.relative {
540 i.write_relative(locale, d.at, RelativeOptions::default(), out)
541 } else {
542 i.write_date(locale, d.at, d.format.as_deref(), out)
543 }
544 };
545 if let Some(i) = i18n {
546 return write(i, locale, out);
547 }
548 let (scoped, scope_locale, ..) = scope::current(true, locale.is_none(), false);
549 let locale = locale.or(scope_locale.as_ref().map(Code::as_str));
550 match scoped.as_ref().or(scope::global_instance()) {
551 Some(i) => write(i, locale, out),
552 None => bare(d, out),
553 }
554}
555
556/// A date with no instance: `en`, defaults, UTC, the system clock.
557fn bare(d: &DateArg<'_>, out: &mut dyn fmt::Write) -> fmt::Result {
558 let loc = &LOCALES[EN];
559 if !d.relative {
560 return super::date::write(d.at, &DateOptions::DEFAULT, loc, &Zone::utc(), out);
561 }
562 match (d.at.millis(), system_now()) {
563 (Some(at), Some(now)) => {
564 super::relative::write(at, now, RelativeOptions::default(), loc, out)
565 }
566 _ => out.write_str("Invalid Date"),
567 }
568}
569
570/// [`I18n::td`] on the implicit instance (see [`tn()`]); without one, `en` in UTC. What
571/// [`td!`](crate::td!) calls.
572///
573/// ```
574/// use i18n_next::{I18n, scope, td};
575///
576/// let i18n = I18n::builder().time_zone("UTC").build()?;
577/// scope().instance(i18n).locale("ru").run(|| {
578/// assert_eq!(td!(1_704_428_640_000i64), "05.01.2024");
579/// });
580/// # Ok::<(), i18n_next::Error>(())
581/// ```
582#[must_use]
583pub fn td(t: impl Into<Timestamp>) -> String {
584 scoped_when(&DateArg::date(t.into(), None))
585}
586
587/// [`I18n::td_as`] on the implicit instance (see [`tn()`]).
588#[must_use]
589pub fn td_as(t: impl Into<Timestamp>, name: &str) -> String {
590 scoped_when(&DateArg::date(t.into(), Some(name.into())))
591}
592
593/// [`I18n::tdr`] on the implicit instance (see [`tn()`]); without one, `en` and the system
594/// clock. What [`tdr!`](crate::tdr!) calls.
595#[must_use]
596pub fn tdr(t: impl Into<Timestamp>) -> String {
597 scoped_when(&DateArg::relative(t.into()))
598}
599
600fn scoped_when(d: &DateArg<'_>) -> String {
601 super::string_of(|out| write_when_in(None, None, d, out))
602}