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的更多相关文章
随机推荐
- Vue3中的响应式api
一.setup文件的认识 特点1:script 中间的内容就是一个对象 特点2:script 在第一层 定义的方法 或者 变量 => 就是这个对象 属性 => 顶层的绑定回被暴露给模板( ...
- 【JS入门小游戏】01-骰子游戏
01-骰子游戏 游戏出自Udemy的JS课程中提到的一个游戏,课程主要是对JS部分进行详细的从0开始的讲解,本篇文章是对整个游戏的分析,包括HTMK,CSS和JS,也主要对JS进行刨析. 游戏链接:h ...
- windows定时任务执行python爬虫
有一些定时爬取的操作,适合用定时任务去执行.个人单独用的项目不适合放在工作所用的服务器上,也没必要单独买个服务器,我们windows电脑本身就有这项功能.接下来是一个windows定时任务执行pyth ...
- PHP封装自定义函数function
最近一直在看PHP教程,毕竟懂点PHP语言还是不错的选择,起初是准备制作一个三文件夹内关键词组合长尾关键词,然后用PHP做一个全站的动态聚合页面的一个PHP插件,不负有心人啊!已写好,稍晚整理会在资源 ...
- Linus对Linux 6.3内核的合并解释不满
Linux 6.3 内核的合并窗口已开启,Linus Torvalds 也收到了大量的 PR,目前总体看来正在有序进行.但 Linus 对部分合并请求的日志信息非常不满:"我之前就已经说过, ...
- C语言学习--指针大小端
// 大端存储: 数据的高位存储在内存的低地址位置 //数据0x12345678, 四字节地址0x0, 0x1,0x2,0x3 //存储方式: 0x0: 存储12, 0x1:存34 0x2: 存5 ...
- CF207C
前言 学习 zzd 博客( 这题超级没有素质. 连个题解都搜不到. 好不容易搜到一个. 看了一下是 pascal. 不过还好我有办法. 树剖做 \(k\) 级祖先. 十万的俩老哥飘过. 三百毫秒优异成 ...
- 关于xtr的一些基础
Q&A 如何找到QSEQ方式的xtt呢? 我们可以去版本的代码中寻找,具体位置在:(modem_proc/rf/rftarget_denali/mtp/xtt/etc/QSEQ/) 在QSEQ ...
- css小技巧【让背景最少是屏幕高度】【让三个字和四个字左右对齐】
怎么让背景最少是屏幕高度 min-height: 100vh; 怎么让三个字和四个字左右对齐 text-align-last: justify;
- SpringBoot启动失败问题
SpringBoot启动失败问题 一.现象 pom.xml没有显示报错,也能查看到各种需要的依赖jar包,但在启动时显示 二.原因 网络不好导致maven导包失败或者导包不完整 三.解决办法 进入项目 ...