xtd 1.0.0
Loading...
Searching...
No Matches
time_span.hpp
Go to the documentation of this file.
1
4#pragma once
5#include "chrono.hpp"
6#include "icomparable.hpp"
7#include "iequatable.hpp"
8#include "iformatable.hpp"
9#include "object.hpp"
10#include "parse.hpp"
11#include "ticks.hpp"
12#include "types.hpp"
13#include "string.hpp"
14
16namespace xtd {
18
29 class time_span : public xtd::object, public xtd::iequatable<time_span>, public icomparable<time_span>, public iformatable {
30 public:
32
36 static const time_span max_value;
37
40 static const time_span min_value;
41
44 static constexpr xtd::int64 nanoseconds_per_tick = 100ll;
45
48 static constexpr xtd::int64 ticks_per_microsecond = 10ll;
49
53
57
61
65
68 static constexpr xtd::int64 ticks_per_day = ticks_per_hour * 24;
69
72 static const time_span zero;
74
76
90 template<typename duration_t, typename period_t = std::ratio<1>> // Can't be explicit by design.
91 time_span(const std::chrono::duration<duration_t, period_t>& value) : time_span(static_cast<int64>(std::chrono::duration_cast<xtd::ticks>(value).count())) {}
138
140 time_span();
141 time_span(const time_span&) = default;
142 time_span(time_span&&) = default;
148
149 auto operator =(const time_span&) -> time_span& = default;
150 auto operator +=(const time_span& value) -> time_span&;
151 auto operator -=(const time_span& value) -> time_span&;
152 auto operator +(const time_span& value) const -> time_span;
153 auto operator -(const time_span& value) const -> time_span;
154 auto operator *(const time_span& value) const -> double;
155 auto operator *(double value) const -> time_span;
156 auto operator /(const time_span& value) const -> double;
157 auto operator /(double value) const -> time_span;
158 auto operator +() const -> time_span;
159 auto operator -() const -> time_span;
160 auto operator ++() -> time_span&;
161 auto operator ++(int) -> time_span;
162 auto operator --() -> time_span&;
163 auto operator --(int) -> time_span;
164 friend auto operator << (std::ostream& os, const time_span& value) -> std::ostream& {return os << value.to_string();}
166
168
173 [[nodiscard]] auto days() const noexcept -> xtd::int32;
174
178 [[nodiscard]] auto hours() const noexcept -> xtd::int32;
179
183 [[nodiscard]] auto microseconds() const noexcept -> xtd::int32;
184
188 [[nodiscard]] auto milliseconds() const noexcept -> xtd::int32;
189
193 [[nodiscard]] auto minutes() const noexcept -> xtd::int32;
194
198 [[nodiscard]] auto nanoseconds() const noexcept -> xtd::int32;
199
203 [[nodiscard]] auto seconds() const noexcept -> xtd::int32;
204
208 [[nodiscard]] auto ticks() const noexcept -> xtd::int64;
209
213 [[nodiscard]] auto ticks_duration() const noexcept -> xtd::ticks;
214
218 [[nodiscard]] auto total_days() const noexcept -> double;
219
223 [[nodiscard]] auto total_days_duration() const noexcept -> std::chrono::days;
224
228 [[nodiscard]] auto total_hours() const noexcept -> double;
229
233 [[nodiscard]] auto total_hours_duration() const noexcept -> std::chrono::hours;
234
238 [[nodiscard]] auto total_microseconds() const noexcept -> double;
239
243 [[nodiscard]] auto total_microseconds_duration() const noexcept -> std::chrono::microseconds;
244
248 [[nodiscard]] auto total_milliseconds() const noexcept -> double;
249
253 [[nodiscard]] auto total_milliseconds_duration() const noexcept -> std::chrono::milliseconds;
254
258 [[nodiscard]] auto total_minutes() const noexcept -> double;
259
263 [[nodiscard]] auto total_minutes_duration() const noexcept -> std::chrono::minutes;
264
268 [[nodiscard]] auto total_nanoseconds() const noexcept -> double;
269
273 [[nodiscard]] auto total_nanoseconds_duration() const noexcept -> std::chrono::nanoseconds;
274
278 [[nodiscard]] auto total_seconds() const noexcept -> double;
279
283 [[nodiscard]] auto total_seconds_duration() const noexcept -> std::chrono::seconds;
284
286
288
294 [[nodiscard]] auto add(const time_span& ts) const noexcept -> time_span;
295
296 [[nodiscard]] auto compare_to(const time_span& value) const noexcept -> xtd::int32 override;
297
301 [[nodiscard]] auto divide(const time_span& ts) const -> double;
305 [[nodiscard]] auto divide(double divisor) const -> time_span;
306
309 [[nodiscard]] auto duration() const noexcept -> time_span;
310
314 [[nodiscard]] auto equals(const object& value) const noexcept -> bool override;
318 [[nodiscard]] auto equals(const time_span& value) const noexcept -> bool override;
319
322 [[nodiscard]] auto get_hash_code() const noexcept -> xtd::usize override;
323
327 [[nodiscard]] auto multiply(const time_span& ts) const noexcept -> double;
331 [[nodiscard]] auto multiply(double factor) const noexcept -> time_span;
332
336 [[nodiscard]] auto negate() const -> time_span;
337
343 static auto parse(const string& value) -> time_span;
344
349 [[nodiscard]] auto subtract(const time_span& ts) const noexcept -> time_span;
350
351 [[nodiscard]] auto to_string() const noexcept -> xtd::string override;
379 [[nodiscard]] auto to_string(const xtd::string& format) const -> xtd::string;
380
409 [[nodiscard]] auto to_string(const xtd::string& format, const xtd::globalization::culture_info& culture) const -> xtd::string override;
411
413
419 [[nodiscard]] static auto equals(time_span t1, time_span t2) -> bool;
420
434 [[nodiscard]] static auto from_days(double value) -> time_span;
438 [[nodiscard]] static auto from_days(std::chrono::days value) -> time_span;
439
453 [[nodiscard]] static auto from_hours(double value) -> time_span;
457 [[nodiscard]] static auto from_hours(std::chrono::hours value) -> time_span;
458
464 [[nodiscard]] static auto from_microseconds(double value) -> time_span;
468 [[nodiscard]] static auto from_microseconds(std::chrono::microseconds value) -> time_span;
469
480 [[nodiscard]] static auto from_milliseconds(double value) -> time_span;
484 [[nodiscard]] static auto from_milliseconds(std::chrono::milliseconds value) -> time_span;
485
496 [[nodiscard]] static auto from_minutes(double value) -> time_span;
500 [[nodiscard]] static auto from_minutes(std::chrono::minutes value) -> time_span;
501
505 [[nodiscard]] static auto from_nanoseconds(double value) -> time_span;
509 [[nodiscard]] static auto from_nanoseconds(std::chrono::nanoseconds value) -> time_span;
510
521 [[nodiscard]] static auto from_seconds(double value) -> time_span;
525 [[nodiscard]] static auto from_seconds(std::chrono::seconds value) -> time_span;
526
530 [[nodiscard]] static auto from_ticks(int64 value) -> time_span;
535 [[nodiscard]] static auto from_ticks(xtd::ticks value) -> time_span;
536
541 [[nodiscard]] static auto try_parse(const string& value, time_span& result) -> bool;
543
544 private:
545 static constexpr int32 millis_per_second = 1000;
546 static constexpr int32 millis_per_minute = millis_per_second * 60; // 60,000
547 static constexpr int32 millis_per_hour = millis_per_minute * 60; // 3,600,000
548 static constexpr int32 millis_per_day = millis_per_hour * 24; // 86,400,000
549
550 static constexpr int32 parse_succeed = 0;
551 static constexpr int32 parse_overflow = 1;
552 static constexpr int32 parse_format = 2;
553
554 [[nodiscard]] static auto interval(double value, int scale) -> time_span;
555 [[nodiscard]] auto make_string_from_duration(bool constant) const -> xtd::string;
556 [[nodiscard]] static auto try_parse_internal(const string& value, time_span& result) -> xtd::int32;
557
558 int64 ticks_ = 0;
559 };
560
561
572 template<>
573 [[nodiscard]] inline auto parse<time_span>(const std::string& str) -> xtd::time_span {return time_span::parse(str);}
574}
575
576#include "literals/time_span.hpp"
Contains std::chrono::days, std::chrono::weeks, std::chrono::months and std::chrono::years duration t...
Defines a generalized comparison method that a value type or class implements to create a type-specif...
Definition icomparable.hpp:22
Defines a generalized method that a value type or class implements to create a type-specific method f...
Definition iequatable.hpp:23
Provides functionality to format the value of an object into a string representation.
Definition iformatable.hpp:42
Supports all classes in the xtd class hierarchy and provides low-level services to derived classes....
Definition object.hpp:45
Represents a time interval.
Definition time_span.hpp:29
auto ticks_duration() const noexcept -> xtd::ticks
Gets the number of ticks that represent the value of the current xtd::time_span structure.
auto total_seconds_duration() const noexcept -> std::chrono::seconds
Gets the value of the current xtd::time_span structure expressed in whole and fractional seconds.
auto get_hash_code() const noexcept -> xtd::usize override
Serves as a hash function for a particular type.
static constexpr xtd::int64 ticks_per_millisecond
Represents the number of ticks in 1 millisecond. This field is constant.
Definition time_span.hpp:52
auto divide(const time_span &ts) const -> double
Returns a double value that's the result of dividing this instance by ts.
static constexpr xtd::int64 ticks_per_day
Represents the number of ticks in 1 day. This field is constant.
Definition time_span.hpp:68
static constexpr xtd::int64 ticks_per_minute
Represents the number of ticks in 1 minute. This field is constant.
Definition time_span.hpp:60
auto total_hours_duration() const noexcept -> std::chrono::hours
Gets the value of the current xtd::time_span structure expressed in whole and fractional hours.
auto seconds() const noexcept -> xtd::int32
Gets the seconds component of the time interval represented by the current xtd::time_span structure.
auto subtract(const time_span &ts) const noexcept -> time_span
Returns a new xtd::time_span object whose value is the difference between the specified xtd::time_spa...
time_span(xtd::int32 hours, xtd::int32 minutes, xtd::int32 seconds)
Initializes a new instance of the xtd::time_span structure to a specified number of hours,...
static auto from_milliseconds(double value) -> time_span
Returns a xtd::time_span that represents a specified number of milliseconds.
auto total_microseconds_duration() const noexcept -> std::chrono::microseconds
Gets the value of the current xtd::time_span structure expressed in whole and fractional microseconds...
auto total_days_duration() const noexcept -> std::chrono::days
Gets the value of the current xtd::time_span structure expressed in whole and fractional days.
static auto from_hours(double value) -> time_span
Returns a xtd::time_span that represents a specified number of hours, where the specification is accu...
auto total_milliseconds() const noexcept -> double
Gets the value of the current xtd::time_span structure expressed in whole and fractional milliseconds...
auto to_string() const noexcept -> xtd::string override
Returns a xtd::string that represents the current object.
time_span(xtd::int64 ticks)
Initializes a new instance of the xtd::time_span structure to the specified number of ticks.
static constexpr xtd::int64 nanoseconds_per_tick
Represents the number of nanoseconds per tick. This field is constant.
Definition time_span.hpp:44
static constexpr xtd::int64 ticks_per_second
Represents the number of ticks in 1 second.
Definition time_span.hpp:56
time_span(const std::chrono::duration< duration_t, period_t > &value)
Initializes a new instance of the xtd::time_span structure to the specified number of ticks.
Definition time_span.hpp:91
static auto parse(const string &value) -> time_span
Converts the string representation of a time interval to its xtd::time_span equivalent.
static const time_span min_value
Represents the minimum xtd::time_span value. This field is read-only.
Definition time_span.hpp:40
time_span(xtd::int32 days, xtd::int32 hours, xtd::int32 minutes, xtd::int32 seconds, xtd::int32 milliseconds, xtd::int32 microseconds, xtd::int32 nanoseconds)
Initializes a new instance of the xtd::time_span structure to a specified number of days,...
auto multiply(const time_span &ts) const noexcept -> double
Returns a new xtd::time_spam object which value is the result of multiplication of this instance and ...
auto total_milliseconds_duration() const noexcept -> std::chrono::milliseconds
Gets the value of the current xtd::time_span structure expressed in whole and fractional milliseconds...
auto nanoseconds() const noexcept -> xtd::int32
Gets the nanoseconds component of the time interval represented by the current xtd::time_span structu...
static auto from_microseconds(double value) -> time_span
Returns a xtd::time_span that represents a specified number of microseconds.
static constexpr xtd::int64 ticks_per_hour
Represents the number of ticks in 1 hour. This field is constant.
Definition time_span.hpp:64
auto total_nanoseconds() const noexcept -> double
Gets the value of the current xtd::time_span structure expressed in whole and fractional nanoseconds.
auto equals(const object &value) const noexcept -> bool override
Determines whether the specified object is equal to the current object.
auto add(const time_span &ts) const noexcept -> time_span
Returns a new xtd::time_span object whose value is the sum of the specified xtd::time_span object and...
auto compare_to(const time_span &value) const noexcept -> xtd::int32 override
Compares the current instance with another object of the same type.
time_span(xtd::ticks ticks)
Initializes a new instance of the xtd::time_span structure to the specified number of ticks.
auto total_microseconds() const noexcept -> double
Gets the value of the current xtd::time_span structure expressed in whole and fractional microseconds...
static auto from_nanoseconds(double value) -> time_span
Returns a xtd::time_span that represents a specified number of nanoseconds.
static constexpr xtd::int64 ticks_per_microsecond
Represents the number of ticks in 1 microsecond. This field is constant.
Definition time_span.hpp:48
auto total_nanoseconds_duration() const noexcept -> std::chrono::nanoseconds
Gets the value of the current xtd::time_span structure expressed in whole and fractional nanoseconds.
auto milliseconds() const noexcept -> xtd::int32
Gets the milliseconds component of the time interval represented by the current xtd::time_span struct...
auto days() const noexcept -> xtd::int32
Gets the days component of the time interval represented by the current xtd::time_span structure.
static auto from_minutes(double value) -> time_span
Returns a xtd::time_span that represents a specified number of minutes, where the specification is ac...
auto total_days() const noexcept -> double
Gets the value of the current xtd::time_span structure expressed in whole and fractional days.
time_span(xtd::int32 days, xtd::int32 hours, xtd::int32 minutes, xtd::int32 seconds, xtd::int32 milliseconds)
Initializes a new instance of the xtd::time_span structure to a specified number of days,...
auto total_minutes() const noexcept -> double
Gets the value of the current xtd::time_span structure expressed in whole and fractional minutes.
auto total_seconds() const noexcept -> double
Gets the value of the current xtd::time_span structure expressed in whole and fractional seconds.
static const time_span max_value
Represents the maximum xtd::time_span value. This field is read-only.
Definition time_span.hpp:36
auto duration() const noexcept -> time_span
Returns a new xtd::time_span object whose value is the absolute value of the current xtd::time_span o...
auto total_hours() const noexcept -> double
Gets the value of the current xtd::time_span structure expressed in whole and fractional hours.
static auto try_parse(const string &value, time_span &result) -> bool
Converts the string representation of a time interval to its xtd::time_span equivalent and returns a ...
auto hours() const noexcept -> xtd::int32
Gets the hours component of the time interval represented by the current xtd::time_span structure.
static const time_span zero
Represents the zero xtd::time_span value. This field is read-only.
Definition time_span.hpp:72
static auto from_ticks(int64 value) -> time_span
Returns a xtd::time_spam that represents a specified time, where the specification is in units of tic...
auto negate() const -> time_span
Returns a new xtd::time_spam object whose value is the negated value of this instance.
auto minutes() const noexcept -> xtd::int32
Gets the minutes component of the time interval represented by the current xtd::time_span structure.
auto total_minutes_duration() const noexcept -> std::chrono::minutes
Gets the value of the current xtd::time_span structure expressed in whole and fractional minutes.
static auto from_days(double value) -> time_span
Returns a xtd::time_span that represents a specified number of days, where the specification is accur...
time_span(xtd::int32 days, xtd::int32 hours, xtd::int32 minutes, xtd::int32 seconds, xtd::int32 milliseconds, xtd::int32 microseconds)
Initializes a new instance of the xtd::time_span structure to a specified number of days,...
static auto from_seconds(double value) -> time_span
Returns a xtd::time_spam that represents a specified number of seconds, where the specification is ac...
auto microseconds() const noexcept -> xtd::int32
Gets the microseconds component of the time interval represented by the current xtd::time_span struct...
time_span(xtd::int32 days, xtd::int32 hours, xtd::int32 minutes, xtd::int32 seconds)
Initializes a new instance of the xtd::time_span structure to a specified number of days,...
auto ticks() const noexcept -> xtd::int64
Gets the number of ticks that represent the value of the current xtd::time_span structure.
auto format(const xtd::string &fmt, args_t &&... args) -> xtd::string
Writes the text representation of the specified arguments list, to string using the specified format ...
Definition format.hpp:21
std::int64_t int64
Represents a 64-bit signed integer.
Definition int64.hpp:23
std::uint32_t uint32
Represents a 32-bit unsigned integer.
Definition uint32.hpp:23
std::int32_t int32
Represents a 32-bit signed integer.
Definition int32.hpp:25
std::size_t usize
Represents an unsigned size of any object in bytes.
Definition usize.hpp:22
std::chrono::duration< int64, tick > ticks
Represents a tick duration.
Definition ticks.hpp:21
auto parse(const std::string &str) -> value_t
Convert a string into a type.
Definition parse.hpp:34
Contains xtd::icomparable interface.
Contains xtd::iequatable interface.
Contains xtd::iformatable interface.
Contains xtd::time_span suffixes.
Contains classes that define culture-related information, including language, country/region,...
Definition culture_info.hpp:19
The xtd namespace contains all fundamental classes to access Hardware, Os, System,...
Definition abstract_object.hpp:8
Contains xtd::object class.
Contains xtd::parse methods.
Contains xtd::string alias.
Contains xtd::ticks typedef.
Contains xtd fundamental types.