mirror of
https://github.com/rust-lang-nursery/rust-cookbook
synced 2025-02-16 12:18:27 +00:00
Adds "converting a local time to UTC time and vice versa" recipe
This commit is contained in:
parent
eec1522d90
commit
3b1a4f5f94
2 changed files with 32 additions and 0 deletions
|
@ -27,6 +27,7 @@
|
|||
| [Check number of logical cpu cores][ex-check-cpu-cores] | [![num_cpus-badge]][num_cpus] | [![cat-hardware-support-badge]][cat-hardware-support] |
|
||||
| [Obtain backtrace of complex error scenarios][ex-error-chain-backtrace] | [![error-chain-badge]][error-chain] | [![cat-rust-patterns-badge]][cat-rust-patterns] |
|
||||
| [Measure elapsed time][ex-measure-elapsed-time] | [![std-badge]][std] | [![cat-time-badge]][cat-time] |
|
||||
| [Convert a local time to an another UTC timezone and vice versa][ex-convert-datetime-timezone] | [![chrono-badge]][chrono] | [![cat-date-and-time-badge]][cat-date-and-time] |
|
||||
| [Display formatted date and time][ex-format-datetime] | [![chrono-badge]][chrono] | [![cat-date-and-time-badge]][cat-date-and-time] |
|
||||
| [Parse string into DateTime struct][ex-parse-datetime] | [![chrono-badge]][chrono] | [![cat-date-and-time-badge]][cat-date-and-time] |
|
||||
| [Perform checked date and time calculations][ex-datetime-arithmetic] | [![chrono-badge]][chrono] | [![cat-date-and-time-badge]][cat-date-and-time] |
|
||||
|
@ -1266,6 +1267,32 @@ fn main() {
|
|||
println!("Time elapsed in expensive_function() is: {:?}", duration);
|
||||
}
|
||||
```
|
||||
[ex-convert-datetime-timezone]: #ex-convert-datetime-timezone
|
||||
<a name="ex-convert-datetime-timezone"></a>
|
||||
## Convert a local time to an another UTC timezone and vice versa
|
||||
[![chrono-badge]][chrono] [![cat-date-and-time-badge]][cat-date-and-time]
|
||||
|
||||
Gets the local time and displays it using [`offset::Local::now`] and then converts it to the UTC standard using the [`DateTime::from_utc`] struct method. A time is then converted using the [`offset::FixedOffset`] struct and the UTC time is then converted to UTC+8 and UTC-2.
|
||||
|
||||
```rust
|
||||
extern crate chrono;
|
||||
|
||||
use chrono::{DateTime, FixedOffset, Local, Utc};
|
||||
|
||||
fn main() {
|
||||
let local_time = Local::now();
|
||||
let utc_time = DateTime::<Utc>::from_utc(local_time.naive_utc(), Utc);
|
||||
let china_timezone = FixedOffset::east(8 * 3600);
|
||||
let rio_timezone = FixedOffset::west(2 * 3600);
|
||||
println!("Local time now is {}", local_time);
|
||||
println!("UTC time now is {}", utc_time);
|
||||
println!(
|
||||
"Time in Hong Kong now is {}",
|
||||
utc_time.with_timezone(&china_timezone)
|
||||
);
|
||||
println!("Time in Rio de Janeiro now is {}", utc_time.with_timezone(&rio_timezone));
|
||||
}
|
||||
```
|
||||
|
||||
[ex-format-datetime]: #ex-format-datetime
|
||||
<a name="ex-format-datetime"></a>
|
||||
|
@ -1451,6 +1478,7 @@ fn main() {
|
|||
[`DateTime::to_rfc2822`]: https://docs.rs/chrono/*/chrono/struct.DateTime.html#method.to_rfc2822
|
||||
[`DateTime::to_rfc3339`]: https://docs.rs/chrono/*/chrono/struct.DateTime.html#method.to_rfc3339
|
||||
[`DateTime::format`]: https://docs.rs/chrono/*/chrono/struct.DateTime.html#method.format
|
||||
[`DateTime::from_utc`]:https://docs.rs/chrono/*/chrono/struct.DateTime.html#method.from_utc
|
||||
[`DateTime::checked_add_signed`]: https://docs.rs/chrono/*/chrono/struct.Date.html#method.checked_add_signed
|
||||
[`DateTime::checked_sub_signed`]: https://docs.rs/chrono/*/chrono/struct.Date.html#method.checked_sub_signed
|
||||
[`DateTime`]: https://docs.rs/chrono/*/chrono/struct.DateTime.html
|
||||
|
@ -1474,6 +1502,8 @@ fn main() {
|
|||
[`Normal`]: https://doc.rust-lang.org/rand/rand/distributions/normal/struct.Normal.html
|
||||
[`num_cpus::get`]: https://docs.rs/num_cpus/*/num_cpus/fn.get.html
|
||||
[`Output`]: https://doc.rust-lang.org/std/process/struct.Output.html
|
||||
[`offset::FixedOffset`]: https://docs.rs/chrono/*/chrono/offset/struct.FixedOffset.html
|
||||
[`offset::Local::now`]: https://docs.rs/chrono/*/chrono/offset/struct.Local.html#method.now
|
||||
[`pbkdf2::derive`]: https://docs.rs/ring/*/ring/pbkdf2/fn.derive.html
|
||||
[`pbkdf2::verify`]: https://docs.rs/ring/*/ring/pbkdf2/fn.verify.html
|
||||
[`process::Stdio`]: https://doc.rust-lang.org/std/process/struct.Stdio.html
|
||||
|
|
|
@ -45,6 +45,7 @@ community. It needs and welcomes help. For details see
|
|||
| [Check number of logical cpu cores][ex-check-cpu-cores] | [![num_cpus-badge]][num_cpus] | [![cat-hardware-support-badge]][cat-hardware-support] |
|
||||
| [Obtain backtrace of complex error scenarios][ex-error-chain-backtrace] | [![error-chain-badge]][error-chain] | [![cat-rust-patterns-badge]][cat-rust-patterns] |
|
||||
| [Measure elapsed time][ex-measure-elapsed-time] | [![std-badge]][std] | [![cat-time-badge]][cat-time] |
|
||||
| [Convert a local time to an another UTC timezone and vice versa][ex-convert-datetime-timezone] | [![chrono-badge]][chrono] | [![cat-date-and-time-badge]][cat-date-and-time] |
|
||||
| [Display formatted date and time][ex-format-datetime] | [![chrono-badge]][chrono] | [![cat-date-and-time-badge]][cat-date-and-time] |
|
||||
| [Parse string into DateTime struct][ex-parse-datetime] | [![chrono-badge]][chrono] | [![cat-date-and-time-badge]][cat-date-and-time] |
|
||||
| [Perform checked date and time calculations][ex-datetime-arithmetic] | [![chrono-badge]][chrono] | [![cat-date-and-time-badge]][cat-date-and-time] |
|
||||
|
@ -161,6 +162,7 @@ community. It needs and welcomes help. For details see
|
|||
[ex-check-broken-links]: net.html#ex-check-broken-links
|
||||
[ex-check-cpu-cores]: basics.html#ex-check-cpu-cores
|
||||
[ex-clap-basic]: app.html#ex-clap-basic
|
||||
[ex-convert-datetime-timezone]: basics.html#ex-convert-datetime-timezone
|
||||
[ex-crossbeam-spawn]: concurrency.html#ex-crossbeam-spawn
|
||||
[ex-csv-delimiter]: encoding.html#ex-csv-delimiter
|
||||
[ex-csv-filter]: encoding.html#ex-csv-filter
|
||||
|
|
Loading…
Add table
Reference in a new issue