Disk ARchive  2.5.2
Full featured and portable backup and archiving tool
datetime.hpp
Go to the documentation of this file.
00001 /*********************************************************************/
00002 // dar - disk archive - a backup/restoration program
00003 // Copyright (C) 2002-2052 Denis Corbin
00004 //
00005 // This program is free software; you can redistribute it and/or
00006 // modify it under the terms of the GNU General Public License
00007 // as published by the Free Software Foundation; either version 2
00008 // of the License, or (at your option) any later version.
00009 //
00010 // This program is distributed in the hope that it will be useful,
00011 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00012 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00013 // GNU General Public License for more details.
00014 //
00015 // You should have received a copy of the GNU General Public License
00016 // along with this program; if not, write to the Free Software
00017 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
00018 //
00019 // to contact the author : http://dar.linux.free.fr/email.html
00020 /*********************************************************************/
00021 
00025 
00026 #ifndef DATETIME_HPP
00027 #define DATETIME_HPP
00028 
00029 extern "C"
00030 {
00031 #if HAVE_SYS_TYPES_H
00032 #include <sys/types.h>
00033 #endif
00034 
00035 #if HAVE_UTIME_H
00036 #include <utime.h>
00037 #endif
00038 
00039 #if HAVE_SYS_TIME_H
00040 #include <sys/time.h>
00041 #endif
00042 
00043 } // end extern "C"
00044 
00045 #include "../my_config.h"
00046 #include "on_pool.hpp"
00047 #include "infinint.hpp"
00048 #include "archive_version.hpp"
00049 
00050 namespace libdar
00051 {
00052     class archive; // needed to be able to use pointer on archive object.
00053 
00056 
00057     class datetime : public on_pool
00058     {
00059     public:
00060         // time units must be sorted: the first is the smallest step, last is the largest increment.
00061         // this makes the comparison operators (<, >, <=, >=,...) become naturally defined on that type
00062     enum time_unit { tu_nanosecond, tu_microsecond, tu_second };
00063 
00065     datetime(const infinint & value = 0) { sec = value; frac = 0; uni = tu_second; };
00066 
00072     datetime(time_t second, time_t subsec, time_unit unit) { sec = second; frac = subsec; uni = unit; if(uni == tu_second && subsec != 0) throw SRC_BUG; };
00073 
00075     datetime(generic_file &x, archive_version ver) { read(x, ver); };
00076 
00077         // comparison operators
00078 
00079     bool operator < (const datetime & ref) const;
00080     bool operator == (const datetime & ref) const;
00081     bool operator != (const datetime & ref) const { return ! (*this == ref); };
00082     bool operator >= (const datetime & ref) const { return ! (*this < ref); };
00083     bool operator > (const datetime & ref) const { return ref < *this; };
00084     bool operator <= (const datetime & ref) const { return ref >= *this; };
00085 
00086         // arithmetic on time
00087     datetime operator - (const datetime & ref) const;
00088     datetime operator + (const datetime & ref) const;
00089 
00091     datetime loose_diff(const datetime & ref) const;
00092 
00094     infinint get_second_value() const { return sec; };
00095 
00097     infinint get_subsecond_value(time_unit unit) const;
00098 
00100     time_unit get_unit() const { return uni; };
00101 
00108     bool get_value(time_t & second, time_t & subsecond, time_unit unit) const;
00109 
00110 
00112     void dump(generic_file &x) const;
00113 
00115     void read(generic_file &f, archive_version ver);
00116 
00118     bool is_null() const { return sec.is_zero() && frac.is_zero(); };
00119 
00121     bool is_integer_second() const { return frac.is_zero(); };
00122 
00124     infinint get_storage_size() const { return sec.get_storage_size() + frac.get_storage_size() + 1; };
00125 
00126     private:
00127         // the date must not be stored as a single integer
00128         // to avoid reducing the possible addressable dates
00129         // when compiling using  32 or 64 bits integer in place
00130         // of infinint. The fraction cannot handle smaller unit
00131         // than nanosecond if using 32 bits integer.
00132 
00133     infinint sec;  //< the date as number of second ellapsed since 1969
00134     infinint frac; //< the fraction of the date expressed in the unit defined by the "uni" field
00135     time_unit uni; //< the time unit used to store the subsecond fraction of the timestamp.
00136 
00138     void reduce_to_largest_unit() const;
00139 
00148     bool is_subsecond_an_integer_value_of(time_unit target, infinint & newval) const;
00149 
00150 
00151     static time_unit min(time_unit a, time_unit b);
00152     static time_unit max(time_unit a, time_unit b);
00153     static const char time_unit_to_char(time_unit a);
00154     static time_unit char_to_time_unit(const char a);
00155 
00160     static const infinint & get_scaling_factor(time_unit source, time_unit dest);
00161 
00163     static infinint how_much_to_make_1_second(time_unit unit);
00164     };
00165 
00167     extern archive_version db2archive_version(unsigned char db_version);
00168 
00169 
00171 
00172 } // end of namespace
00173 
00174 #endif
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Defines