I'm using Ruby 1.9.3 with gems timezone 0.3.1 and tzinfo 0.3.38.
I'm looking to retrieve the current timezone code for a timezone, e.g. 'EST' for 'America/New_York' right now, or 'EDT' in the summer.
I've found nothing in the documentation, yet when I dump the object with the following code:
ptz = Timezone::Zone.new :zone => 'America/New_York'
hash = {}
ptz.instance_variables.each do |var|
hash[var.to_s.tr('#','')] = ptz.instance_variable_get var
end
puts hash.to_json
there is an array of 267 "rules", each a hash where "name" contains the value I am looking.
Does anyone know how I should determine which rule is current, so I can retrieve the name value?
require 'tzinfo'
tz = TZInfo::Timezone.get('America/New_York')
tz.strftime("%Z")
=> "EST"
TZInfo::Timezone.get('America/New_York').period_for_utc(Time.now).abbreviation gives you the abbr. for the time provided.
Related
require 'time'
require 'yaml'
date = DateTime.parse('22018-01-05')
serialized_date = YAML.dump(date) # => "--- !ruby/object:DateTime 22018-01-05 00:00:00.000000000 Z\n"
YAML.load(serialized_date) # => NoMethodError (undefined method `captures' for nil:NilClass)
The same happens for Time and Date serializes it as a String.
For context the date string is actually a bad string, the year should be 2018.
ruby 2.6.0
It looks like this is because psych, in ScalarScanner#parse_time uses a regex which expects a 4 digit year.
EDIT: I have since opened a bug report with the psych project.
Why is 'time' being returned as an invalid date?
val = "9/22/2011 4:23 AM"
time = DateTime.parse(val).strftime("%Y-%m-%d %H:%M:%S").to_datetime
#at breakpoint: time = 2011-09-22T04:23:00+00:00 as a DateTime Object
#form_entry.storage_datetime = time # crashes here with invalid date
If it helps, I'm using gem mysql 2.8.1 and Ruby 1.9.2. Thanks
I got an ArgumentError on line two; couldn't create the DateTime object in the first place.
Try using strptime instead:
val = "9/22/2011 4:23 AM"
DateTime.strptime(val, "%m/%d/%Y %H:%M %p")
=> #<DateTime: 2011-09-22T04:23:00+00:00 (3536390423/1440,0/1,2299161)>
I have a date string 20101129220021, so I will use
require 'date'
d = DateTime.parse('20101129220021')
This part works fine, and I get a date, which is in UTC.
My question is, how can I convert this into my local time? I tried many methods like extracting the time part using d.to_time and manipulate the result, but it didn't work. As far as I know, DateTime object is immutable. Can I please get some help?
irb(main):001:0> require "date"
=> true
irb(main):002:0> d = DateTime.parse('20101129220021')
=> #<DateTime: 2010-11-29T22:00:21+00:00 (70719276007/28800,0/1,2299161)>
irb(main):003:0> d.to_time
=> 2010-11-30 00:00:21 +0200
ruby 1.9.2p180 (2011-02-18)
You can add a rational fraction based on the timezone to get the local time.
require 'date'
# Make this whatever your zone is. Using UTC +0300 here.
ZONE = 3
d = DateTime.parse('20101129220021') + Rational(ZONE,24)
d.to_s # => "2010-11-30T01:00:21+00:00"
I'm looking for the best way to take a datetime string from MySQL, explode it in Ruby and return the month, date, and year in separate elements.
How is the string formatted? You could just convert the datetime string into a datetime object, and call the instance methods.
require 'time'
x = "2009/04/16 19:52:30" #grab your datetime string from the database and assign it
y = DateTime.strptime(x, "%Y/%m/%d %H:%M:%S") #create a new date object
Then a simple y.day() yields:
y.day() = 16
and y.hour():
y.hour() = 19
FYI never actually used Ruby much, so this is what I got out of playing with the console, so hopefully this helps shed some light.
require 'time'
x = "2009/04/16 19:52:30"
begin
y = Time.parse(x)
[y.year, y.month, y.day] # => [2009, 4, 16]
rescue ArgumentError
puts "cannot parse date: #{x}"
end
I write my first project wich using Datamapper as ORM, so please be patient. :)
I try to do get String from DateTime field:
Error.first.submitted_at.to_s
=> "2009-08-24T12:13:32+02:00"
Returned String is not good for me. In ActiveRecord I can do something like that:
Error.first.submitted_at.to_s(:only_date)
or any other date formatter. Is somethig similar available in DataMapper or I must to use strftime method?
That's a feature available using AcitveSupport. You can do require 'activesupport' to get it. That might be overkill, though. You could also use #stamp from Facets to do the same thing, but you have to set up the :only_date format:
require 'facets/date'
Date::FORMAT[:only_date] = '%d.%m.%y' # For Date objects
Time::FORMAT[:only_date] = '%d.%m.%y' # For DateTime objects
d = DateTime.now
d.stamp(:only_date) # => "24.08.09"
If you really want to use it with the to_s method, you can do that, too:
require 'facets/date'
Date::FORMAT[:only_date] = '%d.%m.%y' # For Date objects
Time::FORMAT[:only_date] = '%d.%m.%y' # For DateTime objects
class DateTime
alias :default_to_s :to_s
def to_s(format=nil)
if format.nil?
default_to_s
else
stamp format
end
end
end
d = DateTime.now
d.to_s(:only_date) # => "24.08.09"