AnCH Framework 0.2
Another C++ Hack: C++23 utility libraries
Loading...
Searching...
No Matches
AnCH date library documentation

Introduction

AnCH date library aims to provide facilities for applications manipulate date/time.

Prerequisites

None

Installation

CMake target anch::date:

find_package(Anch REQUIRED)
target_link_libraries(my-app PRIVATE anch::date)

See installation.

Usage

anch::date::DateTime stores a point in time (nanoseconds since epoch, nothing else): its broken down representation ( anch::date::DateFields : local time or UTC) is computed by fields . It is epoch by default, DateTime::now gives the current time ; it can be built from std::chrono::system_clock::time_point , std::time_t , std::tm (local time), std::timespec and local anch::date::DateFields , and explicitly converted to these types. Points in time are ordered.
Local time follows the TZ environment variable, daylight saving time included.

anch::date::DateFormatter formats and parses dates with patterns: %Y , %y , %m , %d , %H , %h (12 hours), %p (a.m./p.m.), %M , %S and %s (milliseconds). Other patterns can be added by implementing anch::date::IDatePartFormatter (on anch::date::DateFields ) and registering it with anch::date::DateFormatter::registerFormatterPart .
Parsing is strict: every field has a fixed size, constant parts have to match and impossible dates are refused.

For simple formatting, std::format with std::chrono specifiers can also be used on the converted time point.

anch::date::rfc3339 formats and parses RFC 3339 dates (JSON schema and OpenAPI date-time , date and time formats): date-time is a anch::date::DateTime (written in UTC or with the local offset), full-date is a anch::date::Date , partial-time a anch::date::Time and full-time an anch::date::OffsetTime . anch-json uses them for these types when ANCH_DATE is ON .
anch::date::Date is a calendar day (0000-01-01 to 9999-12-31, always valid): it is not a point in time, its conversions to and from anch::date::DateTime give the time zone ( anch::date::Zone ). Dates are ordered, days, months and years can be added:

#include "date/date.hpp"
bool
dateExample() {
anch::date::Date birth(2024, 2, 29); // impossible dates are refused
anch::date::Date today = anch::date::Date::today(); // local day
anch::date::Date end = birth.plusMonths(1).plusDays(-1); // 2024-03-28
bool past = birth < today; // dates are ordered
int64_t days = birth.daysUntil(anch::date::Date::parse("2025-02-28")); // 365
anch::date::DateTime midnight = birth.startOfDay(anch::date::Zone::UTC);
std::cout << end << " " << past << " " << days << std::endl; // 2024-03-28 1 365
return end.format() == "2024-03-28" && days == 365 && midnight.date(anch::date::Zone::UTC) == birth;
}


anch::date::Time is a time of day (00:00:00 to 23:59:59.999999999, RFC 3339 partial-time , SQL TIME ), without time zone either:

#include "date/time.hpp"
bool
timeExample() {
using namespace std::chrono_literals;
anch::date::Time opening(9, 30); // impossible times are refused
anch::date::Time closing = opening.plus(9h); // 18:30:00 (wraps around midnight)
bool open = opening <= anch::date::Time::now() && anch::date::Time::now() < closing;
std::cout << closing << " " << open << std::endl; // 18:30:00 ...
return closing.format() == "18:30:00" && meeting.time(anch::date::Zone::UTC) == anch::date::Time(14, 0);
}


anch::date::OffsetTime is a time of day with a fixed offset from UTC (-23:59 to +23:59, RFC 3339 full-time , JSON schema and OpenAPI time format): equality compares the time and the offset, isSameInstant the UTC times ; a day and an offset time give a point in time without time zone:

#include "date/offsetTime.hpp"
bool
offsetTimeExample() {
using namespace std::chrono_literals;
anch::date::OffsetTime call = anch::date::OffsetTime::parse("14:00:00+02:00"); // RFC 3339 full-time
anch::date::OffsetTime utc = call.withOffset(0min); // 12:00:00Z
bool same = call.isSameInstant(utc) && call != utc; // same instant, other offset
anch::date::DateTime point = anch::date::Date(2025, 7, 1).at(call); // no time zone needed
std::cout << utc << " " << same << std::endl; // 12:00:00Z 1
return utc.format() == "12:00:00Z" && same && point.offsetTime(anch::date::Zone::UTC) == utc;
}

Example

#include "date/dateFormatter.hpp"
void
formatterExample() {
anch::date::DateFormatter formatter("%Y-%m-%d %H:%M:%S.%s");
std::string str;
formatter.format(anch::date::DateTime::now(), str);
anch::date::DateTime date = formatter.parse("2012-10-23 10:02:59.998");
std::time_t time = static_cast<std::time_t>(date);
std::cout << str << " " << time << std::endl;
}
#include "date/rfc3339.hpp"
bool
rfc3339Example() {
anch::date::DateTime date = anch::date::rfc3339::parseDateTime("2025-07-01T12:00:00.5+02:00");
std::string utc = anch::date::rfc3339::formatDateTime(date); // 2025-07-01T10:00:00.500Z
std::cout << local << " " << day.year() << std::endl;
return utc == "2025-07-01T10:00:00.500Z";
}