Skip to content

Instantly share code, notes, and snippets.

@stephancom
Created May 6, 2022 09:26
Show Gist options
  • Save stephancom/97ebe728dc2970c6b2063f8dc3037eb4 to your computer and use it in GitHub Desktop.
Save stephancom/97ebe728dc2970c6b2063f8dc3037eb4 to your computer and use it in GitHub Desktop.
Rails 6 date/time calculations, backported
module CoreExtensions
module DateAndTime
# ___ _ _ ___ _ ___ _ _ _ _
# | _ \__ _(_) |__/ __(_)_ __/ __|__ _| |__ _ _| |__ _| |_(_)___ _ _ ___
# | / _` | | (_-<__ \ \ \ / (__/ _` | / _| || | / _` | _| / _ \ ' \(_-<
# |_|_\__,_|_|_/__/___/_/_\_\\___\__,_|_\__|\_,_|_\__,_|\__|_\___/_||_/__/
#
# A little backporting
# https://github.com/rails/rails/pull/32398
# https://github.com/rails/rails/pull/32398/commits/4e68b159d5696ca58c015a790ef3ceacb90b27c1
# https://github.com/Samemura/rails/commit/4e62543b0779388b8b3dbe7ff47a8a682f7e042b
module RailsSixCalculations
# Returns true if the date/time before <tt>date_or_time</tt>.
def before?(date_or_time)
self < date_or_time
end
# Returns true if the date/time after <tt>date_or_time</tt>.
def after?(date_or_time)
self > date_or_time
end
# Returns a new date/time before <tt>date_or_time</tt>.
def before(date_or_time)
since(-date_or_time)
end
# Returns a new date/time after <tt>date_or_time</tt>.
def after(date_or_time)
since(date_or_time)
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment