xtd 0.2.0
Loading...
Searching...
No Matches

◆ to_local_time()

date_time xtd::date_time::to_local_time ( ) const

Converts the value of the current xtd::date_time object to local time.

Returns
An object whose xtd::date_time::kind property is xtd::date_time_kind::local, and whose value is the local time equivalent to the value of the current xtd::date_time object, or xtd::date_time::max_value if the converted value is too large to be represented by a xtd::date_time object, or xtd::date_time::min_value if the converted value is too small to be represented as a xtd::date_time object.
Examples
The following example uses the xtd::date_time::specify_kind method to demonstrate how the xtd::date_time::kind property influences the xtd::date_time::to_local_time and xtd::date_time::to_universal_time conversion methods.
#include <xtd/console>
#include <xtd/date_time>
#include <xtd/startup>
using namespace xtd;
class program {
public:
static auto main() {
// Get the date and time for the current moment, adjusted
// to the local time zone.
auto save_now = date_time::now();
// Get the date and time for the current moment expressed
// as coordinated universal time (UTC).
auto save_utc_now = date_time::utc_now();
auto my_dt = date_time {};
// display the value and kind property of the current moment
// expressed as UTC and local time.
display_now("utc_now: ...........", save_utc_now);
display_now("now: ...............", save_now);
console::write_line();
// Change the kind property of the current moment to
// date_time_kind::utc and display the result.
my_dt = date_time::specify_kind(save_now, date_time_kind::utc);
display("utc: ...............", my_dt);
// Change the kind property of the current moment to
// date_time_kind::local and display the result.
my_dt = date_time::specify_kind(save_now, date_time_kind::local);
display("local: .............", my_dt);
// Change the kind property of the current moment to
// date_time_kind::unspecified and display the result.
my_dt = date_time::specify_kind(save_now, date_time_kind::unspecified);
display("unspecified: .......", my_dt);
}
// display the value and kind() property of a date_time structure, the
// date_time structure converted to local time, and the date_time
// structure converted to universal time.
static void display(const string& title, const date_time& input_dt) {
auto disp_dt = input_dt;
auto dt_string = string::empty_string;
// display the original date_time.
dt_string = disp_dt.to_string("u");
console::write_line("{0} {1}, kind = {2}", title, dt_string, disp_dt.kind());
// Convert input_dt to local time and display the result.
// If input_dt.kind() is date_time_kind.Utc, the conversion is performed.
// If input_dt.kind() is date_time_kind::local, the conversion is not performed.
// If input_dt.kind() is date_time_kind::unspecified, the conversion is
// performed as if input_dt was universal time.
disp_dt = input_dt.to_local_time();
dt_string = disp_dt.to_string("u");
console::write_line(" to_local_time: {0}, kind = {1}", dt_string, disp_dt.kind());
// Convert input_dt to universal time and display the result.
// If input_dt.kind() is date_time_kind.Utc, the conversion is not performed.
// If input_dt.kind() is date_time_kind::local, the conversion is performed.
// If input_dt.kind() is date_time_kind::unspecified, the conversion is
// performed as if input_dt was local time.
disp_dt = input_dt.to_universal_time();
dt_string = disp_dt.to_string("u");
console::write_line(" to_universal_time: {0}, kind = {1}", dt_string, disp_dt.kind());
console::write_line();
}
// display the value and kind property for date_time::now() and date_time::utc_now().
static void display_now(const string& title, const date_time& input_dt) {
auto dt_string = input_dt.to_string("u");
console::write_line("{0} {1}, kind = {2}", title, dt_string, input_dt.kind());
}
};
startup_(program::main);
// This code can produce the following output :
//
// utc_now: ........... 2021-12-31 17:08:41, Kind = utc
// now: ............... 2021-12-31 18:08:41, Kind = local
//
// utc: ............... 2021-12-31 17:08:41, Kind = utc
// to_local_time: 2021-12-31 18:08:41, Kind = local
// to_universal_time: 2021-12-31 17:08:41, Kind = utc
//
// local: ............. 2021-12-31 18:08:41, Kind = local
// to_local_time: 2021-12-31 18:08:41, Kind = local
// to_universal_time: 2021-12-31 17:08:41, Kind = utc
//
// unspecified: ....... 2021-12-31 18:08:41, Kind = unspecified
// to_local_time: 2021-12-31 19:08:41, Kind = local
// to_universal_time: 2021-12-31 17:08:41, Kind = utc
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 to_universal_time() const
Converts the value of the current xtd::date_time object to Coordinated Universal Time (UTC).
date_time to_local_time() const
Converts the value of the current xtd::date_time object to local time.
date_time_kind kind() const noexcept
Gets a value that indicates whether the time represented by this instance is based on local time,...
Represents an instant in time, typically expressed as a date and time of day.
Definition date_time.h:85
#define startup_(main_method)
Defines the entry point to be called when the application loads. Generally this is set either to the ...
Definition startup.h:175
The xtd namespace contains all fundamental classes to access Hardware, Os, System,...
Definition xtd_about_box.h:10
Remarks
The local time is equal to the Coordinated Universal Time (UTC) time plus the UTC offset. For more information about the UTC offset, see xtd::time_zone_info::get_utc_offset. The conversion also takes into account the daylight saving time rule that applies to the time represented by the current xtd::date_time object.
The value returned by the xtd::date_time::to_local_time method is determined by the xtd::date_time::kind property of the current xtd::date_time object. The following table describes the possible results.
Kind Results
xtd::date_time_kind::utc This instance of xtd::date_timextd::date_time is converted to local time.
xtd::date_time_kind::local No conversion is performed.
xtd::date_time_kind::unspecified This instance of xtd::date_time is assumed to be a UTC time, and the conversion is performed as if xtd::date_time::kind were xtd::date_time_kind::utc.
Note
The xtd::date_time::to_local_time method converts a xtd::date_time:: value from UTC to local time. To convert the time in any designated time zone to local time, use the xtd::time_zone_info::convert_time method.
Remarks
The value returned by the conversion is a xtd::date_time whose xtd::date_time::Kind property always returns xtd::date_time_kind::local. Consequently, a valid result is returned even if xtd::date_time::to_local_time is applied repeatedly to the same xtd::date_time.
Examples
date_time_specify_kind.cpp.