1//
2// Timespan.cpp
3//
4// Library: Foundation
5// Package: DateTime
6// Module: Timespan
7//
8// Copyright (c) 2004-2006, Applied Informatics Software Engineering GmbH.
9// and Contributors.
10//
11// SPDX-License-Identifier: BSL-1.0
12//
13
14
15#include "Poco/Timespan.h"
16#include <algorithm>
17
18
19namespace Poco {
20
21
22const Timespan::TimeDiff Timespan::MILLISECONDS = 1000;
23const Timespan::TimeDiff Timespan::SECONDS = 1000*Timespan::MILLISECONDS;
24const Timespan::TimeDiff Timespan::MINUTES = 60*Timespan::SECONDS;
25const Timespan::TimeDiff Timespan::HOURS = 60*Timespan::MINUTES;
26const Timespan::TimeDiff Timespan::DAYS = 24*Timespan::HOURS;
27
28
29Timespan::Timespan():
30 _span(0)
31{
32}
33
34
35Timespan::Timespan(TimeDiff microSeconds):
36 _span(microSeconds)
37{
38}
39
40
41Timespan::Timespan(long otherSeconds, long otherMicroSeconds):
42 _span(TimeDiff(otherSeconds)*SECONDS + otherMicroSeconds)
43{
44}
45
46
47Timespan::Timespan(int otherDays, int otherHours, int otherMinutes, int otherSeconds, int otherMicroSeconds):
48 _span(TimeDiff(otherMicroSeconds) + TimeDiff(otherSeconds)*SECONDS + TimeDiff(otherMinutes)*MINUTES + TimeDiff(otherHours)*HOURS + TimeDiff(otherDays)*DAYS)
49{
50}
51
52
53Timespan::Timespan(const Timespan& timespan):
54 _span(timespan._span)
55{
56}
57
58
59Timespan& Timespan::operator = (const Timespan& timespan)
60{
61 _span = timespan._span;
62 return *this;
63}
64
65
66Timespan& Timespan::operator = (TimeDiff microSeconds)
67{
68 _span = microSeconds;
69 return *this;
70}
71
72
73Timespan& Timespan::assign(int otherDays, int otherHours, int otherMinutes, int otherSeconds, int otherMicroSeconds)
74{
75 _span = TimeDiff(otherMicroSeconds) + TimeDiff(otherSeconds)*SECONDS + TimeDiff(otherMinutes)*MINUTES + TimeDiff(otherHours)*HOURS + TimeDiff(otherDays)*DAYS;
76 return *this;
77}
78
79
80Timespan& Timespan::assign(long otherSeconds, long otherMicroSeconds)
81{
82 _span = TimeDiff(otherSeconds)*SECONDS + TimeDiff(otherMicroSeconds);
83 return *this;
84}
85
86
87void Timespan::swap(Timespan& timespan)
88{
89 std::swap(_span, timespan._span);
90}
91
92
93Timespan Timespan::operator + (const Timespan& d) const
94{
95 return Timespan(_span + d._span);
96}
97
98
99Timespan Timespan::operator - (const Timespan& d) const
100{
101 return Timespan(_span - d._span);
102}
103
104
105Timespan& Timespan::operator += (const Timespan& d)
106{
107 _span += d._span;
108 return *this;
109}
110
111
112Timespan& Timespan::operator -= (const Timespan& d)
113{
114 _span -= d._span;
115 return *this;
116}
117
118
119Timespan Timespan::operator + (TimeDiff microSeconds) const
120{
121 return Timespan(_span + microSeconds);
122}
123
124
125Timespan Timespan::operator - (TimeDiff microSeconds) const
126{
127 return Timespan(_span - microSeconds);
128}
129
130
131Timespan& Timespan::operator += (TimeDiff microSeconds)
132{
133 _span += microSeconds;
134 return *this;
135}
136
137
138Timespan& Timespan::operator -= (TimeDiff microSeconds)
139{
140 _span -= microSeconds;
141 return *this;
142}
143
144
145} // namespace Poco
146