use super::data::{EN, LOCALES};
use super::{DateOptions, Loc, Number, NumberOptions, RelativeOptions, Timestamp, Zone};
use crate::i18n::{I18n, Snapshot, View};
use crate::scope::{self, Code};
use std::fmt;
impl Snapshot {
fn number_setup(
&self,
locale: Option<&str>,
name: Option<&str>,
) -> (&'static Loc, &NumberOptions, bool) {
let loc = locale.map_or(self.number, |c| self.number_of(c));
let named = name.map(|n| self.meta.named_number(locale.unwrap_or(&self.locale), n));
match named {
Some(Some(o)) => (loc, o, false),
Some(None) => (loc, &NumberOptions::DEFAULT, true),
None => (loc, &NumberOptions::DEFAULT, false),
}
}
}
impl Snapshot {
pub(crate) fn date_setup(
&self,
locale: Option<&str>,
name: Option<&str>,
) -> (&'static Loc, &DateOptions, &Zone, bool) {
let loc = locale.map_or(self.number, |c| self.number_of(c));
let named = name.map(|n| self.meta.named_date(locale.unwrap_or(&self.locale), n));
let (o, miss) = match named {
Some(Some(o)) => (o, false),
Some(None) => (&DateOptions::DEFAULT, true),
None => (&DateOptions::DEFAULT, false),
};
(
loc,
o,
o.zone.as_ref().unwrap_or_else(|| self.meta.zone()),
miss,
)
}
}
#[cold]
fn missing(name: &str, locale: &str) {
crate::trace::missing_format("number", name, locale);
}
#[cold]
fn missing_date(name: &str, locale: &str) {
crate::trace::missing_format("datetime", name, locale);
}
pub(crate) fn system_now() -> Option<i64> {
#[cfg(not(all(target_arch = "wasm32", target_os = "unknown")))]
{
Timestamp::from(std::time::SystemTime::now()).millis()
}
#[cfg(all(target_arch = "wasm32", target_os = "unknown", feature = "tz"))]
{
Timestamp::from(js_sys::Date::now()).millis()
}
#[cfg(all(target_arch = "wasm32", target_os = "unknown", not(feature = "tz")))]
{
None
}
}
#[derive(Clone)]
pub(crate) struct Clock(pub(crate) std::sync::Arc<dyn Fn() -> i64 + Send + Sync>);
impl fmt::Debug for Clock {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str("Clock")
}
}
impl I18n {
#[must_use]
pub fn tn(&self, n: impl Into<Number>) -> String {
self.number_string(None, n.into(), None)
}
#[must_use]
pub fn tn_as(&self, n: impl Into<Number>, name: &str) -> String {
self.number_string(None, n.into(), Some(name))
}
pub(crate) fn number_string(
&self,
locale: Option<&str>,
n: Number,
name: Option<&str>,
) -> String {
let (out, miss) = self.inner.read(|s| {
let (loc, o, miss) = s.number_setup(locale, name);
(super::to_string(n, o, loc), miss.then(|| s.locale.clone()))
});
if let (Some(active), Some(name)) = (miss, name) {
missing(name, locale.unwrap_or(&active));
}
out
}
pub(crate) fn write_number(
&self,
locale: Option<&str>,
n: Number,
name: Option<&str>,
out: &mut dyn fmt::Write,
) -> fmt::Result {
let (r, miss) = self.inner.read(|s| {
let (loc, o, miss) = s.number_setup(locale, name);
(super::write(n, o, loc, out), miss.then(|| s.locale.clone()))
});
if let (Some(active), Some(name)) = (miss, name) {
missing(name, locale.unwrap_or(&active));
}
r
}
#[must_use]
pub fn td(&self, t: impl Into<Timestamp>) -> String {
self.date_string(None, t.into(), None)
}
#[must_use]
pub fn set_system_time_zone(&self, iana: &str) -> bool {
self.inner.read(|s| s.meta.set_system_zone(iana))
}
#[must_use]
pub fn td_as(&self, t: impl Into<Timestamp>, name: &str) -> String {
self.date_string(None, t.into(), Some(name))
}
#[must_use]
pub fn tdr(&self, t: impl Into<Timestamp>) -> String {
self.relative_string(None, t.into(), RelativeOptions::default())
}
#[must_use]
pub fn tdr_with(&self, t: impl Into<Timestamp>, options: RelativeOptions) -> String {
self.relative_string(None, t.into(), options)
}
pub(crate) fn date_string(
&self,
locale: Option<&str>,
t: Timestamp,
name: Option<&str>,
) -> String {
let (out, miss) = self.inner.read(|s| {
let (loc, o, zone, miss) = s.date_setup(locale, name);
(
super::string_of(|w| super::date::write(t, o, loc, zone, w)),
miss.then(|| s.locale.clone()),
)
});
if let (Some(active), Some(name)) = (miss, name) {
missing_date(name, locale.unwrap_or(&active));
}
out
}
pub(crate) fn write_date(
&self,
locale: Option<&str>,
t: Timestamp,
name: Option<&str>,
out: &mut dyn fmt::Write,
) -> fmt::Result {
let (r, miss) = self.inner.read(|s| {
let (loc, o, zone, miss) = s.date_setup(locale, name);
(
super::date::write(t, o, loc, zone, out),
miss.then(|| s.locale.clone()),
)
});
if let (Some(active), Some(name)) = (miss, name) {
missing_date(name, locale.unwrap_or(&active));
}
r
}
pub(crate) fn relative_string(
&self,
locale: Option<&str>,
t: Timestamp,
o: RelativeOptions,
) -> String {
super::string_of(|w| self.write_relative(locale, t, o, w))
}
pub(crate) fn write_relative(
&self,
locale: Option<&str>,
t: Timestamp,
o: RelativeOptions,
out: &mut dyn fmt::Write,
) -> fmt::Result {
self.inner.read(|s| {
let loc = locale.map_or(s.number, |c| s.number_of(c));
match (t.millis(), s.meta.now()) {
(Some(at), Some(now)) => super::relative::write(at, now, o, loc, out),
_ => out.write_str("Invalid Date"),
}
})
}
#[must_use]
pub fn language_name(&self, code: &str) -> Option<&'static str> {
self.inner
.read(|s| super::locale::autonym(s.meta.tag(code)))
}
}
impl View {
#[must_use]
pub fn td(&self, t: impl Into<Timestamp>) -> String {
self.i18n
.date_string(self.locale.as_deref(), t.into(), None)
}
#[must_use]
pub fn td_as(&self, t: impl Into<Timestamp>, name: &str) -> String {
self.i18n
.date_string(self.locale.as_deref(), t.into(), Some(name))
}
#[must_use]
pub fn tdr(&self, t: impl Into<Timestamp>) -> String {
self.i18n
.relative_string(self.locale.as_deref(), t.into(), RelativeOptions::default())
}
#[must_use]
pub fn tdr_with(&self, t: impl Into<Timestamp>, options: RelativeOptions) -> String {
self.i18n
.relative_string(self.locale.as_deref(), t.into(), options)
}
#[must_use]
pub fn tn(&self, n: impl Into<Number>) -> String {
self.i18n
.number_string(self.locale.as_deref(), n.into(), None)
}
#[must_use]
pub fn tn_as(&self, n: impl Into<Number>, name: &str) -> String {
self.i18n
.number_string(self.locale.as_deref(), n.into(), Some(name))
}
}
pub(crate) fn write_in(
i18n: Option<&I18n>,
locale: Option<&str>,
n: Number,
name: Option<&str>,
out: &mut dyn fmt::Write,
) -> fmt::Result {
if let Some(i) = i18n {
return i.write_number(locale, n, name, out);
}
let (scoped, scope_locale, ..) = scope::current(true, locale.is_none(), false);
let locale = locale.or(scope_locale.as_ref().map(Code::as_str));
match scoped.as_ref().or(scope::global_instance()) {
Some(i) => i.write_number(locale, n, name, out),
None => super::write(n, &NumberOptions::DEFAULT, &LOCALES[EN], out),
}
}
#[must_use]
pub fn tn(n: impl Into<Number>) -> String {
scoped_string(n.into(), None)
}
#[must_use]
pub fn tn_as(n: impl Into<Number>, name: &str) -> String {
scoped_string(n.into(), Some(name))
}
fn scoped_string(n: Number, name: Option<&str>) -> String {
let (scoped, locale, ..) = scope::current(true, true, false);
let locale = locale.as_ref().map(Code::as_str);
match scoped.as_ref().or(scope::global_instance()) {
Some(i) => i.number_string(locale, n, name),
None => super::to_string(n, &NumberOptions::DEFAULT, &LOCALES[EN]),
}
}
#[derive(Clone)]
pub struct DateArg<'a> {
at: Timestamp,
format: Option<std::borrow::Cow<'a, str>>,
relative: bool,
write: WriteWhen,
}
type WriteWhen = fn(Option<&I18n>, Option<&str>, &DateArg<'_>, &mut dyn fmt::Write) -> fmt::Result;
impl<'a> DateArg<'a> {
pub(crate) fn date(at: Timestamp, format: Option<std::borrow::Cow<'a, str>>) -> Self {
DateArg {
at,
format,
relative: false,
write: write_when_in,
}
}
pub(crate) fn relative(at: Timestamp) -> Self {
DateArg {
at,
format: None,
relative: true,
write: write_when_in,
}
}
pub(crate) fn is_relative(&self) -> bool {
self.relative
}
pub(crate) fn write_in(
&self,
i18n: Option<&I18n>,
locale: Option<&str>,
out: &mut dyn fmt::Write,
) -> fmt::Result {
(self.write)(i18n, locale, self, out)
}
}
impl fmt::Debug for DateArg<'_> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let mut d = f.debug_struct("DateArg");
d.field("at", &self.at);
if let Some(name) = &self.format {
d.field("format", name);
}
if self.relative {
d.field("relative", &true);
}
d.finish()
}
}
impl PartialEq for DateArg<'_> {
fn eq(&self, other: &Self) -> bool {
(self.at, self.relative, &self.format) == (other.at, other.relative, &other.format)
}
}
fn write_when_in(
i18n: Option<&I18n>,
locale: Option<&str>,
d: &DateArg<'_>,
out: &mut dyn fmt::Write,
) -> fmt::Result {
let write = |i: &I18n, locale: Option<&str>, out: &mut dyn fmt::Write| {
if d.relative {
i.write_relative(locale, d.at, RelativeOptions::default(), out)
} else {
i.write_date(locale, d.at, d.format.as_deref(), out)
}
};
if let Some(i) = i18n {
return write(i, locale, out);
}
let (scoped, scope_locale, ..) = scope::current(true, locale.is_none(), false);
let locale = locale.or(scope_locale.as_ref().map(Code::as_str));
match scoped.as_ref().or(scope::global_instance()) {
Some(i) => write(i, locale, out),
None => bare(d, out),
}
}
fn bare(d: &DateArg<'_>, out: &mut dyn fmt::Write) -> fmt::Result {
let loc = &LOCALES[EN];
if !d.relative {
return super::date::write(d.at, &DateOptions::DEFAULT, loc, &Zone::utc(), out);
}
match (d.at.millis(), system_now()) {
(Some(at), Some(now)) => {
super::relative::write(at, now, RelativeOptions::default(), loc, out)
}
_ => out.write_str("Invalid Date"),
}
}
#[must_use]
pub fn td(t: impl Into<Timestamp>) -> String {
scoped_when(&DateArg::date(t.into(), None))
}
#[must_use]
pub fn td_as(t: impl Into<Timestamp>, name: &str) -> String {
scoped_when(&DateArg::date(t.into(), Some(name.into())))
}
#[must_use]
pub fn tdr(t: impl Into<Timestamp>) -> String {
scoped_when(&DateArg::relative(t.into()))
}
fn scoped_when(d: &DateArg<'_>) -> String {
super::string_of(|out| write_when_in(None, None, d, out))
}