Execute the datetime command received as a string

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP



Execute the datetime command received as a string



I have date information "20.days.from_now" in a string format. With this datum, I should be able to find the exact date at any given point in time. But I couldn't find a way to execute it.


"20.days.from_now"



Tried:


1. "20.days.from_now".to_datetime => returns DateTime.now.beginning_of_day + 20.seconds
2. "20.days.from_now".to_datetime => returns Time.now.beginning_of_day + 20.seconds



Can't pass this as an argument to the Time/DateTime methods.


Time


DateTime



Is there an efficient way to achieve this?



Possible inputs could be:


1. "20.days.from_now"
2. "45.seconds.from_now"
3. "-11.months.from_now"
4. "3.years.from_now"
5. "15.hours.from_now"
6. "860.minutes.from_now"




3 Answers
3



No, Rails has no built-in way to do this (short of eval, which you definitely don't want).


eval



You'll need to write a parser, probably using regular expressions, specific for your use case.



Try something like:


ALLOWED_UNITS = %w(years months days hours minutes seconds)

if str =~ /A(-?d+).(w+).from_nowz/ and
ALLOWED_UNITS.include?($2)
$1.to_i.public_send($2).from_now
end



I agree with @matthewd. You could use eval but please don't.


eval



I would not use regex though.



Just split string and process it:


parts = input.split('.')
# assuming first part is always a number
number = parts[0].to_i
# what methods you allow user to chain
allowed_methods = %w[days seconds ...]
method_chain = parts[1..-1]
raise 'forbidden' unless method_chain.all?
# just invoke methods one by one
res = method_chain.reduce(number) r, m



Processing fully depends on what your input is expected to be



You first want to parse a number then its unit. You could achieve it by:


def parse_date_from_string(str)
number = str.split(".")[0].to_i
unit = str.split(".")[1]
return number.send(unit).from_now
end



Given your suggested inputs, the outputs would be:


[21] pry(main)> parse_date_from_string("20.days.from_now")
=> Sat, 01 Sep 2018 17:40:19 -03 -03:00
[22] pry(main)> parse_date_from_string("45.seconds.from_now")
=> Sun, 12 Aug 2018 17:41:04 -03 -03:00
[23] pry(main)> parse_date_from_string("-11.months.from_now")
=> Tue, 12 Sep 2017 17:40:19 -03 -03:00
[24] pry(main)> parse_date_from_string("3.years.from_now")
=> Thu, 12 Aug 2021 17:40:19 -03 -03:00
[25] pry(main)> parse_date_from_string("15.hours.from_now")
=> Mon, 13 Aug 2018 08:40:19 -03 -03:00
[26] pry(main)> parse_date_from_string("860.minutes.from_now")
=> Mon, 13 Aug 2018 08:00:19 -03 -03:00






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

Firebase Auth - with Email and Password - Check user already registered

Dynamically update html content plain JS

How to determine optimal route across keyboard