I have a program that creates information for a time stamp, the time stamp has to match the following format: MM/DD/YYYY HH:MM:SS AM or PM
For example:
Like stated by mu is too short
, regex is great and all but you need more in your arsenal then just that.
def timestamp
print 'Enter time stamp: '
ts = gets.chomp.upcase
verify = DateTime.strptime(ts, '%m/%d/%Y %I:%M:%S %p')
puts ts
rescue ArgumentError
puts 'Invalid timestamp formatting must contain date time AM or PM'
timestamp
end
Of course this could cause an overflow from the buffer because it's recursive, but that chances of that happening are pretty slim.
When this is run:
irb(main):01:0> def timestamp
irb(main):02:1> print 'Enter time stamp: '
irb(main):02:1> ts = gets.chomp.upcase
irb(main):04:1> verify = DateTime.strptime(ts, '%m/%d/%Y %I:%M:%S %p')
irb(main):05:1> puts ts
irb(main):06:1> rescue ArgumentError
timestamp
irb(main):08:1> end
=> :timestamp
irb(main):09:0> timestamp
Enter time stamp: test
Invalid timestamp formatting must contain date time AM or PM
Enter time stamp: 06/30/2016 6:6:54 pm
06/30/2016 6:6:54 PM
=> nil
However it is possible that you could make a better way of checking:
def enter_stamp
print 'Enter timestamp: '
ts = gets.chomp
verify_stamp(ts)
end
def verify_stamp(timestamp)
Date.strptime(timestamp, '%m/%d/%Y %I:%M:%S %p')
puts timestamp unless ArgumentError
rescue ArgumentError
puts 'Invalid timestamp format'
enter_stamp
end
This makes it so that the methods are more OOPish, and Ruby is all about OOP so it kind of works. When this is run:
irb(main):001:0> require 'date'
=> true
irb(main):002:0>
irb(main):003:0* def enter_stamp
irb(main):004:1> print 'Enter timestamp: '
irb(main):005:1> ts = gets.chomp
irb(main):006:1> verify_stamp(ts)
irb(main):007:1> end
=> :enter_stamp
irb(main):008:0>
irb(main):009:0* def verify_stamp(timestamp)
irb(main):010:1> Date.strptime(timestamp, '%m/%d/%Y %I:%M:%S %p')
irb(main):011:1> puts timestamp unless ArgumentError
irb(main):012:1> rescue ArgumentError
irb(main):013:1> puts 'Invalid timestamp format'
irb(main):014:1> enter_stamp
irb(main):015:1> end
=> :verify_stamp
irb(main):016:0> enter_stamp
Enter timestamp: test
Invalid timestamp format
Enter timestamp: 06/12/2016 8:08:54 pm
=> nil
And of course you could also use just a regex:
def verify_timestamp(stamp)
if !(stamp[/^\d{1,2}\/\d{1,2}\/\d{4} \d{1,2}:\d{1,2}:\d{1,2} [AP]M\z/])
puts 'Invalid timestamp formatting'
timestamp
else
stamp
end
end
def timestamp
print 'Enter timestamp for removal: '
ts = gets.chomp
verify_timestamp(ts)
end
As I said regexs are wonderful, they just aren't exactly the best thing to use all the time.. When this is run:
irb(main):001:0> def verify_timestamp(stamp)
puts 'Invalid timestamp formatting'
irb(main):004:2> timestamp
irb(main):005:2> else
irb(main):006:2* stamp
irb(main):007:2> end
irb(main):008:1> end
=> :verify_timestamp
irb(main):009:0>
irb(main):010:0* def timestamp
irb(main):011:1> print 'Enter timestamp for removal: '
irb(main):012:1> ts = gets.chomp
irb(main):013:1> verify_timestamp(ts)
irb(main):014:1> end
=> :timestamp
irb(main):015:0> verify_timestamp(timestamp)
Enter timestamp for removal: test
Invalid timestamp formatting
Enter timestamp for removal: 06/12/2016 8:18:54 pm
=> "06/12/2016 8:18:54 pm"
irb(main):016:0>
So there you have it, there are three different ways to verify your time stamps, depending on what you want to use them for might be another story, but any of these three should do the trick.