如何使用自定义时区中的时间预填充 datetime

编程入门 行业动态 更新时间:2024-10-27 16:38:01
本文介绍了如何使用自定义时区中的时间预填充 datetime_select?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我有活动可以在不同时区.

在编辑我想要时间和;日期与该事件的时区一起显示.

Upon edit I want the time & date to show with the time zone of that very event.

但是,当我点击 edit 时,datetime_select 总是显示用户时区的时间(而不是事件之一).

However, when I hit edit, datetime_select always shows the time of the users time zone (as opposed to the one of the event).

示例:

  • 活动于上午 10 点在阿姆斯特丹开始(GMT+1)
  • 用户时区配置为伦敦 (GMT+0)

结果:编辑时事件时间被错误地预设为上午 9 点

代码片段:

def edit Time.zone = @event.time_zone @event.beginn = @event.beginn.in_time_zone @event.endd = @event.endd.in_time_zone # [...] end

请注意,@event.time_zone 包含所需的时区(上例中的阿姆斯特丹").

Note that @event.time_zone contains the desired time zone ("Amsterdam" in the above example).

如何在编辑时将 datetime_select 预设为其各自区域中的事件时间?

How can I have datetime_select preset to the events time in it's respective zone upon edit?

推荐答案

正如 pixeltrix 在错误报告的线程中指出的那样,覆盖相关属性的读取器/获取器更干净,例如所以:

As pixeltrix pointed out in the thread of the bug report, it's cleaner to override the readers/getters of the attributes in question like so:

# in event model def beginn super.in_time_zone(time_zone) if super end def endd super.in_time_zone(time_zone) if super end

这样可以省略问题中概述的编辑操作中的逻辑,并避免与依赖Time.zone的其他部分发生干扰.

This way the logic in the edit action as outlined in the question can be omitted and interferences with other parts that rely on Time.zone are avoided.

更多推荐

如何使用自定义时区中的时间预填充 datetime

本文发布于:2023-10-17 22:18:48,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1502238.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:自定义   如何使用   时区   时间   datetime

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!