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的更多相关文章
随机推荐
- javaEE(多线程、线程通信、线程安全、线程池、线程池工具)
多线程 多线程的创建 Thread类的方法 线程安全.线程同步 线程通信.线程池 定时器.线程状态.. Thread类 java是通过java.lang.Thread类来代表线程的 按照面向对象的思想 ...
- 02Python输入输出
输入输出 print()函数 sep=' ' 数据之间一空格分割,默认是空格 end='\n' 在打印后会额外的加一个数据,默认是换行符 print("hello", " ...
- JZOJ 3234. 阴阳
阴阳 题面 分析 个人认为是极好的题,很容易写 如果你学点分治是无奈背板的,那就做做这道题,加深你对点分治的理解 一般的,处理树上大规模统计问题,我们分治的关键是找一棵子树的重心 找到分治中心,即新一 ...
- 这些 JavaScript 笔试题你能答对几道?
收藏 javascript-questions 这个仓库很久了,趁着周末来锻炼下自己的 JS 基础水平 因为逐渐也在承担一些面试工作,顺便摘录一些个人觉得比较适合面试的题目和方向 事件流(捕获.冒泡) ...
- Liunx(CentOS)安装Nacos(单机启动,绑定Mysql)
Liunx安装Nacos(单机启动,绑定Mysql) 一,准备安装包 github下载点 二,在/usr/local/目录下创建一个文件夹用于上传和解压Nacos cd /usr/local/ #这里 ...
- 基于IGServer的Web地图要素空间分析
1. 引言 MapGIS IGServer 是中地数码的一款跨平台GIS 服务器产品,提供了空间数据管理.分析.可视化及共享服务 MapGIS IGServer的下载(试用)地址:MapGIS IGS ...
- 内容分发网络 CDN
介绍 CDN 内容分发网络(英语:Content Delivery Network 或 Content Distribution Network,缩写:CDN)是建立并覆盖在承载网上,由不同区域的服务 ...
- Linux操作命令(五)1.find命令 2.xargs命令
1.find命令(一) 查找,沿着文件层次结构向下遍历,匹配符合条件的文件,并执行相应的操作 参数 描述 -print find 命令将匹配的文件输出到标准输出 -exec find 命令对匹配的文件 ...
- MogDB 学习笔记之 -- 索引失效
[[toc]]# 概念描述哪些操作会导致分区表的全局索引失效(比如 move partition,drop partition ,truncate partition ,split partition ...
- 在github上如何克隆带子模块的项目?
使用命令git clone --recursive xxxx(项目地址).