有什么方法可以使用时间戳获取 TimeZone 值 [英] Is there any way by which I can get TimeZone value using time stamp

查看:77
本文介绍了有什么方法可以使用时间戳获取 TimeZone 值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这就是我拥有的价值,

2020 年 3 月 29 日星期日 02:55:00 GMT+0530

我想得到,例如

<块引用>

亚洲/加尔各答

作为输出.提前致谢.

解决方案

偏移量不表示区域

<块引用>

使用时间戳获取时区值

没有.

您无法根据偏移量确定时区.

许多时区可以共享相同的 UTC 偏移量(本初子午线前后的时-分-秒数).

请参阅维基百科中的时区名称列表.单击列标题以按偏移量排序.请注意多个区域共享相同偏移的频率.

具体到您的示例,请注意我们目前有两个区域如何巧合地共享比 UTC 早五个半小时的偏移量:

  • 亚洲/加尔各答(印度)
  • 亚洲/科伦坡(斯里兰卡)

因此,如果没有进一步输入,就无法知道您输入字符串的作者是印度时间还是斯里兰卡时间.

顺便说一下,名称Asia/Calcutta 已更改为Asia/Kolkata.如果您的系统没有这样的名称,那么您的 tzdata 已经过时了几年.在操作系统、数据库服务器(如 Postgres)和运行时(如 Java)中始终保持 tzdata 的所有副本是最新的.

另一个复杂因素:政治家经常更改其管辖范围内使用的抵消额.

因此,虽然今天整个印度都使用相同的 +05:30 偏移量,但情况并非总是如此,未来也不太可能始终如此(基于区域变化频率的历史记录)世界).

ISO 8601

ISO 8601 标准定义了许多将日期时间值表示为文本的合理格式.

2020-01-23T12:34:56.123456789+05:30

Java 8 及更高版本中内置的 java.time 框架通过在方括号中附加时区名称来明智地扩展其中一种格式.如果可行,我建议使用这种格式.

2020-01-23T12:34:56.123456789+05:30[亚洲/加尔各答]

This is the value which I have,

Sun Mar 29 2020 02:55:00 GMT+0530

and I want to get,for example

Asia/Calcutta

as ouput. Thanks in advance.

解决方案

Offset does not indicate zone

get TimeZone value using time stamp

No.

You cannot determine a time zone from an offset.

Many time zones can share the same offset-from-UTC (the number of hours-minutes-seconds ahead or behind the prime meridian).

See the list of time zone names in Wikipedia. Click on the column header to sort by offset. Notice how often several zones share the same offset.

Specific to your example, notice how we currently have two zones that coincidentally share an offset of five and a half hours ahead of UTC:

  • Asia/Kolkata (India)
  • Asia/Colombo (Sri Lanka)

So, without further input, there is no way to know if the author of your input string intended India time or Sri Lanka time.

By the way, the name Asia/Calcutta has been changed to Asia/Kolkata. If your system has no such name, then your tzdata is several years out of date. Always keep all the copies of tzdata up-to-date in OSes, database servers such as Postgres, and runtimes such as Java.

Another complication: politicians frequently change the offset used in their jurisdictions.

So while all of India today uses the same offset of +05:30, that has not always been the case, nor is it likely to always be true in the future (based on the history of how often zones change around the world).

ISO 8601

The ISO 8601 standard defines many sensible formats for representing date-time values as text.

2020-01-23T12:34:56.123456789+05:30

The java.time framework built into Java 8 and later extends one of those format wisely by appending the name of the time zone in square brackets. I suggest using this format if feasible.

2020-01-23T12:34:56.123456789+05:30[Asia/Kolkata]

这篇关于有什么方法可以使用时间戳获取 TimeZone 值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆