1 // ANNA - Anna is Not Nothingness Anymore
3 // (c) Copyright 2005-2014 Eduardo Ramos Testillano & Francisco Ruiz Rayo
5 // http://redmine.teslayout.com/projects/anna-suite
7 // Redistribution and use in source and binary forms, with or without
8 // modification, are permitted provided that the following conditions
11 // * Redistributions of source code must retain the above copyright
12 // notice, this list of conditions and the following disclaimer.
13 // * Redistributions in binary form must reproduce the above
14 // copyright notice, this list of conditions and the following disclaimer
15 // in the documentation and/or other materials provided with the
17 // * Neither the name of the copyright holder nor the names of its
18 // contributors may be used to endorse or promote products derived from
19 // this software without specific prior written permission.
21 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33 // Authors: eduardo.ramos.testillano@gmail.com
34 // cisco.tierra@gmail.com
37 #ifndef anna_time_internal_Timezone_hpp
38 #define anna_time_internal_Timezone_hpp
46 extern char *tzname[2];
48 //------------------------------------------------------------------------------
49 //---------------------------------------------------------------------- #define
50 //------------------------------------------------------------------------------
65 Timezone() { a_initialized = false; };
71 void set(const char *input) {
73 a_value = input ? input : "";
76 // In many C libraries, TZ='' is equivalent to TZ='GMT0' or TZ='UTC0',
77 // and that's the behavior you're observing. An unset TZ is equivalent
78 // to a system-supplied default, typically the local time where the host
79 // is physically located.
81 // if (input && (a_value == "")) a_value="UTC0";
86 Timezone & operator = (const Timezone &tz) {
87 if(this == &tz) return (*this);
89 a_initialized = tz.isInitialized();
90 a_value = tz.getValue();
91 a_unsetTZ = tz.unsetTZ();
99 friend bool operator == (const Timezone & tz1, const Timezone & tz2) {
100 if(tz1.isInitialized() != tz2.isInitialized())
104 return (tz2.unsetTZ());
106 return (tz1.getValue() == tz2.getValue());
109 friend bool operator != (const Timezone & tz1, const Timezone & tz2) {
110 return (!(tz1 == tz2));
113 // be careful: empty if unset
114 const std::string & getValue(void) const throw() {
119 bool isInitialized(void) const throw() {
120 return (a_initialized);
123 //Unset is when getenv("TZ") returns NULL. Then, /etc/localtime used to be a copy or symlink to
124 // any of the /usr/share/zoneinfo available timezones.
125 bool unsetTZ(void) const throw() { return (a_unsetTZ); }
127 bool apply() const throw() {
128 // http://stackoverflow.com/questions/5873029/questions-about-putenv-and-setenv
133 setenv("TZ", a_value.c_str(), 1 /* overwrite */);
136 // If TZ has an invalid setting, tzset() uses GMT0:
139 //std::cout << "tzname[0]: " << tzname[0] << std::endl;
140 //std::cout << "tzname[1]: " << tzname[1] << std::endl;
141 std::string tzname_1 = tzname[1];
142 bool success = (tzname_1 != "");
143 if (!success) std::cerr << "Invalid TZ '" << a_value << "'. Probably GMT0 is going to be used instead !" << std::endl;
151 std::string asString() const throw() {
152 std::string result = getValue();
153 if (unsetTZ()) return "<TZ unset>";
154 if (result == "") return "<TZ empty>";