Version 7.6.3.2-android, tag libreoffice-7.6.3.2-android
[LibreOffice.git] / include / tools / datetime.hxx
blob2194711dc208f94fcb747533add5d87736ae2ceb
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 TOOLS_DLLPUBLIC friend double operator -( const DateTime& rDateTime1, const DateTime& rDateTime2 );
105 TOOLS_DLLPUBLIC friend sal_Int64 operator -( const DateTime& rDateTime, const Date& rDate )
106 { return static_cast<const Date&>(rDateTime) - rDate; }
107 /** Duration can be negative, so adding it will subtract its value. */
108 TOOLS_DLLPUBLIC friend DateTime operator +( const DateTime& rDateTime, const tools::Duration& rDuration );
110 DateTime& operator =( const DateTime& rDateTime );
111 DateTime& operator =( const css::util::DateTime& rUDateTime );
113 void GetWin32FileDateTime( sal_uInt32 & rLower, sal_uInt32 & rUpper ) const;
114 static DateTime CreateFromWin32FileDateTime( sal_uInt32 rLower, sal_uInt32 rUpper );
116 /// Creates DateTime given a unix time, which is the number of seconds
117 /// elapsed since Jan 1st, 1970.
118 static DateTime CreateFromUnixTime( const double fSecondsSinceEpoch );
121 inline DateTime& DateTime::operator =( const DateTime& rDateTime )
123 Date::operator=( rDateTime );
124 Time::operator=( rDateTime );
125 return *this;
128 template< typename charT, typename traits >
129 inline std::basic_ostream<charT, traits> & operator <<(
130 std::basic_ostream<charT, traits> & stream, const DateTime& datetime)
132 return stream << datetime.GetYear() << '-' <<
133 std::setw(2) << std::setfill('0') << datetime.GetMonth() << '-' <<
134 std::setw(2) << std::setfill('0') << datetime.GetDay() << ' ' <<
135 std::setw(2) << std::setfill('0') << datetime.GetHour() << ':' <<
136 std::setw(2) << std::setfill('0') << datetime.GetMin() << ':' <<
137 std::setw(2) << std::setfill('0') << datetime.GetSec() << "." <<
138 std::setw(9) << std::setfill('0') << datetime.GetNanoSec();
141 #endif
143 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */