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<class clock_t, class 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<class clock_t, class 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 date_time date() const noexcept;
246
275 uint32 day() const noexcept;
276
284 xtd::day_of_week day_of_week() const noexcept;
285
292 uint32 day_of_year() const noexcept;
293
327 uint32 hour() const noexcept;
328
336 date_time_kind kind() const noexcept;
337
377 uint32 millisecond() const noexcept;
378
406 uint32 minute() const noexcept;
407
435 uint32 month() const noexcept;
436
464 uint32 second() const noexcept;
465
471 int64 ticks() const noexcept;
472
478 xtd::ticks ticks_duration() const noexcept;
479
487 xtd::time_span time_of_day() const noexcept;
488
517 uint32 year() const noexcept;
519
521
533 date_time add(const xtd::time_span& value) const;
534
545 date_time add_days(double value) const;
546
557 date_time add_hours(double value) const;
558
569 date_time add_milliseconds(double value) const;
570
580 date_time add_minutes(double value) const;
581
592
602 date_time add_seconds(double value) const;
603
610
623
634 int32 compare_to(const date_time& value) const noexcept override;
635
639 bool equals(const object& obj) const noexcept override;
643 bool equals(const date_time& other) const noexcept override;
644
647 xtd::size get_hash_code() const noexcept override;
648
651 array<string> get_date_time_formats() const noexcept;
652
657 bool is_daylight_saving_time() const noexcept;
658
670 xtd::time_span subtract(const date_time& value) const;
690 date_time subtract(const xtd::time_span& value) const;
691
696
703
710
726
730 const xtd::string to_long_date_string() const;
731
735 const xtd::string to_long_time_string() const;
736
740 const xtd::string to_short_date_string() const;
741
745 const xtd::string to_short_time_string() const;
746
750 xtd::string to_string() const noexcept override;
886 xtd::string to_string(const string& format) const;
1023 xtd::string to_string(const string& format, const xtd::globalization::culture_info& culture) const override;
1027 xtd::string to_string(const xtd::globalization::culture_info& culture) const;
1028
1033 std::time_t to_time_t() const;
1034
1039 std::tm to_tm() const;
1040
1055
1057
1066 static date_time now() noexcept;
1067
1075 static date_time today() noexcept;
1076
1085 static date_time utc_now() noexcept;
1086
1101
1116
1123 static date_time from_binary(int64 date_data);
1124
1147 static date_time from_file_time(int64 file_time);
1156
1161 static date_time from_duration(const time_span& value);
1168
1175 static date_time from_time_t(std::time_t value);
1183 static date_time from_time_t(std::time_t value, date_time_kind kind);
1184
1191 static date_time from_tm(const std::tm& value);
1199 static date_time from_tm(const std::tm& value, date_time_kind kind);
1200
1206
1214 static date_time parse(const xtd::string& s);
1222 static date_time parse(const xtd::string& s, const xtd::globalization::culture_info& culture);
1223
1232
1300 static xtd::string sprintf(const string& format, const date_time& value);
1369 static xtd::string sprintf(const string& format, const date_time& value, const xtd::globalization::culture_info& culture);
1370
1376 static bool try_parse(const string& s, date_time& result) noexcept;
1382 static bool try_parse(const string& s, date_time& result, const xtd::globalization::culture_info& culture) noexcept;
1383
1384 static bool try_parse_exact(const string& text, const string& format, date_time& result) noexcept;
1385 static bool try_parse_exact(const string& text, const string& format, date_time& result, const xtd::globalization::culture_info& culture) noexcept;
1386 static bool try_parse_exact(const string& text, const array<string>& formats, date_time& result) noexcept;
1387 static bool try_parse_exact(const string& text, const array<string>& formats, date_time& result, const xtd::globalization::culture_info& culture) noexcept;
1389
1391 operator xtd::time_span() const;
1392 date_time operator +=(const date_time& value) = delete;
1393 date_time operator +=(const time_span& value);
1394 time_span operator -=(const date_time& value);
1395 time_span operator -=(const time_span& value);
1396 date_time operator +();
1397 date_time operator -();
1398 date_time operator +(const date_time& value) const = delete;
1399 date_time operator +(const time_span& value) const;
1400 time_span operator -(const date_time& value) const;
1401 date_time operator -(const time_span& value) const;
1402 date_time& operator ++();
1403 date_time operator ++(int32);
1404 date_time& operator --();
1405 date_time operator --(int32);
1407
1408 private:
1409 friend class time_zone_info;
1410 xtd::ticks utc_offset() const;
1411 std::tuple<uint32, uint32, uint32, uint32, uint32, uint32, uint32, int32> get_date_time() const;
1413 static string standard_format_to_custom_format(char standard_format, const xtd::globalization::culture_info& culture) noexcept;
1414 string to_string_custom(const string& format, const xtd::globalization::culture_info& culture) const;
1415 string to_string_put_time(const string& format, const xtd::globalization::culture_info& culture) const;
1416 string to_string_standard(char format, const xtd::globalization::culture_info& culture) const;
1417
1418 xtd::ticks value_ {0};
1420 };
1421
1422}
1423
1424/*
1427#if defined(__xtd__cpp_lib_format)
1428template <>
1429struct std::formatter<xtd::date_time> : std::formatter<std::string> {
1430 template<class format_context_t>
1431 constexpr auto parse(format_context_t& ctx) {
1432 auto iterator = ctx.begin();
1433 for (; iterator != ctx.end() && *iterator != '}'; ++iterator)
1434 format_value += *iterator;
1435 return iterator;
1436 }
1437
1438 template<class object_t, class format_context_t>
1439 auto format(const object_t& obj, format_context_t& ctx) const {return std::format_to(ctx.out(), "{}", std::string {obj.to_string(format_value)});}
1440
1441private:
1442 std::string format_value;
1443};
1444#endif
1446*/
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:63
Represents an instant in time, typically expressed as a date and time of day.
Definition date_time.hpp:83
xtd::string to_string() const noexcept override
Converts the value of the current xtd::date_time object to its equivalent string representation using...
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,...
date_time add_milliseconds(double value) const
Returns a new xtd::date_time that adds the specified number of milliseconds to the value of this inst...
date_time add_hours(double value) const
Returns a new xtd::date_time that adds the specified number of hours to the value of this instance.
static date_time utc_now() noexcept
Gets a xtd::date_time object that is set to the current date and time on this computer,...
int64 ticks() const noexcept
Gets the number of ticks that represent the date and time of this instance.
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
date_time(int64 ticks)
Initializes a new instance of the xtd::date_time structure to a specified number of ticks.
date_time add_ticks(int64 value) const
Returns a new xtd::date_time that adds the specified number of ticks to the value of this instance.
const xtd::string to_short_time_string() const
Converts the value of the current xtd::date_time object to its equivalent short time string represent...
date_time date() const noexcept
Gets the date component of this instance.
static date_time from_tm(const std::tm &value)
Converts the specified std::tm to an equivalent unspecified time.
const xtd::string to_long_date_string() const
Converts the value of the current xtd::date_time object to its equivalent long date string representa...
const xtd::string to_short_date_string() const
Converts the value of the current xtd::date_time object to its equivalent short date string represent...
std::time_t to_time_t() const
Converts the value of the current xtd::date_time object to std::time_t.
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...
uint32 day_of_year() const noexcept
Gets the day of the year represented by this instance.
static int32 days_in_month(uint32 year, month_of_year month)
Returns the number of days in the specified month and year.
date_time add_seconds(double value) const
Returns a new xtd::date_time that adds the specified number of seconds to the value of this instance.
date_time add_years(int32 value) const
Returns a new xtd::date_time that adds the specified number of years to the value of this instance.
int64 to_file_time_utc() const
Converts the value of the current xtd::date_time object to a Windows file time.
date_time(uint32 year, uint32 month, uint32 day)
Initializes a new instance of the xtd::date_time structure to the specified year, month,...
static date_time from_file_time(int64 file_time)
Converts the specified Windows file time to an equivalent local time.
uint32 minute() const noexcept
Gets the minute component of the date represented by this instance.
static xtd::string sprintf(const string &format, const date_time &value)
Returns a xtd::string that represents the current xtd::date_time.
static date_time from_duration(const time_span &value)
Converts the specified xtd::time_span to an equivalent unspecified time.
static date_time today() noexcept
Gets the current date.
static date_time parse(const xtd::string &s)
Converts the string representation of a date and time to its xtd::date_time equivalent by using the c...
date_time to_universal_time() const
Converts the value of the current xtd::date_time object to Coordinated Universal Time (UTC).
int64 to_binary() const
Serializes the current xtd::date_time object to a 64-bit binary value that subsequently can be used t...
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
uint32 day() const noexcept
Gets the day of the month represented by this instance.
xtd::ticks ticks_duration() const noexcept
Gets the number of ticks that represent the date and time of this instance.
date_time add(const xtd::time_span &value) const
Returns a new xtd::date_time that adds the value of the specified xtd::time_span to the value of this...
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 add_minutes(double value) const
Returns a new xtd::date_time that adds the specified number of minutes to the value of this instance.
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 add_days(double value) const
Returns a new xtd::date_time that adds the specified number of days to the value of this instance.
int32 compare_to(const date_time &value) const noexcept override
Compares the current instance with another object of the same type.
uint32 month() const noexcept
Gets the month component of the date represented by this instance.
static date_time from_file_time_utc(int64 file_time)
Converts the specified Windows file time to an equivalent UTC time.
static date_time from_time_t(std::time_t value)
Converts the specified std::time_t to an equivalent unspecified time.
date_time(xtd::ticks ticks)
Initializes a new instance of the xtd::date_time structure to a specified number of ticks.
xtd::day_of_week day_of_week() const noexcept
Gets the day of the week represented by this instance.
date_time to_local_time() const
Converts the value of the current xtd::date_time object to local time.
date_time()=default
Initializes a new instance of the xtd::date_time structure.
bool is_daylight_saving_time() const noexcept
Indicates whether this instance of xtd::date_time is within the daylight saving time range for the cu...
uint32 year() const noexcept
Gets the year component of the date represented by this instance.
uint32 millisecond() const noexcept
Gets the milliseconds component of the date represented by this instance.
static bool is_leap_year(uint32 year)
Returns an indication whether the specified year is a leap year.
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,...
xtd::time_span subtract(const date_time &value) const
Returns a new xtd::time_span that subtracts the specified date and time from the value of this instan...
xtd::time_span time_of_day() const noexcept
Gets the time of day for 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
static bool try_parse(const string &s, date_time &result) noexcept
Converts the specified string representation of a date and time to its xtd::date_time equivalent and ...
int64 to_file_time() const
Converts the value of the current xtd::date_time object to a Windows file time.
std::tm to_tm() const
Converts the value of the current xtd::date_time object to std::tm.
xtd::size get_hash_code() const noexcept override
Serves as a hash function for a particular type.
date_time_kind kind() const noexcept
Gets a value that indicates whether the time represented by this instance is based on local time,...
static date_time from_binary(int64 date_data)
Deserializes a 64-bit binary value and recreates an original serialized xtd::date_time object.
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,...
const xtd::string to_long_time_string() const
Converts the value of the current xtd::date_time object to its equivalent long time string representa...
static date_time now() noexcept
Gets a xtd::date_time object that is set to the current date and time on this computer,...
static date_time specify_kind(const date_time &value, date_time_kind kind)
Creates a new xtd::date_time object that has the same number of ticks as the specified xtd::date_time...
uint32 second() const noexcept
Gets the seconds component of the date represented by this instance.
bool equals(const object &obj) const noexcept override
Determines whether the specified object is equal to the current object.
date_time add_months(int32 months) const
Returns a new xtd::date_time that adds the specified number of months to the value of this instance.
uint32 hour() const noexcept
Gets the hour component of the date represented by this instance.
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
array< string > get_date_time_formats() const noexcept
Converts the value of this instance to all the string representations supported by the standard date ...
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:41
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: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: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
@ unspecified
The time represented is not specified as either local time or Coordinated Universal Time (UTC).
Definition date_time_kind.hpp:23
@ local
The time represented is local time.
Definition date_time_kind.hpp:27
@ other
The operating system is other.
Definition platform_id.hpp:60
@ s
The S key.
Definition console_key.hpp:124
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.