LibreOffice Module tools (master) 1
datetime.hxx
Go to the documentation of this file.
1/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2/*
3 * This file is part of the LibreOffice project.
4 *
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 *
9 * This file incorporates work covered by the following license notice:
10 *
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
18 */
19#ifndef INCLUDED_TOOLS_DATETIME_HXX
20#define INCLUDED_TOOLS_DATETIME_HXX
21
22#include <tools/toolsdllapi.h>
23#include <tools/date.hxx>
24#include <tools/time.hxx>
25#include <com/sun/star/util/DateTime.hpp>
26
27#include <iomanip>
28
29namespace tools
30{
31class Duration;
32}
33
35{
36public:
38 {
39 SYSTEM
40 };
41
43 {
44 EMPTY
45 };
46
48 explicit DateTime( DateTimeInitSystem );
49 DateTime( const DateTime& rDateTime ) :
50 Date( rDateTime ), Time( rDateTime ) {}
51 DateTime( const Date& rDate ) : Date( rDate ), Time(0) {}
52 DateTime( const tools::Time& rTime ) : Date(0), Time( rTime ) {}
53 DateTime( const Date& rDate, const tools::Time& rTime ) :
54 Date( rDate ), Time( rTime ) {}
55 DateTime( const css::util::DateTime& rDateTime );
56
57 css::util::DateTime
59 { return css::util::DateTime(GetNanoSec(), GetSec(), GetMin(), GetHour(),
60 GetDay(), GetMonth(), GetYear(), false); }
61
62 bool IsBetween( const DateTime& rFrom,
63 const DateTime& rTo ) const;
64
65 bool IsEqualIgnoreNanoSec( const DateTime& rDateTime ) const
66 {
67 if ( Date::operator!=( rDateTime ) )
68 return false;
69 return Time::IsEqualIgnoreNanoSec( rDateTime );
70 }
71
72 bool operator ==( const DateTime& rDateTime ) const
73 { return (Date::operator==( rDateTime ) &&
74 Time::operator==( rDateTime )); }
75 bool operator !=( const DateTime& rDateTime ) const
76 { return (Date::operator!=( rDateTime ) ||
77 Time::operator!=( rDateTime )); }
78 bool operator >( const DateTime& rDateTime ) const;
79 bool operator <( const DateTime& rDateTime ) const;
80 bool operator >=( const DateTime& rDateTime ) const;
81 bool operator <=( const DateTime& rDateTime ) const;
82
83 sal_Int64 GetSecFromDateTime( const Date& rDate ) const;
84
85 void ConvertToUTC() { *this -= Time::GetUTCOffset(); }
87
88 void AddTime( double fTimeInDays );
89 DateTime& operator +=( const tools::Time& rTime );
90 DateTime& operator -=( const tools::Time& rTime );
92 DateTime& operator +=( const tools::Duration& rDuration );
93private:
94 void NormalizeTimeRemainderAndApply( tools::Time& rTime );
95public:
96
97 TOOLS_DLLPUBLIC friend DateTime operator +( const DateTime& rDateTime, sal_Int32 nDays );
98 TOOLS_DLLPUBLIC friend DateTime operator -( const DateTime& rDateTime, sal_Int32 nDays );
99 TOOLS_DLLPUBLIC friend DateTime operator +( const DateTime& rDateTime, double fTimeInDays );
100 TOOLS_DLLPUBLIC friend DateTime operator -( const DateTime& rDateTime, double fTimeInDays )
101 { return operator+( rDateTime, -fTimeInDays ); }
102 TOOLS_DLLPUBLIC friend DateTime operator +( const DateTime& rDateTime, const tools::Time& rTime );
103 TOOLS_DLLPUBLIC friend DateTime operator -( const DateTime& rDateTime, const tools::Time& rTime );
105 TOOLS_DLLPUBLIC friend tools::Duration operator -( const DateTime& rDateTime1, const DateTime& rDateTime2 );
112 static double Sub( const DateTime& rDateTime1, const DateTime& rDateTime2 );
113 TOOLS_DLLPUBLIC friend sal_Int64 operator -( const DateTime& rDateTime, const Date& rDate )
114 { return static_cast<const Date&>(rDateTime) - rDate; }
116 TOOLS_DLLPUBLIC friend DateTime operator +( const DateTime& rDateTime, const tools::Duration& rDuration );
117
118 DateTime& operator =( const DateTime& rDateTime );
119 DateTime& operator =( const css::util::DateTime& rUDateTime );
120
121 void GetWin32FileDateTime( sal_uInt32 & rLower, sal_uInt32 & rUpper ) const;
122 static DateTime CreateFromWin32FileDateTime( sal_uInt32 rLower, sal_uInt32 rUpper );
123
126 static DateTime CreateFromUnixTime( const double fSecondsSinceEpoch );
127};
128
129inline DateTime& DateTime::operator =( const DateTime& rDateTime )
130{
131 Date::operator=( rDateTime );
132 Time::operator=( rDateTime );
133 return *this;
134}
135
136template< typename charT, typename traits >
137inline std::basic_ostream<charT, traits> & operator <<(
138 std::basic_ostream<charT, traits> & stream, const DateTime& datetime)
139{
140 return stream << datetime.GetYear() << '-' <<
141 std::setw(2) << std::setfill('0') << datetime.GetMonth() << '-' <<
142 std::setw(2) << std::setfill('0') << datetime.GetDay() << ' ' <<
143 std::setw(2) << std::setfill('0') << datetime.GetHour() << ':' <<
144 std::setw(2) << std::setfill('0') << datetime.GetMin() << ':' <<
145 std::setw(2) << std::setfill('0') << datetime.GetSec() << "." <<
146 std::setw(9) << std::setfill('0') << datetime.GetNanoSec();
147}
148
149#endif
150
151/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
void ConvertToUTC()
Definition: datetime.hxx:85
DateTimeInitEmpty
Definition: datetime.hxx:43
DateTime(DateTimeInitEmpty)
Definition: datetime.hxx:47
DateTime(const DateTime &rDateTime)
Definition: datetime.hxx:49
DateTime(const Date &rDate)
Definition: datetime.hxx:51
DateTime(const tools::Time &rTime)
Definition: datetime.hxx:52
DateTimeInitSystem
Definition: datetime.hxx:38
void ConvertToLocalTime()
Definition: datetime.hxx:86
css::util::DateTime GetUNODateTime() const
Definition: datetime.hxx:58
DateTime & operator=(const DateTime &rDateTime)
Definition: datetime.hxx:129
bool IsEqualIgnoreNanoSec(const DateTime &rDateTime) const
Definition: datetime.hxx:65
DateTime(const Date &rDate, const tools::Time &rTime)
Definition: datetime.hxx:53
Represents a date in the proleptic Gregorian calendar.
Definition: date.hxx:55
TOOLS_DLLPUBLIC friend Date operator+(const Date &rDate, sal_Int32 nDays)
Definition: tdate.cxx:393
Date & operator=(const Date &rDate)
Definition: date.hxx:223
bool IsBetween(const Date &rFrom, const Date &rTo) const
Definition: date.hxx:206
sal_Int16 GetYear() const
Definition: date.hxx:113
sal_uInt16 GetDay() const
Definition: date.hxx:101
bool operator>(const Date &rDate) const
Definition: date.hxx:214
bool operator==(const Date &rDate) const
Definition: date.hxx:210
bool operator>=(const Date &rDate) const
Definition: date.hxx:218
TOOLS_DLLPUBLIC friend Date operator-(const Date &rDate, sal_Int32 nDays)
Definition: tdate.cxx:400
bool operator<(const Date &rDate) const
Definition: date.hxx:216
bool operator<=(const Date &rDate) const
Definition: date.hxx:220
bool operator!=(const Date &rDate) const
Definition: date.hxx:212
sal_uInt16 GetMonth() const
Definition: date.hxx:107
Duration in days and time.
Definition: duration.hxx:22
sal_uInt16 GetSec() const
Definition: time.hxx:91
tools::Time & operator-=(const tools::Time &rTime)
Definition: ttime.cxx:370
sal_uInt16 GetMin() const
Definition: time.hxx:88
static Time GetUTCOffset()
Definition: ttime.cxx:396
sal_uInt16 GetHour() const
Definition: time.hxx:85
sal_uInt32 GetNanoSec() const
Definition: time.hxx:94
bool IsEqualIgnoreNanoSec(const tools::Time &rTime) const
Definition: ttime.cxx:389
std::basic_ostream< charT, traits > & operator<<(std::basic_ostream< charT, traits > &stream, const DateTime &datetime)
Definition: datetime.hxx:137
Reference< XOutputStream > stream
constexpr OUStringLiteral EMPTY
Note: this class is a true marvel of engineering: because the author could not decide whether it's be...
tools::Rectangle & operator+=(tools::Rectangle &rRect, const SvBorder &rBorder)
Definition: svborder.cxx:22
#define TOOLS_DLLPUBLIC
Definition: toolsdllapi.h:28
#define SAL_WARN_UNUSED