1 // ANNA - Anna is Not Nothingness Anymore //
3 // (c) Copyright 2005-2015 Eduardo Ramos Testillano & Francisco Ruiz Rayo //
5 // See project site at http://redmine.teslayout.com/projects/anna-suite //
6 // See accompanying file LICENSE or copy at http://www.teslayout.com/projects/public/anna.LICENSE //
9 #ifndef anna_time_internal_Timezone_hpp
10 #define anna_time_internal_Timezone_hpp
18 extern char *tzname[2];
20 //------------------------------------------------------------------------------
21 //---------------------------------------------------------------------- #define
22 //------------------------------------------------------------------------------
37 Timezone() { a_initialized = false; };
43 void set(const char *input) {
45 a_value = input ? input : "";
48 // In many C libraries, TZ='' is equivalent to TZ='GMT0' or TZ='UTC0',
49 // and that's the behavior you're observing. An unset TZ is equivalent
50 // to a system-supplied default, typically the local time where the host
51 // is physically located.
53 // if (input && (a_value == "")) a_value="UTC0";
58 Timezone & operator = (const Timezone &tz) {
59 if(this == &tz) return (*this);
61 a_initialized = tz.isInitialized();
62 a_value = tz.getValue();
63 a_unsetTZ = tz.unsetTZ();
71 friend bool operator == (const Timezone & tz1, const Timezone & tz2) {
72 if(tz1.isInitialized() != tz2.isInitialized())
76 return (tz2.unsetTZ());
78 return (tz1.getValue() == tz2.getValue());
81 friend bool operator != (const Timezone & tz1, const Timezone & tz2) {
82 return (!(tz1 == tz2));
85 // be careful: empty if unset
86 const std::string & getValue(void) const throw() {
91 bool isInitialized(void) const throw() {
92 return (a_initialized);
95 //Unset is when getenv("TZ") returns NULL. Then, /etc/localtime used to be a copy or symlink to
96 // any of the /usr/share/zoneinfo available timezones.
97 bool unsetTZ(void) const throw() { return (a_unsetTZ); }
99 bool apply() const throw() {
100 // http://stackoverflow.com/questions/5873029/questions-about-putenv-and-setenv
105 setenv("TZ", a_value.c_str(), 1 /* overwrite */);
108 // If TZ has an invalid setting, tzset() uses GMT0:
111 //std::cout << "tzname[0]: " << tzname[0] << std::endl;
112 //std::cout << "tzname[1]: " << tzname[1] << std::endl;
113 std::string tzname_1 = tzname[1];
114 bool success = (tzname_1 != "");
115 if (!success) std::cerr << "Invalid TZ '" << a_value << "'. Probably GMT0 is going to be used instead !" << std::endl;
123 std::string asString() const throw() {
124 std::string result = getValue();
125 if (unsetTZ()) return "<TZ unset>";
126 if (result == "") return "<TZ empty>";