时区
首先来了解一下UTC
UTC
是协调世界时(Coordinated Universal Time)
的缩写,也被称为世界标准时间。它是一个国际标准时间,用于在全球范围内协调时间。UTC是由原子钟测量的时间标准,每秒钟恒定不变。它是世界上最精确的时间测量方法之一。
UTC不属于任何时区,但是它是所有时区的参考时间,其他时区的时间都是相对于UTC的差异。UTC时间不会受到夏令时等影响,因为它使用原子钟作为时间基准,而夏令时等是地方性规定的。
UTC的时间表示方法是以24小时制表示的,其中小时和分钟之间用冒号分隔,例如:18:30。在国际航班和国际商务中,UTC被广泛使用,因为它可以避免不同时区之间的混淆和误解。
你可以将其理解这个是一个通用的货币,如果类比现实世界可以将其理解为美元,目前时间各个国家进行交易的一个中介货币。
Hive中的from_utc_timestamp函数用于将给定的时间戳从指定的UTC时区转换为当前时区的本地时间。该函数的语法如下:
from_utc_timestamp({*any primitive type*} ts, string timezone)
Converts a timestamp* in UTC to a given timezone (as of Hive [HIVE-2272] add TIMESTAMP data type - ASF JIRA).
* timestamp is a primitive type, including timestamp/date, tinyint/smallint/int/bigint, float/double and decimal.
Fractional values are considered as seconds. Integer values are considered as milliseconds. For example, from_utc_timestamp(2592000.0,'PST'), from_utc_timestamp(2592000000,'PST') and from_utc_timestamp(timestamp '1970-01-30 16:00:00','PST') all return the timestamp 1970-01-30 08:00:00.
from_utc_timestamp(timestamp, string timezone)
其中,timestamp表示要转换的时间戳,通常是一个整数或浮点数类型的列;timezone表示要转换到的目标时区,通常是一个字符串类型的值,例如"UTC"、"GMT+8"等。
to_utc_timestamp('2023-02-27 15:15:04.201','Asia/Shanghai')
将中国时区的时间转化成UTC时间
Hive中的from_utc_timestamp函数用于将给定的时间戳从指定的UTC时区转换为当前时区的本地时间。该函数的语法如下:
to_utc_timestamp({any primitive type} ts, string timezone)
Converts a timestamp* in a given timezone to UTC (as of Hive [HIVE-2272] add TIMESTAMP data type - ASF JIRA).
* timestamp is a primitive type, including timestamp/date, tinyint/smallint/int/bigint, float/double and decimal.
Fractional values are considered as seconds. Integer values are considered as milliseconds. For example, to_utc_timestamp(2592000.0,'PST'), to_utc_timestamp(2592000000,'PST') and to_utc_timestamp(timestamp '1970-01-30 16:00:00','PST') all return the timestamp 1970-01-31 00:00:00.
from_utc_timestamp(timestamp, string timezone)
其中,timestamp表示要转换的时间戳,通常是一个整数或浮点数类型的列;timezone表示要转换到的目标时区,通常是一个字符串类型的值,例如"UTC"、"GMT+8"等。
from_utc_timestamp('2023-02-27 15:15:04.201','GMT+8')
将UTC时间转化成中国时间
America/New_York(美国东部时间,UTC-5)
America/Los_Angeles(美国太平洋时间,UTC-8)
Europe/London(英国格林威治标准时间,UTC+0)
Europe/Paris(中欧时间,UTC+1)
Asia/Shanghai(中国标准时间,UTC+8)
Asia/Tokyo(日本标准时间,UTC+9)
Australia/Sydney(澳大利亚东部标准时间,UTC+11)
Africa/Johannesburg(南非标准时间,UTC+2)
America/Sao_Paulo(巴西利亚标准时间,UTC-3)
Pacific/Auckland(新西兰标准时间,UTC+13)
GMT-8和UTC-8表示的是同一个时区,即世界时(UTC)减去8小时所得到的时区,也就是西八区的时区。因此,GMT-8和UTC-8在时区上没有任何区别,只是表述方式略有不同。
GMT-8表示的是相对于格林威治标准时间(GMT)向西8小时的时区。而UTC-8表示的是相对于协调世界时(UTC)向西8小时的时区。由于UTC现在是国际上最常用的时间标准,因此UTC-8更常用于标识这个时区。
需要注意的是,GMT和UTC虽然在概念上有所区别,但它们之间的差别非常小,因为它们的参考时间几乎是相同的。在日常生活中,通常可以将GMT和UTC视为等同的概念,而不会造成任何实质性的误解。
from_utc_timestamp(to_utc_timestamp(CURRENT_TIMESTAMP(),'GMT+8'),'GMT-3') --取当前是中国🇨🇳时间对应的🇧🇷巴西时间
LanguageManual UDF - Apache Hive - Apache Software Foundation