pj_time_swap
#!/usr/bin/python
# -*- coding: UTF-8 -*-
import time
import re
from datetime import datetime, timezone, timedelta
import pytz
date_time = r'([0-9]{4})[/-]*([0-9]{2})[/-]*([0-9]{2}) ([0-9]{2}):*([0-9]{2}):*([0-9]{2})'
str_time = r'([0-9]{2}):*([0-9]{2}):*([0-9]{2})'
local_timezone = datetime.now(timezone.utc).astimezone().tzinfo
hour_offset = time.timezone /60 /60 *(-1)
timezone_mapping = {}
timezone_mapping['beijing'] = pytz.timezone('Asia/ShangHai')
timezone_mapping['dalian'] = pytz.timezone('Asia/ShangHai')
timezone_mapping['usa'] = pytz.timezone('America/New_York')
timezone_mapping['newyork'] = pytz.timezone('America/New_York')
def _get_timezone(input_name):
if input_name in timezone_mapping:
return timezone_mapping[input_name]
for item in pytz.common_timezones:
if item.lower().find( input_name ) >= 0:
return pytz.timezone(item)
return None
def _main_func_():
while True:
print("welcome, please input your time")
ipt = input()
if ipt == 'exit' or ipt == 'no' or ipt == 'n':
break
if not ipt:
continue
dt_time = ''
if re.match( date_time, ipt ):
m = re.match( date_time, ipt )
dt_time = datetime( int(m.group(1)), int(m.group(2)), int(m.group(3)), int(m.group(4)), int(m.group(5)), int(m.group(6)),0)
elif re.match( r'.* '+ date_time, ipt ):
city = str.lower(ipt[:ipt.find(' ')])
tz = _get_timezone(city)
if not tz:
print('no such city in dictionary')
continue
m = re.match( r'.* '+ date_time, ipt )
dt_time = datetime(int(m.group(1)), int(m.group(2)), int(m.group(3)), int(m.group(4)), int(m.group(5)), int(m.group(6)),0)
dt_time = tz.localize(dt_time)
elif re.match( str_time, ipt):
m = re.match( str_time, ipt)
tt = datetime.now()
dt_time = datetime(tt.year, tt.month, tt.day, int(m.group(1)), int(m.group(2)), int(m.group(3)),0)
elif re.match( r'.* '+ str_time, ipt ):
city = str.lower(ipt[:ipt.find(' ')])
tz = _get_timezone(city)
if not tz:
print('no such city in dictionary')
continue
m = re.match(r'.* '+ str_time, ipt)
tt = datetime.now()
dt_time = datetime( tt.year, tt.month, tt.day, int(m.group(1)), int(m.group(2)), int(m.group(3)),0)
dt_time = tz.localize(dt_time)
else:
print('you have input a wrong format')
dt_ShangHai = dt_time.astimezone(pytz.timezone('Asia/ShangHai'))
dt_Tokyo = dt_time.astimezone(pytz.timezone('Asia/Tokyo'))
dt_NewYork = dt_time.astimezone(pytz.timezone('America/New_York'))
print_result('ShangHai', dt_ShangHai)
print_result('Tokyo', dt_Tokyo)
print_result('New_York', dt_NewYork)
while True:
print('want to change to other timezone')
ipt = input()
if ipt == 'exit' or ipt == 'no' or ipt == 'n':
break
print('input a city')
ipt = input()
tz = _get_timezone(str.lower(ipt))
if not tz:
print('no such city')
continue
dt_new = dt_time.astimezone(tz)
print_result( ipt, dt_new )
def print_result( city, dt_in ):
fmt = "%Y-%m-%d %H:%M:%S %Z"
output = dt_in.strftime(fmt)
output = output + ' <- ' + city + " Time"
print(output)
def _test():
tz = pytz.timezone('Asia/ShangHai')
dt_time = datetime(2022,10,28,11,11,11,0)
print(dt_time.isoformat())
print(dt_time)
dt_time = tz.localize(dt_time)
print(dt_time.isoformat())
print(dt_time)
if __name__ == '__main__':
_main_func_()
# _test()
pj_time_swap的更多相关文章
随机推荐
- 学习Vue过程中遇到的问题---code: 'MODULE_NOT_FOUND'
在学习安装脚手架Vue-cli过程中 步骤为: 第一步(仅第一次执行):全局安装@vue/cli. npm install -g @vue/cli 第二步:切换到你要创建项目的目录,然后使用命令创建项 ...
- 2020.11.30【NOIP提高A组】模拟
总结与反思 很不幸,估分 \(170\),可惜 \(T2\) 暴力 \(50pts\) 全掉了 \(T1\) 结论题,如果想到了,\(O(n)\) 过,只有十几行代码 感觉不好想,不过还是 \(A\) ...
- 重新配置 Idea Webapp 部署
一般 Idea 创建一个 Webapp 时已经自动配置好了,但难免出现意想不到的意外,例如,访问资源 404,编译之后没有把 jsp 页面部署进去等问题. 1️⃣第一步,配置 Project Sett ...
- PostgreSQL lag,lead获取记录前后的数据
场景:获取当前行的下一行某一字段数据,获取当前行的上一行某一字段数据 1.测试数据: postgres=# select * from tb1; id | name ----+------ 1 | a ...
- LeetCode-2104 子数组范围和
来源:力扣(LeetCode)链接:https://leetcode-cn.com/problems/sum-of-subarray-ranges 题目描述 给你一个整数数组 nums .nums 中 ...
- No.2.2
空间转换(使用transform属性实现元素在空间的位移.旋转.缩放等效果) 空间:是从坐标轴角度定义的.x.y.和 z三条坐标轴构成了一个立体空间,z轴位置与视线方向相同. 空间转换也叫3D转换(属 ...
- centos mininet安装-坑
https://blog.csdn.net/milesandnick/article/details/108017349?utm_medium=distribute.pc_relevant.none- ...
- 开源 IM 系统 tinode 部署教程| WSL 环境
背景 我们的需求是在本地部署一套 IM 系统,选择 tinode.为便于后端启动,我们采用 WSL 环境,配合 docker 安装数据库,来启动 IM 应用. 解决 WSL 启动前端和后台服务 cmd ...
- vue实现随机生成图形验证码
效果展示 安装插件 npm i identify 定义组件 verificationCode.vue <template> <!-- 图形验证码 --> <div cla ...
- 利用for循环同步执行异步方法
//定义一个异步函数 const foo1 = (i) => { return new Promise((resolve, reject) => { setTimeout(() => ...