xtd 0.2.0
date_time.h
Go to the documentation of this file.
1 #pragma once
5 #include "chrono.h"
6 #include "date_time_kind.h"
7 #include "day_of_week.h"
8 #include "icomparable.h"
9 #include "iequatable.h"
10 #include "month_of_year.h"
11 #include "object.h"
12 #include "ticks.h"
13 #include "time_span.h"
14 #include "ustring.h"
15 #include <ctime>
16 
18 namespace xtd {
20  class time_zone_info;
22 
24 
78  class core_export_ date_time : public xtd::icomparable<date_time>, public xtd::iequatable<date_time>, public xtd::object {
79  public:
81 
90  static const date_time max_value;
100  static const date_time min_value;
102 
104 
108  date_time() = default;
116  explicit date_time(int64 ticks);
124  explicit date_time(xtd::ticks ticks);
149  date_time(uint32 year, uint32 month, uint32 day);
160  date_time(uint32 year, uint32 month, uint32 day, uint32 hour, uint32 minute, uint32 second);
180  date_time(uint32 year, uint32 month, uint32 day, uint32 hour, uint32 minute, uint32 second, date_time_kind kind);
191  date_time(uint32 year, uint32 month, uint32 day, uint32 hour, uint32 minute, uint32 second, uint32 millisecond);
203  date_time(uint32 year, uint32 month, uint32 day, uint32 hour, uint32 minute, uint32 second, uint32 millisecond, date_time_kind kind);
205 
207  date_time(const date_time&) = default;
208  date_time(date_time&&) = default;
209  date_time& operator =(const date_time&) = default;
211 
212 
214 
223  date_time date() const noexcept;
224 
253  uint32 day() const noexcept;
254 
262  xtd::day_of_week day_of_week() const noexcept;
263 
270  uint32 day_of_year() const noexcept;
271 
305  uint32 hour() const noexcept;
306 
314  date_time_kind kind() const noexcept;
315 
355  uint32 millisecond() const noexcept;
356 
384  uint32 minute() const noexcept;
385 
413  uint32 month() const noexcept;
414 
422  static date_time now() noexcept;
423 
451  uint32 second() const noexcept;
452 
458  int64 ticks() const noexcept;
459 
465  xtd::ticks ticks_duration() const noexcept;
466 
474  xtd::time_span time_of_day() const noexcept;
475 
483  static date_time today() noexcept;
484 
493  static date_time utc_now() noexcept;
494 
523  uint32 year() const noexcept;
525 
527 
539  date_time add(const xtd::time_span& value) const;
540 
551  date_time add_days(double value) const;
552 
563  date_time add_hours(double value) const;
564 
575  date_time add_milliseconds(double value) const;
576 
586  date_time add_minutes(double value) const;
587 
597  date_time add_months(int32 months) const;
598 
608  date_time add_seconds(double value) const;
609 
615  date_time add_ticks(int64 value) const;
616 
628  date_time add_years(int32 value) const;
629 
630  int32 compare_to(const date_time& value) const noexcept override;
631 
645  static int32 days_in_month(uint32 year, month_of_year month);
646 
660  static int32 days_in_month(uint32 year, uint32 month);
661 
662  bool equals(const date_time&) const noexcept override;
663 
670  static date_time from_binary(int64 date_data);
671 
694  static date_time from_file_time(int64 file_time);
702  static date_time from_file_time_utc(int64 file_time);
703 
708  static date_time from_duration(const time_span& value);
714  static date_time from_duration(const time_span& value, date_time_kind kind);
715 
722  static date_time from_time_t(std::time_t value);
730  static date_time from_time_t(std::time_t value, date_time_kind kind);
731 
738  static date_time from_tm(const std::tm& value);
746  static date_time from_tm(const std::tm& value, date_time_kind kind);
747 
752  bool is_daylight_saving_time() const noexcept;
753 
758  static bool is_leap_year(uint32 year);
759 
767  static date_time parse(const xtd::ustring& s);
768 
776  static date_time specify_kind(const date_time& value, date_time_kind kind);
777 
845  static xtd::ustring sprintf(const ustring& format, const date_time& value);
846 
858  xtd::time_span subtract(const date_time& value) const;
878  date_time subtract(const xtd::time_span& value) const;
879 
883  int64 to_binary() const;
884 
890  int64 to_file_time() const;
896  int64 to_file_time_utc() const;
897 
912  date_time to_local_time() const;
913 
917  const xtd::ustring to_long_date_string() const;
918 
922  const xtd::ustring to_long_time_string() const;
923 
927  const xtd::ustring to_short_date_string() const;
928 
932  const xtd::ustring to_short_time_string() const;
933 
937  xtd::ustring to_string() const noexcept override;
994  xtd::ustring to_string(const ustring& format) const;
995 
1000  std::time_t to_time_t() const;
1001 
1006  std::tm to_tm() const;
1007 
1020  date_time to_universal_time() const;
1022 
1024  operator xtd::time_span() const;
1025  date_time operator +=(const date_time& value) = delete;
1026  date_time operator +=(const time_span& value);
1027  time_span operator -=(const date_time& value);
1028  time_span operator -=(const time_span& value);
1029  date_time operator +();
1030  date_time operator -();
1031  date_time operator +(const date_time& value) const = delete;
1032  date_time operator +(const time_span& value) const;
1033  time_span operator -(const date_time& value) const;
1034  date_time operator -(const time_span& value) const;
1035  date_time& operator ++();
1036  date_time operator ++(int32);
1037  date_time& operator --();
1038  date_time operator --(int32);
1040 
1041  private:
1042  friend class time_zone_info;
1043  xtd::ticks utc_offset() const;
1044  std::tuple<uint32, uint32, uint32, uint32, uint32, uint32, uint32, int32> get_date_time() const;
1045  void set_date_time(uint32 year, uint32 month, uint32 day, uint32 hour, uint32 minute, uint32 second, uint32 millisecond, date_time_kind kind);
1046 
1047  xtd::ticks value_ {0};
1049  };
1051 
1062  template<>
1063  inline std::string to_string(const date_time& value, const std::string& fmt, const std::locale& loc) {
1064  return value.to_string(fmt);
1065  }
1066 }
date_time_kind
Specifies whether a DateTime object represents a local time, a Coordinated Universal Time (UTC)...
Definition: date_time_kind.h:19
Contains xtd::iequatable interface.
value_t parse(const std::string &str)
Convert a string into a type.
Definition: parse.h:23
Contains xtd::icomparable interface.
Contains xtd::month_of_year enum class.
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
month_of_year
Specifies the month of the year.
Definition: month_of_year.h:21
Contains xtd::ticks typedef.
The xtd namespace contains all fundamental classes to access Hardware, Os, System, and more.
Definition: system_report.h:17
Contains xtd::time_span typedef.
Represents text as a sequence of UTF-8 code units.
Definition: ustring.h:46
std::string to_string(const date_time &value, const std::string &fmt, const std::locale &loc)
Convert a specified value into a string with specified format and locale.
Definition: date_time.h:1063
day_of_week
Specifies the day of the week.
Definition: day_of_week.h:23
The time represented is not specified as either local time or Coordinated Universal Time (UTC)...
Contains dyas, weeks, months and years durationtypes.
Defines a generalized method that a value type or class implements to create a type-specific method f...
Definition: iequatable.h:18
Represents an instant in time, typically expressed as a date and time of day.
Definition: date_time.h:78
static const date_time min_value
Represents the smallest possible value of xtd::date_time. This field is read-only.
Definition: date_time.h:100
day
Specifies the day of the week.
Definition: day.h:19
Contains xtd::ustring class.
Contains xtd::day_of_week enum class.
Contains xtd::object class.
Supports all classes in the xtd class hierarchy and provides low-level services to derived classes...
Definition: object.h:32
Contains xtd::date_time_kind enum class.
Represents a time interval.
Definition: time_span.h:26
int_least32_t int32
Represents a 32-bit signed integer.
Definition: types.h:129
xtd::ustring to_string() const noexcept override
Converts the value of the current xtd::date_time object to its equivalent string representation using...
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 const date_time max_value
Represents the largest possible value of xtd::date_time. This field is read-only. ...
Definition: date_time.h:90
The Subtract key.