How to parse date and time with Date.strptime - ruby

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"

Related

Change the format of date from "mm/dd/yyyy" to "Month dd, yyyy" in Ruby

I am trying to extract date from XML and compare it with the date in a PDF.
I am using Nokogiri to get the date from XML and PDF-Reader to read the date from PDF.
But the date in XML is in "mm/dd/yyyy" format and the date in PDF is in "Month dd, yyyy" format.
XML Tag:
<LetterSendDate>02/29/2016</LetterSendDate>
Extracting the Date from xml using Nokogiri:
#reader = file('C:\Users\ecz560\Desktop\30004_Standard.pdf').parse_pdf
#xml = file('C:\Users\ecz560\Desktop\30004_Standard.xml').parse_xmlDoc
#LettersendDate = #xml.xpath("//Customer[RTLtr_Loancust='0163426']//RTLtr_LetterSendDate").map(&:text)
Comparing the XML date with the date in PDF:
page_index = 0
#reader.pages.each do |page|
page_index = page_index+1
if expect(page.text).to include #LettersendDate
valid_text = "Given text is present in -- #{page_index}"
puts valid_text
end
end
but expect(page.text) returns February 29, 2016
so it is giving me error while comparing
Error
if expect(page.text).to include #LettersendDate
TypeError: no implicit conversion of String into Array
How can I convert the date from "mm/dd/yy" format to "Month dd, yyyy format" ?

Nicest way to turn "2015-03-18 22:00" into a Time object

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

How to convert Timestamp from mysql into Date/Time object

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

Is there a way to get a TZInfo style string for the local timezone?

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"]

Retrieving date format with am/pm in codeigniter

I have converted date to my local time as below:
$this->date_string = "%Y/%m/%d %h:%i:%s";
$timestamp = now();
$timezone = 'UP45';
$daylight_saving = TRUE;
$time = gmt_to_local($timestamp, $timezone, $daylight_saving);
$this->updated_date = mdate($this->date_string,$time);
And I'm storing this field in to database.
Now at retrieval time I want format like this:
"11-04-2011 4:50:00 PM"
I have used this code:
$timestamp = strtotime($rs->updated_date);
$date1 = "%d-%m-%Y %h:%i:%s %a";
$updat1 = date($date1,$timestamp);
But this will give me only
"11-04-2011 4:50:00 AM"
But I have stored it like it was PM.
Might get voted down, but will have a go at it.
Is it because the MySQL stores it in 24 hour format? (assuming you are using the datetime field type)
Maybe this will help
Converting mysql TIME from 24 HR to AM/PM format
sorry if it doesn't.

Resources