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