blocxx
DateTime.hpp
Go to the documentation of this file.
1 /*******************************************************************************
2 * Copyright (C) 2005, Vintela, Inc. All rights reserved.
3 * Copyright (C) 2006, Novell, Inc. All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
7 *
8 * * Redistributions of source code must retain the above copyright notice,
9 * this list of conditions and the following disclaimer.
10 * * Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * * Neither the name of
14 * Vintela, Inc.,
15 * nor Novell, Inc.,
16 * nor the names of its contributors or employees may be used to
17 * endorse or promote products derived from this software without
18 * specific prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
24 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30 * POSSIBILITY OF SUCH DAMAGE.
31 *******************************************************************************/
32 
33 
39 #ifndef BLOCXX_DATETIME_HPP_INCLUDE_GUARD_
40 #define BLOCXX_DATETIME_HPP_INCLUDE_GUARD_
41 #include "blocxx/BLOCXX_config.h"
42 #include "blocxx/Exception.hpp"
43 #include "blocxx/Types.hpp"
44 #include "blocxx/CommonFwd.hpp"
45 
46 extern "C"
47 {
48 #include <time.h>
49 }
50 
51 namespace BLOCXX_NAMESPACE
52 {
53 
55 
56 
80 class BLOCXX_COMMON_API DateTime
81 {
82 public:
91  {
93  E_UTC_TIME
94  };
95 
100  DateTime();
178  explicit DateTime(const String& str);
189  explicit DateTime(time_t t, UInt32 microseconds=0);
205  DateTime(
206  int year,
207  int month,
208  int day,
209  int hour=0,
210  int minute=0,
211  int second=0,
212  UInt32 microsecond=0,
213  ETimeOffset timeOffset = E_LOCAL_TIME);
217  ~DateTime();
224  int getHour(ETimeOffset timeOffset = E_LOCAL_TIME) const;
231  int getMinute(ETimeOffset timeOffset = E_LOCAL_TIME) const;
239  int getSecond(ETimeOffset timeOffset = E_LOCAL_TIME) const;
247  UInt32 getMicrosecond() const;
254  int getDay(ETimeOffset timeOffset = E_LOCAL_TIME) const;
260  int getDow(ETimeOffset timeOffset = E_LOCAL_TIME) const;
265  int getMonth(ETimeOffset timeOffset = E_LOCAL_TIME) const;
270  int getYear(ETimeOffset timeOffset = E_LOCAL_TIME) const;
274  time_t get() const;
282  void setHour(int hour, ETimeOffset timeOffset = E_LOCAL_TIME);
290  void setMinute(int minute, ETimeOffset timeOffset = E_LOCAL_TIME);
298  void setSecond(int second, ETimeOffset timeOffset = E_LOCAL_TIME);
305  void setMicrosecond(UInt32 microsecond);
315  void setTime(
316  int hour,
317  int minute,
318  int second,
319  ETimeOffset timeOffset = E_LOCAL_TIME);
327  void setDay(int day, ETimeOffset timeOffset = E_LOCAL_TIME);
336  void setMonth(int month, ETimeOffset timeOffset = E_LOCAL_TIME);
345  void setYear(int year, ETimeOffset timeOffset = E_LOCAL_TIME);
355  void set(time_t t, UInt32 microseconds=0);
371  void set(
372  int year,
373  int month,
374  int day,
375  int hour,
376  int minute,
377  int second,
378  UInt32 microseconds,
379  ETimeOffset timeOffset = E_LOCAL_TIME);
383  void setToCurrent();
390  void addDays(int days);
397  void addWeeks(int weeks)
398  {
399  addDays(weeks * 7);
400  }
407  void addMonths(int months);
414  void addYears(int years);
421  void addSeconds(long seconds)
422  {
423  m_time += seconds;
424  }
429  void addMinutes(long minutes)
430  {
431  m_time += minutes * 60;
432  }
437  void addMicroseconds(long microseconds)
438  {
439  m_microseconds += microseconds;
440  m_time += m_microseconds / 1000000;
441  m_microseconds %= 1000000;
442  }
447  void addMilliseconds(long milliseconds)
448  {
449  this->addMicroseconds(milliseconds * 1000);
450  }
455  void addHours(long hours) { m_time += hours * 60 * 60; }
461  bool operator< ( const DateTime& tm ) const
462  {
463  if (m_time == tm.m_time)
464  {
465  return m_microseconds < tm.m_microseconds;
466  }
467  return m_time < tm.m_time;
468  }
475  bool operator> ( const DateTime& tm ) const
476  {
477  return tm < *this;
478  }
484  bool operator== ( const DateTime& tm ) const
485  {
486  return m_time == tm.m_time && m_microseconds == tm.m_microseconds;
487  }
493  bool operator!= ( const DateTime& tm ) const
494  {
495  return !(*this == tm);
496  }
503  bool operator<= ( const DateTime& tm ) const
504  {
505  return !(tm < *this);
506  }
513  bool operator>= ( const DateTime& tm ) const
514  {
515  return !(*this < tm);
516  }
522  DateTime& operator+= (long seconds)
523  {
524  addSeconds(seconds);
525  return *this;
526  }
532  DateTime& operator-= (long seconds)
533  {
534  addSeconds(-seconds);
535  return *this;
536  }
537 
543  String toString(ETimeOffset timeOffset = E_LOCAL_TIME) const;
544 
555  String toString(
556  char const * format, ETimeOffset timeOffset = E_LOCAL_TIME) const;
557 
563  static char const DEFAULT_FORMAT[];
564 
569  String toStringGMT() const BLOCXX_DEPRECATED; // in 3.0.0
570 
571 #if 0
572 
578  static Int16 getGMTOffset();
579  // Removed due to the above problems. Use toLocal() or
580  // getGMTOffsetMinutesNow() instead.
581 #endif
582 
588  static Int16 getGMTOffsetMinutesNow()
589  {
590  time_t t = time(0);
591  struct tm tt;
592  return DateTime::localTimeAndOffset(t, tt);
593  }
594 
601  Int16 toLocal(struct tm & tt) const
602  {
603  return DateTime::localTimeAndOffset(m_time, tt);
604  }
605 
609  static DateTime getCurrent();
610 
611 private:
612  time_t m_time;
614  tm getTm(ETimeOffset timeOffset) const;
615  void setTime(tm& tmarg, ETimeOffset timeOffset);
616  static Int16 localTimeAndOffset(time_t t, struct tm & tt);
617 };
618 
624 BLOCXX_COMMON_API DateTime operator-(DateTime const & x, DateTime const & y);
625 
626 } // end namespace BLOCXX_NAMESPACE
627 
628 #endif
629