xtd 0.2.0
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 int64 nanoseconds_per_tick = 100ll;
45
48 static constexpr int64 ticks_per_microsecond = 10ll;
49
53
56 static constexpr int64 ticks_per_second = ticks_per_millisecond * 1'000;
57
60 static constexpr int64 ticks_per_minute = ticks_per_second * 60;
61
64 static constexpr int64 ticks_per_hour = ticks_per_minute * 60;
65
68 static constexpr int64 ticks_per_day = ticks_per_hour * 24;
69
72 static const time_span zero;
74
76
90 template<class duration_t, class 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 time_span& operator =(const time_span&) = default;
150 time_span& operator +=(const time_span& value);
151 time_span& operator -=(const time_span& value);
152 time_span operator +(const time_span& value);
153 time_span operator -(const time_span& value);
154 double operator *(const time_span& value);
155 time_span operator *(double value);
156 double operator /(const time_span& value);
157 time_span operator /(double value);
158 time_span operator +();
159 time_span operator -();
160 time_span& operator ++();
161 time_span operator ++(int);
162 time_span& operator --();
163 time_span operator --(int);
165
167
172 int32 days() const noexcept;
173
177 int32 hours() const noexcept;
178
182 int32 microseconds() const noexcept;
183
187 int32 milliseconds() const noexcept;
188
192 int32 minutes() const noexcept;
193
197 int32 nanoseconds() const noexcept;
198
202 int32 seconds() const noexcept;
203
207 int64 ticks() const noexcept;
208
212 xtd::ticks ticks_duration() const noexcept;
213
217 double total_days() const noexcept;
218
222 std::chrono::days total_days_duration() const noexcept;
223
227 double total_hours() const noexcept;
228
232 std::chrono::hours total_hours_duration() const noexcept;
233
237 double total_microseconds() const noexcept;
238
242 std::chrono::microseconds total_microseconds_duration() const noexcept;
243
247 double total_milliseconds() const noexcept;
248
252 std::chrono::milliseconds total_milliseconds_duration() const noexcept;
253
257 double total_minutes() const noexcept;
258
262 std::chrono::minutes total_minutes_duration() const noexcept;
263
267 double total_nanoseconds() const noexcept;
268
272 std::chrono::nanoseconds total_nanoseconds_duration() const noexcept;
273
277 double total_seconds() const noexcept;
278
282 std::chrono::seconds total_seconds_duration() const noexcept;
283
285
287
293 time_span add(const time_span& ts) const noexcept;
294
295 int32 compare_to(const time_span& value) const noexcept override;
296
300 double divide(const time_span& ts) const;
304 time_span divide(double divisor) const;
305
308 time_span duration() const noexcept;
309
313 bool equals(const object& value) const noexcept override;
317 bool equals(const time_span& value) const noexcept override;
318
321 xtd::size get_hash_code() const noexcept override;
322
326 double multiply(const time_span& ts) const noexcept;
330 time_span multiply(double factor) const noexcept;
331
336
342 static time_span parse(const string& value);
343
348 time_span subtract(const time_span& ts) const noexcept;
349
350 xtd::string to_string() const noexcept override;
378 string to_string(const string& format) const;
379
408 string to_string(const string& format, const std::locale& loc) const override;
410
412
418 static bool equals(time_span t1, time_span t2);
419
433 static time_span from_days(double value);
437 static time_span from_days(std::chrono::days value);
438
452 static time_span from_hours(double value);
456 static time_span from_hours(std::chrono::hours value);
457
463 static time_span from_microseconds(double value);
467 static time_span from_microseconds(std::chrono::microseconds value);
468
479 static time_span from_milliseconds(double value);
483 static time_span from_milliseconds(std::chrono::milliseconds value);
484
495 static time_span from_minutes(double value);
499 static time_span from_minutes(std::chrono::minutes value);
500
504 static time_span from_nanoseconds(double value);
508 static time_span from_nanoseconds(std::chrono::nanoseconds value);
509
520 static time_span from_seconds(double value);
524 static time_span from_seconds(std::chrono::seconds value);
525
535
540 static bool try_parse(const string& value, time_span& result);
542
543 private:
544 static constexpr int32 millis_per_second = 1000;
545 static constexpr int32 millis_per_minute = millis_per_second * 60; // 60,000
546 static constexpr int32 millis_per_hour = millis_per_minute * 60; // 3,600,000
547 static constexpr int32 millis_per_day = millis_per_hour * 24; // 86,400,000
548
549 static constexpr int32 parse_succeed = 0;
550 static constexpr int32 parse_overflow = 1;
551 static constexpr int32 parse_format = 2;
552
553 static time_span interval(double value, int scale);
554 string make_string_from_duration(bool constant) const;
555 static int32 try_parse_internal(const string& value, time_span& result);
556
557 int64 ticks_ = 0;
558 };
560
571 template<>
572 inline xtd::time_span parse<time_span>(const std::string& str) {return time_span::parse(str);}
573}
574
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:21
Defines a generalized method that a value type or class implements to create a type-specific method f...
Definition iequatable.hpp:22
Provides functionality to format the value of an object into a string representation.
Definition iformatable.hpp:35
Supports all classes in the xtd class hierarchy and provides low-level services to derived classes....
Definition object.hpp:44
Represents a time interval.
Definition time_span.hpp:29
double total_milliseconds() const noexcept
Gets the value of the current xtd::time_span structure expressed in whole and fractional milliseconds...
std::chrono::seconds total_seconds_duration() const noexcept
Gets the value of the current xtd::time_span structure expressed in whole and fractional seconds.
static constexpr int64 ticks_per_second
Represents the number of ticks in 1 second.
Definition time_span.hpp:56
static time_span from_days(double value)
Returns a xtd::time_span that represents a specified number of days, where the specification is accur...
static time_span from_hours(double value)
Returns a xtd::time_span that represents a specified number of hours, where the specification is accu...
static time_span parse(const string &value)
Converts the string representation of a time interval to its xtd::time_span equivalent.
int32 milliseconds() const noexcept
Gets the milliseconds component of the time interval represented by the current xtd::time_span struct...
double total_seconds() const noexcept
Gets the value of the current xtd::time_span structure expressed in whole and fractional seconds.
static time_span from_seconds(double value)
Returns a xtd::time_spam that represents a specified number of seconds, where the specification is ac...
xtd::size get_hash_code() const noexcept override
Serves as a hash function for a particular type.
time_span(int64 ticks)
Initializes a new instance of the xtd::time_span structure to the specified number of ticks.
double total_days() const noexcept
Gets the value of the current xtd::time_span structure expressed in whole and fractional days.
time_span negate() const
Returns a new xtd::time_spam object whose value is the negated value of this instance.
std::chrono::milliseconds total_milliseconds_duration() const noexcept
Gets the value of the current xtd::time_span structure expressed in whole and fractional milliseconds...
int32 nanoseconds() const noexcept
Gets the nanoseconds component of the time interval represented by the current xtd::time_span structu...
double total_microseconds() const noexcept
Gets the value of the current xtd::time_span structure expressed in whole and fractional microseconds...
time_span(int32 days, int32 hours, int32 minutes, int32 seconds, int32 milliseconds)
Initializes a new instance of the xtd::time_span structure to a specified number of days,...
xtd::string to_string() const noexcept override
Returns a xtd::string that represents the current object.
static time_span from_nanoseconds(double value)
Returns a xtd::time_span that represents a specified number of nanoseconds.
std::chrono::microseconds total_microseconds_duration() const noexcept
Gets the value of the current xtd::time_span structure expressed in whole and fractional microseconds...
static time_span from_microseconds(double value)
Returns a xtd::time_span that represents a specified number of microseconds.
static constexpr int64 ticks_per_microsecond
Represents the number of ticks in 1 microsecond. This field is constant.
Definition time_span.hpp:48
time_span(int32 days, int32 hours, int32 minutes, int32 seconds)
Initializes a new instance of the xtd::time_span structure to a specified number of days,...
static time_span from_milliseconds(double value)
Returns a xtd::time_span that represents a specified number of milliseconds.
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 const time_span min_value
Represents the minimum xtd::time_span value. This field is read-only.
Definition time_span.hpp:40
static constexpr int64 ticks_per_day
Represents the number of ticks in 1 day. This field is constant.
Definition time_span.hpp:68
bool equals(const object &value) const noexcept override
Determines whether the specified object is equal to the current object.
int32 days() const noexcept
Gets the days component of the time interval represented by the current xtd::time_span structure.
static bool try_parse(const string &value, time_span &result)
Converts the string representation of a time interval to its xtd::time_span equivalent and returns a ...
int32 seconds() const noexcept
Gets the seconds component of the time interval represented by the current xtd::time_span structure.
int32 compare_to(const time_span &value) const noexcept override
Compares the current instance with another object of the same type.
time_span(int32 days, int32 hours, int32 minutes, int32 seconds, int32 milliseconds, int32 microseconds, int32 nanoseconds)
Initializes a new instance of the xtd::time_span structure to a specified number of days,...
time_span(xtd::ticks ticks)
Initializes a new instance of the xtd::time_span structure to the specified number of ticks.
int32 microseconds() const noexcept
Gets the microseconds component of the time interval represented by the current xtd::time_span struct...
static constexpr int64 ticks_per_hour
Represents the number of ticks in 1 hour. This field is constant.
Definition time_span.hpp:64
int32 minutes() const noexcept
Gets the minutes component of the time interval represented by the current xtd::time_span structure.
double total_nanoseconds() const noexcept
Gets the value of the current xtd::time_span structure expressed in whole and fractional nanoseconds.
std::chrono::hours total_hours_duration() const noexcept
Gets the value of the current xtd::time_span structure expressed in whole and fractional hours.
std::chrono::minutes total_minutes_duration() const noexcept
Gets the value of the current xtd::time_span structure expressed in whole and fractional minutes.
std::chrono::nanoseconds total_nanoseconds_duration() const noexcept
Gets the value of the current xtd::time_span structure expressed in whole and fractional nanoseconds.
static constexpr int64 nanoseconds_per_tick
Represents the number of nanoseconds per tick. This field is constant.
Definition time_span.hpp:44
static time_span from_minutes(double value)
Returns a xtd::time_span that represents a specified number of minutes, where the specification is ac...
time_span duration() const noexcept
Returns a new xtd::time_span object whose value is the absolute value of the current xtd::time_span o...
static constexpr int64 ticks_per_minute
Represents the number of ticks in 1 minute. This field is constant.
Definition time_span.hpp:60
static time_span from_ticks(int64 value)
Returns a xtd::time_spam that represents a specified time, where the specification is in units of tic...
static const time_span max_value
Represents the maximum xtd::time_span value. This field is read-only.
Definition time_span.hpp:36
double total_hours() const noexcept
Gets the value of the current xtd::time_span structure expressed in whole and fractional hours.
double total_minutes() const noexcept
Gets the value of the current xtd::time_span structure expressed in whole and fractional minutes.
xtd::ticks ticks_duration() const noexcept
Gets the number of ticks that represent the value of the current xtd::time_span structure.
time_span(int32 days, int32 hours, int32 minutes, int32 seconds, int32 milliseconds, int32 microseconds)
Initializes a new instance of the xtd::time_span structure to a specified number of days,...
static constexpr int64 ticks_per_millisecond
Represents the number of ticks in 1 millisecond. This field is constant.
Definition time_span.hpp:52
static const time_span zero
Represents the zero xtd::time_span value. This field is read-only.
Definition time_span.hpp:72
time_span(int32 hours, int32 minutes, int32 seconds)
Initializes a new instance of the xtd::time_span structure to a specified number of hours,...
int32 hours() const noexcept
Gets the hours component of the time interval represented by the current xtd::time_span structure.
std::chrono::days total_days_duration() const noexcept
Gets the value of the current xtd::time_span structure expressed in whole and fractional days.
xtd::string format(const xtd::string &fmt, args_t &&... args)
Writes the text representation of the specified arguments list, to string using the specified format ...
Definition format.hpp:20
int32_t int32
Represents a 32-bit signed integer.
Definition int32.hpp:23
int64_t int64
Represents a 64-bit signed integer.
Definition int64.hpp:23
uint32_t uint32
Represents a 32-bit unsigned integer.
Definition uint32.hpp:23
size_t size
Represents a size of any object in bytes.
Definition size.hpp:23
std::chrono::duration< int64, tick > ticks
Represents a tick duration.
Definition ticks.hpp:21
@ multiply
The Multiply key.
@ add
The Add key.
@ divide
The Divide key.
@ subtract
The Subtract key.
Contains xtd::icomparable interface.
Contains xtd::iequatable interface.
Contains xtd::iformatable interface.
The xtd namespace contains all fundamental classes to access Hardware, Os, System,...
Definition xtd_about_box.hpp:10
Contains xtd::object class.
Contains xtd::parse methods.
Contains xtd::string alias.
Contains xtd::ticks typedef.
Contains xtd fundamental types.