xtd 0.2.0
Loading...
Searching...
No Matches
date_time.h
Go to the documentation of this file.
1
4#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 "iformatable.h"
11#include "month_of_year.h"
12#include "object.h"
13#include "ticks.h"
14#include "time_span.h"
15#include "string.h"
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);
212
214 date_time(const date_time&) = default;
215 date_time(date_time&&) = default;
216 date_time& operator =(const date_time&) = default;
218
219
221
230 date_time date() const noexcept;
231
260 uint32 day() const noexcept;
261
269 xtd::day_of_week day_of_week() const noexcept;
270
277 uint32 day_of_year() const noexcept;
278
312 uint32 hour() const noexcept;
313
321 date_time_kind kind() const noexcept;
322
362 uint32 millisecond() const noexcept;
363
391 uint32 minute() const noexcept;
392
420 uint32 month() const noexcept;
421
449 uint32 second() const noexcept;
450
456 int64 ticks() const noexcept;
457
463 xtd::ticks ticks_duration() const noexcept;
464
472 xtd::time_span time_of_day() const noexcept;
473
502 uint32 year() const noexcept;
504
506
518 date_time add(const xtd::time_span& value) const;
519
530 date_time add_days(double value) const;
531
542 date_time add_hours(double value) const;
543
554 date_time add_milliseconds(double value) const;
555
565 date_time add_minutes(double value) const;
566
576 date_time add_months(int32 months) const;
577
587 date_time add_seconds(double value) const;
588
594 date_time add_ticks(int64 value) const;
595
607 date_time add_years(int32 value) const;
608
609 int32 compare_to(const date_time& value) const noexcept override;
610
611 using object::equals;
612 bool equals(const date_time&) const noexcept override;
613
616 std::vector<string> get_date_time_formats() const noexcept;
617
622 bool is_daylight_saving_time() const noexcept;
623
635 xtd::time_span subtract(const date_time& value) const;
655 date_time subtract(const xtd::time_span& value) const;
656
660 int64 to_binary() const;
661
667 int64 to_file_time() const;
668
674 int64 to_file_time_utc() const;
675
690 date_time to_local_time() const;
691
695 const xtd::string to_long_date_string() const;
696
700 const xtd::string to_long_time_string() const;
701
705 const xtd::string to_short_date_string() const;
706
710 const xtd::string to_short_time_string() const;
711
715 xtd::string to_string() const noexcept override;
772 xtd::string to_string(const string& format) const;
830 xtd::string to_string(const string& format, const std::locale& loc) const override;
831
836 std::time_t to_time_t() const;
837
842 std::tm to_tm() const;
843
856 date_time to_universal_time() const;
858
860
869 static date_time now() noexcept;
870
878 static date_time today() noexcept;
879
888 static date_time utc_now() noexcept;
889
903 static int32 days_in_month(uint32 year, month_of_year month);
904
918 static int32 days_in_month(uint32 year, uint32 month);
919
926 static date_time from_binary(int64 date_data);
927
950 static date_time from_file_time(int64 file_time);
958 static date_time from_file_time_utc(int64 file_time);
959
964 static date_time from_duration(const time_span& value);
970 static date_time from_duration(const time_span& value, date_time_kind kind);
971
978 static date_time from_time_t(std::time_t value);
986 static date_time from_time_t(std::time_t value, date_time_kind kind);
987
994 static date_time from_tm(const std::tm& value);
1002 static date_time from_tm(const std::tm& value, date_time_kind kind);
1003
1008 static bool is_leap_year(uint32 year);
1009
1017 static date_time parse(const xtd::string& s);
1018
1026 static date_time specify_kind(const date_time& value, date_time_kind kind);
1027
1095 static xtd::string sprintf(const string& format, const date_time& value);
1096
1102 static bool try_parse(const string& s, date_time& result) noexcept;
1104
1106 operator xtd::time_span() const;
1107 date_time operator +=(const date_time& value) = delete;
1108 date_time operator +=(const time_span& value);
1109 time_span operator -=(const date_time& value);
1110 time_span operator -=(const time_span& value);
1111 date_time operator +();
1112 date_time operator -();
1113 date_time operator +(const date_time& value) const = delete;
1114 date_time operator +(const time_span& value) const;
1115 time_span operator -(const date_time& value) const;
1116 date_time operator -(const time_span& value) const;
1117 date_time& operator ++();
1118 date_time operator ++(int32);
1119 date_time& operator --();
1120 date_time operator --(int32);
1122
1123 private:
1124 friend class time_zone_info;
1125 xtd::ticks utc_offset() const;
1126 std::tuple<uint32, uint32, uint32, uint32, uint32, uint32, uint32, int32> get_date_time() const;
1127 void set_date_time(uint32 year, uint32 month, uint32 day, uint32 hour, uint32 minute, uint32 second, uint32 millisecond, date_time_kind kind);
1128
1129 xtd::ticks value_ {0};
1130 date_time_kind kind_ {date_time_kind::unspecified};
1131 };
1133}
1134
1135/*
1138#if defined(__xtd__cpp_lib_format)
1139template <>
1140struct std::formatter<xtd::date_time> : std::formatter<std::string> {
1141 template<typename format_context_t>
1142 constexpr auto parse(format_context_t& ctx) {
1143 auto iterator = ctx.begin();
1144 for (; iterator != ctx.end() && *iterator != '}'; ++iterator)
1145 format_value += *iterator;
1146 return iterator;
1147 }
1148
1149 template <typename object_t, typename format_context_t>
1150 auto format(const object_t& obj, format_context_t& ctx) const {return std::format_to(ctx.out(), "{}", std::string {obj.to_string(format_value)});}
1151
1152private:
1153 std::string format_value;
1154};
1155#endif
1157*/
Contains dyas, weeks, months and years durationtypes.
Represents an instant in time, typically expressed as a date and time of day.
Definition date_time.h: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.h: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(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(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.h:97
Defines a generalized comparison method that a value type or class implements to create a type-specif...
Definition icomparable.h:21
Defines a generalized method that a value type or class implements to create a type-specific method f...
Definition iequatable.h:22
Provides functionality to format the value of an object into a string representation.
Definition iformatable.h:35
Supports all classes in the xtd class hierarchy and provides low-level services to derived classes....
Definition object.h:42
Represents any time zone in the world.
Definition time_zone_info.h: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.h:13
int32_t int32
Represents a 32-bit signed integer.
Definition int32.h:23
int64_t int64
Represents a 64-bit signed integer.
Definition int64.h:23
uint32_t uint32
Represents a 32-bit unsigned integer.
Definition uint32.h:23
std::chrono::duration< int64, tick > ticks
Represents a tick duration.
Definition ticks.h:21
date_time_kind
Specifies whether a DateTime object represents a local time, a Coordinated Universal Time (UTC),...
Definition date_time_kind.h:21
month_of_year
Specifies the month of the year.
Definition month_of_year.h:21
day_of_week
Specifies the day of the week.
Definition day_of_week.h: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.h:10
Contains xtd::object class.
Contains xtd::string alias.
Represents a time interval.
Definition time_span.h:29
Contains xtd::ticks typedef.
Contains xtd::time_span typedef.