hemplate

Simple XML template engine
git clone git://git.dimitrijedobrota.com/hemplate.git
Log | Files | Refs | README | LICENSE

commit 5bd98de2a3f7172979d2807874c80f1573d0e103
parent e110ecd2946f29e9b761e672950aba4c1f1827f0
Author: Dimitrije Dobrota <mail@dimitrijedobrota.com>
Date:   Sun, 19 Jan 2025 20:39:13 +0100

Add functions to format time according to standard

Diffstat:
MCMakeLists.txt | 3++-
Minclude/hemplate/classes.hpp | 9++++++++-
Asource/classes.cpp | 49+++++++++++++++++++++++++++++++++++++++++++++++++
3 files changed, 59 insertions(+), 2 deletions(-)

diff --git a/CMakeLists.txt b/CMakeLists.txt @@ -4,7 +4,7 @@ include(cmake/prelude.cmake) project( hemplate - VERSION 0.2.0 + VERSION 0.2.2 DESCRIPTION "Simple HTML template engine" HOMEPAGE_URL "https://git.dimitrijedobrota.com/hemplate.git" LANGUAGES CXX @@ -17,6 +17,7 @@ include(cmake/variables.cmake) add_library( hemplate_hemplate + source/classes.cpp source/element.cpp source/attribute.cpp ) diff --git a/include/hemplate/classes.hpp b/include/hemplate/classes.hpp @@ -197,6 +197,9 @@ using wbr = elementAtomic<tag<"wbr">>; namespace rss { +std::string format_time(int64_t sec); +std::string format_time_now(); + class rss : public elementBoolean<tag<"rss">> { public: @@ -255,6 +258,9 @@ using width = elementBoolean<tag<"width">>; namespace atom { +std::string format_time(int64_t sec); +std::string format_time_now(); + class feed : public elementBoolean<tag<"feed">> { public: @@ -269,6 +275,7 @@ using category = elementBoolean<tag<"category">>; using content = elementBoolean<tag<"content">>; using contributor = elementBoolean<tag<"contributor">>; using div = elementBoolean<tag<"div">>; +using email = elementBoolean<tag<"email">>; using entry = elementBoolean<tag<"entry">>; using generator = elementBoolean<tag<"generator">>; using icon = elementBoolean<tag<"icon">>; @@ -286,8 +293,8 @@ using subtitle = elementBoolean<tag<"subtitle">>; using summary = elementBoolean<tag<"summary">>; using text = elementBoolean<tag<"">>; using title = elementBoolean<tag<"title">>; -using update = elementBoolean<tag<"update">>; using updated = elementBoolean<tag<"updated">>; +using uri = elementBoolean<tag<"uri">>; using usagePoint = elementBoolean<tag<"usagePoint">>; } // namespace atom diff --git a/source/classes.cpp b/source/classes.cpp @@ -0,0 +1,49 @@ +#include <chrono> +#include <ctime> +#include <format> +#include <string> + +auto sec_since_epoch(int64_t sec) +{ + return std::chrono::time_point_cast<std::chrono::seconds>( + std::chrono::system_clock::from_time_t(time_t {0}) + + std::chrono::seconds(sec)); +} + +auto get_time_now() +{ + return std::chrono::current_zone() + ->to_local(std::chrono::system_clock::now()) + .time_since_epoch() + / std::chrono::seconds(1); +} + +namespace hemplate::atom { + +std::string format_time(int64_t sec) +{ + static constexpr const char* rfc3339_f = "{:%FT%H:%M:%SZ}"; + return std::format(rfc3339_f, sec_since_epoch(sec)); +} + +std::string format_time_now() +{ + return format_time(get_time_now()); +} + +} // namespace hemplate::atom + +namespace hemplate::rss { + +std::string format_time(int64_t sec) +{ + static constexpr const char* rfc882_f = "{:%a, %d %b %Y %H:%M:%S %z}"; + return std::format(rfc882_f, sec_since_epoch(sec)); +} + +std::string format_time_now() +{ + return format_time(get_time_now()); +} + +} // namespace hemplate::rss