从本地系统获取当前位置区域 [英] get current location region from local system

查看:41
本文介绍了从本地系统获取当前位置区域的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要从本地系统中获取国家/城市(例如: America/New_York ),然后才能使用时间包中的LoadLocation(值字符串)将其提供给api.

I need to get the Country/City (eg: America/New_York) from my local system to give to an api using the LoadLocation(value string) in the time package.

如何在不使用任何外部配置的情况下获取该区域?

How can I get this without using any external configuration to get the region?

推荐答案

该位置的IANA名称不能保证在您的系统上正确设置.

The IANA name of the location is not something that is guaranteed to be set properly on your system.

要获取它,请首先检查 TZ 环境变量.如果存在,则可能是有效的IANA名称(例如: America/New_York ).

To get it, first check the TZ environment variable. If it is present, it might be a valid IANA name (eg: America/New_York).

如果未设置 TZ ,请查看/etc/localtime 指向的内容(通常是符号链接).在大多数情况下,它会指向/usr/share/zoneinfo/America/New_York 之类的东西.您需要自己提取区域/位置分量.

If TZ is not set, look at what /etc/localtime points to (it is usually a symlink). In most cases, it will point to something like /usr/share/zoneinfo/America/New_York. You would need to extract the area/location components yourself.

重要提示:这里有很多警告:

  • TZ的内容或/etc/localtime 指向的文件可以是时区的缩写名称.例如: EST .没有办法将其转换为IANA名称.
  • TZ可以设置但为空,表示 UTC
  • /etc/localtime 可以是实际tzdata文件的副本,该文件仅包含时间偏移,而不包含IANA名称.
  • the content of TZ, or the file pointed to by /etc/localtime could be the time zone abbreviated name. eg: EST. There is no way to go from that to a IANA name.
  • TZ could be set but empty, meaning UTC
  • /etc/localtime could be a copy of the actual tzdata file, which only contains time offsets, not the IANA name.

简而言之,如果服务器接受缩写的时区名称或时间偏移,那么对每个参与人员来说都会更好.

In short, it would be much better for everyone involved if the server accepted an abbreviated time zone name or a time offset.

这篇关于从本地系统获取当前位置区域的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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