Disk ARchive  2.6.8
Full featured and portable backup and archiving tool
datetime.hpp
Go to the documentation of this file.
1 /*********************************************************************/
2 // dar - disk archive - a backup/restoration program
3 // Copyright (C) 2002-2020 Denis Corbin
4 //
5 // This program is free software; you can redistribute it and/or
6 // modify it under the terms of the GNU General Public License
7 // as published by the Free Software Foundation; either version 2
8 // of the License, or (at your option) any later version.
9 //
10 // This program is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU General Public License for more details.
14 //
15 // You should have received a copy of the GNU General Public License
16 // along with this program; if not, write to the Free Software
17 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 //
19 // to contact the author : http://dar.linux.free.fr/email.html
20 /*********************************************************************/
21 
25 
26 #ifndef DATETIME_HPP
27 #define DATETIME_HPP
28 
29 extern "C"
30 {
31 #if HAVE_SYS_TYPES_H
32 #include <sys/types.h>
33 #endif
34 
35 #if HAVE_UTIME_H
36 #include <utime.h>
37 #endif
38 
39 #if HAVE_SYS_TIME_H
40 #include <sys/time.h>
41 #endif
42 
43 } // end extern "C"
44 
45 #include "../my_config.h"
46 #include "infinint.hpp"
47 
48 namespace libdar
49 {
52 
54  class archive_version;
55  class generic_file;
56 
58  class datetime
59  {
60  public:
61  // time units must be sorted: the first is the smallest step, last is the largest increment.
62  // this makes the comparison operators (<, >, <=, >=,...) become naturally defined on that type
63  enum time_unit { tu_nanosecond, tu_microsecond, tu_second };
64 
66  datetime(const infinint & value = 0) { val = value; uni = tu_second; };
67 
69 
73  datetime(time_t second, time_t subsec, time_unit unit);
74 
77 
78  datetime(const datetime & ref) = default;
79  datetime(datetime && ref) noexcept = default;
80  datetime & operator = (const datetime & ref) = default;
81  datetime & operator = (datetime && ref) noexcept = default;
82  ~datetime() = default;
83 
84 
85  // comparison operators
86 
87  bool operator < (const datetime & ref) const;
88  bool operator == (const datetime & ref) const;
89  bool operator != (const datetime & ref) const { return ! (*this == ref); };
90  bool operator >= (const datetime & ref) const { return ! (*this < ref); };
91  bool operator > (const datetime & ref) const { return ref < *this; };
92  bool operator <= (const datetime & ref) const { return ref >= *this; };
93 
94  // arithmetic on time
95  void operator -= (const datetime & ref);
96  void operator += (const datetime & ref);
97  datetime operator - (const datetime & ref) const { datetime tmp(*this); tmp -= ref; return tmp; };
98  datetime operator + (const datetime & ref) const { datetime tmp(*this); tmp += ref; return tmp; };
99 
101  bool loose_equal(const datetime & ref) const;
102 
104  datetime loose_diff(const datetime & ref) const;
105 
107  infinint get_second_value() const { infinint sec, sub; get_value(sec, sub, uni); return sec; };
108 
110  infinint get_subsecond_value(time_unit unit) const;
111 
113  time_unit get_unit() const { return uni; };
114 
116 
121  bool get_value(time_t & second, time_t & subsecond, time_unit unit) const;
122 
123 
125  void dump(generic_file &x) const;
126 
128  void read(generic_file &f, archive_version ver);
129 
131  bool is_null() const { return val.is_zero(); };
132 
134  bool is_integer_second() const { return (uni == tu_second); };
135 
137  infinint get_storage_size() const;
138 
140  void nullify() { val = 0; uni = tu_second ; };
141 
142  private:
143  // the date must not be stored as a single integer
144  // to avoid reducing the possible addressable dates
145  // when compiling using 32 or 64 bits integer in place
146  // of infinint. The fraction cannot handle smaller unit
147  // than nanosecond if using 32 bits integer.
148 
149  infinint val; //< the date expressed in the "uni" time unit
150  time_unit uni; //< the time unit used to store the subsecond fraction of the timestamp.
151 
153  void reduce_to_largest_unit() const;
154  void get_value(infinint & sec, infinint & sub, time_unit unit) const;
155  void build(const infinint & sec, const infinint & sub, time_unit unit);
156 
157  static time_unit min(time_unit a, time_unit b);
158  static time_unit max(time_unit a, time_unit b);
159  static const char time_unit_to_char(time_unit a);
160  static time_unit char_to_time_unit(const char a);
161 
163 
166  static const infinint & get_scaling_factor(time_unit source, time_unit dest);
167 
168  };
169 
171  extern archive_version db2archive_version(unsigned char db_version);
172 
173 
175 
176 } // end of namespace
177 
178 #endif
libdar::infinint::is_zero
bool is_zero() const
libdar::datetime::nullify
void nullify()
set to null (zero)
Definition: datetime.hpp:140
libdar::datetime::dump
void dump(generic_file &x) const
write down this to file
libdar::datetime::get_second_value
infinint get_second_value() const
return the integer number of second
Definition: datetime.hpp:107
libdar::datetime::reduce_to_largest_unit
void reduce_to_largest_unit() const
reduce the value to the largest unit possible
infinint.hpp
switch module to limitint (32 ou 64 bits integers) or infinint
libdar::infinint
the arbitrary large positive integer class
Definition: real_infinint.hpp:61
libdar::datetime::get_unit
time_unit get_unit() const
returns the time unit used internally to store the subsecond time fraction
Definition: datetime.hpp:113
libdar::datetime::read
void read(generic_file &f, archive_version ver)
read this from file
libdar::archive_version
class archive_version manages the version of the archive format
Definition: archive_version.hpp:46
libdar::datetime::is_null
bool is_null() const
return true if the datetime is exactly January 1st, 1970, 0 h 0 mn 0 s
Definition: datetime.hpp:131
libdar::datetime::loose_equal
bool loose_equal(const datetime &ref) const
equivalent to operator == but if compared object use different time unit, do the comparison rounding ...
libdar::datetime::datetime
datetime(const infinint &value=0)
constructor based on the number of second ellasped since the end of 1969
Definition: datetime.hpp:66
libdar::datetime::is_integer_second
bool is_integer_second() const
return true if the datetime is an integer number of second (subsecond part is zero)
Definition: datetime.hpp:134
libdar::datetime
stores time information
Definition: datetime.hpp:58
libdar::datetime::get_value
bool get_value(time_t &second, time_t &subsecond, time_unit unit) const
return a time as time_t arguments
libdar::generic_file
this is the interface class from which all other data transfer classes inherit
Definition: generic_file.hpp:76
libdar::datetime::loose_diff
datetime loose_diff(const datetime &ref) const
at the difference of operator - provides the difference using the less precise unit used between the ...
libdar::datetime::get_storage_size
infinint get_storage_size() const
return the storage it would require to dump this object
libdar::datetime::get_subsecond_value
infinint get_subsecond_value(time_unit unit) const
return the subsecond time fraction expressed in the given time unit
libdar::datetime::get_scaling_factor
static const infinint & get_scaling_factor(time_unit source, time_unit dest)
return the factor between two units
libdar::db2archive_version
archive_version db2archive_version(unsigned char db_version)
converts dar_manager database version to dar archive version in order to properly read time fields
libdar
libdar namespace encapsulate all libdar symbols
Definition: archive.hpp:46