convert timezone

For the time zone "BRAZIL" in database table TTZZ, a shift of -3 hours from the UTC reference time is entered in database table TTZR. The end of the summer time is defined in database table TTZDV as the second Sunday in March at 02:00, which in the year 2003 is March 9. With these settings in the rules, the two conversions below both result in the same local time of "01:30:00". The first conversion shows that the time is still in the summer time.

DATA: time_stamp TYPE timestamp,
dat TYPE d,
tim TYPE t,
tz TYPE ttzz-tzone,
dst TYPE c LENGTH 1.

tz = 'BRAZIL'.
time_stamp = 20030309033000.
CONVERT TIME STAMP time_stamp TIME ZONE tz
INTO DATE dat TIME tim DAYLIGHT SAVING TIME dst.
WRITE: /(10) dat, (8) tim, dst.

time_stamp = 20030309043000.
CONVERT TIME STAMP time_stamp TIME ZONE tz
INTO DATE dat TIME tim DAYLIGHT SAVING TIME dst.
WRITE: /(10) dat, (8) tim, dst.

Exceptions

Non-Catchable Exceptions

  • Cause: Inconsistent control tables for the conversion. 
### 使用 `timezone.utc` 进行时区处理 在 Python 中,可以利用标准库中的 `datetime` 和 `timezone` 来创建 UTC 时间对象并执行相应的时区转换。下面展示如何具体应用 `timezone.utc`。 #### 创建带有 UTC 时区的信息 可以直接通过指定 `tzinfo=timezone.utc` 参数来定义一个基于协调世界时(UTC)的时间实例: ```python from datetime import datetime, timezone utc_now = datetime.now(timezone.utc) print(f"Current time in UTC is {utc_now}") ``` 这段代码会获取当前时刻对应的 UTC 时间,并将其作为带有时区信息的对象返回[^1]。 #### 将本地时间转换为 UTC 时间 如果已经有一个不带时区信息的本地时间点,则可以通过 `.replace()` 方法为其添加 UTC 时区属性;对于已知特定时区的时间戳来说,应该先设置原始时区再调用 `.astimezone(timezone.utc)` 转换成 UTC 格式: ```python local_dt_without_tz = datetime(2023, 9, 15, hour=8) # 错误示范:直接 replace 可能导致错误的结果 incorrect_utc_conversion = local_dt_without_tz.replace(tzinfo=timezone.utc) # 正确做法:假设这是上海时间,应先设定正确时区后再转成 UTC correct_localization_then_convert_to_utc = ( local_dt_without_tz .replace(tzinfo=timezone(timedelta(hours=8))) .astimezone(timezone.utc) ) print(f"Incorrect conversion result: {incorrect_utc_conversion}") print(f"Correct conversion after localization: {correct_localization_then_convert_to_utc}") ``` 这里强调了正确的时区转换流程的重要性——即不应简单地给定 `tzinfo=timezone.utc`,而是要确保源数据拥有准确的初始时区标记之后再做变换[^4]。 #### 输出格式化后的 UTC 时间字符串 当需要显示或记录经过处理过的 UTC 时间时,可借助于 `.strftime()` 函数来自定义输出样式: ```python formatted_utc_string = utc_now.strftime("%Y-%m-%d %H:%M:%S Z") print(formatted_utc_string) ``` 此部分展示了怎样把之前得到的 UTC 时间按照 ISO 8601 或其他自定义模式呈现出来[^3]。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值