Branch data TLA Line data Source code
1 : : // Copyright (c) 2014-2022 Thomas Fussell
2 : : // Copyright (c) 2024-2025 xlnt-community
3 : : //
4 : : // Permission is hereby granted, free of charge, to any person obtaining a copy
5 : : // of this software and associated documentation files (the "Software"), to deal
6 : : // in the Software without restriction, including without limitation the rights
7 : : // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8 : : // copies of the Software, and to permit persons to whom the Software is
9 : : // furnished to do so, subject to the following conditions:
10 : : //
11 : : // The above copyright notice and this permission notice shall be included in
12 : : // all copies or substantial portions of the Software.
13 : : //
14 : : // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 : : // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 : : // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 : : // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 : : // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19 : : // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
20 : : // THE SOFTWARE
21 : : //
22 : : // @license: http://www.opensource.org/licenses/mit-license.php
23 : : // @author: see AUTHORS file
24 : : #include <cmath>
25 : : #include <cstdint>
26 : : #include <ctime>
27 : :
28 : : #include <xlnt/utils/timedelta.hpp>
29 : :
30 : : namespace xlnt {
31 : :
32 :CBC 3 : timedelta::timedelta()
33 : 3 : : timedelta(0, 0, 0, 0, 0)
34 : : {
35 : 3 : }
36 : :
37 : 9 : timedelta::timedelta(int days_, int hours_, int minutes_, int seconds_, int microseconds_)
38 : 9 : : days(days_), hours(hours_), minutes(minutes_), seconds(seconds_), microseconds(microseconds_)
39 : : {
40 : 9 : }
41 : :
42 : 7 : double timedelta::to_number() const
43 : : {
44 : 7 : std::uint64_t total_microseconds = static_cast<std::uint64_t>(microseconds);
45 : 7 : total_microseconds += static_cast<std::uint64_t>(seconds * 1e6);
46 : 7 : total_microseconds += static_cast<std::uint64_t>(minutes * 1e6 * 60);
47 : 7 : auto microseconds_per_hour = static_cast<std::uint64_t>(1e6) * 60 * 60;
48 : 7 : total_microseconds += static_cast<std::uint64_t>(hours) * microseconds_per_hour;
49 : 7 : auto number = static_cast<double>(total_microseconds) / (24.0 * static_cast<double>(microseconds_per_hour));
50 : 7 : number = std::floor(number * 100e9 + 0.5) / 100e9;
51 : 7 : number += days;
52 : :
53 : 7 : return number;
54 : : }
55 : :
56 : 3 : timedelta timedelta::from_number(double raw_time)
57 : : {
58 : 3 : timedelta result;
59 : :
60 : 3 : result.days = static_cast<int>(raw_time);
61 : 3 : double fractional_part = raw_time - result.days;
62 : :
63 : 3 : fractional_part *= 24;
64 : 3 : result.hours = static_cast<int>(fractional_part);
65 : 3 : fractional_part = 60 * (fractional_part - result.hours);
66 : 3 : result.minutes = static_cast<int>(fractional_part);
67 : 3 : fractional_part = 60 * (fractional_part - result.minutes);
68 : 3 : result.seconds = static_cast<int>(fractional_part);
69 : 3 : fractional_part = 1000000 * (fractional_part - result.seconds);
70 : 3 : result.microseconds = static_cast<int>(fractional_part);
71 : :
72 [ + + + - ]: 3 : if (result.microseconds == 999999 && fractional_part - result.microseconds > 0.5)
73 : : {
74 : 1 : result.microseconds = 0;
75 : 1 : result.seconds += 1;
76 : :
77 [ + - ]: 1 : if (result.seconds == 60)
78 : : {
79 : 1 : result.seconds = 0;
80 : 1 : result.minutes += 1;
81 : :
82 : : // TODO: too much nesting
83 [ + - ]: 1 : if (result.minutes == 60)
84 : : {
85 : 1 : result.minutes = 0;
86 : 1 : result.hours += 1;
87 : :
88 [ + - ]: 1 : if (result.hours == 24)
89 : : {
90 : 1 : result.hours = 0;
91 : 1 : result.days += 1;
92 : : }
93 : : }
94 : : }
95 : : }
96 : :
97 : 3 : return result;
98 : : }
99 : :
100 : : } // namespace xlnt
|