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