提问人:Codrin 提问时间:2/25/2023 最后编辑:BijendraCodrin 更新时间:2/26/2023 访问量:62
如何从小时范围之间的日期数组中删除元素 Rails?
How to remove elements from array of dates between hours ranges Rails?
问:
我的数组是由
start_date = (Date.today + 10.hours + 30.minutes).to_datetime
end_date = (Date.today + 1.day + 18.hours).to_datetime
period = 0.5/24
array = start_date.step(end_date, period).to_a
这是结果(但此结果最多将涵盖 62 天)
array =
[Sat, 25 Feb 2023 10:30:00 +0000, Sat, 25 Feb 2023 11:00:00 +0000, Sat, 25 Feb 2023 11:30:00 +0000,
Sat, 25 Feb 2023 12:00:00 +0000, Sat, 25 Feb 2023 12:30:00 +0000, Sat, 25 Feb 2023 13:00:00 +0000,
Sat, 25 Feb 2023 13:30:00 +0000, Sat, 25 Feb 2023 14:00:00 +0000, Sat, 25 Feb 2023 14:30:00 +0000,
Sat, 25 Feb 2023 15:00:00 +0000, Sat, 25 Feb 2023 15:30:00 +0000, Sat, 25 Feb 2023 16:00:00 +0000,
Sat, 25 Feb 2023 16:30:00 +0000, Sat, 25 Feb 2023 17:00:00 +0000, Sat, 25 Feb 2023 17:30:00 +0000,
Sat, 25 Feb 2023 18:00:00 +0000, Sat, 25 Feb 2023 18:30:00 +0000, Sat, 25 Feb 2023 19:00:00 +0000,
Sat, 25 Feb 2023 19:30:00 +0000, Sat, 25 Feb 2023 20:00:00 +0000, Sat, 25 Feb 2023 20:30:00 +0000,
Sat, 25 Feb 2023 21:00:00 +0000, Sat, 25 Feb 2023 21:30:00 +0000, Sat, 25 Feb 2023 22:00:00 +0000,
Sat, 25 Feb 2023 22:30:00 +0000, Sat, 25 Feb 2023 23:00:00 +0000, Sat, 25 Feb 2023 23:30:00 +0000,
Sun, 26 Feb 2023 00:00:00 +0000, Sun, 26 Feb 2023 00:30:00 +0000, Sun, 26 Feb 2023 01:00:00 +0000,
Sun, 26 Feb 2023 01:30:00 +0000, Sun, 26 Feb 2023 02:00:00 +0000, Sun, 26 Feb 2023 02:30:00 +0000,
Sun, 26 Feb 2023 03:00:00 +0000, Sun, 26 Feb 2023 03:30:00 +0000, Sun, 26 Feb 2023 04:00:00 +0000,
Sun, 26 Feb 2023 04:30:00 +0000, Sun, 26 Feb 2023 05:00:00 +0000, Sun, 26 Feb 2023 05:30:00 +0000,
Sun, 26 Feb 2023 06:00:00 +0000, Sun, 26 Feb 2023 06:30:00 +0000, Sun, 26 Feb 2023 07:00:00 +0000,
Sun, 26 Feb 2023 07:30:00 +0000, Sun, 26 Feb 2023 08:00:00 +0000, Sun, 26 Feb 2023 08:30:00 +0000,
Sun, 26 Feb 2023 09:00:00 +0000, Sun, 26 Feb 2023 09:30:00 +0000, Sun, 26 Feb 2023 10:00:00 +0000,
Sun, 26 Feb 2023 10:30:00 +0000, Sun, 26 Feb 2023 11:00:00 +0000, Sun, 26 Feb 2023 11:30:00 +0000,
Sun, 26 Feb 2023 12:00:00 +0000, Sun, 26 Feb 2023 12:30:00 +0000, Sun, 26 Feb 2023 13:00:00 +0000,
Sun, 26 Feb 2023 13:30:00 +0000, Sun, 26 Feb 2023 14:00:00 +0000, Sun, 26 Feb 2023 14:30:00 +0000,
Sun, 26 Feb 2023 15:00:00 +0000, Sun, 26 Feb 2023 15:30:00 +0000, Sun, 26 Feb 2023 16:00:00 +0000,
Sun, 26 Feb 2023 16:30:00 +0000, Sun, 26 Feb 2023 17:00:00 +0000, Sun, 26 Feb 2023 17:30:00 +0000,
Sun, 26 Feb 2023 18:00:00 +0000]
从这个数组中,我想删除从每天开始到 10:29 点到每天 18:00 之后到一天结束之间的所有日期时间。
期望结果:
array =
[Sat, 25 Feb 2023 10:30:00 +0000, Sat, 25 Feb 2023 11:00:00 +0000, ....... Sat, 25 Feb 2023 18:00:00 +0000,
Sun, 26 Feb 2023 10:30:00 +0000, Sun, 26 Feb 2023 11:00:00 +0000, ....... Sun, 26 Feb 2023 18:00:00 +0000]
我试过了
w_start_time = (start_date + 8.hours + 1.minute).strftime('%k:%M:%S')
w_end_time = (start_date - 1.minute).strftime('%k:%M:%S')
hours = array_of_datestimes.reject! { |n| n.strftime('%k:%M:%S').between?(w_start_time, w_end_time) }
这将返回 nil
有人可以帮我吗?
谢谢
答:
0赞
Vit Ruve
2/25/2023
#1
也许你应该试试这个:
array = array.reject { |date|
(date.hour < 10 || (date.hour == 10 && date.min < 30)) ||
(date.hour >= 18)
}
评论
0赞
Codrin
2/25/2023
谢谢你,这几乎可以工作,问题是它也在删除,我想保留Sat, 25 Feb 2023 18:00:00 +0000
Sun, 26 Feb 2023 18:00:00 +0000
0赞
maya_nk99
2/25/2023
#2
这对我有用
w_start_time = (start_date - 1.minute).strftime('%k:%M:%S')
w_end_time = (start_date + 8.hours - 1.minute).strftime('%k:%M:%S')
hours = array.filter { |n| n.strftime('%k:%M:%S').between?(w_start_time, w_end_time) }
2赞
Cary Swoveland
2/26/2023
#3
这里有两种方法。
转换为分钟。
array.accept! { |date| (630..1080).cover?(60*date.hour + date.min) }
其中和 .参见 Range#cover?,它只使用范围端点,因此非常高效。630 = 60*10+30
1080 = 60*18
计数以确定要保留的元素
我们看到,在第一天,我们希望保留第一个
(18.0 - 10.5)/0.5 + 1 = 16
元素。由于每天有 48 个 30 分钟的间隔,我们希望删除下一个
48 - 16 = 32
元素。因此,我们可以创建一个枚举器,它生成 16 次,然后是 32 次,然后重复,如果枚举器的相应值等于 .我们可以使用 Array#cycle 来创建这个枚举器。false
true
array
true
reject = ([false]*16 + [true]*32).cycle
#=> #<Enumerator: [false, false, false, false, false, false, false,
# false, false, false, false, false, false, false, false, false,
# true, true, true, true, true, true, true, true, true, true,
# true, true, true, true, true, true, true, true, true, true,
# true, true, true, true, true, true, true, true, true, true,
# true, true]:cycle>
然后我们简单地写
array.reject! { reject.next }
为了说明此计算,假设持有一个与给定数组的元素相对应的字符串数组。array
DateTime
array = [
"Sat, 25 Feb 2023 10:30:00 +0000", "Sat, 25 Feb 2023 11:00:00 +0000",
"Sat, 25 Feb 2023 11:30:00 +0000", "Sat, 25 Feb 2023 12:00:00 +0000",
"Sat, 25 Feb 2023 12:30:00 +0000", "Sat, 25 Feb 2023 13:00:00 +0000",
"Sat, 25 Feb 2023 13:30:00 +0000", "Sat, 25 Feb 2023 14:00:00 +0000",
"Sat, 25 Feb 2023 14:30:00 +0000", "Sat, 25 Feb 2023 15:00:00 +0000",
"Sat, 25 Feb 2023 15:30:00 +0000", "Sat, 25 Feb 2023 16:00:00 +0000",
"Sat, 25 Feb 2023 16:30:00 +0000", "Sat, 25 Feb 2023 17:00:00 +0000",
"Sat, 25 Feb 2023 17:30:00 +0000", "Sat, 25 Feb 2023 18:00:00 +0000",
"Sat, 25 Feb 2023 18:30:00 +0000", "Sat, 25 Feb 2023 19:00:00 +0000",
"Sat, 25 Feb 2023 19:30:00 +0000", "Sat, 25 Feb 2023 20:00:00 +0000",
"Sat, 25 Feb 2023 20:30:00 +0000", "Sat, 25 Feb 2023 21:00:00 +0000",
"Sat, 25 Feb 2023 21:30:00 +0000", "Sat, 25 Feb 2023 22:00:00 +0000",
"Sat, 25 Feb 2023 22:30:00 +0000", "Sat, 25 Feb 2023 23:00:00 +0000",
"Sat, 25 Feb 2023 23:30:00 +0000", "Sun, 26 Feb 2023 00:00:00 +0000",
"Sun, 26 Feb 2023 00:30:00 +0000", "Sun, 26 Feb 2023 01:00:00 +0000",
"Sun, 26 Feb 2023 01:30:00 +0000", "Sun, 26 Feb 2023 02:00:00 +0000",
"Sun, 26 Feb 2023 02:30:00 +0000", "Sun, 26 Feb 2023 03:00:00 +0000",
"Sun, 26 Feb 2023 03:30:00 +0000", "Sun, 26 Feb 2023 04:00:00 +0000",
"Sun, 26 Feb 2023 04:30:00 +0000", "Sun, 26 Feb 2023 05:00:00 +0000",
"Sun, 26 Feb 2023 05:30:00 +0000", "Sun, 26 Feb 2023 06:00:00 +0000",
"Sun, 26 Feb 2023 06:30:00 +0000", "Sun, 26 Feb 2023 07:00:00 +0000",
"Sun, 26 Feb 2023 07:30:00 +0000", "Sun, 26 Feb 2023 08:00:00 +0000",
"Sun, 26 Feb 2023 08:30:00 +0000", "Sun, 26 Feb 2023 09:00:00 +0000",
"Sun, 26 Feb 2023 09:30:00 +0000", "Sun, 26 Feb 2023 10:00:00 +0000",
"Sun, 26 Feb 2023 10:30:00 +0000", "Sun, 26 Feb 2023 11:00:00 +0000",
"Sun, 26 Feb 2023 11:30:00 +0000", "Sun, 26 Feb 2023 12:00:00 +0000",
"Sun, 26 Feb 2023 12:30:00 +0000", "Sun, 26 Feb 2023 13:00:00 +0000",
"Sun, 26 Feb 2023 13:30:00 +0000", "Sun, 26 Feb 2023 14:00:00 +0000",
"Sun, 26 Feb 2023 14:30:00 +0000", "Sun, 26 Feb 2023 15:00:00 +0000",
"Sun, 26 Feb 2023 15:30:00 +0000", "Sun, 26 Feb 2023 16:00:00 +0000",
"Sun, 26 Feb 2023 16:30:00 +0000", "Sun, 26 Feb 2023 17:00:00 +0000",
"Sun, 26 Feb 2023 17:30:00 +0000", "Sun, 26 Feb 2023 18:00:00 +0000"
]
然后
array.reject! { reject.next }
#=> ["Sat, 25 Feb 2023 10:30:00 +0000", "Sat, 25 Feb 2023 11:00:00 +0000",
# "Sat, 25 Feb 2023 11:30:00 +0000", "Sat, 25 Feb 2023 12:00:00 +0000",
# "Sat, 25 Feb 2023 12:30:00 +0000", "Sat, 25 Feb 2023 13:00:00 +0000",
# "Sat, 25 Feb 2023 13:30:00 +0000", "Sat, 25 Feb 2023 14:00:00 +0000",
# "Sat, 25 Feb 2023 14:30:00 +0000", "Sat, 25 Feb 2023 15:00:00 +0000",
# "Sat, 25 Feb 2023 15:30:00 +0000", "Sat, 25 Feb 2023 16:00:00 +0000",
# "Sat, 25 Feb 2023 16:30:00 +0000", "Sat, 25 Feb 2023 17:00:00 +0000",
# "Sat, 25 Feb 2023 17:30:00 +0000", "Sat, 25 Feb 2023 18:00:00 +0000",
# "Sun, 26 Feb 2023 10:30:00 +0000", "Sun, 26 Feb 2023 11:00:00 +0000",
# "Sun, 26 Feb 2023 11:30:00 +0000", "Sun, 26 Feb 2023 12:00:00 +0000",
# "Sun, 26 Feb 2023 12:30:00 +0000", "Sun, 26 Feb 2023 13:00:00 +0000",
# "Sun, 26 Feb 2023 13:30:00 +0000", "Sun, 26 Feb 2023 14:00:00 +0000",
# "Sun, 26 Feb 2023 14:30:00 +0000", "Sun, 26 Feb 2023 15:00:00 +0000",
# "Sun, 26 Feb 2023 15:30:00 +0000", "Sun, 26 Feb 2023 16:00:00 +0000",
# "Sun, 26 Feb 2023 16:30:00 +0000", "Sun, 26 Feb 2023 17:00:00 +0000",
# "Sun, 26 Feb 2023 17:30:00 +0000", "Sun, 26 Feb 2023 18:00:00 +0000"]
这显然适用于任何天数,前提是数组的第一个元素对应于第一天。与数组的最后一个元素关联的时间不需要是 。10:30
18:00
这种计算将相对较快,因为它确实需要将每个元素分解为小时和分钟。DateTime
评论