Tag: 时区

如何使用Ruby和MongoId正确保存时区?

如果这是一个noob问题,请原谅我: 我有一个应用程序,用户可以在他们的个人资料中设置自己的时区。 当有人添加阵容(特定于应用程序的术语)时,我会执行以下操作: time = ActiveSupport::TimeZone.new(user.timezone).parse( “Wednesday, 26 October, 2011 13:30:00” ) # This outputs: 2011-10-26 13:30:00 +0200 – valid according to the user selected TZ 然后我保存阵容: Lineup.create({ :date => time.gmtime, :uid => user._id, :pid => product._id }) 这应该(理论上)将日期保存为gmtime,但在查看记录时我会得到以下信息: { “_id”: ObjectId(“4e9c6613e673454f93000002”), “date”: “Wed, 26 Oct 2011 13: 30: 00 +0200”, “uid”: “4e9b81f6e673454c8a000001”, “pid”: “4e9c6613e673454f93000001”, […]

在Logstash中转换时间戳时区以获取输出索引名称

在我的场景中,Logstash接收的syslog行的“timestamp”是UTC,我们在Elasticsearch输出中使用事件“timestamp”: output { elasticsearch { embedded => false host => localhost port => 9200 protocol => http cluster => ‘elasticsearch’ index => “syslog-%{+YYYY.MM.dd}” } } 我的问题是,在UTC午夜,Logstash在一天结束之前在时区(GMT-4 => America / Montreal)发送日志到不同的索引,并且由于“时间戳”,索引在20h(晚上8点)之后没有日志“是UTC。 我们已经完成了转换时区的工作,但我们遇到了显着的性能下降: filter { mutate { add_field => { # Create a new field with string value of the UTC event date “timestamp_zoned” => “%{@timestamp}” } […]

如何使用rails3将本地时区的日期保存到db?

我有模型用户的Rails3应用程序和字段expires_at创建如下: t.column :expires_at, :timestamp 在我的数据库(postgresql)中它有类型: timestamp without timezone 问题是我打电话的时候: @user.expires_at = Time.now @user.save 它使用UTC时区保存到数据库中(我的当地时间是UTC + 1:00,华沙),但我不希望这样。 我只想把我的本地时区保存到数据库中(2011-03-30 01:29:01.766709,而不是2011-03-29 23:29:01.766709) 我可以使用rails3实现这个目标吗?

如何计算ruby中UTC的给定时区的偏移量(以小时为单位)?

我需要计算Ruby中UTC的给定时区的偏移量(以小时为单位)。 这行代码一直在为我工作,所以我想: offset_in_hours = (TZInfo::Timezone.get(self.timezone).current_period.offset.utc_offset).to_f / 3600.0 但是,事实certificate,它返回给我标准偏移,而不是DST偏移。 例如,假设 self.timezone = “America/New_York” 如果我运行上面的行,offset_in_hours = -5,而不是-4应该是,因为今天的日期是2012年4月1日。 任何人都可以告诉我如何计算来自UTC的offset_in_hours,因为Ruby中的有效字符串TimeZone既考虑了标准时间又节省了夏令时? 谢谢! 更新 以下是IRB的一些输出。 请注意,由于夏令时,纽约比UTC晚4小时,而不是5小时: >> require ‘tzinfo’ => false >> timezone = “America/New_York” => “America/New_York” >> offset_in_hours = TZInfo::Timezone.get(timezone).current_period.utc_offset / (60*60) => -5 >> 这表明TZInfo中存在错误,或者它不是dst-aware 更新2 根据joelparkerhender的评论,上面代码中的错误是我使用的是utc_offset,而不是utc_total_offset。 因此,根据我原来的问题,正确的代码行是: offset_in_hours = (TZInfo::Timezone.get(self.timezone).current_period.offset.utc_total_offset).to_f / 3600.0