DateTime.h

Go to the documentation of this file.
00001 /*
00002  * The information in this file is
00003  * Copyright(c) 2007 Ball Aerospace & Technologies Corporation
00004  * and is subject to the terms and conditions of the
00005  * GNU Lesser General Public License Version 2.1
00006  * The license text is available from   
00007  * http://www.gnu.org/licenses/lgpl.html
00008  */
00009 
00010 #ifndef DATETIME_H
00011 #define DATETIME_H
00012 
00013 #include <time.h>
00014 #include <string>
00015 
00016 #include "DataVariantValidator.h"
00017 
00018 /**
00019  *  Specifies a date and a time.
00020  *
00021  *  Base class for date and time object, based upon the 
00022  *  capability provided by the standard C library's <time.h>.
00023  */
00024 class DateTime
00025 {
00026 public:
00027    /**
00028     *  Gets the time in local coordinates, formatted as a string.
00029     *
00030     *  @warning This method uses the C library to perform UTC to
00031     *  local time conversions and on some versions of Windows this
00032     *  can incorrectly calculate daylight savings time for
00033     *  historical dates (ie. dates prior to the 2007 Daylight
00034     *  Savings Time switch)
00035     *
00036     *  @param   fmt
00037     *           A format specification, consistent with that documented
00038     *           for strftime in <time.h>
00039     *  @return  The contents of this date/time object, formatted as 
00040     *           requested by the client into a string.
00041     */
00042    virtual std::string getFormattedLocal(const std::string& fmt) const = 0;
00043 
00044    /**
00045     *  Gets the time in UTC coordinates, formatted as a string.
00046     *
00047     *  @param   fmt
00048     *           A format specification, consistent with that documented
00049     *           for strftime in <time.h>
00050     *  @return  The contents of this date/time object, formatted as 
00051     *           requested by the client into a string.
00052     */
00053    virtual std::string getFormattedUtc(const std::string& fmt) const = 0;
00054 
00055    /**
00056     *  Gets the time value as a value maniuplated by C.
00057     *
00058     *  @return  The time_t value that can be passed to traditional
00059     *           C runtime library functions.
00060     */
00061    virtual time_t getStructured() const = 0;
00062 
00063    /**
00064     *  Gets the time elapsed between this object and the argument object.
00065     *
00066     *  @return  The difference, in seconds, between the two date/time values.
00067     */
00068    virtual double getSecondsSince(const DateTime& other) const = 0;
00069 
00070    /**
00071     *  Resets this object to have the current operating system date and time.
00072     */
00073    virtual void setToCurrentTime() = 0;
00074    
00075    /**
00076     *  Sets the time value as a value manipulated by C.
00077     *
00078     *  @param val
00079     *           The time_t value that can be passed to traditional
00080     *           C runtime library functions.
00081     */
00082    virtual void setStructured(time_t val) = 0;
00083 
00084    /**
00085     *  Sets the date and time to explicit client values.
00086     *
00087     *  @param   year
00088     *           The four-digit year. Note that years greater than 2047
00089     *           or less than 1900 are problematical with C.
00090     *  @param   month
00091     *           Starting from 1 for January, up to 12 for December.
00092     *  @param   day
00093     *           The day within the month, starting from 1.
00094     *  @param   hour
00095     *           The hour within the day, from 0 up to 23.
00096     *  @param   minute
00097     *           The minute within the hour, from 0 to 59.
00098     *  @param   second
00099     *           The seconds within the minute, from 0 to 59.
00100     *
00101     *  @return  A boolean indicating if the full set of values was
00102     *           an acceptable date time, including checks for leap
00103     *           years, month-day limits, and so on.
00104     */
00105    virtual bool set(unsigned short year, unsigned short month,
00106       unsigned short day, unsigned short hour,
00107       unsigned short minute, unsigned short second) = 0;
00108 
00109    /**
00110     *  Sets the date and time to explicit client values.
00111     *
00112     *  @param   year
00113     *           The four-digit year. Note that years greater than 2047
00114     *           or less than 1900 are problematical with C.
00115     *  @param   month
00116     *           Starting from 1 for January, up to 12 for December.
00117     *  @param   day
00118     *           The day within the month, starting from 1.
00119     *
00120     *  @return  A boolean indicating if the full set of values was
00121     *           an acceptable date time, including checks for leap
00122     *           years, month-day limits, and so on.
00123     */
00124    virtual bool set(unsigned short year = 2000, unsigned short month = 01,
00125       unsigned short day = 01) = 0;
00126 
00127    /**
00128     *  Set the date and time from a specially formatted string.
00129     *
00130     *  @param   fromTime
00131     *           A string in a sub-set of ISO8601 format. No validation is performed
00132     *           on this string. This function will also accept strings that
00133     *           omit the time information, ie. from the 'T' onward.
00134     *           The lexical description is:
00135     *           yyyy '-' mm '-' dd 'T' hh ':' MM ':' ss 'Z'
00136     *           where:
00137     *              strings in '' are literal characters
00138     *              yyyy is a four-or-more digit numeral representing the year.
00139     *                   0000 is not valid and this function does not accept
00140     *                   negative years even though they are part of ISO8601
00141     *              mm is a two-digit numeral representing the month
00142     *              dd is a two-digit numeral representing the day
00143     *              hh is a two-digit numeral representing the hour. 24 is permitted if
00144     *                   hours and minutes are 00 and 00
00145     *              MM is a two-digit numeral representing the minute
00146     *              ss is a two-digit numeral representing the whole second
00147     *
00148     *  @return  true if successfull, false otherwise
00149     */
00150    virtual bool set(const std::string &fromTime) = 0;
00151 
00152    /**
00153     *  Queries whether the time component is valid.
00154     *
00155     *  The time component of the DateTime object will only be invalid if the
00156     *  date was most recently set with the set method that only takes the date.
00157     *
00158     *  @return  True if the time component is valid, otherwise false.
00159     *
00160     *  @see     set()
00161     */
00162    virtual bool isTimeValid() const = 0;
00163 
00164    /**
00165     *  Queries whether the DateTime is valid.
00166     *
00167     *  @return  True if the DateTime is valid, otherwise false.
00168     *
00169     *  @see     set()
00170     */
00171    virtual bool isValid() const = 0;
00172 
00173 protected:
00174    /**
00175     * This should be destroyed by calling ObjectFactory::destroyObject.
00176     */
00177    virtual ~DateTime() {}
00178 };
00179 
00180 /**
00181  * \cond INTERNAL
00182  * These template specialization are required to allow these types to be put into a DataVariant.
00183  */
00184 template <> class VariantTypeValidator<DateTime> {};
00185 template <> class VariantTypeValidator<const DateTime> {};
00186 /// \endcond
00187 
00188 #endif

Software Development Kit - Opticks 4.9.0 Build 16218