Line data Source code
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 :
21 0 : oks::tz::DB::DB()
22 : {
23 0 : const char* tz_spec_file = ::getenv("BOOST_DATE_TIME_TZ_SPEC");
24 :
25 0 : if (!tz_spec_file || !*tz_spec_file) {
26 0 : throw std::runtime_error("cannot read value of BOOST_DATE_TIME_TZ_SPEC environment variable");
27 : }
28 :
29 0 : TLOG_DEBUG(1) << "Boost time-zone specification file is \'" << tz_spec_file << '\'';
30 :
31 0 : try {
32 0 : m_tz_db.load_from_file(tz_spec_file);
33 0 : } catch (const std::exception& ex) {
34 0 : std::ostringstream text;
35 0 : text << "cannot read Boost time-zone specification file \"" << tz_spec_file << "\": " << ex.what();
36 0 : throw std::runtime_error(text.str().c_str());
37 0 : }
38 0 : }
39 :
40 : boost::local_time::time_zone_ptr
41 0 : oks::tz::DB::get_tz_ptr(const std::string& region)
42 : {
43 0 : boost::local_time::time_zone_ptr tz_ptr = m_tz_db.time_zone_from_region(region);
44 :
45 0 : if (!tz_ptr) {
46 0 : std::ostringstream text;
47 0 : text << "cannot find time-zone \'" << region << '\'';
48 0 : throw std::runtime_error(text.str().c_str());
49 0 : }
50 :
51 0 : return tz_ptr;
52 0 : }
53 :
54 : std::vector<std::string>
55 0 : oks::tz::DB::get_regions()
56 : {
57 0 : return m_tz_db.region_list();
58 : }
59 :
60 : boost::posix_time::ptime
61 0 : oks::tz::str_2_posix_time(const std::string& in, boost::local_time::time_zone_ptr tz_ptr, const char* value)
62 : {
63 0 : std::string s(in);
64 0 : std::replace(s.begin(), s.end(), 'T', ' ');
65 :
66 0 : boost::posix_time::ptime t;
67 :
68 : // convert string to time
69 0 : try {
70 0 : t = boost::posix_time::time_from_string(s);
71 0 : } catch (const std::exception& ex) {
72 0 : std::ostringstream text;
73 0 : text << "cannot parse " << value << " = \'" << in << "\': \"" << ex.what()
74 0 : << "\" (ISO 8601 format (YYYY-MM-DD HH:MM:SS) is expected).";
75 0 : throw std::runtime_error(text.str().c_str());
76 0 : }
77 :
78 : // convert local time to UTC, if the time zone was provided
79 0 : if (tz_ptr) {
80 0 : try {
81 0 : boost::local_time::local_date_time lt(
82 0 : t.date(), t.time_of_day(), tz_ptr, boost::local_time::local_date_time::EXCEPTION_ON_ERROR);
83 0 : TLOG_DEBUG(1) << "Build zone\'s time \'" << in << "\' => \'" << lt.to_string() << "\' using \'"
84 0 : << tz_ptr->to_posix_string() << '\'';
85 0 : t = lt.utc_time();
86 0 : } catch (std::exception& e) {
87 0 : std::ostringstream text;
88 0 : text << "cannot parse " << value << " = \'" << in << "\' in time zone \"" << tz_ptr->to_posix_string() << "\": \""
89 0 : << e.what() << '\"' << std::endl;
90 0 : throw std::runtime_error(text.str().c_str());
91 0 : }
92 : }
93 :
94 0 : return t;
95 0 : }
96 :
97 : uint64_t
98 0 : oks::tz::posix_time_2_to_ns(boost::posix_time::ptime t)
99 : {
100 0 : static boost::posix_time::ptime epoch(boost::gregorian::date(1970, 1, 1));
101 0 : return ((t - epoch).total_nanoseconds());
102 : }
103 :
104 : std::string
105 0 : oks::tz::posix_time_2_iso_string(boost::posix_time::ptime t)
106 : {
107 0 : return boost::posix_time::to_iso_string(t);
108 : }
|