DESCRIPTION
I need to get input as a string date that can be complete date-time format with timezone, it could be just date, it could be just time in 24 hour format, time with timezone in 24 hour format, or a time in am and pm or any combination of above.
As a output I want to get current(or given) date along with time in 24 hour format and a timezone, which we can parse in a ruby Time.parse(). I have done some work and you can find source code below. I have also added rspec (test case) for some of the expectation.
You can provide your own solution or update my source as you like.
EXAMPLES
NOTE: here expected date is current date if date not provided
'2200+5'should get converted to"2016-1-1T22:00:00 +0500"'100'should get converted to"2016-1-1T01:00:00 +0000"'1700+8'should get converted to"2016-1-1T17:00:00 +0800"'5pm+8'should get converted to"2016-1-1T17:00:00 +0800"'5am+8'should get converted to"2016-1-1T5:00:00 +0800"'2016-12-15T2300+5'should get converted to'2016-12-15T23:00:00 +0500''1730'should get converted to"2016-1-1T17:30:00 +0000"'530pm+8'should get converted to"2016-1-1T17:30:00 +0800"'1210am+8'should get converted to"2016-1-1T12:10:00 +0800"
RSpec
describe "should convert datetime to accepted format"
Timecop.freeze(Time.utc(2016,1,1,2,0,0))
it {expect(parse('2200+5')).to eq({time: Time.parse("2016-1-1T22:00:00 +0500")})}
it {expect(parse('100')).to eq({time: Time.parse("2016-1-1T01:00:00 +0000")})}
it {expect(parse('1700+8')).to eq({time: Time.parse("2016-1-1T17:00:00 +0800")})}
it {expect(parse('5pm+8')).to eq({time: Time.parse("2016-1-1T17:00:00 +0800")})}
it {expect(parse('5am+8')).to eq({time: Time.parse("2016-1-1T5:00:00 +0800")})}
it {expect(parse('2016-12-15T2300+5')).to eq({time: Time.parse("2016-12-15T23:00:00 +0500")})}
it {expect(parse('1730')).to eq({time: Time.parse("2016-1-1T17:30:00 +0000")})}
Timecop.return
end
WHAT I HAVE DONE SO FAR
# ensure 2 char in time format
def ensure_2_char_in_time(t)
return "0#{t}" if t.to_s.length == 1
t.to_s
end
def get_time_zone_from(aTimeParams)
date, tzm, tzm_type = nil
# get date, time, timezon from given string
if aTimeParams.include?('-') && aTimeParams.include?('T')
date, time_zone = aTimeParams.split('T')
else
time_zone = aTimeParams
end
# get time, zone from given string
if time_zone.include?('+')
tzm_type = '+'
time_str, tzm = aTimeParams.split('+')
elsif time_zone.include?('-')
tzm_type = '-'
time_str, tzm = aTimeParams.split('-')
else
tzm_type = '+'
time_str = aTimeParams
end
date = "#{Date.today.year}-#{Date.today.month}-#{Date.today.day}" if date.blank?
if time_str.include?('pm')
hour = ensure_2_char_in_time(time_str.to_i + 12)
min = '00'
elsif time_str.include?('am')
hour = ensure_2_char_in_time(time_str.to_i)
min = '00'
else
hour = ensure_2_char_in_time(time_str.to_i / 100)
min = ensure_2_char_in_time(time_str.to_i % 100)
end
if tzm.to_s.length <= 2
tzm_h = ensure_2_char_in_time tzm.to_i % 100
tzm_m = "00"
else
tzm_h = ensure_2_char_in_time tzm.to_i / 100
tzm_m = ensure_2_char_in_time tzm.to_i % 100
end
{
time: Time.parse("#{date}T#{hour}:#{min}:00 #{tzm_type}#{tzm_h}:#{tzm_m}"),
tzm: (tzm_h.to_i * 60 + tzm_m.to_i)
}
end
Please let me know for further clarification.
Thanks