backport_activesupport_time_calculations 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/backport_activesupport_time_calculations.rb +81 -0
- metadata +58 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 10ebdf51241460d4b013daf6e78847c4093483e2
|
4
|
+
data.tar.gz: 1138f1fe87c6d776a32bae274d5c6c7fda324621
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 0d10e71ab7710dc8c1322a0e3aab3c5a97dc73ebf4cae0b98047e238b346b7c26283e46aa916e6a1405f28cd5bfc6b93e829f15bdcb9a2597b1eecd60caa1abc
|
7
|
+
data.tar.gz: 356f109ffcc4ff3b72737a4f17c3a39ecb89c4b05e704fd49265450d258ff9b03c51fecaa20b7fa065a49594ae059232379cbe2558bcb33bd48d4acfd7cdb8ea
|
@@ -0,0 +1,81 @@
|
|
1
|
+
require 'rails'
|
2
|
+
|
3
|
+
module DateAndTime
|
4
|
+
module Calculations
|
5
|
+
# Add `#prev_day` and `#next_day` counterparts to `#yesterday` and
|
6
|
+
#`#tomorrow` for `Date`, `Time`, and `DateTime`.
|
7
|
+
#
|
8
|
+
# Add `same_time` option to `#next_week` and `#prev_week` for `Date`, `Time`,
|
9
|
+
# and `DateTime`.
|
10
|
+
#
|
11
|
+
# Add `#on_weekend?`, `#next_weekday`, `#prev_weekday` methods to `Date`,
|
12
|
+
#`Time`, and `DateTime`.
|
13
|
+
#
|
14
|
+
# `#on_weekend?` returns true if the receiving date/time falls on a Saturday
|
15
|
+
# or Sunday.
|
16
|
+
#
|
17
|
+
# `#next_weekday` returns a new date/time representing the next day that does
|
18
|
+
# not fall on a Saturday or Sunday.
|
19
|
+
#
|
20
|
+
# `#prev_weekday` returns a new date/time representing the previous day that
|
21
|
+
# does not fall on a Saturday or Sunday.
|
22
|
+
#
|
23
|
+
# https://github.com/rails/rails/pull/18335
|
24
|
+
|
25
|
+
WEEKEND_DAYS = [ 6, 0 ]
|
26
|
+
|
27
|
+
# Returns a new date/time representing the next day.
|
28
|
+
def next_day
|
29
|
+
advance(days: 1)
|
30
|
+
end
|
31
|
+
|
32
|
+
# Returns true if the date/time falls on a Saturday or Sunday.
|
33
|
+
def on_weekend?
|
34
|
+
wday.in?(WEEKEND_DAYS)
|
35
|
+
end
|
36
|
+
|
37
|
+
# Returns a new date/time representing the given day in the next week.
|
38
|
+
# The +given_day_in_next_week+ defaults to the beginning of the week
|
39
|
+
# which is determined by +Date.beginning_of_week+ or +config.beginning_of_week+
|
40
|
+
# when set. +DateTime+ objects have their time set to 0:00 unless +same_time+ is true.
|
41
|
+
def next_week(given_day_in_next_week = Date.beginning_of_week, same_time: false)
|
42
|
+
result = first_hour(weeks_since(1).beginning_of_week.days_since(days_span(given_day_in_next_week)))
|
43
|
+
same_time ? copy_time_to(result) : result
|
44
|
+
end
|
45
|
+
|
46
|
+
# Returns a new date/time representing the next weekday.
|
47
|
+
def next_weekday
|
48
|
+
if next_day.on_weekend?
|
49
|
+
next_week(:monday, same_time: true)
|
50
|
+
else
|
51
|
+
next_day
|
52
|
+
end
|
53
|
+
end
|
54
|
+
|
55
|
+
# Returns a new date/time representing the given day in the previous week.
|
56
|
+
# Week is assumed to start on +start_day+, default is
|
57
|
+
# +Date.beginning_of_week+ or +config.beginning_of_week+ when set.
|
58
|
+
# DateTime objects have their time set to 0:00 unless +same_time+ is true.
|
59
|
+
def prev_week(start_day = Date.beginning_of_week, same_time: false)
|
60
|
+
result = first_hour(weeks_ago(1).beginning_of_week.days_since(days_span(start_day)))
|
61
|
+
same_time ? copy_time_to(result) : result
|
62
|
+
end
|
63
|
+
|
64
|
+
# Returns a new date/time representing the previous weekday.
|
65
|
+
def prev_weekday
|
66
|
+
if prev_day.on_weekend?
|
67
|
+
copy_time_to(beginning_of_week(:friday))
|
68
|
+
else
|
69
|
+
prev_day
|
70
|
+
end
|
71
|
+
end
|
72
|
+
alias_method :last_weekday, :prev_weekday
|
73
|
+
|
74
|
+
private
|
75
|
+
|
76
|
+
def copy_time_to(other)
|
77
|
+
other.change(hour: hour, min: min, sec: sec, usec: try(:usec))
|
78
|
+
end
|
79
|
+
|
80
|
+
end
|
81
|
+
end
|
metadata
ADDED
@@ -0,0 +1,58 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: backport_activesupport_time_calculations
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.0.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Robert Coleman
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2016-02-28 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: rails
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ">="
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '0'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ">="
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '0'
|
27
|
+
description: https://github.com/rails/rails/pull/18335
|
28
|
+
email:
|
29
|
+
- github@robert.net.nz
|
30
|
+
executables: []
|
31
|
+
extensions: []
|
32
|
+
extra_rdoc_files: []
|
33
|
+
files:
|
34
|
+
- backport_activesupport_time_calculations.rb
|
35
|
+
homepage:
|
36
|
+
licenses: []
|
37
|
+
metadata: {}
|
38
|
+
post_install_message:
|
39
|
+
rdoc_options: []
|
40
|
+
require_paths:
|
41
|
+
- "."
|
42
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
43
|
+
requirements:
|
44
|
+
- - ">="
|
45
|
+
- !ruby/object:Gem::Version
|
46
|
+
version: '0'
|
47
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
48
|
+
requirements:
|
49
|
+
- - ">="
|
50
|
+
- !ruby/object:Gem::Version
|
51
|
+
version: '0'
|
52
|
+
requirements: []
|
53
|
+
rubyforge_project:
|
54
|
+
rubygems_version: 2.5.1
|
55
|
+
signing_key:
|
56
|
+
specification_version: 4
|
57
|
+
summary: Backport ActiveSupport DateTime Calculations from Rails 5
|
58
|
+
test_files: []
|