Enum Datetime
pub enum Datetime {
Date(Date),
Time(Time),
Datetime(PrimitiveDateTime),
}
Expand description
Represents a date, a time, or a combination of both.
Can be created by either specifying a custom datetime using this type’s
constructor function or getting the current date with
datetime.today
.
§Example
#let date = datetime(
year: 2020,
month: 10,
day: 4,
)
#date.display() \
#date.display(
"y:[year repr:last_two]"
)
#let time = datetime(
hour: 18,
minute: 2,
second: 23,
)
#time.display() \
#time.display(
"h:[hour repr:12][period]"
)
§Datetime and Duration
You can get a [duration] by subtracting two datetime:
#let first-of-march = datetime(day: 1, month: 3, year: 2024)
#let first-of-jan = datetime(day: 1, month: 1, year: 2024)
#let distance = first-of-march - first-of-jan
#distance.hours()
You can also add/subtract a datetime and a duration to retrieve a new, offset datetime:
#let date = datetime(day: 1, month: 3, year: 2024)
#let two-days = duration(days: 2)
#let two-days-earlier = date - two-days
#let two-days-later = date + two-days
#date.display() \
#two-days-earlier.display() \
#two-days-later.display()
§Format
You can specify a customized formatting using the
display
method. The format of a datetime is
specified by providing components with a specified number of modifiers.
A component represents a certain part of the datetime that you want to
display, and with the help of modifiers you can define how you want to
display that component. In order to display a component, you wrap the name
of the component in square brackets (e.g. [[year]]
will display the year).
In order to add modifiers, you add a space after the component name followed
by the name of the modifier, a colon and the value of the modifier (e.g.
[[month repr:short]]
will display the short representation of the month).
The possible combination of components and their respective modifiers is as follows:
year
: Displays the year of the datetime.padding
: Can be eitherzero
,space
ornone
. Specifies how the year is padded.repr
Can be eitherfull
in which case the full year is displayed orlast_two
in which case only the last two digits are displayed.sign
: Can be eitherautomatic
ormandatory
. Specifies when the sign should be displayed.
month
: Displays the month of the datetime.padding
: Can be eitherzero
,space
ornone
. Specifies how the month is padded.repr
: Can be eithernumerical
,long
orshort
. Specifies if the month should be displayed as a number or a word. Unfortunately, when choosing the word representation, it can currently only display the English version. In the future, it is planned to support localization.
day
: Displays the day of the datetime.padding
: Can be eitherzero
,space
ornone
. Specifies how the day is padded.
week_number
: Displays the week number of the datetime.padding
: Can be eitherzero
,space
ornone
. Specifies how the week number is padded.repr
: Can be eitherISO
,sunday
ormonday
. In the case ofISO
, week numbers are between 1 and 53, while the other ones are between 0 and 53.
weekday
: Displays the weekday of the date.repr
Can be eitherlong
,short
,sunday
ormonday
. In the case oflong
andshort
, the corresponding English name will be displayed (same as for the month, other languages are currently not supported). In the case ofsunday
andmonday
, the numerical value will be displayed (assuming Sunday and Monday as the first day of the week, respectively).one_indexed
: Can be eithertrue
orfalse
. Defines whether the numerical representation of the week starts with 0 or 1.
hour
: Displays the hour of the date.padding
: Can be eitherzero
,space
ornone
. Specifies how the hour is padded.repr
: Can be either24
or12
. Changes whether the hour is displayed in the 24-hour or 12-hour format.
period
: The AM/PM part of the hourcase
: Can belower
to display it in lower case andupper
to display it in upper case.
minute
: Displays the minute of the date.padding
: Can be eitherzero
,space
ornone
. Specifies how the minute is padded.
second
: Displays the second of the date.padding
: Can be eitherzero
,space
ornone
. Specifies how the second is padded.
Keep in mind that not always all components can be used. For example, if you
create a new datetime with {datetime(year: 2023, month: 10, day: 13)}
, it
will be stored as a plain date internally, meaning that you cannot use
components such as hour
or minute
, which would only work on datetimes
that have a specified time.
Variants§
Date(Date)
Representation as a date.
Time(Time)
Representation as a time.
Datetime(PrimitiveDateTime)
Representation as a combination of date and time.
Implementations§
§impl Datetime
impl Datetime
pub fn from_ymd(year: i32, month: u8, day: u8) -> Option<Datetime>
pub fn from_ymd(year: i32, month: u8, day: u8) -> Option<Datetime>
Create a datetime from year, month, and day.
pub fn from_hms(hour: u8, minute: u8, second: u8) -> Option<Datetime>
pub fn from_hms(hour: u8, minute: u8, second: u8) -> Option<Datetime>
Create a datetime from hour, minute, and second.
pub fn from_ymd_hms(
year: i32,
month: u8,
day: u8,
hour: u8,
minute: u8,
second: u8,
) -> Option<Datetime>
pub fn from_ymd_hms( year: i32, month: u8, day: u8, hour: u8, minute: u8, second: u8, ) -> Option<Datetime>
Create a datetime from day and time.
pub fn from_toml_dict(dict: &Dict) -> Option<Datetime>
pub fn from_toml_dict(dict: &Dict) -> Option<Datetime>
Try to parse a dictionary as a TOML date.
§impl Datetime
impl Datetime
pub fn construct(
year: Option<i32>,
month: Option<Month>,
day: Option<u8>,
hour: Option<u8>,
minute: Option<u8>,
second: Option<u8>,
) -> Result<Datetime, EcoString>
pub fn construct( year: Option<i32>, month: Option<Month>, day: Option<u8>, hour: Option<u8>, minute: Option<u8>, second: Option<u8>, ) -> Result<Datetime, EcoString>
Creates a new datetime.
You can specify the [datetime] using a year, month, day, hour, minute, and second.
Note: Depending on which components of the datetime you specify, Typst will store it in one of the following three ways:
- If you specify year, month and day, Typst will store just a date.
- If you specify hour, minute and second, Typst will store just a time.
- If you specify all of year, month, day, hour, minute and second, Typst will store a full datetime.
Depending on how it is stored, the display
method
will choose a different formatting by default.
#datetime(
year: 2012,
month: 8,
day: 3,
).display()
pub fn today(
engine: &mut Engine<'_>,
offset: Smart<i64>,
) -> Result<Datetime, EcoString>
pub fn today( engine: &mut Engine<'_>, offset: Smart<i64>, ) -> Result<Datetime, EcoString>
Returns the current date.
Today's date is
#datetime.today().display().
pub fn display(
&self,
pattern: Smart<DisplayPattern>,
) -> Result<EcoString, EcoString>
pub fn display( &self, pattern: Smart<DisplayPattern>, ) -> Result<EcoString, EcoString>
Displays the datetime in a specified format.
Depending on whether you have defined just a date, a time or both, the
default format will be different. If you specified a date, it will be
[[year]-[month]-[day]]
. If you specified a time, it will be
[[hour]:[minute]:[second]]
. In the case of a datetime, it will be
[[year]-[month]-[day] [hour]:[minute]:[second]]
.
See the format syntax for more information.
pub fn weekday(&self) -> Option<u8>
pub fn weekday(&self) -> Option<u8>
The weekday (counting Monday as 1) or {none}
for times without a date.
pub fn minute(&self) -> Option<u8>
pub fn minute(&self) -> Option<u8>
The minute if it was specified, or {none}
for dates without a time.
Trait Implementations§
§impl FromValue for Datetime
impl FromValue for Datetime
§fn from_value(value: Value) -> Result<Datetime, HintedString>
fn from_value(value: Value) -> Result<Datetime, HintedString>
Self
.§impl NativeScope for Datetime
impl NativeScope for Datetime
§fn constructor() -> Option<&'static NativeFuncData>
fn constructor() -> Option<&'static NativeFuncData>
§impl NativeType for Datetime
impl NativeType for Datetime
§impl PartialOrd for Datetime
impl PartialOrd for Datetime
§impl Reflect for Datetime
impl Reflect for Datetime
§fn error(found: &Value) -> HintedString
fn error(found: &Value) -> HintedString
impl Copy for Datetime
impl StructuralPartialEq for Datetime
Auto Trait Implementations§
impl Freeze for Datetime
impl RefUnwindSafe for Datetime
impl Send for Datetime
impl Sync for Datetime
impl Unpin for Datetime
impl UnwindSafe for Datetime
Blanket Implementations§
Source§impl<S, D, Swp, Dwp, T> AdaptInto<D, Swp, Dwp, T> for Swhere
T: Real + Zero + Arithmetics + Clone,
Swp: WhitePoint<T>,
Dwp: WhitePoint<T>,
D: AdaptFrom<S, Swp, Dwp, T>,
impl<S, D, Swp, Dwp, T> AdaptInto<D, Swp, Dwp, T> for Swhere
T: Real + Zero + Arithmetics + Clone,
Swp: WhitePoint<T>,
Dwp: WhitePoint<T>,
D: AdaptFrom<S, Swp, Dwp, T>,
Source§fn adapt_into_using<M>(self, method: M) -> Dwhere
M: TransformMatrix<T>,
fn adapt_into_using<M>(self, method: M) -> Dwhere
M: TransformMatrix<T>,
Source§fn adapt_into(self) -> D
fn adapt_into(self) -> D
§impl<T> ArchivePointee for T
impl<T> ArchivePointee for T
§type ArchivedMetadata = ()
type ArchivedMetadata = ()
§fn pointer_metadata(
_: &<T as ArchivePointee>::ArchivedMetadata,
) -> <T as Pointee>::Metadata
fn pointer_metadata( _: &<T as ArchivePointee>::ArchivedMetadata, ) -> <T as Pointee>::Metadata
Source§impl<T, C> ArraysFrom<C> for Twhere
C: IntoArrays<T>,
impl<T, C> ArraysFrom<C> for Twhere
C: IntoArrays<T>,
Source§fn arrays_from(colors: C) -> T
fn arrays_from(colors: C) -> T
Source§impl<T, C> ArraysInto<C> for Twhere
C: FromArrays<T>,
impl<T, C> ArraysInto<C> for Twhere
C: FromArrays<T>,
Source§fn arrays_into(self) -> C
fn arrays_into(self) -> C
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
§impl<T> CallHasher for T
impl<T> CallHasher for T
Source§impl<WpParam, T, U> Cam16IntoUnclamped<WpParam, T> for Uwhere
T: FromCam16Unclamped<WpParam, U>,
impl<WpParam, T, U> Cam16IntoUnclamped<WpParam, T> for Uwhere
T: FromCam16Unclamped<WpParam, U>,
Source§type Scalar = <T as FromCam16Unclamped<WpParam, U>>::Scalar
type Scalar = <T as FromCam16Unclamped<WpParam, U>>::Scalar
parameters
when converting.Source§fn cam16_into_unclamped(
self,
parameters: BakedParameters<WpParam, <U as Cam16IntoUnclamped<WpParam, T>>::Scalar>,
) -> T
fn cam16_into_unclamped( self, parameters: BakedParameters<WpParam, <U as Cam16IntoUnclamped<WpParam, T>>::Scalar>, ) -> T
self
into C
, using the provided parameters.Source§impl<T> CheckedAs for T
impl<T> CheckedAs for T
Source§fn checked_as<Dst>(self) -> Option<Dst>where
T: CheckedCast<Dst>,
fn checked_as<Dst>(self) -> Option<Dst>where
T: CheckedCast<Dst>,
Source§impl<Src, Dst> CheckedCastFrom<Src> for Dstwhere
Src: CheckedCast<Dst>,
impl<Src, Dst> CheckedCastFrom<Src> for Dstwhere
Src: CheckedCast<Dst>,
Source§fn checked_cast_from(src: Src) -> Option<Dst>
fn checked_cast_from(src: Src) -> Option<Dst>
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T, C> ComponentsFrom<C> for Twhere
C: IntoComponents<T>,
impl<T, C> ComponentsFrom<C> for Twhere
C: IntoComponents<T>,
Source§fn components_from(colors: C) -> T
fn components_from(colors: C) -> T
§impl<F, W, T, D> Deserialize<With<T, W>, D> for F
impl<F, W, T, D> Deserialize<With<T, W>, D> for F
§fn deserialize(
&self,
deserializer: &mut D,
) -> Result<With<T, W>, <D as Fallible>::Error>
fn deserialize( &self, deserializer: &mut D, ) -> Result<With<T, W>, <D as Fallible>::Error>
§impl<T> Downcast for Twhere
T: Any,
impl<T> Downcast for Twhere
T: Any,
§fn into_any(self: Box<T>) -> Box<dyn Any>
fn into_any(self: Box<T>) -> Box<dyn Any>
Box<dyn Trait>
(where Trait: Downcast
) to Box<dyn Any>
. Box<dyn Any>
can
then be further downcast
into Box<ConcreteType>
where ConcreteType
implements Trait
.§fn into_any_rc(self: Rc<T>) -> Rc<dyn Any>
fn into_any_rc(self: Rc<T>) -> Rc<dyn Any>
Rc<Trait>
(where Trait: Downcast
) to Rc<Any>
. Rc<Any>
can then be
further downcast
into Rc<ConcreteType>
where ConcreteType
implements Trait
.§fn as_any(&self) -> &(dyn Any + 'static)
fn as_any(&self) -> &(dyn Any + 'static)
&Trait
(where Trait: Downcast
) to &Any
. This is needed since Rust cannot
generate &Any
’s vtable from &Trait
’s.§fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)
fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)
&mut Trait
(where Trait: Downcast
) to &Any
. This is needed since Rust cannot
generate &mut Any
’s vtable from &mut Trait
’s.§impl<T> DowncastSync for T
impl<T> DowncastSync for T
§impl<T> Filterable for T
impl<T> Filterable for T
Source§impl<T> FromAngle<T> for T
impl<T> FromAngle<T> for T
Source§fn from_angle(angle: T) -> T
fn from_angle(angle: T) -> T
angle
.Source§impl<T, U> FromStimulus<U> for Twhere
U: IntoStimulus<T>,
impl<T, U> FromStimulus<U> for Twhere
U: IntoStimulus<T>,
Source§fn from_stimulus(other: U) -> T
fn from_stimulus(other: U) -> T
other
into Self
, while performing the appropriate scaling,
rounding and clamping.§impl<T> FromValue<Spanned<Value>> for Twhere
T: FromValue,
impl<T> FromValue<Spanned<Value>> for Twhere
T: FromValue,
§fn from_value(value: Spanned<Value>) -> Result<T, HintedString>
fn from_value(value: Spanned<Value>) -> Result<T, HintedString>
Self
.Source§impl<T, U> IntoAngle<U> for Twhere
U: FromAngle<T>,
impl<T, U> IntoAngle<U> for Twhere
U: FromAngle<T>,
Source§fn into_angle(self) -> U
fn into_angle(self) -> U
T
.Source§impl<WpParam, T, U> IntoCam16Unclamped<WpParam, T> for Uwhere
T: Cam16FromUnclamped<WpParam, U>,
impl<WpParam, T, U> IntoCam16Unclamped<WpParam, T> for Uwhere
T: Cam16FromUnclamped<WpParam, U>,
Source§type Scalar = <T as Cam16FromUnclamped<WpParam, U>>::Scalar
type Scalar = <T as Cam16FromUnclamped<WpParam, U>>::Scalar
parameters
when converting.Source§fn into_cam16_unclamped(
self,
parameters: BakedParameters<WpParam, <U as IntoCam16Unclamped<WpParam, T>>::Scalar>,
) -> T
fn into_cam16_unclamped( self, parameters: BakedParameters<WpParam, <U as IntoCam16Unclamped<WpParam, T>>::Scalar>, ) -> T
self
into C
, using the provided parameters.Source§impl<T, U> IntoColor<U> for Twhere
U: FromColor<T>,
impl<T, U> IntoColor<U> for Twhere
U: FromColor<T>,
Source§fn into_color(self) -> U
fn into_color(self) -> U
Source§impl<T, U> IntoColorUnclamped<U> for Twhere
U: FromColorUnclamped<T>,
impl<T, U> IntoColorUnclamped<U> for Twhere
U: FromColorUnclamped<T>,
Source§fn into_color_unclamped(self) -> U
fn into_color_unclamped(self) -> U
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self
into a Left
variant of Either<Self, Self>
if into_left
is true
.
Converts self
into a Right
variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self
into a Left
variant of Either<Self, Self>
if into_left(&self)
returns true
.
Converts self
into a Right
variant of Either<Self, Self>
otherwise. Read more§impl<T> IntoResult for Twhere
T: IntoValue,
impl<T> IntoResult for Twhere
T: IntoValue,
§fn into_result(self, _: Span) -> Result<Value, EcoVec<SourceDiagnostic>>
fn into_result(self, _: Span) -> Result<Value, EcoVec<SourceDiagnostic>>
Source§impl<T> IntoStimulus<T> for T
impl<T> IntoStimulus<T> for T
Source§fn into_stimulus(self) -> T
fn into_stimulus(self) -> T
self
into T
, while performing the appropriate scaling,
rounding and clamping.§impl<T> LayoutRaw for T
impl<T> LayoutRaw for T
§fn layout_raw(_: <T as Pointee>::Metadata) -> Result<Layout, LayoutError>
fn layout_raw(_: <T as Pointee>::Metadata) -> Result<Layout, LayoutError>
Source§impl<T> OverflowingAs for T
impl<T> OverflowingAs for T
Source§fn overflowing_as<Dst>(self) -> (Dst, bool)where
T: OverflowingCast<Dst>,
fn overflowing_as<Dst>(self) -> (Dst, bool)where
T: OverflowingCast<Dst>,
Source§impl<Src, Dst> OverflowingCastFrom<Src> for Dstwhere
Src: OverflowingCast<Dst>,
impl<Src, Dst> OverflowingCastFrom<Src> for Dstwhere
Src: OverflowingCast<Dst>,
Source§fn overflowing_cast_from(src: Src) -> (Dst, bool)
fn overflowing_cast_from(src: Src) -> (Dst, bool)
§impl<T> Pointable for T
impl<T> Pointable for T
Source§impl<T> SaturatingAs for T
impl<T> SaturatingAs for T
Source§fn saturating_as<Dst>(self) -> Dstwhere
T: SaturatingCast<Dst>,
fn saturating_as<Dst>(self) -> Dstwhere
T: SaturatingCast<Dst>,
Source§impl<Src, Dst> SaturatingCastFrom<Src> for Dstwhere
Src: SaturatingCast<Dst>,
impl<Src, Dst> SaturatingCastFrom<Src> for Dstwhere
Src: SaturatingCast<Dst>,
Source§fn saturating_cast_from(src: Src) -> Dst
fn saturating_cast_from(src: Src) -> Dst
Source§impl<T, C> TryComponentsInto<C> for Twhere
C: TryFromComponents<T>,
impl<T, C> TryComponentsInto<C> for Twhere
C: TryFromComponents<T>,
Source§type Error = <C as TryFromComponents<T>>::Error
type Error = <C as TryFromComponents<T>>::Error
try_into_colors
fails to cast.Source§fn try_components_into(self) -> Result<C, <T as TryComponentsInto<C>>::Error>
fn try_components_into(self) -> Result<C, <T as TryComponentsInto<C>>::Error>
Source§impl<T, U> TryIntoColor<U> for Twhere
U: TryFromColor<T>,
impl<T, U> TryIntoColor<U> for Twhere
U: TryFromColor<T>,
Source§fn try_into_color(self) -> Result<U, OutOfBounds<U>>
fn try_into_color(self) -> Result<U, OutOfBounds<U>>
OutOfBounds
error is returned which contains
the unclamped color. Read more