#!/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的更多相关文章

随机推荐

  1. vue3 setup语法糖下,vue自定义指令的实现,以及指令全局挂载,自定义v-loading的实现

    最近一段时间,在做h5的移动端项目,UI组件库使用的vant,vant组件中的loading实在难用,无法包裹某个块进行loading,也无法对非组件的标签进行loading,所以想着自定义写个指令, ...

  2. 【Raspberry Pi / 树莓派】小小工控机担起大大责任

    CM4 Nano是一款基于Raspberry Pi / 树莓派 Compute Module 4(简称CM4),由EDATEC面向工业应用设计的嵌入式计算机, 充分利用CM4在结构上灵活性解决CPU, ...

  3. Hexo系列(四):Hexo写文章

    作者:独笔孤行 官网:​​ ​http://anyamaze.com​​ 公众号:云实战 可以执行下列命令来创建一篇新文章或者新的页面. $ hexo new [layout] <title&g ...

  4. js实现图片选中马上显示图片名,选择后可以预览,即选即显

    <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...

  5. ChatGPT Java客户端,OpenAi的Java版本SDK已完成,请火速接入。

    已经支持OpenAI官方的全部api,有bug欢迎朋友们指出,互相学习. 源码地址:https://github.com/Grt1228/chatgpt-java 不对之处欢迎指正. 注意:由于这个接 ...

  6. oculus 安装其他盘方法

    把安装文件OculusSetup.exe安装到D盘(默认直接按C盘) D:\softbackup\OculusSetup.exe /drive=D

  7. (unsigned)short溢出后隐式转换为int

    同学被面试官问到一个细节中的细节,虽然个人觉得意义不大,但还是好奇并在和同学一起实验后搞清楚了,记录一下 如下: int main() { unsigned short a = 65535, b = ...

  8. rsut 字节数组和字符串转换

    一.字符串转换为字节数组 let s = String::from("str"); let v = s.as_bytes(); // &[u8] println!(&quo ...

  9. go 语言 for循环的一个坑

    1.案例1 package main import "fmt" type Card struct { id int } func main() { list := make([]* ...

  10. mysql explain 查看sql语句执行计划概述

    mysql explain 查看sql语句执行计划概述 id:选择标识符select_type:表示查询的类型.table:输出结果集的表partitions:匹配的分区type:表示表的连接类型po ...