Crate strftime

source ·
Expand description

This crate provides a Ruby 3.1.2 compatible strftime function, which formats time according to the directives in the given format string.

The directives begin with a percent % character. Any text not listed as a directive will be passed through to the output string.

Each directive consists of a percent % character, zero or more flags, optional minimum field width, optional modifier and a conversion specifier as follows:

%<flags><width><modifier><conversion>

§Usage

The various strftime functions in this crate take a generic time parameter that implements the Time trait.

§Format Specifiers

§Flags

FlagDescription
-Use left padding, ignoring width and removing all other padding options in most cases.
_Use spaces for padding.
0Use zeros for padding.
^Convert the resulting string to uppercase.
#Change case of the resulting string.

§Width

The minimum field width specifies the minimum width.

§Modifiers

The modifiers are E and O. They are ignored.

§Specifiers

SpecifierExampleDescription
%Y-2001Year with century if provided, zero-padded to at least 4 digits plus the possible negative sign.
%C-21Year / 100 using Euclidean division, zero-padded to at least 2 digits.
%y99Year % 100 in 00..=99, using Euclidean remainder, zero-padded to 2 digits.
%m01Month of the year in 01..=12, zero-padded to 2 digits.
%BJulyLocale independent full month name.
%b, %hJulLocale independent abbreviated month name, using the first 3 letters.
%d01Day of the month in 01..=31, zero-padded to 2 digits.
%e 1Day of the month in 1..=31, blank-padded to 2 digits.
%j001Day of the year in 001..=366, zero-padded to 3 digits.
%H00Hour of the day (24-hour clock) in 00..=23, zero-padded to 2 digits.
%k 0Hour of the day (24-hour clock) in 0..=23, blank-padded to 2 digits.
%I01Hour of the day (12-hour clock) in 01..=12, zero-padded to 2 digits.
%l 1Hour of the day (12-hour clock) in 1..=12, blank-padded to 2 digits.
%PamLowercase meridian indicator ("am" or "pm").
%pAMUppercase meridian indicator ("AM" or "PM").
%M00Minute of the hour in 00..=59, zero-padded to 2 digits.
%S00Second of the minute in 00..=60, zero-padded to 2 digits.
%L123Truncated fractional seconds digits, with 3 digits by default. Number of digits is specified by the width field.
%N123456789Truncated fractional seconds digits, with 9 digits by default. Number of digits is specified by the width field.
%z+0200Zero-padded signed time zone UTC hour and minute offsets (+hhmm).
%:z+02:00Zero-padded signed time zone UTC hour and minute offsets with colons (+hh:mm).
%::z+02:00:00Zero-padded signed time zone UTC hour, minute and second offsets with colons (+hh:mm:ss).
%:::z+02Zero-padded signed time zone UTC hour offset, with optional minute and second offsets with colons (+hh[:mm[:ss]]).
%ZCESTPlatform-dependent abbreviated time zone name.
%ASundayLocale independent full weekday name.
%aSunLocale independent abbreviated weekday name, using the first 3 letters.
%u1Day of the week from Monday in 1..=7, zero-padded to 1 digit.
%w0Day of the week from Sunday in 0..=6, zero-padded to 1 digit.
%G-2001Same as %Y, but using the ISO 8601 week-based year. 1
%g99Same as %y, but using the ISO 8601 week-based year. 1
%V01ISO 8601 week number in 01..=53, zero-padded to 2 digits. 1
%U00Week number from Sunday in 00..=53, zero-padded to 2 digits. The week 1 starts with the first Sunday of the year.
%W00Week number from Monday in 00..=53, zero-padded to 2 digits. The week 1 starts with the first Monday of the year.
%s86400Number of seconds since 1970-01-01 00:00:00 UTC, zero-padded to at least 1 digit.
%n\nNewline character '\n'.
%t\tTab character '\t'.
%%%Literal '%' character.
%cSun Jul 8 00:23:45 2001Date and time, equivalent to "%a %b %e %H:%M:%S %Y".
%D, %x07/08/01Date, equivalent to "%m/%d/%y".
%F2001-07-08ISO 8601 date, equivalent to "%Y-%m-%d".
%v 8-JUL-2001VMS date, equivalent to "%e-%^b-%4Y".
%r12:23:45 AM12-hour time, equivalent to "%I:%M:%S %p".
%R00:2324-hour time without seconds, equivalent to "%H:%M".
%T, %X00:23:4524-hour time, equivalent to "%H:%M:%S".

  1. %G, %g, %V: Week 1 of ISO 8601 is the first week with at least 4 days in that year. The days before the first week are in the last week of the previous year. 

Modules§

  • Provides a strftime implementation using a format string with arbitrary bytes, writing to a provided byte slice.
  • bytesalloc
    Provides a strftime implementation using a format string with arbitrary bytes, writing to a newly allocated Vec.
  • Provides a strftime implementation using a UTF-8 format string, writing to a core::fmt::Write object.
  • stringalloc
    Provides a strftime implementation using a UTF-8 format string, writing to a newly allocated String.

Enums§

  • Error type returned by the strftime functions.

Constants§

Traits§

  • Common methods needed for formatting time.