Get Pacific Time in Ruby without Rails or installing gems - ruby

In Ruby, I want to get the current Pacific Time, without having to worry if it’s in Daylight Savings or not. I can get either PST or PDT just fine:
Time.parse('7am PST')
Time.parse('7am PDT')
But I’m having no luck getting Pacific time in general. I’ve tried Pacific, US/Pacific, PT, and a bunch of others.
Is there a way to figure out if current Pacific Time is PST or PDT using a vanila Ruby installation (i.e. no Rails or downloading gems)?

ENV['TZ'] variable can be set to work with Ruby's Time in the specified timezone:
ENV['TZ'] = 'US/Pacific'
Time.now # => 2018-09-09 07:22:30 -0700
Time.local(2018, 9, 9, 7, 20) # => 2018-09-09 07:20:00 -0700
Time.local(2018, 3, 9, 7, 20) # => 2018-03-09 07:20:00 -0800

Related

brownie ganache chain has incorrect timestamp

I'm playing around with an ethereum webapp. To test my frontend client, I want to perform some transactions 'in the past', so when my client reads the current timestamp, the visuals will update accordingly. For that to work, I want to start my local chain with initial block at 1980-01-01 00:00:00+00:00.
I've edited brownie-config.yaml to add a --time option to ganache (time: 1980-01-01T00:00:00+00:00), and it seems to be propagated to the ganache-cli command, but the chain.time method gives me the current time:
$ brownie console
Brownie v1.14.6 - Python development framework for Ethereum
MyProject is the active project.
Launching 'ganache-cli --accounts 10 --hardfork istanbul --gasLimit 12000000 --mnemonic hill --port 8545 --time 1980-01-01 00:00:00+00:00'...
Brownie environment is ready.
>>> from datetime import datetime
>>> datetime.fromtimestamp(chain.time())
datetime.datetime(2021, 5, 15, 17, 28, 37) # Should be (1980, 1, 1, 0, 0, 0)
>>>
Am I missing something?

Combine multiple folders into one

I was trying to combine content( jpg files: IMG_6837.jpg IMG_6838.jpg IMG_6839.jpg IMG_6840.jpg) of following folders into one "July.2015", I tried many ways from former question posters (In terminal, merging multiple folders into one) but still does not work.
Hope you guys can help me out.
Thanks so much!
July 1, 2015
July 17, 2015
July 19, 2015
Manchester - Robertson - Tolland Tpke, July 18, 2015
Mansfield - Connecticut, August 1, 2015
Mansfield - Connecticut, July 11, 2015
Mansfield, CT - Knollwood, July 15, 2015
Newport - Rhode Island, July 4, 2015
University of Connecticut - Mansfield, CT, August 4, 2015
University of Connecticut - Mansfield, CT, July 10, 2015
University of Connecticut - Mansfield, CT, July 13, 2015
Willimantic - Connecticut, July 23, 2015
Assuming all those folders are under a folder called UNMERGED, then do this:
$ for f in /UNMERGED/* ; do rsync -a ./$f/ ./MERGED/ ; done

l10n support in ruby

I am able to parse localized dates using python locale module and posix localization database:
import locale, datetime
locale.setlocale(locale.LC_TIME, 'tr_TR.UTF-8')
print datetime.datetime.strptime("1 Haziran 2014", "%d %B %Y")
===
Edit
This example loads the locale and datetime modules, parses the localized date to create an instance of python's datetime class. I'm looking specifically for Ruby code that can parse localized dates using posix database.
===
Is there any equivalent of this in ruby? If there is a ruby library like python's locale module or Boost.Locale in C++, can you give example code? I tried the gettext gem and locale gem (I set current locale and tried Time.strptime, which failed).
I do not expect to do custom gsub or an i18n config file parsing. I am asking for code that uses posix database to parse dates.
You will need 2 custom gems in your Gemfile
Chronic:
$ git clone git://github.com/mojombo/chronic.git
$ cd chronic && gem build chronic.gemspec
$ gem install chronic-*.gem
Chronic-l10n:
$ git clone git://github.com/luan/chronic-l10n.git
$ cd chronic-l10n && gem build chronic-l10n.gemspec
$ gem install chronic-l10n-*.gem
Usage:
require 'chronic'
require 'chronic-l10n'
Time.now #=> Sun Aug 27 23:18:25 PDT 2006
Chronic.locale = :'pt-BR'
Chronic.parse('amanhã')
#=> Mon Aug 28 12:00:00 PDT 2006
Chronic.parse('segunda', :context => :past)
#=> Mon Aug 21 12:00:00 PDT 2006
Chronic.parse('essa terça 5:00')
#=> Tue Aug 29 17:00:00 PDT 2006
Chronic.parse('essa terça 5:00', :ambiguous_time_range => :none)
#=> Tue Aug 29 05:00:00 PDT 2006
Chronic.parse('27 de maio', :now => Time.local(2000, 1, 1))
#=> Sat May 27 12:00:00 PDT 2000
Chronic.parse('27 de maio', :guess => false)
#=> Sun May 27 00:00:00 PDT 2007..Mon May 28 00:00:00 PDT 2007
Chronic.parse('6/4/2012', :endian_precedence => :little)
#=> Fri Apr 06 00:00:00 PDT 2012
It seems there is currently no ruby gem or code that can load posix database and parse dates using strptime style flags. (copy-paste answer is not accepted)

How to create a new timezone in Ruby?

I want to create a timezone definition for UTC+n without any DST changes for our testing purposes, but haven't figured out how to do it. I'm using ActiveSupport::TimeZone, but it seems that it has a hard-coded list of time zones which cannot be programmatically extended.
In particular, however I try to create a new timezone, it is always missing DST information. If I provide an existing timezone as a basis, it ignores the time offset I provided instead.
Below are some attempts I tried to create a timezone in UTC+1:
> ActiveSupport::TimeZone[1].now
=> Tue, 06 Aug 2013 12:39:35 CEST +02:00
> ActiveSupport::TimeZone.create("UTC", 3600).now
=> Tue, 06 Aug 2013 10:39:40 UTC +00:00
> ActiveSupport::TimeZone.create("foo", 3600).now
TZInfo::InvalidTimezoneIdentifier: cannot load such file -- tzinfo/definitions/foo
> ActiveSupport::TimeZone.create("foo", 3600, TZInfo::Timezone.get("UTC")).now
=> Tue, 06 Aug 2013 10:39:48 UTC +00:00

feedzirra update doesn't find updates

I am trying to use feedzirra to grab rss/atom feeds, but feedzirra doesn't seem to find updates. I start by grabbing new feed and dumping the returned structure to a file.
require 'rubygems'
require 'yaml'
require 'feedzirra'
feed = Feedzirra::Feed.fetch_and_parse("http://rss.slashdot.org/Slashdot/slashdot")
File.open('slashdot.yaml','w'){|f| f.puts feed.to_yaml}
Then I wait a while, so that there are update to the feed, and I try:
require 'rubygems'
require 'yaml'
require 'feedzirra'
feed = YAML.load_file('slashdot.yaml')
puts feed.entries.first.published
updated_feed = Feedzirra::Feed.update(feed)
puts updated_feed.new_entries.first.published
all_new = Feedzirra::Feed.fetch_and_parse("http://rss.slashdot.org/Slashdot/slashdot")
puts all_new.entries.first.published
This results in:
Thu Apr 04 15:28:00 UTC 2013
Thu Apr 04 15:28:00 UTC 2013
Thu Apr 04 21:50:00 UTC 2013
The third line confirms that there are newer posts available, but Feed.update doesn't see them.
What am I doing wrong?

Resources