AnCH Framework 0.2
Another C++ Hack: C++23 utility libraries
Loading...
Searching...
No Matches
anch::date::DateFormatter Class Reference

DateTime string formatter and parser More...

#include <dateFormatter.hpp>

Collaboration diagram for anch::date::DateFormatter:

Public Member Functions

 DateFormatter (const std::string &dateFormat)
 DateFormatter (const DateFormatter &)=delete
 DateFormatter (DateFormatter &&) noexcept=default
virtual ~DateFormatter () noexcept
void format (const anch::date::DateTime &date, std::string &output) const noexcept
void format (const anch::date::DateTime &date, std::ostream &output) const noexcept
void parse (const std::string &strDate, anch::date::DateTime &date) const
anch::date::DateTime parse (const std::string &strDate) const

Static Public Member Functions

static void registerFormatterPart (const std::string &pattern, getInstance instGetter) noexcept

Detailed Description

DateTime string formatter and parser

Patterns are:

  • %Y: 4 digits year
  • %y: 2 digits year (parsed as 1969-2068 like strptime )
  • %m: month (01-12)
  • %d: day in month (01-31)
  • %H: hours (00-23)
  • %h: hours (01-12, like strptime %I ; 12 for noon and midnight)
  • %p: a.m./p.m. marker
  • %M: minutes (00-59)
  • %S: seconds (00-60)
  • %s: milliseconds (000-999)

Other characters, and unknown patterns, are written as is. Other patterns can be registered with registerFormatterPart .
Parsing requires every field to have its exact size (no sign, no space) ; fields which are not in the pattern are set to 1970-01-01 00:00:00.000 local time ; impossible dates (2025-02-30 for example) are refused.
format and parse can be called concurrently. Formatter parts have to be registered before formatters which use them are built.

Author
Vincent Lachenal

Constructor & Destructor Documentation

◆ DateFormatter() [1/3]

anch::date::DateFormatter::DateFormatter ( const std::string & dateFormat)
explicit

DateFormatter constructor

Parameters
dateFormatthe date format

◆ DateFormatter() [2/3]

anch::date::DateFormatter::DateFormatter ( const DateFormatter & )
delete

Forbids DateFormatter copy constructor

Here is the call graph for this function:

◆ DateFormatter() [3/3]

anch::date::DateFormatter::DateFormatter ( DateFormatter && )
defaultnoexcept

DateFormatter move constructor

Here is the call graph for this function:

◆ ~DateFormatter()

virtual anch::date::DateFormatter::~DateFormatter ( )
virtualnoexcept

DateFormatter destructor

Here is the call graph for this function:

Member Function Documentation

◆ format() [1/2]

void anch::date::DateFormatter::format ( const anch::date::DateTime & date,
std::ostream & output ) const
noexcept

Format date

Parameters
datethe date to format
outputthe output stream
Here is the call graph for this function:

◆ format() [2/2]

void anch::date::DateFormatter::format ( const anch::date::DateTime & date,
std::string & output ) const
noexcept

Format date

Parameters
datethe date to format
outputthe output string
Here is the call graph for this function:

◆ parse() [1/2]

anch::date::DateTime anch::date::DateFormatter::parse ( const std::string & strDate) const

Build date from string

Parameters
strDatestring formatted date
Returns
the date
Exceptions
std::invalid_argumentinvalid format, impossible date or date out of range (years 1678 to 2261)
Here is the call graph for this function:

◆ parse() [2/2]

void anch::date::DateFormatter::parse ( const std::string & strDate,
anch::date::DateTime & date ) const

Build date from string

Parameters
strDatestring formatted date
datethe output date (all its fields are set)
Exceptions
std::invalid_argumentinvalid format, impossible date or date out of range (years 1678 to 2261)
Here is the call graph for this function:

◆ registerFormatterPart()

void anch::date::DateFormatter::registerFormatterPart ( const std::string & pattern,
getInstance instGetter )
staticnoexcept

Register a new formatter part (when pattern has not already been registered)

Parameters
patternthe formatter part pattern ('%' followed by one character)
instGetterthe formatter part new instance getter
Here is the call graph for this function:

The documentation for this class was generated from the following file: