datetime.hpp
Go to the documentation of this file.
/*********************************************************************/
// dar - disk archive - a backup/restoration program
// Copyright (C) 2002-2023 Denis Corbin
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation; either version 2
// of the License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
//
// to contact the author, see the AUTHOR file
/*********************************************************************/
#ifndef DATETIME_HPP
#define DATETIME_HPP
extern "C"
{
#if HAVE_SYS_TYPES_H
#include <sys/types.h>
#endif
#if HAVE_UTIME_H
#include <utime.h>
#endif
#if HAVE_SYS_TIME_H
#include <sys/time.h>
#endif
} // end extern "C"
#include "../my_config.h"
#include "infinint.hpp"
namespace libdar
{
class archive_version;
class generic_file;
class datetime
{
public:
// time units must be sorted: the first is the smallest step, last is the largest increment.
// this makes the comparison operators (<, >, <=, >=,...) become naturally defined on that type
enum time_unit { tu_nanosecond, tu_microsecond, tu_second };
datetime(const infinint & value = 0) { val = value; uni = tu_second; };
datetime(time_t second, time_t subsec, time_unit unit);
datetime(generic_file &x, archive_version ver);
datetime(const datetime & ref) = default;
datetime(datetime && ref) noexcept = default;
datetime & operator = (const datetime & ref) = default;
datetime & operator = (datetime && ref) noexcept = default;
~datetime() = default;
// comparison operators
bool operator < (const datetime & ref) const;
bool operator == (const datetime & ref) const;
bool operator != (const datetime & ref) const { return ! (*this == ref); };
bool operator >= (const datetime & ref) const { return ! (*this < ref); };
bool operator > (const datetime & ref) const { return ref < *this; };
bool operator <= (const datetime & ref) const { return ref >= *this; };
// arithmetic on time
void operator -= (const datetime & ref);
void operator += (const datetime & ref);
datetime operator - (const datetime & ref) const { datetime tmp(*this); tmp -= ref; return tmp; };
datetime operator + (const datetime & ref) const { datetime tmp(*this); tmp += ref; return tmp; };
bool loose_equal(const datetime & ref) const;
datetime loose_diff(const datetime & ref) const;
infinint get_second_value() const { infinint sec, sub; get_value(sec, sub, uni); return sec; };
infinint get_subsecond_value(time_unit unit) const;
time_unit get_unit() const { return uni; };
bool get_value(time_t & second, time_t & subsecond, time_unit unit) const;
void dump(generic_file &x) const;
void read(generic_file &f, archive_version ver);
bool is_null() const { return val.is_zero(); };
bool is_integer_second() const { return (uni == tu_second); };
infinint get_storage_size() const;
void nullify() { val = 0; uni = tu_second ; };
private:
// the date must not be stored as a single integer
// to avoid reducing the possible addressable dates
// when compiling using 32 or 64 bits integer in place
// of infinint. The fraction cannot handle smaller unit
// than nanosecond if using 32 bits integer.
infinint val; //< the date expressed in the "uni" time unit
time_unit uni; //< the time unit used to store the subsecond fraction of the timestamp.
void reduce_to_largest_unit() const;
void get_value(infinint & sec, infinint & sub, time_unit unit) const;
void build(const infinint & sec, const infinint & sub, time_unit unit);
static time_unit min(time_unit a, time_unit b);
static time_unit max(time_unit a, time_unit b);
static const char time_unit_to_char(time_unit a);
static time_unit char_to_time_unit(const char a);
static const infinint & get_scaling_factor(time_unit source, time_unit dest);
};
extern archive_version db2archive_version(unsigned char db_version);
} // end of namespace
#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::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::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::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::infinint::is_zero
bool is_zero() const
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
libdar
libdar namespace encapsulate all libdar symbols
Definition: archive.hpp:47