首页 \ 问答 \ Ruby on Rails中的TimeZone转换错误(Wrong TimeZone Conversion in Ruby on Rails)

Ruby on Rails中的TimeZone转换错误(Wrong TimeZone Conversion in Ruby on Rails)

我真的很喜欢这个。 基本上我想导入Excel文件。 所以我最终使用Rails中的CSV导入。

CSV.open('[path-to-file]', 'r').each do |row|

实际上导入工作正常,但Excel / CSV文件中的日期列和数据库中的日期时间列除其他外。 我在CSV.open-Method中进行了以下操作:

date = DateTime.strptime(row[0], "%Y-%m-%d").strftime("%Y-%m-%d")
start_datetime = DateTime.parse(date + " 6:30:00").utc
Event.create(:event_start => start_datetime)

这将在数​​据库中使用正确的DateTime创建正确的事件。 寻找例如:

2008-11-18 09:30:00

我的问题:

如果我选择在application.rb中注释该行

config.time_zone = 'Berlin'

Rails使用UTC来显示我的事件,一切看起来像数据库的内容。

如果我选择取消注释config.time_zone部分(我必须要这样做),Rails应该增加1小时(柏林:UTC / GMT +1小时)。 实际上它确实增加了至少1小时,但有时增加了2小时。 没有连接(对我来说),在这种情况下Rails选择添加1或2小时。

如果我在普通的Websurface(在浏览器中)创建一个事件,一切正常(在1小时内减去以将UTC保存在数据库中并添加1小时以在正确的TimeZone中显示)。

如果您有一些技巧可以尝试本地化此问题,那将非常有用。

我的系统:Ruby 1.9.3p194上的Rails 3.2.3,Suse Enterprise上的MySQL

例:

CSV:

2012-01-08
2012-02-09
2012-03-10
2012-04-11
2012-05-12
2012-06-13
2012-07-14
2012-08-15
2012-09-16
2012-10-17
2012-11-18
2012-12-19

DB(MySQL):

| id | event_start
+----+---------------------    
|  1 | 2012-01-08 06:30:00
|  2 | 2012-02-09 06:30:00
|  3 | 2012-03-10 06:30:00
|  4 | 2012-04-11 06:30:00
|  5 | 2012-05-12 06:30:00
|  6 | 2012-06-13 06:30:00
|  7 | 2012-07-14 06:30:00
|  8 | 2012-08-15 06:30:00
|  9 | 2012-09-16 06:30:00
| 10 | 2012-10-17 06:30:00
| 11 | 2012-11-18 06:30:00
| 12 | 2012-12-19 06:30:00

查看(浏览器) - 这里我只使用了.order(“event_start ASC”)

events.each do |ev|
ev.event_start

2012-12-19 07:30:00 +0100
2012-11-18 07:30:00 +0100
2012-10-17 08:30:00 +0200
2012-09-16 08:30:00 +0200
2012-08-15 08:30:00 +0200
2012-07-14 08:30:00 +0200
2012-06-13 08:30:00 +0200
2012-05-12 08:30:00 +0200
2012-14-11 08:30:00 +0200
2012-03-10 07:30:00 +0100
2012-02-09 07:30:00 +0100
2012-01-08 07:30:00 +0100

I'm really stucked on this one. Basically I want to Import a Excel-File. So I ended up using the CSV import from Rails.

CSV.open('[path-to-file]', 'r').each do |row|

Actually the Import is working fine, but there is among other things a Date-Column in the Excel/CSV-File and a DateTime-Column in the Database. I did following in the CSV.open-Method:

date = DateTime.strptime(row[0], "%Y-%m-%d").strftime("%Y-%m-%d")
start_datetime = DateTime.parse(date + " 6:30:00").utc
Event.create(:event_start => start_datetime)

This creates the correct Events with the correct DateTime in the Database. Looks for example like:

2008-11-18 09:30:00

My Problem:

If I choose to comment the line in the application.rb

config.time_zone = 'Berlin'

Rails uses UTC to display my Events and everything looks like the content of the Database.

If I choose to uncomment the config.time_zone part (what I definitely have to), Rails should add 1 hour (Berlin: UTC/GMT +1 hour). Actually it does add at least 1 hour, but sometimes 2 hours. There is no connection (for me), in which case Rails chooses to add 1 or 2 hours.

If I create an Event on the normal Websurface (in the Browser), everything is working fine (subtract on 1 hour to save UTC in DB and add 1 hour to display in correct TimeZone).

It would be really helpful, if you had some tips for me how I can try to localize this problem.

My System: Rails 3.2.3 on Ruby 1.9.3p194, MySQL on Suse Enterprise

Example:

CSV:

2012-01-08
2012-02-09
2012-03-10
2012-04-11
2012-05-12
2012-06-13
2012-07-14
2012-08-15
2012-09-16
2012-10-17
2012-11-18
2012-12-19

DB (MySQL):

| id | event_start
+----+---------------------    
|  1 | 2012-01-08 06:30:00
|  2 | 2012-02-09 06:30:00
|  3 | 2012-03-10 06:30:00
|  4 | 2012-04-11 06:30:00
|  5 | 2012-05-12 06:30:00
|  6 | 2012-06-13 06:30:00
|  7 | 2012-07-14 06:30:00
|  8 | 2012-08-15 06:30:00
|  9 | 2012-09-16 06:30:00
| 10 | 2012-10-17 06:30:00
| 11 | 2012-11-18 06:30:00
| 12 | 2012-12-19 06:30:00

View (Browser) - Here I just used an .order("event_start ASC")

events.each do |ev|
ev.event_start

2012-12-19 07:30:00 +0100
2012-11-18 07:30:00 +0100
2012-10-17 08:30:00 +0200
2012-09-16 08:30:00 +0200
2012-08-15 08:30:00 +0200
2012-07-14 08:30:00 +0200
2012-06-13 08:30:00 +0200
2012-05-12 08:30:00 +0200
2012-14-11 08:30:00 +0200
2012-03-10 07:30:00 +0100
2012-02-09 07:30:00 +0100
2012-01-08 07:30:00 +0100

原文:https://stackoverflow.com/questions/15436253
更新时间:2024-04-14 18:04

最满意答案

这应该是诀窍:

val table = classOf[Foo].getAnnotation(classOf[Table]).name()

如果你有这样的东西,这是有效的:

@java.lang.annotation.Retention(java.lang.annotation.RetentionPolicy.RUNTIME)
@java.lang.annotation.Target({java.lang.annotation.ElementType.ANNOTATION_TYPE})
public @interface Table {
    public String name();
}

这是应该存在于Java源文件中的注释定义。

和Foo类:

@Table(name="hello")
class Foo {

}

This should do the trick:

val table = classOf[Foo].getAnnotation(classOf[Table]).name()

That works if you have something like this:

@java.lang.annotation.Retention(java.lang.annotation.RetentionPolicy.RUNTIME)
@java.lang.annotation.Target({java.lang.annotation.ElementType.ANNOTATION_TYPE})
public @interface Table {
    public String name();
}

This is the annotation definition that should exist in a Java source file.

And the Foo class:

@Table(name="hello")
class Foo {

}

相关问答

更多
  • 在引擎盖下,功能和方法之间还有其他差异。 一般来说,普通方法比函数产生的开销要小(这在技术上是带有apply方法的对象)。 然而,如果你不去关心这些差异,并把def , val和var成不同语义的字段 ,那么简单的说, def每次调用时都会评估它,而val只评估一次。 因此, val isEven = isDivisibleBy(2)应在其定义期间调用isDivisibleBy(2)并分配isDivisibleBy(2)的结果。 例如,它取代了k def isDivisibleBy(k: Int): Int ...
  • 他们被命名为zip,因为你正在拉链像拉链一样的两个数据集。 为了使其可视化,需要两个数据集: x = [1,2,3,4,5,6] y = [a,b,c,d,e,f] 然后将它们一起压缩得到 1 a 2 b 3 c 4 d 5 e 6 f 当您向下移动数据集时,我将额外的间距放在拉链错觉处:) They are named zip because you are zipping two datasets like a zipper. To visualize ...
  • Scala没有全局类型推断,只有本地类型推断。 val或var的类型被推断为用于初始化它的表达式的类型。 抽象的val s和var s没有被初始化,所以没有什么可以从中推断出类型。 如果未指定抽象方法的返回类型,则暗示为Unit 。 这里没有类型推断。 但是对于val或var没有任何意义:只能有一个Unit类型的值,那么为什么要将它存储在变量中呢? Scala does not have global type inference, only local type inference. The type o ...
  • 你不想改变你想测量时间的代码吗? 如果你不介意改变代码,那么你可以这样做: def time[R](block: => R): R = { val t0 = System.nanoTime() val result = block // call-by-name val t1 = System.nanoTime() println("Elapsed time: " + (t1 - t0) + "ns") result } // Now wrap your me ...
  • 这应该是诀窍: val table = classOf[Foo].getAnnotation(classOf[Table]).name() 如果你有这样的东西,这是有效的: @java.lang.annotation.Retention(java.lang.annotation.RetentionPolicy.RUNTIME) @java.lang.annotation.Target({java.lang.annotation.ElementType.ANNOTATION_TYPE}) public @i ...
  • 你不应该在注释和类定义之间放置空行,这个应该工作: import com.xx.{Bar,Baz} @Bar(value = "XX", description = "xx") class Foo extends Baz { } You shall not put empty line between annotation and class definition, this one should work: import com.xx.{Bar,Baz} @Bar(value = "XX", des ...
  • 我没有尝试过,但http://paranamer.codehaus.org/是为这项任务设计的。 If debugging info is present in the classes, it can be done as follows. I am basically using Adam Paynter's answer and copy-pasting the code from here after slight edit to get it to work in Scala. package te ...
  • 否则,你怎么知道(val another: AnotherClass)是构造函数参数列表还是@Inject参数? Otherwise, how could you tell if (val another: AnotherClass) is constructor parameter list or arguments to @Inject?
  • 如果函数能够针对尾递归进行优化,那么为什么不自动应用此优化 是的 。 不幸的是,我没有从SLS中找到一个可以保证这一点的报价。 为什么我需要标记一个可以用@tailrec优化的@tailrec ? 注意:Scala不保证函数的正确尾递归,仅适用于方法 ! 您没有注释可以优化的方法。 您注释必须优化的方法,以便在无法优化时出现编译错误。 请参阅scala.annotation.tailrec的文档 : 一种方法注释,用于验证将使用尾调用优化来编译该方法。 如果存在,如果无法将方法优化为循环,编译器将发出错误。 ...
  • 在宏天堂2.0.0-SNAPSHOT中,我们有一个非常棘手的方法来访问宏注释的类型参数(当我们有专门的API时,情况将会改善,但是现在很难为scala-reflect引入新功能.jar在宏天堂,所以目前的API有点粗糙)。 现在,有必要在注释类上指定type参数,而不是在macroTransform方法上声明任何类型参数。 然后,在宏扩展中,访问c.macroApplication并提取与传递的类型参数对应的无类型树。 之后,按照处理宏注释时无法访问父级成员的描述执行c.typeCheck 。 In mac ...

相关文章

更多

最新问答

更多
  • 您如何使用git diff文件,并将其应用于同一存储库的副本的本地分支?(How do you take a git diff file, and apply it to a local branch that is a copy of the same repository?)
  • 将长浮点值剪切为2个小数点并复制到字符数组(Cut Long Float Value to 2 decimal points and copy to Character Array)
  • OctoberCMS侧边栏不呈现(OctoberCMS Sidebar not rendering)
  • 页面加载后对象是否有资格进行垃圾回收?(Are objects eligible for garbage collection after the page loads?)
  • codeigniter中的语言不能按预期工作(language in codeigniter doesn' t work as expected)
  • 在计算机拍照在哪里进入
  • 使用cin.get()从c ++中的输入流中丢弃不需要的字符(Using cin.get() to discard unwanted characters from the input stream in c++)
  • No for循环将在for循环中运行。(No for loop will run inside for loop. Testing for primes)
  • 单页应用程序:页面重新加载(Single Page Application: page reload)
  • 在循环中选择具有相似模式的列名称(Selecting Column Name With Similar Pattern in a Loop)
  • System.StackOverflow错误(System.StackOverflow error)
  • KnockoutJS未在嵌套模板上应用beforeRemove和afterAdd(KnockoutJS not applying beforeRemove and afterAdd on nested templates)
  • 散列包括方法和/或嵌套属性(Hash include methods and/or nested attributes)
  • android - 如何避免使用Samsung RFS文件系统延迟/冻结?(android - how to avoid lag/freezes with Samsung RFS filesystem?)
  • TensorFlow:基于索引列表创建新张量(TensorFlow: Create a new tensor based on list of indices)
  • 企业安全培训的各项内容
  • 错误:RPC失败;(error: RPC failed; curl transfer closed with outstanding read data remaining)
  • C#类名中允许哪些字符?(What characters are allowed in C# class name?)
  • NumPy:将int64值存储在np.array中并使用dtype float64并将其转换回整数是否安全?(NumPy: Is it safe to store an int64 value in an np.array with dtype float64 and later convert it back to integer?)
  • 注销后如何隐藏导航portlet?(How to hide navigation portlet after logout?)
  • 将多个行和可变行移动到列(moving multiple and variable rows to columns)
  • 提交表单时忽略基础href,而不使用Javascript(ignore base href when submitting form, without using Javascript)
  • 对setOnInfoWindowClickListener的意图(Intent on setOnInfoWindowClickListener)
  • Angular $资源不会改变方法(Angular $resource doesn't change method)
  • 在Angular 5中不是一个函数(is not a function in Angular 5)
  • 如何配置Composite C1以将.m和桌面作为同一站点提供服务(How to configure Composite C1 to serve .m and desktop as the same site)
  • 不适用:悬停在悬停时:在元素之前[复制](Don't apply :hover when hovering on :before element [duplicate])
  • 常见的python rpc和cli接口(Common python rpc and cli interface)
  • Mysql DB单个字段匹配多个其他字段(Mysql DB single field matching to multiple other fields)
  • 产品页面上的Magento Up出售对齐问题(Magento Up sell alignment issue on the products page)