Disk ARchive: datetime.hpp Source File

| Disk ARchive 2.8.1 Full featured and portable backup and archiving tool

Loading...
Searching...
No Matches
datetime.hpp
Go to the documentation of this file.
1/*********************************************************************/
2// dar - disk archive - a backup/restoration program
3// Copyright (C) 2002-2025 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, see the AUTHOR file
20/*********************************************************************/
21
25
26#ifndef DATETIME_HPP
27#define DATETIME_HPP
28
29extern "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
48namespace 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
76 datetime(generic_file &x, archive_version ver);
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
143 static std::string unit_symbol(time_unit tu);
144
145 private:
146 // the date must not be stored as a single integer
147 // to avoid reducing the possible addressable dates
148 // when compiling using 32 or 64 bits integer in place
149 // of infinint. The fraction cannot handle smaller unit
150 // than nanosecond if using 32 bits integer.
151
152 infinint val;
153 time_unit uni;
154
156 void reduce_to_largest_unit() const;
157 void get_value(infinint & sec, infinint & sub, time_unit unit) const;
158 void build(const infinint & sec, const infinint & sub, time_unit unit);
159
160 static time_unit min(time_unit a, time_unit b);
161 static time_unit max(time_unit a, time_unit b);
162 static char time_unit_to_char(time_unit a);
163 static time_unit char_to_time_unit(const char a);
164
166
169 static const infinint & get_scaling_factor(time_unit source, time_unit dest);
170
171 };
172
174 extern archive_version db2archive_version(unsigned char db_version);
175
176
178
179} // end of namespace
180
181#endif
libdar::archive_version
class archive_version manages the version of the archive format
Definition archive_version.hpp:47
libdar::datetime
stores time information
Definition datetime.hpp:59
libdar::datetime::uni
time_unit uni
the time unit used to store the subsecond fraction of the timestamp.
Definition datetime.hpp:153
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::get_value
bool get_value(time_t &second, time_t &subsecond, time_unit unit) const
return a time as time_t arguments
libdar::datetime::dump
void dump(generic_file &x) const
write down this to file
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::datetime::read
void read(generic_file &f, archive_version ver)
read this from file
libdar::datetime::get_storage_size
infinint get_storage_size() const
return the storage it would require to dump this object
libdar::datetime::datetime
datetime(generic_file &x, archive_version ver)
constructor reading data dump() into a generic_file
libdar::datetime::val
infinint val
the date expressed in the "uni" time unit
Definition datetime.hpp:152
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::nullify
void nullify()
set to null (zero)
Definition datetime.hpp:140
libdar::datetime::reduce_to_largest_unit
void reduce_to_largest_unit() const
reduce the value to the largest unit possible
libdar::datetime::datetime
datetime(time_t second, time_t subsec, time_unit unit)
general constructor
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::unit_symbol
static std::string unit_symbol(time_unit tu)
return the SI symbol of the time unit (s, ms, us (should be mu greek letter), ns)
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::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::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::get_second_value
infinint get_second_value() const
return the integer number of second
Definition datetime.hpp:107
libdar::generic_file
this is the interface class from which all other data transfer classes inherit
Definition generic_file.hpp:77
libdar::infinint
the arbitrary large positive integer class
Definition real_infinint.hpp:62
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
infinint.hpp
switch module to limitint (32 ou 64 bits integers) or infinint
my_config.h
include macro defined by the configure script and some specific additional ones
libdar
libdar namespace encapsulate all libdar symbols
Definition archive.hpp:47