xtd 0.2.0
time_span.h
Go to the documentation of this file.
1 #pragma once
5 #include "chrono.h"
6 #include "icomparable.h"
7 #include "iequatable.h"
8 #include "object.h"
9 #include "ticks.h"
10 #include "types.h"
11 
13 namespace xtd {
15 
26  struct time_span : public xtd::object, xtd::iequatable<time_span>, icomparable<time_span> {
27  public:
29 
33  static const time_span max_value;
34 
37  static const time_span min_value;
38 
41  static constexpr int64 nanoseconds_per_tick = 100ll;
42 
45  static constexpr int64 ticks_per_microsecond = 10ll;
46 
49  static constexpr int64 ticks_per_millisecond = ticks_per_microsecond * 1'000;
50 
53  static constexpr int64 ticks_per_second = ticks_per_millisecond * 1'000;
54 
57  static constexpr int64 ticks_per_minute = ticks_per_second * 60;
58 
61  static constexpr int64 ticks_per_hour = ticks_per_minute * 60;
62 
65  static constexpr int64 ticks_per_day = ticks_per_hour * 24;
66 
69  static const time_span zero;
71 
73 
78  explicit time_span(int64 ticks);
82  explicit time_span(xtd::ticks ticks);
87  template<typename duration_t, typename period_t = std::ratio<1>> // Can't be explicit by design.
88  time_span(const std::chrono::duration<duration_t, period_t>& value) : time_span(static_cast<int64>(std::chrono::duration_cast<xtd::ticks>(value).count())) {}
103  time_span(int32 days, int32 hours, int32 minutes, int32 seconds);
112  time_span(int32 days, int32 hours, int32 minutes, int32 seconds, int32 milliseconds);
122  time_span(int32 days, int32 hours, int32 minutes, int32 seconds, int32 milliseconds, int32 microseconds);
133  time_span(int32 days, int32 hours, int32 minutes, int32 seconds, int32 milliseconds, int32 microseconds, int32 nanoseconds);
135 
137  time_span();
138  time_span(const time_span&) = default;
139  time_span(time_span&&) = default;
140  time_span(uint32 hours, uint32 minutes, uint32 seconds);
141  time_span(uint32 days, uint32 hours, uint32 minutes, uint32 seconds);
142  time_span(uint32 days, uint32 hours, uint32 minutes, uint32 seconds, uint32 milliseconds);
143  time_span(uint32 days, uint32 hours, uint32 minutes, uint32 seconds, uint32 milliseconds, uint32 microseconds);
144  time_span(uint32 days, uint32 hours, uint32 minutes, uint32 seconds, uint32 milliseconds, uint32 microseconds, uint32 nanoseconds);
145 
146  time_span& operator =(const time_span&) = default;
147  time_span& operator +=(const time_span& value);
148  time_span& operator -=(const time_span& value);
149  time_span operator +(const time_span& value);
150  time_span operator -(const time_span& value);
151  double operator *(const time_span& value);
152  time_span operator *(double value);
153  double operator /(const time_span& value);
154  time_span operator /(double value);
155  time_span operator +();
156  time_span operator -();
157  time_span& operator ++();
158  time_span operator ++(int);
159  time_span& operator --();
160  time_span operator --(int);
162 
164 
169  int32 days() const noexcept;
170 
174  int32 hours() const noexcept;
175 
179  int32 microseconds() const noexcept;
180 
184  int32 milliseconds() const noexcept;
185 
189  int32 minutes() const noexcept;
190 
194  int32 nanoseconds() const noexcept;
195 
199  int32 seconds() const noexcept;
200 
204  int64 ticks() const noexcept;
205 
209  xtd::ticks ticks_duration() const noexcept;
210 
214  double total_days() const noexcept;
215 
219  std::chrono::days total_days_duration() const noexcept;
220 
224  double total_hours() const noexcept;
225 
229  std::chrono::hours total_hours_duration() const noexcept;
230 
234  double total_microseconds() const noexcept;
235 
239  std::chrono::microseconds total_microseconds_duration() const noexcept;
240 
244  double total_milliseconds() const noexcept;
245 
249  std::chrono::milliseconds total_milliseconds_duration() const noexcept;
250 
254  double total_minutes() const noexcept;
255 
259  std::chrono::minutes total_minutes_duration() const noexcept;
260 
264  double total_nanoseconds() const noexcept;
265 
269  std::chrono::nanoseconds total_nanoseconds_duration() const noexcept;
270 
274  double total_seconds() const noexcept;
275 
279  std::chrono::seconds total_seconds_duration() const noexcept;
280 
282 
284 
290  time_span add(const time_span& ts) const noexcept;
291 
292  int32 compare_to(const time_span& value) const noexcept override;
293 
297  double divide(const time_span& ts) const;
301  time_span divide(double divisor) const;
302 
305  time_span duration() const noexcept;
306 
307  bool equals(const time_span& value) const noexcept override;
308 
313  static bool equals(time_span t1, time_span t2);
314 
328  static time_span from_days(double value);
332  static time_span from_days(std::chrono::days value);
333 
347  static time_span from_hours(double value);
351  static time_span from_hours(std::chrono::hours value);
352 
358  static time_span from_microseconds(double value);
362  static time_span from_microseconds(std::chrono::microseconds value);
363 
374  static time_span from_milliseconds(double value);
378  static time_span from_milliseconds(std::chrono::milliseconds value);
379 
390  static time_span from_minutes(double value);
394  static time_span from_minutes(std::chrono::minutes value);
395 
399  static time_span from_nanoseconds(double value);
403  static time_span from_nanoseconds(std::chrono::nanoseconds value);
404 
415  static time_span from_seconds(double value);
419  static time_span from_seconds(std::chrono::seconds value);
420 
424  static time_span from_ticks(int64 value);
429  static time_span from_ticks(xtd::ticks value);
430 
434  double multiply(const time_span& ts) const noexcept;
438  time_span multiply(double factor) const noexcept;
439 
443  time_span negate() const;
444 
450  static time_span parse(const ustring& value);
451 
456  time_span subtract(const time_span& ts) const noexcept;
457 
458  xtd::ustring to_string() const noexcept override;
485  ustring to_string(const ustring& format) const;
486 
491  static bool try_parse(const ustring& value, time_span& result);
493 
494  private:
495  static constexpr int32 millis_per_second = 1000;
496  static constexpr int32 millis_per_minute = millis_per_second * 60; // 60,000
497  static constexpr int32 millis_per_hour = millis_per_minute * 60; // 3,600,000
498  static constexpr int32 millis_per_day = millis_per_hour * 24; // 86,400,000
499 
500  static constexpr int32 parse_succeed = 0;
501  static constexpr int32 parse_overflow = 1;
502  static constexpr int32 parse_format = 2;
503 
504  static time_span interval(double value, int scale);
505  ustring make_string_from_duration(bool constant) const;
506  static int32 try_parse_internal(const ustring& value, time_span& result);
507 
508  int64 ticks_ = 0;
509  };
511 
512  template<>
513  inline std::string to_string(const time_span& value, const std::string& fmt, const std::locale& loc) {return value.to_string(fmt);}
514 }
515 
time_span negate() const
Returns a new xtd::time_spam object whose value is the negated value of this instance.
static const time_span min_value
Represents the minimum xtd::time_span value. This field is read-only.
Definition: time_span.h:37
Contains xtd fundamental types.
static constexpr int64 ticks_per_second
Represents the number of ticks in 1 second.
Definition: time_span.h:53
std::chrono::microseconds total_microseconds_duration() const noexcept
Gets the value of the current xtd::time_span structure expressed in whole and fractional microseconds...
int32 days() const noexcept
Gets the days component of the time interval represented by the current xtd::time_span structure...
Contains xtd::iequatable interface.
std::chrono::days total_days_duration() const noexcept
Gets the value of the current xtd::time_span structure expressed in whole and fractional days...
Contains xtd::icomparable interface.
double total_seconds() const noexcept
Gets the value of the current xtd::time_span structure expressed in whole and fractional seconds...
int32 seconds() const noexcept
Gets the seconds component of the time interval represented by the current xtd::time_span structure...
int32 microseconds() const noexcept
Gets the microseconds component of the time interval represented by the current xtd::time_span struct...
static time_span from_days(double value)
Returns a xtd::time_span that represents a specified number of days, where the specification is accur...
std::chrono::duration< int64, tick > ticks
Represents a tick duration.
Definition: ticks.h:21
Defines a generalized comparison method that a value type or class implements to create a type-specif...
Definition: icomparable.h:17
xtd::ticks ticks_duration() const noexcept
Gets the number of ticks that represent the value of the current xtd::time_span structure.
static constexpr int64 ticks_per_hour
Represents the number of ticks in 1 hour. This field is constant.
Definition: time_span.h:61
Contains xtd::ticks typedef.
The xtd namespace contains all fundamental classes to access Hardware, Os, System, and more.
Definition: system_report.h:17
std::chrono::nanoseconds total_nanoseconds_duration() const noexcept
Gets the value of the current xtd::time_span structure expressed in whole and fractional nanoseconds...
time_span subtract(const time_span &ts) const noexcept
Returns a new xtd::time_span object whose value is the difference between the specified xtd::time_spa...
double divide(const time_span &ts) const
Returns a double value that&#39;s the result of dividing this instance by ts.
static time_span parse(const ustring &value)
Converts the string representation of a time interval to its xtd::time_span equivalent.
double total_nanoseconds() const noexcept
Gets the value of the current xtd::time_span structure expressed in whole and fractional nanoseconds...
std::chrono::milliseconds total_milliseconds_duration() const noexcept
Gets the value of the current xtd::time_span structure expressed in whole and fractional milliseconds...
bool equals(const time_span &value) const noexcept override
Indicates whether the current object is equal to another object of the same type. ...
Represents text as a sequence of UTF-8 code units.
Definition: ustring.h:46
static bool try_parse(const ustring &value, time_span &result)
Converts the string representation of a time interval to its xtd::time_span equivalent and returns a ...
static const time_span max_value
Represents the maximum xtd::time_span value. This field is read-only.
Definition: time_span.h:33
static const time_span zero
Represents the zero xtd::time_span value. This field is read-only.
Definition: time_span.h:69
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.h:88
static time_span from_milliseconds(double value)
Returns a xtd::time_span that represents a specified number of milliseconds.
time_span add(const time_span &ts) const noexcept
Returns a new xtd::time_span object whose value is the sum of the specified xtd::time_span object and...
double total_hours() const noexcept
Gets the value of the current xtd::time_span structure expressed in whole and fractional hours...
int32 milliseconds() const noexcept
Gets the milliseconds component of the time interval represented by the current xtd::time_span struct...
xtd::ustring to_string() const noexcept override
Returns a sxd::ustring that represents the current object.
int32 hours() const noexcept
Gets the hours component of the time interval represented by the current xtd::time_span structure...
int32 minutes() const noexcept
Gets the minutes component of the time interval represented by the current xtd::time_span structure...
Contains dyas, weeks, months and years durationtypes.
static time_span from_minutes(double value)
Returns a xtd::time_span that represents a specified number of minutes, where the specification is ac...
double total_milliseconds() const noexcept
Gets the value of the current xtd::time_span structure expressed in whole and fractional milliseconds...
double total_days() const noexcept
Gets the value of the current xtd::time_span structure expressed in whole and fractional days...
int32 nanoseconds() const noexcept
Gets the nanoseconds component of the time interval represented by the current xtd::time_span structu...
Defines a generalized method that a value type or class implements to create a type-specific method f...
Definition: iequatable.h:18
double multiply(const time_span &ts) const noexcept
Returns a new xtd::time_spam object which value is the result of multiplication of this instance and ...
int32 compare_to(const time_span &value) const noexcept override
Compares the current instance with another object of the same type.
static constexpr int64 ticks_per_minute
Represents the number of ticks in 1 minute. This field is constant.
Definition: time_span.h:57
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 from_seconds(double value)
Returns a xtd::time_spam that represents a specified number of seconds, 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...
time_span(int64 ticks)
Initializes a new instance of the xtd::time_span structure to the specified number of ticks...
Contains xtd::object class.
static time_span from_microseconds(double value)
Returns a xtd::time_span that represents a specified number of microseconds.
Supports all classes in the xtd class hierarchy and provides low-level services to derived classes...
Definition: object.h:32
Represents a time interval.
Definition: time_span.h:26
int_least32_t int32
Represents a 32-bit signed integer.
Definition: types.h:129
std::chrono::hours total_hours_duration() const noexcept
Gets the value of the current xtd::time_span structure expressed in whole and fractional hours...
double total_microseconds() const noexcept
Gets the value of the current xtd::time_span structure expressed in whole and fractional microseconds...
static constexpr int64 nanoseconds_per_tick
Represents the number of nanoseconds per tick. This field is constant.
Definition: time_span.h:41
static time_span from_nanoseconds(double value)
Returns a xtd::time_span that represents a specified number of nanoseconds.
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 constexpr int64 ticks_per_millisecond
Represents the number of ticks in 1 millisecond. This field is constant.
Definition: time_span.h:49
double total_minutes() const noexcept
Gets the value of the current xtd::time_span structure expressed in whole and fractional minutes...
int_least64_t int64
Represents a 64-bit signed integer.
Definition: types.h:140
uint_least32_t uint32
Represents a 32-bit unsigned integer.
Definition: types.h:239
static constexpr int64 ticks_per_day
Represents the number of ticks in 1 day. This field is constant.
Definition: time_span.h:65
static constexpr int64 ticks_per_microsecond
Represents the number of ticks in 1 microsecond. This field is constant.
Definition: time_span.h:45
int64 ticks() const noexcept
Gets the number of ticks that represent the value of the current xtd::time_span structure.
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::seconds total_seconds_duration() const noexcept
Gets the value of the current xtd::time_span structure expressed in whole and fractional seconds...