Function to_string
pub fn to_string(zdt: &Zoned) -> Result<String, Error> ⓘ
dep_jiff
and alloc
only.Expand description
Convert a Zoned
to an RFC 2822 datetime string.
This is a convenience function for using DateTimePrinter
. In
particular, this always creates and allocates a new String
. For writing
to an existing string, or converting a Timestamp
to an RFC 2822
datetime string, you’ll need to use DateTimePrinter
.
§Warning
The RFC 2822 format only supports writing a precise instant in time
expressed via a time zone offset. It does not support serializing
the time zone itself. This means that if you format a zoned datetime
in a time zone like America/New_York
and then deserialize it, the
zoned datetime you get back will be a “fixed offset” zoned datetime.
This in turn means it will not perform daylight saving time safe
arithmetic.
Basically, you should use the RFC 2822 format if it’s required (for example, when dealing with email). But you should not choose it as a general interchange format for new applications.
§Errors
This returns an error if the year corresponding to this timestamp cannot be represented in the RFC 2822 format. For example, a negative year.
§Example
This example shows how to convert a zoned datetime to the RFC 2822 format:
use jiff::{civil::date, fmt::rfc2822};
let zdt = date(2024, 6, 15).at(7, 0, 0, 0).intz("Australia/Tasmania")?;
assert_eq!(rfc2822::to_string(&zdt)?, "Sat, 15 Jun 2024 07:00:00 +1000");