tempoch-cpp 0.5.3
Header-only C++ wrapper for tempoch
Loading...
Searching...
No Matches
civil_time.hpp
Go to the documentation of this file.
1#pragma once
2
11#include "ffi_core.hpp"
12#include <iomanip>
13#include <ostream>
14
15namespace tempoch {
16
28struct CivilTime {
29 int32_t year;
30 uint8_t month;
31 uint8_t day;
32 uint8_t hour;
33 uint8_t minute;
34 uint8_t second;
35 uint32_t nanosecond;
36
38 CivilTime() : year(2000), month(1), day(1), hour(12), minute(0), second(0), nanosecond(0) {}
39
43 CivilTime(int32_t y, uint8_t mo, uint8_t d, uint8_t h = 0, uint8_t mi = 0, uint8_t s = 0,
44 uint32_t ns = 0)
45 : year(y), month(mo), day(d), hour(h), minute(mi), second(s), nanosecond(ns) {}
46
48 tempoch_utc_t to_c() const { return {year, month, day, hour, minute, second, nanosecond}; }
49
51 static CivilTime from_c(const tempoch_utc_t &c) {
52 return CivilTime(c.year, c.month, c.day, c.hour, c.minute, c.second, c.nanosecond);
53 }
54};
55
57inline std::ostream &operator<<(std::ostream &os, const CivilTime &u) {
58 const char prev = os.fill();
59 os << u.year << '-' << std::setfill('0') << std::setw(2) << static_cast<int>(u.month) << '-'
60 << std::setw(2) << static_cast<int>(u.day) << ' ' << std::setw(2) << static_cast<int>(u.hour)
61 << ':' << std::setw(2) << static_cast<int>(u.minute) << ':' << std::setw(2)
62 << static_cast<int>(u.second);
63 if (u.nanosecond != 0)
64 os << '.' << std::setw(9) << u.nanosecond;
65 os.fill(prev);
66 return os;
67}
68
69} // namespace tempoch
Error handling for the tempoch C++ wrapper.
std::ostream & operator<<(std::ostream &os, const CivilTime &u)
Stream CivilTime as YYYY-MM-DD HH:MM:SS[.nnnnnnnnn].
UTC date-time breakdown.
uint8_t second
Second [0, 60] (60 only during a positive leap second).
static CivilTime from_c(const tempoch_utc_t &c)
Create from the C FFI struct.
uint32_t nanosecond
Nanosecond [0, 999 999 999].
uint8_t month
Month [1, 12].
tempoch_utc_t to_c() const
Convert to the C FFI struct.
uint8_t day
Day of month [1, 31].
uint8_t minute
Minute [0, 59].
int32_t year
Gregorian year (astronomical year numbering).
CivilTime()
Default constructor: J2000 epoch noon-like civil representation.
uint8_t hour
Hour [0, 23].
CivilTime(int32_t y, uint8_t mo, uint8_t d, uint8_t h=0, uint8_t mi=0, uint8_t s=0, uint32_t ns=0)
Construct from civil UTC components.