xtd 0.2.0
Loading...
Searching...
No Matches
date_time.hpp
Go to the documentation of this file.
1
4#pragma once
6#include "chrono.hpp"
7#include "date_time_kind.hpp"
8#include "day_of_week.hpp"
9#include "icomparable.hpp"
10#include "iequatable.hpp"
11#include "iformatable.hpp"
12#include "month_of_year.hpp"
13#include "object.hpp"
14#include "ticks.hpp"
15#include "time_span.hpp"
16#include "string.hpp"
17#include <ctime>
18
20namespace xtd {
22 class time_zone_info;
24
26
83 class core_export_ date_time : public xtd::object, public xtd::icomparable<date_time>, public xtd::iequatable<date_time>, public xtd::iformatable {
84 public:
86
95 static const date_time max_value;
105 static const date_time min_value;
107
109
113 date_time() = default;
209
213 template<typename clock_t, typename duration_t = clock_t::duration> // Can't be explicit by design.
214 date_time(const std::chrono::time_point<clock_t, duration_t>& time_point) {
215 *this = from_time_t(std::chrono::system_clock::to_time_t(std::chrono::system_clock::now() + std::chrono::duration_cast<std::chrono::system_clock::duration>(time_point - clock_t::now())), date_time_kind::local);
216 }
217
222 template<typename clock_t, typename duration_t = clock_t::duration>
223 date_time(const std::chrono::time_point<clock_t, duration_t>& time_point, date_time_kind kind) {
224 *this = from_time_t(std::chrono::system_clock::to_time_t(std::chrono::system_clock::now() + std::chrono::duration_cast<std::chrono::system_clock::duration>(time_point - clock_t::now())), kind);
225 }
226
227
229 date_time(const date_time&) = default;
230 date_time(date_time&&) = default;
231 date_time& operator =(const date_time&) = default;
233
234
236
245 [[nodiscard]] auto date() const noexcept -> date_time;
246
275 [[nodiscard]] auto day() const noexcept -> xtd::uint32;
276
284 [[nodiscard]] auto day_of_week() const noexcept -> xtd::day_of_week;
285
292 [[nodiscard]] auto day_of_year() const noexcept -> xtd::uint32;
293
327 [[nodiscard]] auto hour() const noexcept -> xtd::uint32;
328
336 [[nodiscard]] auto kind() const noexcept -> xtd::date_time_kind;
337
377 [[nodiscard]] auto millisecond() const noexcept -> xtd::uint32;
378
406 [[nodiscard]] auto minute() const noexcept -> xtd::uint32;
407
435 [[nodiscard]] auto month() const noexcept -> xtd::uint32;
436
464 [[nodiscard]] auto second() const noexcept -> xtd::uint32;
465
471 [[nodiscard]] auto ticks() const noexcept -> xtd::int64;
472
478 [[nodiscard]] auto ticks_duration() const noexcept -> xtd::ticks;
479
487 [[nodiscard]] auto time_of_day() const noexcept -> xtd::time_span;
488
517 [[nodiscard]] auto year() const noexcept -> xtd::uint32;
519
521
533 [[nodiscard]] auto add(const xtd::time_span& value) const -> date_time;
534
545 [[nodiscard]] auto add_days(double value) const -> date_time;
546
557 [[nodiscard]] auto add_hours(double value) const -> date_time;
558
569 [[nodiscard]] auto add_milliseconds(double value) const -> date_time;
570
580 [[nodiscard]] auto add_minutes(double value) const -> date_time;
581
591 [[nodiscard]] auto add_months(xtd::int32 months) const -> date_time;
592
602 [[nodiscard]] auto add_seconds(double value) const -> date_time;
603
609 [[nodiscard]] auto add_ticks(xtd::int64 value) const -> date_time;
610
622 [[nodiscard]] auto add_years(xtd::int32 value) const -> date_time;
623
634 [[nodiscard]] auto compare_to(const date_time& value) const noexcept -> xtd::int32 override;
635
639 [[nodiscard]] auto equals(const object& obj) const noexcept -> bool override;
643 [[nodiscard]] auto equals(const date_time& other) const noexcept -> bool override;
644
647 [[nodiscard]] auto get_hash_code() const noexcept -> xtd::size override;
648
651 [[nodiscard]] auto get_date_time_formats() const noexcept -> xtd::array<xtd::string>;
652
657 [[nodiscard]] auto is_daylight_saving_time() const noexcept -> bool;
658
670 [[nodiscard]] auto subtract(const date_time& value) const -> xtd::time_span;
690 [[nodiscard]] auto subtract(const xtd::time_span& value) const -> date_time;
691
695 [[nodiscard]] auto to_binary() const -> xtd::int64;
696
702 [[nodiscard]] auto to_file_time() const -> xtd::int64;
703
709 [[nodiscard]] auto to_file_time_utc() const -> xtd::int64;
710
725 [[nodiscard]] auto to_local_time() const -> date_time;
726
730 [[nodiscard]] auto to_long_date_string() const -> xtd::string;
731
735 [[nodiscard]] auto to_long_time_string() const -> xtd::string;
736
740 [[nodiscard]] auto to_short_date_string() const -> xtd::string;
741
745 [[nodiscard]] auto to_short_time_string() const -> xtd::string;
746
750 [[nodiscard]] auto to_string() const noexcept -> xtd::string override;
886 [[nodiscard]] auto to_string(const xtd::string& format) const -> xtd::string;
1023 [[nodiscard]] auto to_string(const xtd::string& format, const xtd::globalization::culture_info& culture) const -> xtd::string override;
1027 [[nodiscard]] auto to_string(const xtd::globalization::culture_info& culture) const -> xtd::string;
1028
1033 [[nodiscard]] auto to_time_t() const -> std::time_t;
1034
1039 [[nodiscard]] auto to_tm() const -> std::tm;
1040
1053 [[nodiscard]] auto to_universal_time() const -> date_time;
1055
1057
1066 [[nodiscard]] static auto now() noexcept -> date_time;
1067
1075 [[nodiscard]] static auto today() noexcept -> date_time;
1076
1085 [[nodiscard]] static auto utc_now() noexcept -> date_time;
1086
1100 [[nodiscard]] static auto days_in_month(xtd::uint32 year, xtd::month_of_year month) -> xtd::int32;
1101
1115 [[nodiscard]] static auto days_in_month(xtd::uint32 year, xtd::uint32 month) -> xtd::int32;
1116
1123 [[nodiscard]] static auto from_binary(xtd::int64 date_data) -> date_time;
1124
1147 [[nodiscard]] static auto from_file_time(xtd::int64 file_time) -> date_time;
1155 [[nodiscard]] static auto from_file_time_utc(xtd::int64 file_time) -> date_time;
1156
1161 [[nodiscard]] static auto from_duration(const xtd::time_span& value) -> date_time;
1167 static auto from_duration(const xtd::time_span& value, xtd::date_time_kind kind) -> date_time;
1168
1175 [[nodiscard]] static auto from_time_t(std::time_t value) -> date_time;
1183 [[nodiscard]] static auto from_time_t(std::time_t value, xtd::date_time_kind kind) -> date_time;
1184
1191 [[nodiscard]] static auto from_tm(const std::tm& value) -> date_time;
1199 [[nodiscard]] static auto from_tm(const std::tm& value, xtd::date_time_kind kind) -> date_time;
1200
1205 [[nodiscard]] static auto is_leap_year(xtd::uint32 year) -> bool;
1206
1214 [[nodiscard]] static auto parse(const xtd::string& s) -> date_time;
1222 [[nodiscard]] static auto parse(const xtd::string& s, const xtd::globalization::culture_info& culture) -> date_time;
1223
1231 [[nodiscard]] static auto specify_kind(const date_time& value, xtd::date_time_kind kind) -> date_time;
1232
1300 [[nodiscard]] static auto sprintf(const xtd::string& format, const xtd::date_time& value) -> xtd::string;
1369 [[nodiscard]] static auto sprintf(const xtd::string& format, const date_time& value, const xtd::globalization::culture_info& culture) -> xtd::string;
1370
1376 [[nodiscard]] static auto try_parse(const xtd::string& s, date_time& result) noexcept -> bool;
1382 [[nodiscard]] static auto try_parse(const xtd::string& s, date_time& result, const xtd::globalization::culture_info& culture) noexcept -> bool;
1383
1384 [[nodiscard]] static auto try_parse_exact(const xtd::string& text, const xtd::string& format, date_time& result) noexcept -> bool;
1385 [[nodiscard]] static auto try_parse_exact(const xtd::string& text, const xtd::string& format, date_time& result, const xtd::globalization::culture_info& culture) noexcept -> bool;
1386 [[nodiscard]] static auto try_parse_exact(const xtd::string& text, const xtd::array<xtd::string>& formats, date_time& result) noexcept -> bool;
1387 [[nodiscard]] static auto try_parse_exact(const xtd::string& text, const xtd::array<xtd::string>& formats, date_time& result, const xtd::globalization::culture_info& culture) noexcept -> bool;
1389
1391 operator xtd::time_span() const;
1392 auto operator +=(const date_time& value) -> date_time = delete;
1393 auto operator +=(const xtd::time_span& value) -> date_time;
1394 auto operator -=(const date_time& value) -> xtd::time_span;
1395 auto operator -=(const xtd::time_span& value) -> xtd::time_span;
1396 auto operator +() const -> date_time;
1397 auto operator -() const -> date_time;
1398 auto operator +(const date_time& value) const -> date_time = delete;
1399 auto operator +(const xtd::time_span& value) const -> date_time;
1400 auto operator -(const date_time& value) const -> xtd::time_span;
1401 auto operator -(const xtd::time_span& value) const -> date_time;
1402 auto operator ++() -> date_time&;
1403 auto operator ++(int) -> date_time;
1404 auto operator --() -> date_time&;
1405 auto operator --(int) -> date_time;
1406 friend auto operator << (std::ostream& os, const date_time& value) -> std::ostream& {return os << value.to_string();}
1408
1409 private:
1410 friend class xtd::time_zone_info;
1411 [[nodiscard]] auto utc_offset() const -> xtd::ticks;
1412 [[nodiscard]] auto get_date_time() const -> std::tuple<xtd::uint32, xtd::uint32, xtd::uint32, xtd::uint32, xtd::uint32, xtd::uint32, xtd::uint32, xtd::int32>;
1413 auto set_date_time(xtd::uint32 year, xtd::uint32 month, xtd::uint32 day, xtd::uint32 hour, xtd::uint32 minute, xtd::uint32 second, xtd::uint32 millisecond, xtd::date_time_kind kind) -> void;
1414 [[nodiscard]] static auto standard_format_to_custom_format(char standard_format, const xtd::globalization::culture_info& culture) noexcept -> xtd::string;
1415 [[nodiscard]] auto to_string_custom(const xtd::string& format, const xtd::globalization::culture_info& culture) const -> xtd::string;
1416 [[nodiscard]] auto to_string_put_time(const xtd::string& format, const xtd::globalization::culture_info& culture) const -> xtd::string;
1417 [[nodiscard]] auto to_string_standard(char format, const xtd::globalization::culture_info& culture) const -> xtd::string;
1418
1419 xtd::ticks value_ {0};
1420 date_time_kind kind_ {date_time_kind::unspecified};
1421 };
1422
1423}
1424
1425/*
1428#if defined(__xtd__cpp_lib_format)
1429template <>
1430struct std::formatter<xtd::date_time> : std::formatter<std::string> {
1431 template<typename format_context_t>
1432 constexpr auto parse(format_context_t& ctx) {
1433 auto iterator = ctx.begin();
1434 for (; iterator != ctx.end() && *iterator != '}'; ++iterator)
1435 format_value += *iterator;
1436 return iterator;
1437 }
1438
1439 template<typename object_t, typename format_context_t>
1440 auto format(const object_t& obj, format_context_t& ctx) const {return std::format_to(ctx.out(), "{}", std::string {obj.to_string(format_value)});}
1441
1442private:
1443 std::string format_value;
1444};
1445#endif
1447*/
Contains std::chrono::days, std::chrono::weeks, std::chrono::months and std::chrono::years duration t...
Provides methods for creating, manipulating, searching, and sorting arrays, thereby serving as the ba...
Definition array.hpp:64
Represents an instant in time, typically expressed as a date and time of day.
Definition date_time.hpp:83
auto add_years(xtd::int32 value) const -> date_time
Returns a new xtd::date_time that adds the specified number of years to the value of this instance.
date_time(xtd::uint32 year, xtd::uint32 month, xtd::uint32 day, xtd::uint32 hour, xtd::uint32 minute, xtd::uint32 second, xtd::date_time_kind kind)
Initializes a new instance of the xtd::date_time structure to the specified year, month,...
auto get_hash_code() const noexcept -> xtd::size override
Serves as a hash function for a particular type.
auto hour() const noexcept -> xtd::uint32
Gets the hour component of the date represented by this instance.
auto month() const noexcept -> xtd::uint32
Gets the month component of the date represented by this instance.
date_time(xtd::uint32 year, xtd::uint32 month, xtd::uint32 day, xtd::uint32 hour, xtd::uint32 minute, xtd::uint32 second, xtd::uint32 millisecond)
Initializes a new instance of the xtd::date_time structure to the specified year, month,...
static auto now() noexcept -> date_time
Gets a xtd::date_time object that is set to the current date and time on this computer,...
static const date_time min_value
Represents the smallest possible value of xtd::date_time. This field is read-only.
Definition date_time.hpp:105
auto to_universal_time() const -> date_time
Converts the value of the current xtd::date_time object to Coordinated Universal Time (UTC).
auto date() const noexcept -> date_time
Gets the date component of this instance.
auto day_of_week() const noexcept -> xtd::day_of_week
Gets the day of the week represented by this instance.
auto day_of_year() const noexcept -> xtd::uint32
Gets the day of the year represented by this instance.
auto to_short_date_string() const -> xtd::string
Converts the value of the current xtd::date_time object to its equivalent short date string represent...
auto add_months(xtd::int32 months) const -> date_time
Returns a new xtd::date_time that adds the specified number of months to the value of this instance.
auto to_long_date_string() const -> xtd::string
Converts the value of the current xtd::date_time object to its equivalent long date string representa...
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...
static auto from_file_time_utc(xtd::int64 file_time) -> date_time
Converts the specified Windows file time to an equivalent UTC time.
auto compare_to(const date_time &value) const noexcept -> xtd::int32 override
Compares the current instance with another object of the same type.
date_time(xtd::uint32 year, xtd::uint32 month, xtd::uint32 day, xtd::uint32 hour, xtd::uint32 minute, xtd::uint32 second)
Initializes a new instance of the xttd::date_time structure to the specified year,...
auto to_file_time_utc() const -> xtd::int64
Converts the value of the current xtd::date_time object to a Windows file time.
static auto from_duration(const xtd::time_span &value) -> date_time
Converts the specified xtd::time_span to an equivalent unspecified time.
static auto today() noexcept -> date_time
Gets the current date.
static auto specify_kind(const date_time &value, xtd::date_time_kind kind) -> date_time
Creates a new xtd::date_time object that has the same number of ticks as the specified xtd::date_time...
auto add_minutes(double value) const -> date_time
Returns a new xtd::date_time that adds the specified number of minutes to the value of this instance.
static auto utc_now() noexcept -> date_time
Gets a xtd::date_time object that is set to the current date and time on this computer,...
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:223
auto ticks_duration() const noexcept -> xtd::ticks
Gets the number of ticks that represent the date and time of this instance.
auto to_file_time() const -> xtd::int64
Converts the value of the current xtd::date_time object to a Windows file time.
auto subtract(const date_time &value) const -> xtd::time_span
Returns a new xtd::time_span that subtracts the specified date and time from the value of this instan...
date_time(xtd::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...
auto equals(const object &obj) const noexcept -> bool override
Determines whether the specified object is equal to the current object.
static auto days_in_month(xtd::uint32 year, xtd::month_of_year month) -> xtd::int32
Returns the number of days in the specified month and year.
auto to_short_time_string() const -> xtd::string
Converts the value of the current xtd::date_time object to its equivalent short time string represent...
auto get_date_time_formats() const noexcept -> xtd::array< xtd::string >
Converts the value of this instance to all the string representations supported by the standard date ...
static auto from_file_time(xtd::int64 file_time) -> date_time
Converts the specified Windows file time to an equivalent local time.
auto to_string() const noexcept -> xtd::string override
Converts the value of the current xtd::date_time object to its equivalent string representation using...
auto add_milliseconds(double value) const -> date_time
Returns a new xtd::date_time that adds the specified number of milliseconds to the value of this inst...
date_time(xtd::uint32 year, xtd::uint32 month, xtd::uint32 day, xtd::uint32 hour, xtd::uint32 minute, xtd::uint32 second, xtd::uint32 millisecond, xtd::date_time_kind kind)
Initializes a new instance of the xtd::date_time structure to the specified year, month,...
auto is_daylight_saving_time() const noexcept -> bool
Indicates whether this instance of xtd::date_time is within the daylight saving time range for the cu...
static auto try_parse(const xtd::string &s, date_time &result) noexcept -> bool
Converts the specified string representation of a date and time to its xtd::date_time equivalent and ...
date_time(xtd::ticks ticks)
Initializes a new instance of the xtd::date_time structure to a specified number of ticks.
static auto is_leap_year(xtd::uint32 year) -> bool
Returns an indication whether the specified year is a leap year.
static auto parse(const xtd::string &s) -> date_time
Converts the string representation of a date and time to its xtd::date_time equivalent by using the c...
auto minute() const noexcept -> xtd::uint32
Gets the minute component of the date represented by this instance.
auto add_days(double value) const -> date_time
Returns a new xtd::date_time that adds the specified number of days to the value of this instance.
auto kind() const noexcept -> xtd::date_time_kind
Gets a value that indicates whether the time represented by this instance is based on local time,...
date_time()=default
Initializes a new instance of the xtd::date_time structure.
date_time(xtd::int64 ticks)
Initializes a new instance of the xtd::date_time structure to a specified number of ticks.
auto day() const noexcept -> xtd::uint32
Gets the day of the month represented by this instance.
auto time_of_day() const noexcept -> xtd::time_span
Gets the time of day for this instance.
auto millisecond() const noexcept -> xtd::uint32
Gets the milliseconds component of the date represented by this instance.
static auto from_binary(xtd::int64 date_data) -> date_time
Deserializes a 64-bit binary value and recreates an original serialized xtd::date_time object.
auto year() const noexcept -> xtd::uint32
Gets the year component of the date represented by this instance.
auto second() const noexcept -> xtd::uint32
Gets the seconds component of the date represented by this instance.
static auto from_time_t(std::time_t value) -> date_time
Converts the specified std::time_t to an equivalent unspecified time.
auto add_hours(double value) const -> date_time
Returns a new xtd::date_time that adds the specified number of hours to the value of this instance.
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:214
auto to_time_t() const -> std::time_t
Converts the value of the current xtd::date_time object to std::time_t.
auto add_ticks(xtd::int64 value) const -> date_time
Returns a new xtd::date_time that adds the specified number of ticks to the value of this instance.
date_time(xtd::uint32 year, xtd::uint32 month, xtd::uint32 day)
Initializes a new instance of the xtd::date_time structure to the specified year, month,...
auto to_long_time_string() const -> xtd::string
Converts the value of the current xtd::date_time object to its equivalent long time string representa...
auto ticks() const noexcept -> xtd::int64
Gets the number of ticks that represent the date and time of this instance.
auto add_seconds(double value) const -> date_time
Returns a new xtd::date_time that adds the specified number of seconds to the value of this instance.
auto to_local_time() const -> date_time
Converts the value of the current xtd::date_time object to local time.
static auto sprintf(const xtd::string &format, const xtd::date_time &value) -> xtd::string
Returns a xtd::string that represents the current xtd::date_time.
static auto from_tm(const std::tm &value) -> date_time
Converts the specified std::tm to an equivalent unspecified time.
auto add(const xtd::time_span &value) const -> date_time
Returns a new xtd::date_time that adds the value of the specified xtd::time_span to the value of this...
auto to_binary() const -> xtd::int64
Serializes the current xtd::date_time object to a 64-bit binary value that subsequently can be used t...
static const date_time max_value
Represents the largest possible value of xtd::date_time. This field is read-only.
Definition date_time.hpp:95
auto to_tm() const -> std::tm
Converts the value of the current xtd::date_time object to std::tm.
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
Represents any time zone in the world.
Definition time_zone_info.hpp:39
Contains xtd::globalization::culture_info class.
Contains xtd::date_time_kind enum class.
Contains xtd::day_of_week enum class.
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:21
#define core_export_
Define shared library export.
Definition core_export.hpp:13
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
size_t size
Represents a size of any object in bytes.
Definition size.hpp:23
std::int32_t int32
Represents a 32-bit signed integer.
Definition int32.hpp:25
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
@ local
The time represented is local time.
Definition date_time_kind.hpp:27
Contains xtd::icomparable interface.
Contains xtd::iequatable interface.
Contains xtd::iformatable interface.
Contains xtd::month_of_year enum class.
Contains classes that define culture-related information, including language, country/region,...
Definition culture_info.hpp:20
Contains classes that represent ASCII and Unicode character encodings; abstract base classes for conv...
Definition basic_string_builder.hpp:17
The xtd namespace contains all fundamental classes to access Hardware, Os, System,...
Definition abstract_object.hpp:8
Contains xtd::object class.
Contains xtd::string alias.
Contains xtd::ticks typedef.
Contains xtd::time_span class.