/src/libreoffice/include/tools/datetime.hxx
Line | Count | Source |
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 | | |
29 | | namespace tools |
30 | | { |
31 | | class Duration; |
32 | | } |
33 | | |
34 | | class SAL_WARN_UNUSED TOOLS_DLLPUBLIC DateTime : public Date, public tools::Time |
35 | | { |
36 | | public: |
37 | | enum DateTimeInitSystem |
38 | | { |
39 | | SYSTEM |
40 | | }; |
41 | | |
42 | | enum DateTimeInitEmpty |
43 | | { |
44 | | EMPTY |
45 | | }; |
46 | | |
47 | 208k | explicit DateTime( DateTimeInitEmpty ) : Date( Date::EMPTY ), Time( Time::EMPTY ) {} |
48 | | explicit DateTime( DateTimeInitSystem ); |
49 | | DateTime( const DateTime& rDateTime ) : |
50 | 1.52M | Date( rDateTime ), Time( rDateTime ) {} |
51 | 249k | explicit DateTime( const Date& rDate ) : Date( rDate ), Time(Time::EMPTY) {} |
52 | 0 | explicit DateTime( const tools::Time& rTime ) : Date(0), Time( rTime ) {} |
53 | | DateTime( const Date& rDate, const tools::Time& rTime ) : |
54 | 539k | Date( rDate ), Time( rTime ) {} |
55 | | explicit DateTime( const css::util::DateTime& rDateTime ); |
56 | | |
57 | | css::util::DateTime |
58 | | GetUNODateTime() const |
59 | 27.2k | { return css::util::DateTime(GetNanoSec(), GetSec(), GetMin(), GetHour(), |
60 | 27.2k | 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 | 0 | { |
67 | 0 | if ( GetDate() != rDateTime.GetDate() ) |
68 | 0 | return false; |
69 | 0 | return Time::IsEqualIgnoreNanoSec( rDateTime ); |
70 | 0 | } |
71 | | |
72 | | auto operator <=>( const DateTime& rDateTime ) const |
73 | 508k | { |
74 | 508k | return std::make_pair(GetDate(), GetTime()) <=> |
75 | 508k | std::make_pair(rDateTime.GetDate(), rDateTime.GetTime()); |
76 | 508k | } |
77 | | bool operator==(const DateTime& rDateTime) const |
78 | 805k | { |
79 | 805k | return (Date::operator==(rDateTime) && tools::Time::operator==(rDateTime)); |
80 | 805k | } |
81 | | |
82 | | sal_Int64 GetSecFromDateTime( const Date& rDate ) const; |
83 | | |
84 | 4.33M | void ConvertToUTC() { *this -= Time::GetUTCOffset(); } |
85 | 14.5k | void ConvertToLocalTime() { *this += Time::GetUTCOffset(); } |
86 | | |
87 | | void AddTime( double fTimeInDays ); |
88 | | DateTime& operator +=( const tools::Time& rTime ); |
89 | | DateTime& operator -=( const tools::Time& rTime ); |
90 | | /** Duration can be negative, so adding it will subtract its value. */ |
91 | | DateTime& operator +=( const tools::Duration& rDuration ); |
92 | | private: |
93 | | void NormalizeTimeRemainderAndApply( tools::Time& rTime ); |
94 | | public: |
95 | | |
96 | | TOOLS_DLLPUBLIC friend DateTime operator +( const DateTime& rDateTime, sal_Int32 nDays ); |
97 | | TOOLS_DLLPUBLIC friend DateTime operator -( const DateTime& rDateTime, sal_Int32 nDays ); |
98 | | TOOLS_DLLPUBLIC friend DateTime operator +( const DateTime& rDateTime, double fTimeInDays ); |
99 | | TOOLS_DLLPUBLIC friend DateTime operator -( const DateTime& rDateTime, double fTimeInDays ) |
100 | 0 | { return operator+( rDateTime, -fTimeInDays ); } |
101 | | TOOLS_DLLPUBLIC friend DateTime operator +( const DateTime& rDateTime, const tools::Time& rTime ); |
102 | | TOOLS_DLLPUBLIC friend DateTime operator -( const DateTime& rDateTime, const tools::Time& rTime ); |
103 | | /** Use operator-() if a duration is to be remembered or processed. */ |
104 | | TOOLS_DLLPUBLIC friend tools::Duration operator -( const DateTime& rDateTime1, const DateTime& rDateTime2 ); |
105 | | /** Use Sub() if the floating point "time in days" value is to be |
106 | | processed. This also takes a shortcut for whole days values (equal |
107 | | times), and only for times inflicted values uses an intermediary |
108 | | tools::Duration for conversion. Note that the resulting floating point |
109 | | value nevertheless in many cases is not an exact representation down to |
110 | | nanoseconds. */ |
111 | | static double Sub( const DateTime& rDateTime1, const DateTime& rDateTime2 ); |
112 | | TOOLS_DLLPUBLIC friend sal_Int64 operator -( const DateTime& rDateTime, const Date& rDate ) |
113 | 29 | { return static_cast<const Date&>(rDateTime) - rDate; } |
114 | | /** Duration can be negative, so adding it will subtract its value. */ |
115 | | TOOLS_DLLPUBLIC friend DateTime operator +( const DateTime& rDateTime, const tools::Duration& rDuration ); |
116 | | |
117 | | DateTime& operator =( const DateTime& rDateTime ); |
118 | | DateTime& operator =( const css::util::DateTime& rUDateTime ); |
119 | | |
120 | | void GetWin32FileDateTime( sal_uInt32 & rLower, sal_uInt32 & rUpper ) const; |
121 | | static DateTime CreateFromWin32FileDateTime( sal_uInt32 rLower, sal_uInt32 rUpper ); |
122 | | |
123 | | /// Creates DateTime given a unix time, which is the number of seconds |
124 | | /// elapsed since Jan 1st, 1970. |
125 | | static DateTime CreateFromUnixTime( const double fSecondsSinceEpoch ); |
126 | | }; |
127 | | |
128 | | inline DateTime& DateTime::operator =( const DateTime& rDateTime ) |
129 | 142k | { |
130 | 142k | Date::operator=( rDateTime ); |
131 | 142k | Time::operator=( rDateTime ); |
132 | 142k | return *this; |
133 | 142k | } |
134 | | |
135 | | template< typename charT, typename traits > |
136 | | inline std::basic_ostream<charT, traits> & operator <<( |
137 | | std::basic_ostream<charT, traits> & stream, const DateTime& datetime) |
138 | | { |
139 | | return stream << datetime.GetYear() << '-' << |
140 | | std::setw(2) << std::setfill('0') << datetime.GetMonth() << '-' << |
141 | | std::setw(2) << std::setfill('0') << datetime.GetDay() << ' ' << |
142 | | std::setw(2) << std::setfill('0') << datetime.GetHour() << ':' << |
143 | | std::setw(2) << std::setfill('0') << datetime.GetMin() << ':' << |
144 | | std::setw(2) << std::setfill('0') << datetime.GetSec() << "." << |
145 | | std::setw(9) << std::setfill('0') << datetime.GetNanoSec(); |
146 | | } |
147 | | |
148 | | #endif |
149 | | |
150 | | /* vim:set shiftwidth=4 softtabstop=4 expandtab: */ |