summaryrefslogtreecommitdiffstats
path: root/tools/source/datetime/datetime.cxx
blob: 00790ff78dd4f82d9a562f0487afeb9a21aa34fb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
 * This file is part of the LibreOffice project.
 *
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
 *
 * This file incorporates work covered by the following license notice:
 *
 *   Licensed to the Apache Software Foundation (ASF) under one or more
 *   contributor license agreements. See the NOTICE file distributed
 *   with this work for additional information regarding copyright
 *   ownership. The ASF licenses this file to you under the Apache
 *   License, Version 2.0 (the "License"); you may not use this file
 *   except in compliance with the License. You may obtain a copy of
 *   the License at http://www.apache.org/licenses/LICENSE-2.0 .
 */
#include <tools/datetime.hxx>
#include <rtl/math.hxx>
#include <sal/log.hxx>

#include <systemdatetime.hxx>

DateTime::DateTime(DateTimeInitSystem)
    : Date( Date::EMPTY )
    , Time( Time::EMPTY )
{
    sal_Int32 nD = 0;
    sal_Int64 nT = 0;
    if ( GetSystemDateTime( &nD, &nT ) )
    {
        Date::operator=( Date( nD ) );
        SetTime( nT );
    }
    else
        Date::operator=( Date( 1, 1, 1900 ) ); // Time::nTime is already 0
}

DateTime::DateTime( const css::util::DateTime& rDateTime )
  : Date( rDateTime.Day, rDateTime.Month, rDateTime.Year ),
    Time( rDateTime.Hours, rDateTime.Minutes, rDateTime.Seconds, rDateTime.NanoSeconds )
{
}

DateTime& DateTime::operator =( const css::util::DateTime& rUDateTime )
{
    Date::operator=( Date( rUDateTime.Day, rUDateTime.Month, rUDateTime.Year));
    Time::operator=( Time( rUDateTime));
    return *this;
}

bool DateTime::IsBetween( const DateTime& rFrom, const DateTime& rTo ) const
{
    return (*this >= rFrom) && (*this <= rTo);
}

bool DateTime::operator >( const DateTime& rDateTime ) const
{
    return (Date::operator>( rDateTime )) ||
         (Date::operator==( rDateTime ) && tools::Time::operator>( rDateTime ));
}

bool DateTime::operator <( const DateTime& rDateTime ) const
{
    return (Date::operator<( rDateTime )) ||
         (Date::operator==( rDateTime ) && tools::Time::operator<( rDateTime ));
}

bool DateTime::operator >=( const DateTime& rDateTime ) const
{
    return (Date::operator>( rDateTime )) ||
         (Date::operator==( rDateTime ) && tools::Time::operator>=( rDateTime ));
}

bool DateTime::operator <=( const DateTime& rDateTime ) const
{
    return (Date::operator<( rDateTime )) ||
         (Date::operator==( rDateTime ) && tools::Time::operator<=( rDateTime ));
}

sal_Int64 DateTime::GetSecFromDateTime( const Date& rDate ) const
{
    if ( Date::operator<( rDate ) )
        return 0;
    else
    {
        sal_Int64 nSec = Date( *this ) - rDate;
        nSec *= 24UL*60*60;
        sal_Int64 nHour = GetHour();
        sal_Int64 nMin  = GetMin();
        nSec += (nHour*3600)+(nMin*60)+GetSec();
        return nSec;
    }
}

DateTime& DateTime::operator +=( const tools::Time& rTime )
{
    tools::Time aTime = *this;
    aTime += rTime;
    sal_uInt16 nHours = aTime.GetHour();
    if ( aTime.GetTime() > 0 )
    {
        while ( nHours >= 24 )
        {
            Date::operator++();
            nHours -= 24;
        }
        aTime.SetHour( nHours );
    }
    else if ( aTime.GetTime() != 0 )
    {
        while ( nHours >= 24 )
        {
            Date::operator--();
            nHours -= 24;
        }
        Date::operator--();
        aTime = Time( 24, 0, 0 )+aTime;
    }
    tools::Time::operator=( aTime );

    return *this;
}

DateTime& DateTime::operator -=( const tools::Time& rTime )
{
    tools::Time aTime = *this;
    aTime -= rTime;
    sal_uInt16 nHours = aTime.GetHour();
    if ( aTime.GetTime() > 0 )
    {
        while ( nHours >= 24 )
        {
            Date::operator++();
            nHours -= 24;
        }
        aTime.SetHour( nHours );
    }
    else if ( aTime.GetTime() != 0 )
    {
        while ( nHours >= 24 )
        {
            Date::operator--();
            nHours -= 24;
        }
        Date::operator--();
        aTime = Time( 24, 0, 0 )+aTime;
    }
    tools::Time::operator=( aTime );

    return *this;
}

DateTime operator +( const DateTime& rDateTime, sal_Int32 nDays )
{
    DateTime aDateTime( rDateTime );
    aDateTime.AddDays( nDays );
    return aDateTime;
}

DateTime operator -( const DateTime& rDateTime, sal_Int32 nDays )
{
    DateTime aDateTime( rDateTime );
    aDateTime.AddDays( -nDays );
    return aDateTime;
}

DateTime operator +( const DateTime& rDateTime, const tools::Time& rTime )
{
    DateTime aDateTime( rDateTime );
    aDateTime += rTime;
    return aDateTime;
}

DateTime operator -( const DateTime& rDateTime, const tools::Time& rTime )
{
    DateTime aDateTime( rDateTime );
    aDateTime -= rTime;
    return aDateTime;
}

void DateTime::AddTime( double fTimeInDays )
{
    double fInt, fFrac;
    if ( fTimeInDays < 0.0 )
    {
        fInt = ::rtl::math::approxCeil( fTimeInDays );
        fFrac = fInt <= fTimeInDays ? 0.0 : fTimeInDays - fInt;
    }
    else
    {
        fInt = ::rtl::math::approxFloor( fTimeInDays );
        fFrac = fInt >= fTimeInDays ? 0.0 : fTimeInDays - fInt;
    }
    AddDays( sal_Int32(fInt) );     // full days
    if ( fFrac )
    {
        tools::Time aTime(0);  // default ctor calls system time, we don't need that
        fFrac *= ::tools::Time::nanoSecPerDay;   // time expressed in nanoseconds
        aTime.MakeTimeFromNS( static_cast<sal_Int64>(fFrac) );    // method handles negative ns
        operator+=( aTime );
    }
}

DateTime operator +( const DateTime& rDateTime, double fTimeInDays )
{
    DateTime aDateTime( rDateTime );
    aDateTime.AddTime( fTimeInDays );
    return aDateTime;
}

double operator -( const DateTime& rDateTime1, const DateTime& rDateTime2 )
{
    sal_Int32 nDays = static_cast<const Date&>(rDateTime1)
        - static_cast<const Date&>(rDateTime2);
    sal_Int64 nTime = rDateTime1.GetNSFromTime() - rDateTime2.GetNSFromTime();
    if ( nTime )
    {
        double fTime = double(nTime);
        fTime /= ::tools::Time::nanoSecPerDay; // convert from nanoseconds to fraction
        if ( nDays < 0 && fTime > 0.0 )
            fTime = 1.0 - fTime;
        return double(nDays) + fTime;
    }
    return double(nDays);
}

void DateTime::GetWin32FileDateTime( sal_uInt32 & rLower, sal_uInt32 & rUpper ) const
{
    const sal_Int64 a100nPerSecond = SAL_CONST_INT64( 10000000 );
    const sal_Int64 a100nPerDay = a100nPerSecond * sal_Int64( 60 * 60 * 24 );

    // FILETIME is indirectly documented as uint64, see
    // https://msdn.microsoft.com/en-us/library/windows/desktop/ms724284.aspx
    // mentioning the ULARGE_INTEGER structure.
    // https://msdn.microsoft.com/en-us/library/windows/desktop/ms724280.aspx
    // mentions that if FILETIME is not less than 0x8000000000000000 then the
    // FileTimeToSystemTime function fails, which is another indicator.
    // Unless there's evidence that FILETIME can represent a signed offset from
    // 1601-01-01 truncate at 0. (reading part below in
    // CreateFromWin32FileDateTime() would had to be adapted to signed as
    // well).
    sal_Int16 nYear = GetYear();
    SAL_WARN_IF( nYear < 1601, "tools.datetime", "DateTime::GetWin32FileDateTime - year < 1601: " << nYear);

    sal_Int64 aTime = (nYear < 1601 ? 0 : (
        a100nPerDay * (*this - Date(1,1,1601)) +
        GetNSFromTime()/100));

    rLower = sal_uInt32( aTime % SAL_CONST_UINT64( 0x100000000 ) );
    rUpper = sal_uInt32( aTime / SAL_CONST_UINT64( 0x100000000 ) );
}

DateTime DateTime::CreateFromWin32FileDateTime( sal_uInt32 rLower, sal_uInt32 rUpper )
{
    // (rUpper|rLower) = 100-nanosecond intervals since 1601-01-01 00:00
    const sal_uInt64 a100nPerSecond = SAL_CONST_UINT64( 10000000 );
    const sal_uInt64 a100nPerDay = a100nPerSecond * sal_uInt64( 60 * 60 * 24 );

    sal_uInt64 aTime =
            sal_uInt64( rUpper ) * SAL_CONST_UINT64( 0x100000000 ) +
            sal_uInt64( rLower );

    SAL_WARN_IF( static_cast<sal_Int64>(aTime) < 0, "tools.datetime",
            "DateTime::CreateFromWin32FileDateTime - absurdly high value expected?");

    sal_uInt64 nDays = aTime / a100nPerDay;

    Date aDate(1,1,1601);
    // (0xffffffffffffffff / a100nPerDay = 21350398) fits into sal_Int32
    // (0x7fffffff = 2147483647)
    aDate.AddDays(nDays);

    SAL_WARN_IF( aDate - Date(1,1,1601) != static_cast<sal_Int32>(nDays), "tools.datetime",
            "DateTime::CreateFromWin32FileDateTime - date truncated to max");

    sal_uInt64 nNanos = (aTime - (nDays * a100nPerDay)) * 100;
    return DateTime( aDate, tools::Time(
                static_cast<sal_uInt32>((nNanos / tools::Time::nanoSecPerHour)   % sal_uInt64( 24 )),
                static_cast<sal_uInt32>((nNanos / tools::Time::nanoSecPerMinute) % sal_uInt64( 60 )),
                static_cast<sal_uInt32>((nNanos / tools::Time::nanoSecPerSec)    % sal_uInt64( 60 )),
                static_cast<sal_uInt64>( nNanos % tools::Time::nanoSecPerSec)));
}

DateTime DateTime::CreateFromUnixTime(const double fSecondsSinceEpoch)
{
    double fValue = fSecondsSinceEpoch / Time::secondPerDay;
    const sal_Int32 nDays = static_cast <sal_Int32>(::rtl::math::approxFloor(fValue));

    Date aDate (1, 1, 1970);
    aDate.AddDays(nDays);
    SAL_WARN_IF(aDate - Date(1, 1, 1970) != nDays, "tools.datetime",
                "DateTime::CreateFromUnixTime - date truncated to max");

    fValue -= nDays;

    const sal_uInt64 nNanos = fValue * tools::Time::nanoSecPerDay;
    return DateTime( aDate, tools::Time(
                static_cast<sal_uInt32>((nNanos / tools::Time::nanoSecPerHour)   % sal_uInt64( 24 )),
                static_cast<sal_uInt32>((nNanos / tools::Time::nanoSecPerMinute) % sal_uInt64( 60 )),
                static_cast<sal_uInt32>((nNanos / tools::Time::nanoSecPerSec)    % sal_uInt64( 60 )),
                static_cast<sal_uInt64>( nNanos % tools::Time::nanoSecPerSec)));
}

/* vim:set shiftwidth=4 softtabstop=4 expandtab: */