简体   繁体   English

给定一个DateTime,如何获取特定时区中具有相同日期的时间范围?

[英]Given a DateTime, how do I get the range of times with the same date in a particular time zone?

I have a DateTime object representing a particular date, as in "2011-01-15 00:00:00 UTC" to represent January 15th. 我有一个代表特定日期的DateTime对象,如“ 2011-01-15 00:00:00 UTC”代表1月15日。 I would like to produce the range of times in a particular time zone that have the same date. 我想产生一个具有相同日期的特定时区的时间范围。

The method signature would probably be something like 方法签名可能类似于

def day_range_for(date, tz)
  # ...
end

For example, if I have range_for(DateTime.parse('2011-01-15'), 'CST') , then I want the result to be a range like 2011-01-15 00:00:00 -0600 .. 2011-01-15 23:59:59 -0600 . 例如,如果我具有range_for(DateTime.parse('2011-01-15'), 'CST') ,则我希望结果为类似2011-01-15 00:00:00 -0600 .. 2011-01-15 23:59:59 -0600的范围2011-01-15 00:00:00 -0600 .. 2011-01-15 23:59:59 -0600

Can you take the input string instead of the object? 您可以使用输入字符串代替对象吗? If you pass in a DateTime object, its a tad counter intuitive because the time the object represents isn't the actual time you are looking for. 如果传递DateTime对象,则它的提示计数器很直观,因为该对象表示的时间不是您要查找的实际时间。 It would conform to my expectations if you either passed in the correct, absolute DateTime, or the string itself. 如果您传入了正确的绝对DateTime或字符串本身,它将符合我的期望。 The actual meat of the problem is entirely handled by ActiveSupport, which I think you are using since you tagged this question with Rails. 该问题的实际原因完全由ActiveSupport处理,我认为您正在使用它,因为您已用Rails标记了此问题。 How does this look? 看起来如何?

def range_for(input, tz=nil)
    if tz.is_a?(String)
        tz = ActiveSupport::TimeZone.new(tz)
    else
        tz = Time.zone
    end
    if input.acts_like?(:date) || input.acts_like?(:time)
        d = input.in_time_zone(tz)
    else
        d = tz.parse(input)
    end
    return d.beginning_of_day..d.end_of_day
end

Have a look: 看一看:

ruby-1.9.2-p0 > range_for('2011-01-15', 'Alaska')
 => Sat, 15 Jan 2011 00:00:00 AKST -09:00..Sat, 15 Jan 2011 23:59:59 AKST -09:00 
ruby-1.9.2-p0 > range_for(Time.zone.now)
 => Mon, 10 Jan 2011 00:00:00 EST -05:00..Mon, 10 Jan 2011 23:59:59 EST -05:00 
ruby-1.9.2-p0 > range_for('2011-01-15', 'EST')
 => Sat, 15 Jan 2011 00:00:00 EST -05:00..Sat, 15 Jan 2011 23:59:59 EST -05:00  

How about: 怎么样:

def day_range_for date, zone
  (DateTime.new(date.year,date.month,date.day,0,0,0,zone)..DateTime.new(date.year,date.month,date.day,23,59,59,zone))
end

day_range_for(DateTime.parse('2011-01-15'), 'CST')
#=> #<DateTime: 2011-01-15T00:00:00-06:00 (9822307/4,-1/4,2299161)>..#<DateTime: 2011-01-15T23:59:59-06:00 (212161917599/86400,-1/4,2299161)> 

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为什么我得到相同的输出DateTime.now.in_time_zone(-4)和DateTime.now.in_time_zone(-3) - Why do I get the same output for DateTime.now.in_time_zone(-4) and DateTime.now.in_time_zone(-3) 我应该总是用哪个解析日期时间? DateTime,Time,Time.zone? - Which should I always parse date times with? DateTime, Time, Time.zone? Rails获取带有时区的DateTime的Date部分 - Rails get Date part of DateTime with Time Zone 如何为给定日期时间的每个日期创建一个哈希/数组次数 - How do I create a hash/array of times for each date given a datetime 如何在自定义时区中按时间预填充datetime_select? - How can i prefill datetime_select with times in custom time zone? Rails / Postgresql:如何在特定时区按日期使用日期时间字段快速分组记录? - Rails / Postgresql: How can I quickly group records using a datetime field by date in a specific time zone? 如何使用与config.time_zone中设置的时区不同的时区保存日期时间 - How do I save a datetime with a different timezone than what is set in config.time_zone Rails,如何在正确的时区中获取日期时间? - Rails, how to get hours of a datetime in the correct time zone? 如何在 rails 3 中保存 DATETIME 列的当前日期/时间? - How do I save a current date/time for a DATETIME column in rails 3? 如何将“ DateTime”转换为柏林的相应日期和时间? - How do I convert a `DateTime` to the corresponding date and time in Berlin?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM