如何从 tstzrange 列中获取 ActiveSupport::TimeWithZone 的范围?

How do I get a Range of ActiveSupport::TimeWithZone from a tstzrange column?

我有一个tstzrange column。 Rails 正在将其转换为 RangeTime 对象,这会丢失时区信息。我想要 RangeActiveSupport::TimeWithZone 个对象。我该怎么做?

或者,我如何检查 tstzrange 是否恰好跨越当前时区的一天,以及哪一天?

来自 ActiveRecord::Timestamp docs...

You can also add database specific timezone aware types. For example, for PostgreSQL:

ActiveRecord::Base.time_zone_aware_types += [:tsrange, :tstzrange]

in_time_zone 会将应用程序时区中的 Time 变为 ActiveSupport::TimeWithZone。时间范围可以变成 ActiveSupport::TimeWithZone 的范围,如下所示:

range = Range.new(
  tstzrange.begin.in_time_zone,
  tstzrange.end.in_time_zone,
  # Taking care to preserve whether the range is inclusive or exclusive.
  tstzrange.exclude_end?
)