I am fetching data from a mysql database in a non rails project using ruby. The data has a TIMESTAMP type, how can I convert it to a ruby date/time object so that I can do date comparisons on it?
These are some of the values coming from the db:
2014-03-17 22:56:02
2011-05-17 21:46:22
Use ::strptime
require 'date'
string = '2014-03-17 22:56:02'
DateTime.strptime(string, "%Y-%m-%d %H:%M:%S")
# => #<DateTime: 2014-03-17T22:56:02+00:00 ((2456734j,82562s,0n),+0s,2299161j)>
Try this
t = Time.at(<msqlTimestamp>)
puts t.to_date
Related
If I use Date#strptime to parse an Exif date like 2017:03:11 18:02:30 the time is ignored:
Date.strptime("2017:03:11 18:02:30", '%Y:%m:%d %H:%M:%S').strftime('%Y:%m:%d %H:%M:%S')
=> "2017:03:11 00:00:00"
What am I doing wrong?
Date doesn't contain information about the exact time, use DateTime instead:
DateTime.strptime("2017:03:11 18:02:30", '%Y:%m:%d %H:%M:%S').strftime('%Y:%m:%d %H:%M:%S')
=> "2017:03:11 18:02:30"
I am using spreadsheet (1.0.3) and I am trying to read the date from a cell that has a =TODAY() formula in it. This is what I've tried so far:
require 'spreadsheet'
require 'date'
Spreadsheet.client_encoding = 'UTF-8'
book = Spreadsheet.open 'document.xls', 'rb'
sheet1 = book.worksheet 0
puts sheet1.row(0)[0].value # => 42167.0
I get 42167.0 when the date returned value for the =TODAY() formula in the spreadsheet is 2015-06-12, for 2015-06-13 I get 42168.0. The thing is that I am not seing a way to convert this number to a Date object.
Any DATETIME in excel is stored as DAY.Hour format (with 0.0 being 00 Jan 1900).
See: https://stackoverflow.com/a/14597674/91830
I have three strings, pulled from a database:
"2015-03-18" (the date the event occurs)
"22:00" (the hour an event occurs)
"-05:00" (the UTC offset in the location the event occurs).
I want to combine these three strings to produce a Ruby Time object. I'm doing:
utc_offset = "-05:00"
airtime = "22:00"
airday = "2015-03-18"
year,month,day,hour,minutes = airday.split("-").map(&:to_i) + airtime.split(":").map(&:to_i)
Time.new(year,month,day,hour,minutes,0,utc_offset)
This works; I'm just wondering if it's the correct/standard/idiomatic/clearest way.
By using Time.parse
When ‘time’ is required, Time is extended with additional methods for
parsing and converting Times.
require 'time'
utc_offset = "-05:00"
airtime = "22:00"
airday = "2015-03-18"
time = Time.parse("#{airday} #{airtime} #{utc_offset}")
I think this is the way to do it.
Time.new(*airday.split("-"), *airtime.split(":"), 0, utc_offset)
How about this? http://ruby-doc.org/stdlib-2.1.5/libdoc/time/rdoc/Time.html#method-c-strptime
require 'time'
raw_time = '2015-03-18 22:00'
parsed_time = Time.strptime(raw_time, '%Y-%m-%d %H:%M') # 2015-03-18 22:00:00 +0100
I have a datestring in this format
yyyy-mm-ddThh:mm:ss[Z]
And i have a timezone string. for e.g "Asia/Kolkata"
Now i want to convert this date string into the timezone of the given timezone
for e.g. if the date is 2014-01-03T23:30:00Z , then in "Asia/Kolkata" timezone it will be 2014-01-04T05:00:00 .
I tried using Time library , but Time library does not seem to have any method which can convert to other timzone http://ruby-doc.org/core-1.8.6/Time.html#method-c-mktime .
You should use the TZInfo gem.
require 'tzinfo'
tz = TZInfo::Timezone.get('Asia/Kolkata')
utc = DateTime.iso8601('2014-01-03T23:30:00Z')
local = tz.utc_to_local(utc)
If it just a ruby project without rails, also you do not want to install third party gems.
Then you have to do it yourself, the following is what I did:
def convert_time_to_timezone(time, timezone)
timezone_in_hours = timezone.to_i
time_in_seconds = time.to_i
time_in_seconds_in_timezone = time_in_seconds + timezone_in_hours*3600
utc_time = Time.at(time_in_seconds_in_timezone).utc
Time.new(utc_time.year, utc_time.month, utc_time.day, utc_time.hour, utc_time.min, utc_time.sec, "#{timezone}:00")
end
Just provide time (e.g., Time.now) and timezone (e.g., "+11:00", or "-05:00")
it will return the time with the specified timezone.
example:
call convert_time_to_timezone(Time.now, "+11:00") it will return something like
2017-05-31 18:17:13 +1100
If it has rails installed, then you can directly call in_time_zone('"Central Time (US & Canada)"')
I need to try to get a TZInfo style string a-la 'America/New_York' representing the local timezone of the system I'm on. I can't figure out how to do it.
Time.zone
#<ActiveSupport::TimeZone:0x007ff2e4f89240 #name="UTC", #utc_offset=nil, #tzinfo=#<TZInfo::TimezoneProxy: Etc/UTC>, #current_period=#<TZInfo::TimezonePeriod: nil,nil,#<TZInfo::TimezoneOffsetInfo: 0,0,UTC>>>>
Here the TimezoneProxy#Etc/UTC field is the style I want but is UTC not local time.
Time.now.zone
"EST"
Here the "EST" is not what I want and I don't see a way to pass Time.now or EST to TZInfo to get what I want?
Is there a way to get "America/New_York" or even a list of all equivalent timezone strings based on my current timezone?
You can try to find all EST aliases with:
current_tz = ActiveSupport::TimeZone['EST']
ActiveSupport::TimeZone.
all.
select{|tz| tz.utc_offset == current_tz.utc_offset }.
map(&:tzinfo).
map(&:name).
uniq
will produce
["America/Bogota", "EST", "America/New_York", "America/Indiana/Indianapolis", "America/Lima"]
But I think this is incorrect, because of daylight saving issues. More correct code:
current_tz = ActiveSupport::TimeZone['EST']
ActiveSupport::TimeZone.
all.
select{|tz|
tz.utc_offset == current_tz.utc_offset &&
tz.tzinfo.current_period.dst? == current_tz.tzinfo.current_period.dst?
}.
map(&:tzinfo).
map(&:name).
uniq
will produce
["America/Bogota", "EST", "America/Lima"]