tempoch-cpp
Header-only C++ wrapper for tempoch
Loading...
Searching...
No Matches
time_example.cpp
Go to the documentation of this file.
1
11#include <tempoch/tempoch.hpp>
12#include <iostream>
13#include <iomanip>
14
15int main() {
16 using namespace tempoch;
17
18 // ---------------------------------------------------------------
19 // UTC → JulianDate → MJD round-trip
20 // ---------------------------------------------------------------
21 UTC utc(2026, 7, 15, 22, 0, 0);
22 std::cout << "UTC: " << utc.year << "-"
23 << std::setfill('0') << std::setw(2) << (int)utc.month << "-"
24 << std::setw(2) << (int)utc.day << " "
25 << std::setw(2) << (int)utc.hour << ":"
26 << std::setw(2) << (int)utc.minute << ":"
27 << std::setw(2) << (int)utc.second << "\n";
28
29 auto jd = JulianDate::from_utc(utc);
30 std::cout << "JD: " << std::fixed << std::setprecision(6) << jd.value() << "\n";
31
32 auto mjd = MJD::from_jd(jd);
33 std::cout << "MJD: " << std::fixed << std::setprecision(6) << mjd.value() << "\n";
34
35 auto utc2 = mjd.to_utc();
36 std::cout << "Back: " << utc2.year << "-"
37 << std::setfill('0') << std::setw(2) << (int)utc2.month << "-"
38 << std::setw(2) << (int)utc2.day << " "
39 << std::setw(2) << (int)utc2.hour << ":"
40 << std::setw(2) << (int)utc2.minute << ":"
41 << std::setw(2) << (int)utc2.second << "\n\n";
42
43 // ---------------------------------------------------------------
44 // J2000 epoch and Julian centuries
45 // ---------------------------------------------------------------
46 auto j2000 = JulianDate::J2000();
47 std::cout << "J2000.0: " << j2000.value() << "\n";
48 std::cout << "Centuries since J2000: " << jd.julian_centuries() << "\n\n";
49
50 // ---------------------------------------------------------------
51 // Period intersection
52 // ---------------------------------------------------------------
53 Period night(60200.0, 60200.5);
54 Period obs(60200.2, 60200.8);
55 auto overlap = night.intersection(obs);
56 std::cout << "Night: [" << night.start_mjd() << ", " << night.end_mjd() << "]\n";
57 std::cout << "Obs: [" << obs.start_mjd() << ", " << obs.end_mjd() << "]\n";
58 std::cout << "Overlap: [" << overlap.start_mjd() << ", " << overlap.end_mjd() << "]\n";
59 std::cout << "Overlap duration: " << overlap.duration_days() * 24.0 << " hours\n";
60
61 return 0;
62}
A time period [start, end] in MJD.
Definition period.hpp:33
double start_mjd() const
Inclusive period start as raw MJD days.
Definition period.hpp:66
Period intersection(const Period &other) const
Compute the overlapping interval with another period.
Definition period.hpp:88
double end_mjd() const
Inclusive period end as raw MJD days.
Definition period.hpp:69
UTC date-time breakdown.
Definition time.hpp:28
uint8_t hour
Hour in range [0, 23].
Definition time.hpp:36
uint8_t second
Second in range [0, 60], leap second aware.
Definition time.hpp:40
uint8_t day
Day of month in range [1, 31].
Definition time.hpp:34
int32_t year
Gregorian year (astronomical year numbering).
Definition time.hpp:30
uint8_t month
Month in range [1, 12].
Definition time.hpp:32
uint8_t minute
Minute in range [0, 59].
Definition time.hpp:38
Umbrella header for the tempoch C++ wrapper library.
int main()