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的更多相关文章
随机推荐
- Docker安装Tomcat应用服务器
1.安装镜像 1. Install the image: 可以先到https://hub.docker.com/ 搜索镜像 You can get there first. https://hub. ...
- nuxt+vant+rem项目构建
原文链接:https://blog.csdn.net/Young_Gao/article/details/93605428 一.创建项目 1.使用如下命令生成项目 vue init nuxt-comm ...
- ctfshow_web入门 xss
额,怎么说呢,对xss理解不深刻,虽然做了XSS-LAB,但是感觉不会用,看了群主的视频,知道了原因,用群主的话来说就是,X的是自己... 这个文章写得比较潦草... 准备一个带nc的工具: 无vps ...
- 欧拉函数和遗忘自动机 SX 的故逝
欧拉函数 \(\varphi(n)\) 定义为小于 \(n\) 与 \(n\) 互质的数字,炒个例子,\(\varphi(10) = 4\),因为 \(1,3,7,9\) 与 \(10\) 互质. 怎 ...
- XAMPP环境下数据库密码保存文件目录(数据库密码忘记)
转自百度经验: https://jingyan.baidu.com/article/09ea3ede4e2523c0afde3943.html ---------------------------- ...
- CCRD总目录(2007年至今,动态更新中)
中信国健临床通讯总目录 (动态更新.末次更新: 2015-07-06) 年份 目录网址 2010年 1.2010年第01期 (或者浏览有备注的目录: 2010年第01期 ) 2. 2010年第02期 ...
- U-Boot-基础概念与学习分享
U-Boot 基础概念与学习分享 Board: rockchip-px30, armv8, Cortex-A35 U-Boot: rockchip-linux/u-boot, branch next- ...
- pycharm取消代码长度的竖线
- 监控系统grafana常见问题合集
监控系统搭建完毕后,使用中确实存在不少的习惯问题. 系统组成: 展示界面:Grafana 核心系统:Promethus snmp监控:SNMP Exporter ping监控:Blackbox Exp ...
- 浅谈flume
flume做日志收集的工具,将数据源导入到指定目标中.flume之间可以相互连接组件 source:如何从数据源中取数据,可以认为是两种主动source(主动取数据)和被动source(推给so ...