Join GitHub today
GitHub is home to over 31 million developers working together to host and review code, manage projects, and build software together.
Sign upAdds "converting a local time to UTC time and vice versa" recipe #364
Conversation
Lapz
changed the title
Fixes #360
Adds "converting a local time to UTC time and vice versa" recipe
Nov 9, 2017
budziq
requested changes
Nov 11, 2017
| [`HashMap`]: https://doc.rust-lang.org/std/collections/struct.HashMap.html | ||
| [`hmac::Signature`]: https://docs.rs/ring/*/ring/hmac/struct.Signature.html | ||
| [`IndependentSample::ind_sample`]: https://doc.rust-lang.org/rand/rand/distributions/trait.IndependentSample.html#tymethod.ind_sample | ||
| [`Lines`]: https://doc.rust-lang.org/std/io/struct.Lines.html | ||
| [`Local::now`]: | ||
| https://docs.rs/chrono/0.4.0/chrono/offset/struct.Local.html#method.now |
This comment has been minimized.
This comment has been minimized.
| @@ -1415,10 +1442,14 @@ fn main() { | |||
| [`File::open`]: https://doc.rust-lang.org/std/fs/struct.File.html#method.open | |||
| [`File::try_clone`]: https://doc.rust-lang.org/std/fs/struct.File.html#method.try_clone | |||
| [`File`]: https://doc.rust-lang.org/std/fs/struct.File.html | |||
| [`FixedOffset`]: | |||
This comment has been minimized.
This comment has been minimized.
budziq
Nov 11, 2017
Collaborator
- please keep the reference links in a single line
- I'd suggest to qualify the name with at least one more hierarchy segment
| [`HashMap`]: https://doc.rust-lang.org/std/collections/struct.HashMap.html | ||
| [`hmac::Signature`]: https://docs.rs/ring/*/ring/hmac/struct.Signature.html | ||
| [`IndependentSample::ind_sample`]: https://doc.rust-lang.org/rand/rand/distributions/trait.IndependentSample.html#tymethod.ind_sample | ||
| [`Lines`]: https://doc.rust-lang.org/std/io/struct.Lines.html | ||
| [`Local::now`]: |
This comment has been minimized.
This comment has been minimized.
budziq
Nov 11, 2017
Collaborator
- please keep the reference links in a single line
- I'd suggest to qualify the name with at least one more hierarchy segment
chrono::offset::Local::noworoffset::Local::now
| @@ -1265,6 +1266,32 @@ fn main() { | |||
| println!("Time elapsed in expensive_function() is: {:?}", duration); | |||
| } | |||
| ``` | |||
| [ex-convert-datetime]: #ex-convert-datetime | |||
This comment has been minimized.
This comment has been minimized.
| @@ -1265,6 +1266,32 @@ fn main() { | |||
| println!("Time elapsed in expensive_function() is: {:?}", duration); | |||
| } | |||
| ``` | |||
| [ex-convert-datetime]: #ex-convert-datetime | |||
| <a name="ex-convert-datetime"></a> | |||
| ## Converting a local time to UTC time and vice versa | |||
This comment has been minimized.
This comment has been minimized.
budziq
Nov 11, 2017
Collaborator
Adding "timezone" to the title would go a long way in making it more discoverable
| ## Converting a local time to UTC time and vice versa | ||
| [![chrono-badge]][chrono] [![cat-date-and-time-badge]][cat-date-and-time] | ||
|
|
||
| Gets the local time and displays it using [`Local::now`] and then converts it to the UTC format using the [`DateTime`] struct. A timezone is then constructed using the [`FixedOffset`] struct and the UTC time is the converted to UTC+8 and UTC-8. |
This comment has been minimized.
This comment has been minimized.
budziq
Nov 11, 2017
Collaborator
- s/time is the converted/time is then converted/
- "using the [
DateTime] struct" could use a linked mention aboutDateTime::from_utc
| let local_time: DateTime<Local> = Local::now(); | ||
| let utc_time: DateTime<Utc> = DateTime::<Utc>::from_utc(local_time.naive_utc(), Utc); | ||
| let china_timezone: FixedOffset = FixedOffset::east(28_800); | ||
| let la_timezone: FixedOffset = FixedOffset::west(28_800); |
This comment has been minimized.
This comment has been minimized.
budziq
Nov 11, 2017
Collaborator
I'd suggest to add a little more variety to the example by changint the offset in either "east" or "west" wariant
Lapz
force-pushed the
Lapz:master
branch
from
c9e4a7d
to
ae7732a
Nov 12, 2017
This comment has been minimized.
This comment has been minimized.
|
@budziq I've made the necessary changes requested. |
budziq
requested changes
Nov 12, 2017
|
Some final suggestions and we are good to merge. |
| @@ -1265,6 +1266,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> | |||
| ## Converting a local time to UTC time and vice versa | |||
This comment has been minimized.
This comment has been minimized.
budziq
Nov 12, 2017
Collaborator
lets change the title to include "timezone" for better discoverability
| ## Converting a local time to UTC time 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 format using the [`DateTime::from_utc`] struct method. A time is then converted using the [`offset::FixedOffset`] struct and the UTC time is the converted to UTC+8 and UTC-. |
This comment has been minimized.
This comment has been minimized.
budziq
Nov 12, 2017
Collaborator
- UTC is a "standard" not a "format"
- s/time is the converted/time is then converted/
- "UTC-" is missing the value
| let china_timezone: FixedOffset = FixedOffset::east(28_800); | ||
| let rio_timezone: FixedOffset = FixedOffset::west(7_200); | ||
| println!("Local time now is {}", local_time); | ||
| println!("Utc time now is {}", utc_time); |
This comment has been minimized.
This comment has been minimized.
| @@ -1415,10 +1443,12 @@ fn main() { | |||
| [`File::open`]: https://doc.rust-lang.org/std/fs/struct.File.html#method.open | |||
| [`File::try_clone`]: https://doc.rust-lang.org/std/fs/struct.File.html#method.try_clone | |||
| [`File`]: https://doc.rust-lang.org/std/fs/struct.File.html | |||
| [`offset::FixedOffset`: https://docs.rs/chrono/*/chrono/offset/struct.FixedOffset.html | |||
This comment has been minimized.
This comment has been minimized.
| [`HashMap`]: https://doc.rust-lang.org/std/collections/struct.HashMap.html | ||
| [`hmac::Signature`]: https://docs.rs/ring/*/ring/hmac/struct.Signature.html | ||
| [`IndependentSample::ind_sample`]: https://doc.rust-lang.org/rand/rand/distributions/trait.IndependentSample.html#tymethod.ind_sample | ||
| [`Lines`]: https://doc.rust-lang.org/std/io/struct.Lines.html | ||
| [`offset::Local::now`]:https://docs.rs/chrono/*/chrono/offset/struct.Local.html#method.now |
This comment has been minimized.
This comment has been minimized.
| @@ -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] | | |||
| | [Converting a local time to UTC time and vice versa][ex-convert-datetime-timezone] | [![chrono-badge]][chrono] | [![cat-date-and-time-badge]][cat-date-and-time] | | |||
This comment has been minimized.
This comment has been minimized.
budziq
Nov 12, 2017
Collaborator
Please use imperative form instead of continuous one in the example title.
s/Converting/Convert/
| @@ -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] | | |||
| | [Converting a local time to UTC time and vice versa][ex-convert-datetime-timezone] | [![chrono-badge]][chrono] | [![cat-date-and-time-badge]][cat-date-and-time] | | |||
This comment has been minimized.
This comment has been minimized.
budziq
Nov 12, 2017
Collaborator
Please use imperative form instead of continuous one in the example title.
s/Converting/Convert/
| let utc_time: DateTime<Utc> = DateTime::<Utc>::from_utc(local_time.naive_utc(), Utc); | ||
| let china_timezone: FixedOffset = FixedOffset::east(28_800); | ||
| let rio_timezone: FixedOffset = FixedOffset::west(7_200); | ||
| println!("Local time now is {}", local_time); |
This comment has been minimized.
This comment has been minimized.
budziq
Nov 12, 2017
Collaborator
It would be cool to show what is the local TZ offset (as well as printout the other TZ offsets)
But I'll leave it up to you.
ludwigpacifici
reviewed
Nov 13, 2017
| fn main() { | ||
| let local_time: DateTime<Local> = Local::now(); | ||
| let utc_time: DateTime<Utc> = DateTime::<Utc>::from_utc(local_time.naive_utc(), Utc); | ||
| let china_timezone: FixedOffset = FixedOffset::east(28_800); |
This comment has been minimized.
This comment has been minimized.
ludwigpacifici
Nov 13, 2017
•
Contributor
@budziq, To ease the understanding, is it worth to change 28_800 to 8 * 3600?
This comment has been minimized.
This comment has been minimized.
budziq
Nov 13, 2017
Collaborator
Looking at the api examples for east I see that the author thinks such information is nontrivial.
So I'm inclined to follow their convention
ludwigpacifici
reviewed
Nov 13, 2017
| let local_time: DateTime<Local> = Local::now(); | ||
| let utc_time: DateTime<Utc> = DateTime::<Utc>::from_utc(local_time.naive_utc(), Utc); | ||
| let china_timezone: FixedOffset = FixedOffset::east(28_800); | ||
| let rio_timezone: FixedOffset = FixedOffset::west(7_200); |
This comment has been minimized.
This comment has been minimized.
ludwigpacifici
Nov 13, 2017
•
Contributor
@budziq, To ease the understanding, is it worth to change 7_200 to 2 * 3600?
ludwigpacifici
reviewed
Nov 13, 2017
| fn main() { | ||
| let local_time: DateTime<Local> = Local::now(); | ||
| let utc_time: DateTime<Utc> = DateTime::<Utc>::from_utc(local_time.naive_utc(), Utc); |
This comment has been minimized.
This comment has been minimized.
ludwigpacifici
Nov 13, 2017
Contributor
@budziq, is it worth it to specify all the types for the variable? For me, it does not bring useful information and makes the example slightly shorter.
This comment has been minimized.
This comment has been minimized.
budziq
Nov 13, 2017
•
Collaborator
If the information could be inferred the any type parameters or annotations should be removed for brevity. Good catch!
This comment has been minimized.
This comment has been minimized.
|
@Lapz there are still some unaddressed review comments left. Would you be able to look into those and squash+rebase the commit to latest master (without the merge commit)? |
Lapz commentedNov 9, 2017
•
edited
fixes #360