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