Changing license to New BSD (3-Clause version)
[anna.git] / include / anna / time / internal / Timezone.hpp
1 // ANNA - Anna is Not Nothingness Anymore
2 //
3 // (c) Copyright 2005-2014 Eduardo Ramos Testillano & Francisco Ruiz Rayo
4 //
5 // http://redmine.teslayout.com/projects/anna-suite
6 //
7 // Redistribution and use in source and binary forms, with or without
8 // modification, are permitted provided that the following conditions
9 // are met:
10 //
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
16 // distribution.
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.
20 //
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.
32 //
33 // Authors: eduardo.ramos.testillano@gmail.com
34 //          cisco.tierra@gmail.com
35
36
37 #ifndef anna_time_internal_Timezone_hpp
38 #define anna_time_internal_Timezone_hpp
39
40
41 // Standard
42 #include <string>
43 #include <stdlib.h>
44 #include <iostream>
45
46 extern char *tzname[2];
47
48 //------------------------------------------------------------------------------
49 //---------------------------------------------------------------------- #define
50 //------------------------------------------------------------------------------
51
52
53 namespace anna {
54
55 namespace time {
56
57 class Timezone {
58
59   bool a_initialized;
60   std::string a_value;
61   bool a_unsetTZ;
62
63 public:
64
65   Timezone() { a_initialized = false; };
66   ~Timezone() {};
67
68
69   // sets
70   // pass NULL to unset
71   void set(const char *input) {
72     a_initialized = true;
73     a_value = input ? input : "";
74
75   //  /*
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.
80   //  */
81   //  if (input && (a_value == "")) a_value="UTC0";
82
83     a_unsetTZ = !input;
84   }
85
86   Timezone & operator = (const Timezone &tz) {
87     if(this == &tz) return (*this);
88
89     a_initialized = tz.isInitialized();
90     a_value = tz.getValue();
91     a_unsetTZ = tz.unsetTZ();
92     return (*this);
93   }
94
95   // gets
96
97   // Operators
98
99   friend bool operator == (const Timezone & tz1, const Timezone & tz2) {
100     if(tz1.isInitialized() != tz2.isInitialized())
101       return (false);
102
103     if(tz1.unsetTZ())
104       return (tz2.unsetTZ());
105
106     return (tz1.getValue() == tz2.getValue());
107   }
108
109   friend bool operator != (const Timezone & tz1, const Timezone & tz2) {
110     return (!(tz1 == tz2));
111   }
112
113   // be careful: empty if unset
114   const std::string & getValue(void) const throw() {
115     return (a_value);
116   }
117
118
119   bool isInitialized(void) const throw() {
120     return (a_initialized);
121   }
122
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); }
126
127   bool apply() const throw() {
128     //    http://stackoverflow.com/questions/5873029/questions-about-putenv-and-setenv
129     if(unsetTZ()) {
130       unsetenv("TZ");                                                             
131     }                                                                             
132     else {                                                                        
133       setenv("TZ", a_value.c_str(), 1 /* overwrite */);                              
134
135       /*
136       // If TZ has an invalid setting, tzset() uses GMT0:
137       // Check it:
138       tzset();
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;
144       return success;
145       */
146     }                                                                             
147
148     return true;
149   }
150
151   std::string asString() const throw() {
152     std::string result = getValue();
153     if (unsetTZ()) return "<TZ unset>";
154     if (result == "") return "<TZ empty>";
155     return result;
156   }
157 };
158
159
160 }
161 }
162
163 #endif
164