Google APIs Client Library for C++
date_time.h
Go to the documentation of this file.
00001 /*
00002  * \copyright Copyright 2013 Google Inc. All Rights Reserved.
00003  * \license @{
00004  *
00005  * Licensed under the Apache License, Version 2.0 (the "License");
00006  * you may not use this file except in compliance with the License.
00007  * You may obtain a copy of the License at
00008  *
00009  *     http://www.apache.org/licenses/LICENSE-2.0
00010  *
00011  * Unless required by applicable law or agreed to in writing, software
00012  * distributed under the License is distributed on an "AS IS" BASIS,
00013  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00014  * See the License for the specific language governing permissions and
00015  * limitations under the License.
00016  *
00017  * @}
00018  */
00019 
00020 
00021 #ifndef APISERVING_CLIENTS_CPP_UTIL_DATE_TIME_H_
00022 #define APISERVING_CLIENTS_CPP_UTIL_DATE_TIME_H_
00023 
00024 #include <time.h>
00025 #ifndef _MSC_VER
00026 #include <sys/time.h>
00027 #else
00028 #include <stdlib.h>
00029 #endif
00030 #include <string>
00031 using std::string;
00032 
00033 #include "googleapis/base/integral_types.h"
00034 #include "googleapis/base/port.h"
00035 namespace googleapis {
00036 #ifdef _MSC_VER
00037 inline int localtime_r(const long* tv_secs, struct tm* out) {
00038   time_t secs = *tv_secs;
00039   return _localtime64_s(out, &secs);
00040 }
00041 
00042 inline void gmtime_r(const long* secs, struct tm* out) {
00043   time_t timer = *secs;
00044   gmtime_s(out, &timer);
00045 }
00046 #endif
00047 
00048 namespace client {
00049 
00050 /*
00051  * Represents a date convertable among various standard
00052  * date represenations including RFC 3339 used by JSON encodings.
00053  * @ingroup PlatformLayer
00054  */
00055 class DateTime {
00056  public:
00057   /*
00058    * Construct a date from a UTC struct tm.
00059    * @return An invalid date if the specified time is not valid.
00060    *
00061    * @see DateTimeFromLocal
00062    */
00063   static DateTime DateTimeFromUtc(const struct tm& utc);
00064 
00065   /*
00066    * Construct a date from a local-time struct tm.
00067    * @return An invalid date if the specified time is not valid.
00068    *
00069    * @see DateTimeFromUTC
00070    */
00071   static DateTime DateTimeFromLocal(const struct tm& local) {
00072     return DateTime(local);
00073   }
00074 
00075   /*
00076    * Construct a date with the current time.
00077    */
00078   DateTime();
00079 
00080   /*
00081    * Construct a date from an RFC 3339 formatted string.
00082    */
00083   explicit DateTime(const string& date);
00084 
00085   /*
00086    * Construct a date from an epoch time.
00087    */
00088   explicit DateTime(time_t time);
00089 
00090   /*
00091    * Construct a date from a timeval.
00092    *
00093    * @param[in] time Can contain fractional seconds.
00094    */
00095   explicit DateTime(const struct timeval& time);
00096 
00097   /*
00098    * Copy constructor.
00099    */
00100   DateTime(const DateTime& date);
00101 
00102   /*
00103    * Standard destructor.
00104    */
00105   ~DateTime();
00106 
00107   /*
00108    * Convert the data to an RFC 3330 encoded string.
00109    */
00110   string ToString() const;
00111 
00112   /*
00113    * Convert the date to local time.
00114    */
00115   void GetLocalTime(struct tm* out) const { localtime_r(&t_.tv_sec, out); }
00116 
00117   /*
00118    * Convert the date to universal time.
00119    */
00120   void GetUniversalTime(struct tm* out) const { gmtime_r(&t_.tv_sec, out); }
00121 
00122   void GetTimeval(struct timeval* timeval) const { *timeval = t_; }
00123 
00124   /*
00125    * Convert the date to epoch time.
00126    */
00127   time_t ToEpochTime() const { return t_.tv_sec; }
00128 
00129   /*
00130    * Determine if we have a valid date or not.
00131    */
00132   bool is_valid() const { return t_.tv_sec != kInvalidEpoch_; }
00133 
00134   /*
00135    * Determine relative ordering of this date relative to another.
00136    * @param[in]  date The date to compare against
00137    * @return < 0 if this date is earlier, > 0 if this date is later, or 0
00138    *          if the dates are equal.
00139    */
00140   int Compare(const DateTime& date) const {
00141     return t_.tv_sec == date.t_.tv_sec
00142         ? t_.tv_usec - date.t_.tv_usec
00143         : t_.tv_sec - date.t_.tv_sec;
00144   }
00145 
00146   /*
00147    * Determine if this date is equal to another.
00148    */
00149   bool operator ==(const DateTime& date) const {
00150     return Compare(date) == 0 && is_valid();
00151   }
00152 
00153   /*
00154    * Determine if this date is earlier than another.
00155    */
00156   bool operator <(const DateTime& date) const {
00157     return Compare(date) < 0 && is_valid();
00158   }
00159 
00160   /*
00161    * Determine if this date is later than another.
00162    */
00163   bool operator >(const DateTime& date) const {
00164     return Compare(date) > 0 && date.is_valid();
00165   }
00166 
00167   /*
00168    * Determine if this date is different from another.
00169    */
00170   bool operator !=(const DateTime& date) const {
00171     return Compare(date) != 0 && is_valid();
00172   }
00173 
00174   /*
00175    * Determine if this earlier or equal to another.
00176    */
00177   bool operator <=(const DateTime& date) const {
00178     return Compare(date) <= 0 && is_valid();
00179   }
00180 
00181   /*
00182    * Determine if this later or equal to another.
00183    */
00184   bool operator >=(const DateTime& date) const {
00185     return Compare(date) >= 0 && date.is_valid();
00186   }
00187 
00188   /*
00189    * Reasign this date to another.
00190    */
00191   DateTime& operator=(const DateTime& date) {
00192     t_ = date.t_;
00193     return *this;
00194   }
00195 
00196  protected:
00197   struct timeval t_;
00198   static const time_t kInvalidEpoch_;
00199 
00200   /*
00201    * Marks this date as being invalid.
00202    */
00203   void MarkInvalid();
00204 
00205   /*
00206    * Construct the data from a struct tm that is in local time.
00207    *
00208    * This constructir is exposed through the public factory methods
00209    * the struct tm is in.
00210    */
00211   explicit DateTime(const struct tm& local);
00212 };
00213 
00214 }  // namespace client
00215 
00216 } // namespace googleapis
00217 #endif  // APISERVING_CLIENTS_CPP_UTIL_DATE_TIME_H_
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines