New URL for NEMO forge!   http://forge.nemo-ocean.eu

Since March 2022 along with NEMO 4.2 release, the code development moved to a self-hosted GitLab.
This present forge is now archived and remained online for history.
local_date_time.hpp in vendors/XIOS/current/extern/boost/include/boost/date_time/local_time – NEMO

source: vendors/XIOS/current/extern/boost/include/boost/date_time/local_time/local_date_time.hpp @ 3428

Last change on this file since 3428 was 3428, checked in by rblod, 12 years ago

importing initial XIOS vendor drop

File size: 19.4 KB
Line 
1#ifndef LOCAL_TIME_LOCAL_DATE_TIME_HPP__
2#define LOCAL_TIME_LOCAL_DATE_TIME_HPP__
3
4/* Copyright (c) 2003-2005 CrystalClear Software, Inc.
5 * Subject to the Boost Software License, Version 1.0.
6 * (See accompanying file LICENSE_1_0.txt or http://www.boost.org/LICENSE_1_0.txt)
7 * Author: Jeff Garland, Bart Garst
8 * $Date: 2010-01-10 14:17:23 -0500 (Sun, 10 Jan 2010) $
9 */
10
11#include <string>
12#include <iomanip>
13#include <sstream>
14#include <stdexcept>
15#include <boost/shared_ptr.hpp>
16#include <boost/throw_exception.hpp>
17#include <boost/date_time/time.hpp>
18#include <boost/date_time/posix_time/posix_time.hpp> //todo remove?
19#include <boost/date_time/dst_rules.hpp>
20#include <boost/date_time/time_zone_base.hpp>
21#include <boost/date_time/special_defs.hpp>
22#include <boost/date_time/time_resolution_traits.hpp> // absolute_value
23
24namespace boost {
25namespace local_time {
26
27  //! simple exception for reporting when STD or DST cannot be determined
28  struct ambiguous_result : public std::logic_error
29  {
30    ambiguous_result (std::string const& msg = std::string()) :
31      std::logic_error(std::string("Daylight Savings Results are ambiguous: " + msg)) {}
32  };
33  //! simple exception for when time label given cannot exist
34  struct time_label_invalid : public std::logic_error
35  {
36    time_label_invalid (std::string const& msg = std::string()) :
37      std::logic_error(std::string("Time label given is invalid: " + msg)) {}
38  };
39  struct dst_not_valid: public std::logic_error
40  {
41    dst_not_valid(std::string const& msg = std::string()) :
42      std::logic_error(std::string("is_dst flag does not match resulting dst for time label given: " + msg)) {}
43  };
44
45  //TODO: I think these should be in local_date_time_base and not
46  // necessarily brought into the namespace
47  using date_time::time_is_dst_result;
48  using date_time::is_in_dst;
49  using date_time::is_not_in_dst;
50  using date_time::ambiguous;
51  using date_time::invalid_time_label;
52
53  //! Representation of "wall-clock" time in a particular time zone
54  /*! Representation of "wall-clock" time in a particular time zone
55   * Local_date_time_base holds a time value (date and time offset from 00:00)
56   * along with a time zone. The time value is stored as UTC and conversions
57   * to wall clock time are made as needed. This approach allows for
58   * operations between wall-clock times in different time zones, and
59   * daylight savings time considerations, to be made. Time zones are
60   * required to be in the form of a boost::shared_ptr<time_zone_base>.
61   */
62  template<class utc_time_=posix_time::ptime,
63           class tz_type=date_time::time_zone_base<utc_time_,char> >
64  class local_date_time_base :  public date_time::base_time<utc_time_,
65                                                            boost::posix_time::posix_time_system> {
66  public:
67    typedef utc_time_ utc_time_type;
68    typedef typename utc_time_type::time_duration_type time_duration_type;
69    typedef typename utc_time_type::date_type date_type;
70    typedef typename date_type::duration_type date_duration_type;
71    typedef typename utc_time_type::time_system_type time_system_type;
72    /*! This constructor interprets the passed time as a UTC time.
73     *  So, for example, if the passed timezone is UTC-5 then the
74     *  time will be adjusted back 5 hours.  The time zone allows for
75     *  automatic calculation of whether the particular time is adjusted for
76     *  daylight savings, etc.
77     *  If the time zone shared pointer is null then time stays unadjusted.
78     *@param t A UTC time
79     *@param tz Timezone for to adjust the UTC time to.
80     */
81    local_date_time_base(utc_time_type t,
82                         boost::shared_ptr<tz_type> tz) :
83      date_time::base_time<utc_time_type, time_system_type>(t),
84      zone_(tz)
85    {
86      // param was already utc so nothing more to do
87    }
88
89    /*! This constructs a local time -- the passed time information
90     * understood to be in the passed tz. The DST flag must be passed
91     * to indicate whether the time is in daylight savings or not.
92     *  @throws -- time_label_invalid if the time passed does not exist in
93     *             the given locale. The non-existent case occurs typically
94     *             during the shift-back from daylight savings time.  When
95     *             the clock is shifted forward a range of times
96     *             (2 am to 3 am in the US) is skipped and hence is invalid.
97     *  @throws -- dst_not_valid if the DST flag is passed for a period
98     *             where DST is not active.
99     */
100    local_date_time_base(date_type d,
101                         time_duration_type td,
102                         boost::shared_ptr<tz_type> tz,
103                         bool dst_flag) : //necessary for constr_adj()
104      date_time::base_time<utc_time_type,time_system_type>(construction_adjustment(utc_time_type(d, td), tz, dst_flag)),
105      zone_(tz)
106    {
107      if(tz != boost::shared_ptr<tz_type>() && tz->has_dst()){
108
109        // d & td are already local so we use them
110        time_is_dst_result result = check_dst(d, td, tz);
111        bool in_dst = (result == is_in_dst); // less processing than is_dst()
112
113        // ambig occurs at end, invalid at start
114        if(result == invalid_time_label){
115          // Ex: 2:15am local on trans-in day in nyc, dst_flag irrelevant
116          std::ostringstream ss;
117          ss << "time given: " << d << ' ' << td;
118          boost::throw_exception(time_label_invalid(ss.str()));
119        }
120        else if(result != ambiguous && in_dst != dst_flag){
121          // is dst_flag accurate?
122          // Ex: false flag in NYC in June
123          std::ostringstream ss;
124          ss.setf(std::ios_base::boolalpha);
125          ss << "flag given: dst=" << dst_flag << ", dst calculated: dst=" << in_dst;
126          boost::throw_exception(dst_not_valid(ss.str()));
127        }
128
129        // everything checks out and conversion to utc already done
130      }
131    }
132
133    //TODO maybe not the right set...Ignore the last 2 for now...
134    enum DST_CALC_OPTIONS { EXCEPTION_ON_ERROR, NOT_DATE_TIME_ON_ERROR };
135                            //ASSUME_DST_ON_ERROR, ASSUME_NOT_DST_ON_ERROR };
136
137    /*! This constructs a local time -- the passed time information
138     * understood to be in the passed tz.  The DST flag is calculated
139     * according to the specified rule.
140     */
141    local_date_time_base(date_type d,
142                         time_duration_type td,
143                         boost::shared_ptr<tz_type> tz,
144                         DST_CALC_OPTIONS calc_option) :
145      // dummy value - time_ is set in constructor code
146      date_time::base_time<utc_time_type,time_system_type>(utc_time_type(d,td)),
147      zone_(tz)
148    {
149      time_is_dst_result result = check_dst(d, td, tz);
150      if(result == ambiguous) {
151        if(calc_option == EXCEPTION_ON_ERROR){
152          std::ostringstream ss;
153          ss << "time given: " << d << ' ' << td;
154          boost::throw_exception(ambiguous_result(ss.str()));
155        }
156        else{ // NADT on error
157          this->time_ = posix_time::posix_time_system::get_time_rep(date_type(date_time::not_a_date_time), time_duration_type(date_time::not_a_date_time));
158        }
159      }
160      else if(result == invalid_time_label){
161        if(calc_option == EXCEPTION_ON_ERROR){
162          std::ostringstream ss;
163          ss << "time given: " << d << ' ' << td;
164          boost::throw_exception(time_label_invalid(ss.str()));
165        }
166        else{ // NADT on error
167          this->time_ = posix_time::posix_time_system::get_time_rep(date_type(date_time::not_a_date_time), time_duration_type(date_time::not_a_date_time));
168        }
169      }
170      else if(result == is_in_dst){
171        utc_time_type t =
172          construction_adjustment(utc_time_type(d, td), tz, true);
173        this->time_ = posix_time::posix_time_system::get_time_rep(t.date(),
174                                                            t.time_of_day());
175      }
176      else{
177        utc_time_type t =
178          construction_adjustment(utc_time_type(d, td), tz, false);
179        this->time_ = posix_time::posix_time_system::get_time_rep(t.date(),
180                                                            t.time_of_day());
181      }
182    }
183
184
185    //! Determines if given time label is in daylight savings for given zone
186    /*! Determines if given time label is in daylight savings for given zone.
187     * Takes a date and time_duration representing a local time, along
188     * with time zone, and returns a time_is_dst_result object as result.
189     */
190    static time_is_dst_result check_dst(date_type d,
191                                        time_duration_type td,
192                                        boost::shared_ptr<tz_type> tz)
193    {
194      if(tz != boost::shared_ptr<tz_type>() && tz->has_dst()) {
195        typedef typename date_time::dst_calculator<date_type, time_duration_type> dst_calculator;
196        return dst_calculator::local_is_dst(
197            d, td,
198            tz->dst_local_start_time(d.year()).date(),
199            tz->dst_local_start_time(d.year()).time_of_day(),
200            tz->dst_local_end_time(d.year()).date(),
201            tz->dst_local_end_time(d.year()).time_of_day(),
202            tz->dst_offset()
203        );
204      }
205      else{
206        return is_not_in_dst;
207      }
208    }
209
210    //! Simple destructor, releases time zone if last referrer
211    ~local_date_time_base() {};
212
213    //! Copy constructor
214    local_date_time_base(const local_date_time_base& rhs) :
215      date_time::base_time<utc_time_type, time_system_type>(rhs),
216      zone_(rhs.zone_)
217    {}
218
219    //! Special values constructor
220    explicit local_date_time_base(const boost::date_time::special_values sv,
221                                  boost::shared_ptr<tz_type> tz = boost::shared_ptr<tz_type>()) :
222      date_time::base_time<utc_time_type, time_system_type>(utc_time_type(sv)),
223      zone_(tz)
224    {}
225
226    //! returns time zone associated with calling instance
227    boost::shared_ptr<tz_type> zone() const
228    {
229      return zone_;
230    }
231    //! returns false is time_zone is NULL and if time value is a special_value
232    bool is_dst() const
233    {
234      if(zone_ != boost::shared_ptr<tz_type>() && zone_->has_dst() && !this->is_special()) {
235        // check_dst takes a local time, *this is utc
236        utc_time_type lt(this->time_);
237        lt += zone_->base_utc_offset();
238        // dst_offset only needs to be considered with ambiguous time labels
239        // make that adjustment there
240
241        switch(check_dst(lt.date(), lt.time_of_day(), zone_)){
242          case is_not_in_dst:
243            return false;
244          case is_in_dst:
245            return true;
246          case ambiguous:
247            if(lt + zone_->dst_offset() < zone_->dst_local_end_time(lt.date().year())) {
248              return true;
249            }
250            break;
251          case invalid_time_label:
252            if(lt >= zone_->dst_local_start_time(lt.date().year())) {
253              return true;
254            }
255            break;
256        }
257      }
258      return false;
259    }
260    //! Returns object's time value as a utc representation
261    utc_time_type utc_time() const
262    {
263      return utc_time_type(this->time_);
264    }
265    //! Returns object's time value as a local representation
266    utc_time_type local_time() const
267    {
268      if(zone_ != boost::shared_ptr<tz_type>()){
269        utc_time_type lt = this->utc_time() + zone_->base_utc_offset();
270        if (is_dst()) {
271          lt += zone_->dst_offset();
272        }
273        return lt;
274      }
275      return utc_time_type(this->time_);
276    }
277    //! Returns string in the form "2003-Aug-20 05:00:00 EDT"
278    /*! Returns string in the form "2003-Aug-20 05:00:00 EDT". If
279     * time_zone is NULL the time zone abbreviation will be "UTC". The time
280     * zone abbrev will not be included if calling object is a special_value*/
281    std::string to_string() const
282    {
283      //TODO is this a temporary function ???
284      std::ostringstream ss;
285      if(this->is_special()){
286        ss << utc_time();
287        return ss.str();
288      }
289      if(zone_ == boost::shared_ptr<tz_type>()) {
290        ss << utc_time() << " UTC";
291        return ss.str();
292      }
293      bool is_dst_ = is_dst();
294      utc_time_type lt = this->utc_time() + zone_->base_utc_offset();
295      if (is_dst_) {
296        lt += zone_->dst_offset();
297      }
298      ss << local_time() << " ";
299      if (is_dst()) {
300        ss << zone_->dst_zone_abbrev();
301      }
302      else {
303        ss << zone_->std_zone_abbrev();
304      }
305      return ss.str();
306    }
307    /*! returns a local_date_time_base in the given time zone with the
308     * optional time_duration added. */
309    local_date_time_base local_time_in(boost::shared_ptr<tz_type> new_tz,
310                                       time_duration_type td=time_duration_type(0,0,0)) const
311    {
312      return local_date_time_base(utc_time_type(this->time_) + td, new_tz);
313    }
314
315    //! Returns name of associated time zone or "Coordinated Universal Time".
316    /*! Optional bool parameter will return time zone as an offset
317     * (ie "+07:00" extended iso format). Empty string is returned for
318     * classes that do not use a time_zone */
319    std::string zone_name(bool as_offset=false) const
320    {
321      if(zone_ == boost::shared_ptr<tz_type>()) {
322        if(as_offset) {
323          return std::string("Z");
324        }
325        else {
326          return std::string("Coordinated Universal Time");
327        }
328      }
329      if (is_dst()) {
330        if(as_offset) {
331          time_duration_type td = zone_->base_utc_offset();
332          td += zone_->dst_offset();
333          return zone_as_offset(td, ":");
334        }
335        else {
336          return zone_->dst_zone_name();
337        }
338      }
339      else {
340        if(as_offset) {
341          time_duration_type td = zone_->base_utc_offset();
342          return zone_as_offset(td, ":");
343        }
344        else {
345          return zone_->std_zone_name();
346        }
347      }
348    }
349    //! Returns abbreviation of associated time zone or "UTC".
350    /*! Optional bool parameter will return time zone as an offset
351     * (ie "+0700" iso format). Empty string is returned for classes
352     * that do not use a time_zone */
353    std::string zone_abbrev(bool as_offset=false) const
354    {
355      if(zone_ == boost::shared_ptr<tz_type>()) {
356        if(as_offset) {
357          return std::string("Z");
358        }
359        else {
360          return std::string("UTC");
361        }
362      }
363      if (is_dst()) {
364        if(as_offset) {
365          time_duration_type td = zone_->base_utc_offset();
366          td += zone_->dst_offset();
367          return zone_as_offset(td, "");
368        }
369        else {
370          return zone_->dst_zone_abbrev();
371        }
372      }
373      else {
374        if(as_offset) {
375          time_duration_type td = zone_->base_utc_offset();
376          return zone_as_offset(td, "");
377        }
378        else {
379          return zone_->std_zone_abbrev();
380        }
381      }
382    }
383
384    //! returns a posix_time_zone string for the associated time_zone. If no time_zone, "UTC+00" is returned.
385    std::string zone_as_posix_string() const
386    {
387      if(zone_ == shared_ptr<tz_type>()) {
388        return std::string("UTC+00");
389      }
390      return zone_->to_posix_string();
391    }
392
393    //! Equality comparison operator
394    /*bool operator==(const date_time::base_time<boost::posix_time::ptime,boost::posix_time::posix_time_system>& rhs) const
395    { // fails due to rhs.time_ being protected
396      return date_time::base_time<boost::posix_time::ptime,boost::posix_time::posix_time_system>::operator==(rhs);
397      //return this->time_ == rhs.time_;
398    }*/
399    //! Equality comparison operator
400    bool operator==(const local_date_time_base& rhs) const
401    {
402      return time_system_type::is_equal(this->time_, rhs.time_);
403    }
404    //! Non-Equality comparison operator
405    bool operator!=(const local_date_time_base& rhs) const
406    {
407      return !(*this == rhs);
408    }
409    //! Less than comparison operator
410    bool operator<(const local_date_time_base& rhs) const
411    {
412      return time_system_type::is_less(this->time_, rhs.time_);
413    }
414    //! Less than or equal to comparison operator
415    bool operator<=(const local_date_time_base& rhs) const
416    {
417      return (*this < rhs || *this == rhs);
418    }
419    //! Greater than comparison operator
420    bool operator>(const local_date_time_base& rhs) const
421    {
422      return !(*this <= rhs);
423    }
424    //! Greater than or equal to comparison operator
425    bool operator>=(const local_date_time_base& rhs) const
426    {
427      return (*this > rhs || *this == rhs);
428    }
429
430    //! Local_date_time + date_duration
431    local_date_time_base operator+(const date_duration_type& dd) const
432    {
433      return local_date_time_base(time_system_type::add_days(this->time_,dd), zone_);
434    }
435    //! Local_date_time += date_duration
436    local_date_time_base operator+=(const date_duration_type& dd)
437    {
438      this->time_ = time_system_type::add_days(this->time_,dd);
439      return *this;
440    }
441    //! Local_date_time - date_duration
442    local_date_time_base operator-(const date_duration_type& dd) const
443    {
444      return local_date_time_base(time_system_type::subtract_days(this->time_,dd), zone_);
445    }
446    //! Local_date_time -= date_duration
447    local_date_time_base operator-=(const date_duration_type& dd)
448    {
449      this->time_ = time_system_type::subtract_days(this->time_,dd);
450      return *this;
451    }
452    //! Local_date_time + time_duration
453    local_date_time_base operator+(const time_duration_type& td) const
454    {
455      return local_date_time_base(time_system_type::add_time_duration(this->time_,td), zone_);
456    }
457    //! Local_date_time += time_duration
458    local_date_time_base operator+=(const time_duration_type& td)
459    {
460      this->time_ = time_system_type::add_time_duration(this->time_,td);
461      return *this;
462    }
463    //! Local_date_time - time_duration
464    local_date_time_base operator-(const time_duration_type& td) const
465    {
466      return local_date_time_base(time_system_type::subtract_time_duration(this->time_,td), zone_);
467    }
468    //! Local_date_time -= time_duration
469    local_date_time_base operator-=(const time_duration_type& td)
470    {
471      this->time_ = time_system_type::subtract_time_duration(this->time_,td);
472      return *this;
473    }
474    //! local_date_time -= local_date_time --> time_duration_type
475    time_duration_type operator-(const local_date_time_base& rhs) const
476    {
477      return utc_time_type(this->time_) - utc_time_type(rhs.time_);
478    }
479  private:
480    boost::shared_ptr<tz_type> zone_;
481    //bool is_dst_;
482
483    /*! Adjust the passed in time to UTC?
484     */
485    utc_time_type construction_adjustment(utc_time_type t,
486                                          boost::shared_ptr<tz_type> z,
487                                          bool dst_flag)
488    {
489      if(z != boost::shared_ptr<tz_type>()) {
490        if(dst_flag && z->has_dst()) {
491          t -= z->dst_offset();
492        } // else no adjust
493        t -= z->base_utc_offset();
494      }
495      return t;
496    }
497
498    /*! Simple formatting code -- todo remove this?
499     */
500    std::string zone_as_offset(const time_duration_type& td,
501                               const std::string& separator) const
502    {
503      std::ostringstream ss;
504      if(td.is_negative()) {
505        // a negative duration is represented as "-[h]h:mm"
506        // we require two digits for the hour. A positive duration
507        // with the %H flag will always give two digits
508        ss << "-";
509      }
510      else {
511        ss << "+";
512      }
513      ss  << std::setw(2) << std::setfill('0')
514          << date_time::absolute_value(td.hours())
515          << separator
516          << std::setw(2) << std::setfill('0')
517          << date_time::absolute_value(td.minutes());
518      return ss.str();
519    }
520  };
521
522  //!Use the default parameters to define local_date_time
523  typedef local_date_time_base<> local_date_time;
524
525} }
526
527
528#endif
Note: See TracBrowser for help on using the repository browser.