Rails应用未遵循config / application.rb中配置的时区

时间:2019-04-10 01:50:29

标签: ruby-on-rails postgresql timezone utc

我是配置时区的新手,并且对几点感到困惑。关于正确配置的任何建议将不胜感激。

我的理解是,最佳实践是将所有时间戳记都以UTC格式存储在数据库中(我使用的是PostgreSQL)。另一方面,在实际的应用程序中,我想查看本地时区(JST +9:00)的时间戳。 我已经这样配置<build> <plugins> <plugin> <groupId>org.codehaus.mojo</groupId> <artifactId>jaxb2-maven-plugin</artifactId> <version>2.2</version> <executions> <execution> <id>xjc</id> <goals> <goal>xjc</goal> </goals> </execution> </executions> <configuration> <sources> <source>/src/main/resources/adapters/charging/payments/wsdls/charging-payments-v1.wsdl</source> </sources> <packageName>com.equifax.icc.charging.payment.service.wsdl</packageName> <clearOutputDir>false</clearOutputDir> </configuration> </plugin> </plugins> </build> [ERROR] Failed to execute goal org.codehaus.mojo:jaxb2-maven-plugin:2.2:xjc (xjc) on project payment-manager-domain: Execution xjc of goal org.codehaus.mojo:jaxb2-maven-plugin:2.2:xjc failed: A required class was missing while executing org.codehaus.mojo:jaxb2-maven-plugin:2.2:xjc: com/sun/codemodel/CodeWriter [ERROR] ----------------------------------------------------- [ERROR] realm = plugin>org.codehaus.mojo:jaxb2-maven-plugin:2.2 [ERROR] strategy = org.codehaus.plexus.classworlds.strategy.SelfFirstStrategy [ERROR] urls[0] = file:/C:/Users/vxa90/.m2/repository/org/codehaus/mojo/jaxb2-maven-plugin/2.2/jaxb2-maven-plugin-2.2.jar [ERROR] urls[1] = file:/C:/Users/vxa90/.m2/repository/javax/xml/bind/jaxb-api/2.2.11/jaxb-api-2.2.11.jar [ERROR] urls[2] = file:/C:/Users/vxa90/.m2/repository/org/glassfish/jaxb/jaxb-core/2.2.11/jaxb-core-2.2.11.jar [ERROR] urls[3] = file:/C:/Users/vxa90/.m2/repository/org/glassfish/jaxb/jaxb-runtime/2.2.11/jaxb-runtime-2.2.11.jar [ERROR] urls[4] = file:/C:/Users/vxa90/.m2/repository/org/glassfish/jaxb/jaxb-xjc/2.2.11/jaxb-xjc-2.2.11.jar [ERROR] urls[5] = file:/C:/Users/vxa90/.m2/repository/org/glassfish/jaxb/jaxb-jxc/2.2.11/jaxb-jxc-2.2.11.jar [ERROR] urls[6] = file:/C:/Users/vxa90/.m2/repository/com/thoughtworks/qdox/qdox/2.0-M3/qdox-2.0-M3.jar [ERROR] urls[7] = file:/C:/Users/vxa90/.m2/repository/org/sonatype/sisu/sisu-inject-bean/1.4.2/sisu-inject-bean-1.4.2.jar [ERROR] urls[8] = file:/C:/Users/vxa90/.m2/repository/org/sonatype/sisu/sisu-guice/2.1.7/sisu-guice-2.1.7-noaop.jar [ERROR] urls[9] = file:/C:/Users/vxa90/.m2/repository/org/sonatype/aether/aether-util/1.7/aether-util-1.7.jar [ERROR] urls[10] = file:/C:/Users/vxa90/.m2/repository/org/codehaus/plexus/plexus-interpolation/1.14/plexus-interpolation-1.14.jar [ERROR] urls[11] = file:/C:/Users/vxa90/.m2/repository/org/codehaus/plexus/plexus-component-annotations/1.5.5/plexus-component-annotations-1.5.5.jar [ERROR] urls[12] = file:/C:/Users/vxa90/.m2/repository/org/sonatype/plexus/plexus-sec-dispatcher/1.3/plexus-sec-dispatcher-1.3.jar [ERROR] urls[13] = file:/C:/Users/vxa90/.m2/repository/org/sonatype/plexus/plexus-cipher/1.4/plexus-cipher-1.4.jar [ERROR] urls[14] = file:/C:/Users/vxa90/.m2/repository/org/codehaus/plexus/plexus-compiler-api/2.5/plexus-compiler-api-2.5.jar [ERROR] urls[15] = file:/C:/Users/vxa90/.m2/repository/org/codehaus/plexus/plexus-utils/3.0.22/plexus-utils-3.0.22.jar [ERROR] urls[16] = file:/C:/Users/vxa90/.m2/repository/org/sonatype/plexus/plexus-build-api/0.0.7/plexus-build-api-0.0.7.jar [ERROR] Number of foreign imports: 1 [ERROR] import: Entry[import from realm ClassRealm[maven.api, parent: null]] [ERROR] [ERROR] -----------------------------------------------------: com.sun.codemodel.CodeWriter

config/application.rb

但是,我不确定它的配置是否正确,因为在module MyApp class Application < Rails::Application config.load_defaults 5.2 config.time_zone = 'Tokyo' end end 中得到的结果参差不齐。 rails consoleTime.zone告诉我时区设置为JST,但是当我执行Time.now时,created_atupdated_at时间戳会呈现为UTC。

User.first

但是,如果我专门要求User.first #=> #<User id: 1, first_name: "Bob", last_name: "Smith", created_at: "2019-04-09 08:54:30", updated_at: "2019-04-09 08:54:30"> 时间,则该时间将显示为JST:

created_at

为什么除非我特别要求时间本身,否则将时间戳呈现为UTC?这正常吗?我的其他表中的User.first.created_at #=> Tue, 09 Apr 2019 17:54:30 JST +09:00 列也发生了同样的现象。

1 个答案:

答案 0 :(得分:1)

您所有的日期似乎都是相同的,只是它们在不同上下文中的表示方式。

此:

User.first
#=> #<User id: 1, first_name: "Bob", last_name: "Smith", created_at: "2019-04-09 08:54:30", updated_at: "2019-04-09 08:54:30">

提供.inspect

的结果

此:

User.first.created_at
#=> Tue, 09 Apr 2019 17:54:30 JST +09:00

是控制台在猜测您要使用当前时区格式化日期。

您可以强制某些表示是明确的

User.first.created_at.to_formatted_s(:db) #should print the same as you see on the inspect
I18n.localize(User.first.created_at) #should localize the date with the default date format
I18n.localize(USer.first.created_at, format: :something) #should localize the date as the format you defined as ":something" on your locale file