diff --git a/README.md b/README.md index 206469c..3e63163 100644 --- a/README.md +++ b/README.md @@ -10,7 +10,7 @@ pairing with smart people at Hashrocket. For a steady stream of TILs, [sign up for my newsletter](https://crafty-builder-6996.ck.page/e169c61186). -_1418 TILs and counting..._ +_1419 TILs and counting..._ --- @@ -905,6 +905,7 @@ _1418 TILs and counting..._ - [Load A File When Starting Rails Console](rails/load-a-file-when-starting-rails-console.md) - [Load Records In Batches With find_each](rails/load-records-in-batches-with-find-each.md) - [Log SQL Queries Executed By ActiveRecord](rails/log-sql-queries-executed-by-activerecord.md) +- [Look Up Time Zone Info For Identifier](rails/look-up-time-zone-info-for-identifier.md) - [Mark A Migration As Irreversible](rails/mark-a-migration-as-irreversible.md) - [Make A String Attribute Easy To Inquire About](rails/make-a-string-attribute-easy-to-inquire-about.md) - [Make ActionMailer Synchronous In Test](rails/make-action-mailer-synchronous-in-test.md) diff --git a/rails/look-up-time-zone-info-for-identifier.md b/rails/look-up-time-zone-info-for-identifier.md new file mode 100644 index 0000000..b415c75 --- /dev/null +++ b/rails/look-up-time-zone-info-for-identifier.md @@ -0,0 +1,33 @@ +# Look Up Time Zone Info For Identifier + +The `ActiveSupport::TimeZone` class overrides the `#[]` method to be a lookup +mechanism for IANA Time Zone Identifier strings. These are strings like +`America/Chicago` (or anything else listed under `TZInfo::Timezone.all`). + +Let's get an instance for `America/Chicago`. + +```ruby +> chi = ActiveSupport::TimeZone['America/Chicago'] +=> #, + @utc_offset=nil> +``` + +Notice it has a `tzinfo` instance variable that we can access. That object +contains all kinds of useful things. + +```ruby +> chi.tzinfo.name +=> "America/Chicago" +> chi.tzinfo.friendly_identifier +=> "America - Chicago" +> chi.tzinfo.abbr +=> "CDT" +> chi.tzinfo.utc_offset +=> -18000 +> chi.tzinfo.dst? +=> true +``` + +All of these and more. Run `ls chi.tzinfo` in a `pry` session to see what else.