Version 24.8.3.2, tag libreoffice-24.8.3.2
[LibreOffice.git] / include / tools / datetime.hxx
blobddf29896c1a289e6c4338fb298802c9b9de21cfd
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
19 #ifndef INCLUDED_TOOLS_DATETIME_HXX
20 #define INCLUDED_TOOLS_DATETIME_HXX
22 #include <tools/toolsdllapi.h>
23 #include <tools/date.hxx>
24 #include <tools/time.hxx>
25 #include <com/sun/star/util/DateTime.hpp>
27 #include <iomanip>
29 namespace tools
31 class Duration;
34 class SAL_WARN_UNUSED TOOLS_DLLPUBLIC DateTime : public Date, public tools::Time
36 public:
37 enum DateTimeInitSystem
39 SYSTEM
42 enum DateTimeInitEmpty
44 EMPTY
47 explicit DateTime( DateTimeInitEmpty ) : Date( Date::EMPTY ), Time( Time::EMPTY ) {}
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 );
57 css::util::DateTime
58 GetUNODateTime() const
59 { return css::util::DateTime(GetNanoSec(), GetSec(), GetMin(), GetHour(),
60 GetDay(), GetMonth(), GetYear(), false); }
62 bool IsBetween( const DateTime& rFrom,
63 const DateTime& rTo ) const;
65 bool IsEqualIgnoreNanoSec( const DateTime& rDateTime ) const
67 if ( Date::operator!=( rDateTime ) )
68 return false;
69 return Time::IsEqualIgnoreNanoSec( rDateTime );
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;
83 sal_Int64 GetSecFromDateTime( const Date& rDate ) const;
85 void ConvertToUTC() { *this -= Time::GetUTCOffset(); }
86 void ConvertToLocalTime() { *this += Time::GetUTCOffset(); }
88 void AddTime( double fTimeInDays );
89 DateTime& operator +=( const tools::Time& rTime );
90 DateTime& operator -=( const tools::Time& rTime );
91 /** Duration can be negative, so adding it will subtract its value. */
92 DateTime& operator +=( const tools::Duration& rDuration );
93 private:
94 void NormalizeTimeRemainderAndApply( tools::Time& rTime );
95 public:
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 );
104 /** Use operator-() if a duration is to be remembered or processed. */
105 TOOLS_DLLPUBLIC friend tools::Duration operator -( const DateTime& rDateTime1, const DateTime& rDateTime2 );
106 /** Use Sub() if the floating point "time in days" value is to be
107 processed. This also takes a shortcut for whole days values (equal
108 times), and only for times inflicted values uses an intermediary
109 tools::Duration for conversion. Note that the resulting floating point
110 value nevertheless in many cases is not an exact representation down to
111 nanoseconds. */
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; }
115 /** Duration can be negative, so adding it will subtract its value. */
116 TOOLS_DLLPUBLIC friend DateTime operator +( const DateTime& rDateTime, const tools::Duration& rDuration );
118 DateTime& operator =( const DateTime& rDateTime );
119 DateTime& operator =( const css::util::DateTime& rUDateTime );
121 void GetWin32FileDateTime( sal_uInt32 & rLower, sal_uInt32 & rUpper ) const;
122 static DateTime CreateFromWin32FileDateTime( sal_uInt32 rLower, sal_uInt32 rUpper );
124 /// Creates DateTime given a unix time, which is the number of seconds
125 /// elapsed since Jan 1st, 1970.
126 static DateTime CreateFromUnixTime( const double fSecondsSinceEpoch );
129 inline DateTime& DateTime::operator =( const DateTime& rDateTime )
131 Date::operator=( rDateTime );
132 Time::operator=( rDateTime );
133 return *this;
136 template< typename charT, typename traits >
137 inline std::basic_ostream<charT, traits> & operator <<(
138 std::basic_ostream<charT, traits> & stream, const DateTime& datetime)
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();
149 #endif
151 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */