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
最满意答案
这应该是诀窍:
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 { }
相关问答
更多-
函数与Scala中的方法(Functions vs methods in Scala)[2021-07-22]
在引擎盖下,功能和方法之间还有其他差异。 一般来说,普通方法比函数产生的开销要小(这在技术上是带有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 ...
-
如何在Scala中配置方法?(How to profile methods in Scala?)[2023-02-07]
你不想改变你想测量时间的代码吗? 如果你不介意改变代码,那么你可以这样做: 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 ...
-
如何通过Scala / Java方法中的反射获取参数名称和类型?(How to get parameter names and types via reflection in Scala/Java methods?)[2022-02-22]
我没有尝试过,但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 ... -
Scala和@Inject注释(Scala and @Inject annotation)[2022-06-14]
否则,你怎么知道(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 ...