xtd 0.2.0
date_time.hpp
Go to the documentation of this file.
1
4#pragma once
5#include "chrono.hpp"
6#include "date_time_kind.hpp"
7#include "day_of_week.hpp"
8#include "icomparable.hpp"
9#include "iequatable.hpp"
10#include "iformatable.hpp"
11#include "month_of_year.hpp"
12#include "object.hpp"
13#include "ticks.hpp"
14#include "time_span.hpp"
15#include "string.hpp"
16#include <ctime>
17#if defined(__xtd__cpp_lib_format)
18#include <format>
19#endif
20
22namespace xtd {
24 class time_zone_info;
26
28
85 class core_export_ date_time : public xtd::object, public xtd::icomparable<date_time>, public xtd::iequatable<date_time>, public xtd::iformatable {
86 public:
88
97 static const date_time max_value;
107 static const date_time min_value;
109
111
115 date_time() = default;
156 date_time(uint32 year, uint32 month, uint32 day);
167 date_time(uint32 year, uint32 month, uint32 day, uint32 hour, uint32 minute, uint32 second);
187 date_time(uint32 year, uint32 month, uint32 day, uint32 hour, uint32 minute, uint32 second, date_time_kind kind);
198 date_time(uint32 year, uint32 month, uint32 day, uint32 hour, uint32 minute, uint32 second, uint32 millisecond);
210 date_time(uint32 year, uint32 month, uint32 day, uint32 hour, uint32 minute, uint32 second, uint32 millisecond, date_time_kind kind);
211
215 template<class clock_t, class duration_t = clock_t::duration> // Can't be explicit by design.
216 date_time(const std::chrono::time_point<clock_t, duration_t>& time_point) {
217 using namespace std::chrono;
218 *this = from_time_t(system_clock::to_time_t(system_clock::now() + duration_cast<system_clock::duration>(time_point - clock_t::now())), date_time_kind::local);
219 }
220
225 template<class clock_t, class duration_t = clock_t::duration>
226 date_time(const std::chrono::time_point<clock_t, duration_t>& time_point, date_time_kind kind) {
227 using namespace std::chrono;
228 *this = from_time_t(system_clock::to_time_t(system_clock::now() + duration_cast<system_clock::duration>(time_point - clock_t::now())), kind);
229 }
231
233 date_time(const date_time&) = default;
234 date_time(date_time&&) = default;
235 date_time& operator =(const date_time&) = default;
237
238
240
249 date_time date() const noexcept;
250
279 uint32 day() const noexcept;
280
288 xtd::day_of_week day_of_week() const noexcept;
289
296 uint32 day_of_year() const noexcept;
297
331 uint32 hour() const noexcept;
332
340 date_time_kind kind() const noexcept;
341
381 uint32 millisecond() const noexcept;
382
410 uint32 minute() const noexcept;
411
439 uint32 month() const noexcept;
440
468 uint32 second() const noexcept;
469
475 int64 ticks() const noexcept;
476
482 xtd::ticks ticks_duration() const noexcept;
483
491 xtd::time_span time_of_day() const noexcept;
492
521 uint32 year() const noexcept;
523
525
537 date_time add(const xtd::time_span& value) const;
538
549 date_time add_days(double value) const;
550
561 date_time add_hours(double value) const;
562
573 date_time add_milliseconds(double value) const;
574
584 date_time add_minutes(double value) const;
585
595 date_time add_months(int32 months) const;
596
606 date_time add_seconds(double value) const;
607
613 date_time add_ticks(int64 value) const;
614
626 date_time add_years(int32 value) const;
627
638 int32 compare_to(const date_time& value) const noexcept override;
639
643 bool equals(const object& obj) const noexcept override;
647 bool equals(const date_time& other) const noexcept override;
648
651 xtd::size get_hash_code() const noexcept override;
652
655 std::vector<string> get_date_time_formats() const noexcept;
656
661 bool is_daylight_saving_time() const noexcept;
662
674 xtd::time_span subtract(const date_time& value) const;
694 date_time subtract(const xtd::time_span& value) const;
695
699 int64 to_binary() const;
700
706 int64 to_file_time() const;
707
713 int64 to_file_time_utc() const;
714
729 date_time to_local_time() const;
730
734 const xtd::string to_long_date_string() const;
735
739 const xtd::string to_long_time_string() const;
740
744 const xtd::string to_short_date_string() const;
745
749 const xtd::string to_short_time_string() const;
750
754 xtd::string to_string() const noexcept override;
811 xtd::string to_string(const string& format) const;
869 xtd::string to_string(const string& format, const std::locale& loc) const override;
870
875 std::time_t to_time_t() const;
876
881 std::tm to_tm() const;
882
895 date_time to_universal_time() const;
897
899
908 static date_time now() noexcept;
909
917 static date_time today() noexcept;
918
927 static date_time utc_now() noexcept;
928
942 static int32 days_in_month(uint32 year, month_of_year month);
943
957 static int32 days_in_month(uint32 year, uint32 month);
958
965 static date_time from_binary(int64 date_data);
966
989 static date_time from_file_time(int64 file_time);
997 static date_time from_file_time_utc(int64 file_time);
998
1003 static date_time from_duration(const time_span& value);
1009 static date_time from_duration(const time_span& value, date_time_kind kind);
1010
1017 static date_time from_time_t(std::time_t value);
1025 static date_time from_time_t(std::time_t value, date_time_kind kind);
1026
1033 static date_time from_tm(const std::tm& value);
1041 static date_time from_tm(const std::tm& value, date_time_kind kind);
1042
1047 static bool is_leap_year(uint32 year);
1048
1056 static date_time parse(const xtd::string& s);
1057
1065 static date_time specify_kind(const date_time& value, date_time_kind kind);
1066
1134 static xtd::string sprintf(const string& format, const date_time& value);
1135
1141 static bool try_parse(const string& s, date_time& result) noexcept;
1143
1145 operator xtd::time_span() const;
1146 date_time operator +=(const date_time& value) = delete;
1147 date_time operator +=(const time_span& value);
1148 time_span operator -=(const date_time& value);
1149 time_span operator -=(const time_span& value);
1150 date_time operator +();
1151 date_time operator -();
1152 date_time operator +(const date_time& value) const = delete;
1153 date_time operator +(const time_span& value) const;
1154 time_span operator -(const date_time& value) const;
1155 date_time operator -(const time_span& value) const;
1156 date_time& operator ++();
1157 date_time operator ++(int32);
1158 date_time& operator --();
1159 date_time operator --(int32);
1161
1162 private:
1163 friend class time_zone_info;
1164 xtd::ticks utc_offset() const;
1165 std::tuple<uint32, uint32, uint32, uint32, uint32, uint32, uint32, int32> get_date_time() const;
1166 void set_date_time(uint32 year, uint32 month, uint32 day, uint32 hour, uint32 minute, uint32 second, uint32 millisecond, date_time_kind kind);
1167
1168 xtd::ticks value_ {0};
1169 date_time_kind kind_ {date_time_kind::unspecified};
1170 };
1172}
1173
1174/*
1177#if defined(__xtd__cpp_lib_format)
1178template <>
1179struct std::formatter<xtd::date_time> : std::formatter<std::string> {
1180 template<class format_context_t>
1181 constexpr auto parse(format_context_t& ctx) {
1182 auto iterator = ctx.begin();
1183 for (; iterator != ctx.end() && *iterator != '}'; ++iterator)
1184 format_value += *iterator;
1185 return iterator;
1186 }
1187
1188 template<class object_t, class format_context_t>
1189 auto format(const object_t& obj, format_context_t& ctx) const {return std::format_to(ctx.out(), "{}", std::string {obj.to_string(format_value)});}
1190
1191private:
1192 std::string format_value;
1193};
1194#endif
1196*/
Contains std::chrono::days, std::chrono::weeks, std::chrono::months and std::chrono::years duration t...
Represents an instant in time, typically expressed as a date and time of day.
Definition date_time.hpp:85
date_time(uint32 year, uint32 month, uint32 day, uint32 hour, uint32 minute, uint32 second, uint32 millisecond, date_time_kind kind)
Initializes a new instance of the xtd::date_time structure to the specified year, month,...
static const date_time min_value
Represents the smallest possible value of xtd::date_time. This field is read-only.
Definition date_time.hpp:107
date_time(int64 ticks)
Initializes a new instance of the xtd::date_time structure to a specified number of ticks.
date_time date() const noexcept
Gets the date component of this instance.
date_time(xtd::ticks ticks, xtd::date_time_kind kind)
Initializes a new instance of the xtd::date_time structure to a specified number of ticks and to Coor...
date_time(uint32 year, uint32 month, uint32 day)
Initializes a new instance of the xtd::date_time structure to the specified year, month,...
date_time(const std::chrono::time_point< clock_t, duration_t > &time_point, date_time_kind kind)
Initializes a new instance of the xtd::date_time structure to a specified time point,...
Definition date_time.hpp:226
date_time(int64 ticks, xtd::date_time_kind kind)
Initializes a new instance of the xtd::date_time structure to a specified number of ticks and to Coor...
date_time(uint32 year, uint32 month, uint32 day, uint32 hour, uint32 minute, uint32 second, date_time_kind kind)
Initializes a new instance of the xtd::date_time structure to the specified year, month,...
date_time(xtd::ticks ticks)
Initializes a new instance of the xtd::date_time structure to a specified number of ticks.
date_time()=default
Initializes a new instance of the xtd::date_time structure.
date_time(uint32 year, uint32 month, uint32 day, uint32 hour, uint32 minute, uint32 second, uint32 millisecond)
Initializes a new instance of the xtd::date_time structure to the specified year, month,...
date_time(const std::chrono::time_point< clock_t, duration_t > &time_point)
Initializes a new instance of the xtd::date_time structure to a specified time point.
Definition date_time.hpp:216
date_time(uint32 year, uint32 month, uint32 day, uint32 hour, uint32 minute, uint32 second)
Initializes a new instance of the xttd::date_time structure to the specified year,...
static const date_time max_value
Represents the largest possible value of xtd::date_time. This field is read-only.
Definition date_time.hpp:97
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
Represents any time zone in the world.
Definition time_zone_info.hpp:38
Contains xtd::date_time_kind enum class.
Contains xtd::day_of_week enum class.
#define core_export_
Define shared library export.
Definition core_export.hpp:13
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
date_time_kind
Specifies whether a DateTime object represents a local time, a Coordinated Universal Time (UTC),...
Definition date_time_kind.hpp:21
month_of_year
Specifies the month of the year.
Definition month_of_year.hpp:21
day_of_week
Specifies the day of the week.
Definition day_of_week.hpp:25
Contains xtd::icomparable interface.
Contains xtd::iequatable interface.
Contains xtd::iformatable interface.
Contains xtd::month_of_year enum class.
The xtd namespace contains all fundamental classes to access Hardware, Os, System,...
Definition xtd_about_box.hpp:10
Contains xtd::object class.
Contains xtd::string alias.
Contains xtd::ticks typedef.
Contains xtd::time_span class.