DUNE-DAQ
DUNE Trigger and Data Acquisition software
Loading...
Searching...
No Matches
oks_tz.cpp
Go to the documentation of this file.
1//
2// DUNE DAQ modification notice:
3// This file has been modified from the original ATLAS oks_utils source for the DUNE DAQ project.
4// Fork baseline commit: c2e7dfc7 (2022-03-30).
5// Renamed since fork: no.
6//
7
8#include "oksutils/oks/tz.hpp"
9
10#include "ers/ers.hpp"
11#include "logging/Logging.hpp"
12
13#include <boost/date_time/posix_time/posix_time_types.hpp>
14#include <boost/date_time/posix_time/time_formatters.hpp>
15#include <boost/date_time/posix_time/time_parsers.hpp>
16
17#include <iostream>
18#include <stdexcept>
19#include <string>
20
22{
23 const char* tz_spec_file = ::getenv("BOOST_DATE_TIME_TZ_SPEC");
24
25 if (!tz_spec_file || !*tz_spec_file) {
26 throw std::runtime_error("cannot read value of BOOST_DATE_TIME_TZ_SPEC environment variable");
27 }
28
29 TLOG_DEBUG(1) << "Boost time-zone specification file is \'" << tz_spec_file << '\'';
30
31 try {
32 m_tz_db.load_from_file(tz_spec_file);
33 } catch (const std::exception& ex) {
34 std::ostringstream text;
35 text << "cannot read Boost time-zone specification file \"" << tz_spec_file << "\": " << ex.what();
36 throw std::runtime_error(text.str().c_str());
37 }
38}
39
40boost::local_time::time_zone_ptr
41oks::tz::DB::get_tz_ptr(const std::string& region)
42{
43 boost::local_time::time_zone_ptr tz_ptr = m_tz_db.time_zone_from_region(region);
44
45 if (!tz_ptr) {
46 std::ostringstream text;
47 text << "cannot find time-zone \'" << region << '\'';
48 throw std::runtime_error(text.str().c_str());
49 }
50
51 return tz_ptr;
52}
53
54std::vector<std::string>
56{
57 return m_tz_db.region_list();
58}
59
60boost::posix_time::ptime
61oks::tz::str_2_posix_time(const std::string& in, boost::local_time::time_zone_ptr tz_ptr, const char* value)
62{
63 std::string s(in);
64 std::replace(s.begin(), s.end(), 'T', ' ');
65
66 boost::posix_time::ptime t;
67
68 // convert string to time
69 try {
70 t = boost::posix_time::time_from_string(s);
71 } catch (const std::exception& ex) {
72 std::ostringstream text;
73 text << "cannot parse " << value << " = \'" << in << "\': \"" << ex.what()
74 << "\" (ISO 8601 format (YYYY-MM-DD HH:MM:SS) is expected).";
75 throw std::runtime_error(text.str().c_str());
76 }
77
78 // convert local time to UTC, if the time zone was provided
79 if (tz_ptr) {
80 try {
81 boost::local_time::local_date_time lt(
82 t.date(), t.time_of_day(), tz_ptr, boost::local_time::local_date_time::EXCEPTION_ON_ERROR);
83 TLOG_DEBUG(1) << "Build zone\'s time \'" << in << "\' => \'" << lt.to_string() << "\' using \'"
84 << tz_ptr->to_posix_string() << '\'';
85 t = lt.utc_time();
86 } catch (std::exception& e) {
87 std::ostringstream text;
88 text << "cannot parse " << value << " = \'" << in << "\' in time zone \"" << tz_ptr->to_posix_string() << "\": \""
89 << e.what() << '\"' << std::endl;
90 throw std::runtime_error(text.str().c_str());
91 }
92 }
93
94 return t;
95}
96
97uint64_t
98oks::tz::posix_time_2_to_ns(boost::posix_time::ptime t)
99{
100 static boost::posix_time::ptime epoch(boost::gregorian::date(1970, 1, 1));
101 return ((t - epoch).total_nanoseconds());
102}
103
104std::string
105oks::tz::posix_time_2_iso_string(boost::posix_time::ptime t)
106{
107 return boost::posix_time::to_iso_string(t);
108}
std::vector< std::string > get_regions()
Get known time zone regions.
Definition oks_tz.cpp:55
boost::local_time::tz_database m_tz_db
Definition tz.hpp:56
boost::local_time::time_zone_ptr get_tz_ptr(const std::string &region)
Get boost time-zone pointer from string.
Definition oks_tz.cpp:41
#define TLOG_DEBUG(lvl,...)
Definition Logging.hpp:116
boost::posix_time::ptime str_2_posix_time(const std::string &in, boost::local_time::time_zone_ptr tz_ptr, const char *value)
Definition oks_tz.cpp:61
std::string posix_time_2_iso_string(boost::posix_time::ptime t)
Definition oks_tz.cpp:105
uint64_t posix_time_2_to_ns(boost::posix_time::ptime t)
Definition oks_tz.cpp:98