当前位置: 首页 > news >正文

商务网站建设与维护论文爱站网反链查询

商务网站建设与维护论文,爱站网反链查询,赣州做公司网站,中国四川机械加工网文章目录 1.背景2. 遇到的坑3. 一些小案例3.1 当前日期、日期时间、UTC日期时间3.2 昨天、昨天UTC日期、昨天现在这个时间点的时间戳3.3 日期转时间戳3.4 时间戳转日期3.5 日期加减、小时的加减 4. 总结5. 完整的编码 1.背景 最近项目是国际项目,所以需要经常需要用…

文章目录

  • 1.背景
  • 2. 遇到的坑
  • 3. 一些小案例
    • 3.1 当前日期、日期时间、UTC日期时间
    • 3.2 昨天、昨天UTC日期、昨天现在这个时间点的时间戳
    • 3.3 日期转时间戳
    • 3.4 时间戳转日期
    • 3.5 日期加减、小时的加减
  • 4. 总结
  • 5. 完整的编码

1.背景

最近项目是国际项目,所以需要经常需要用到UTC时间和local时间的转换。
所以整理了一下时间戳工具类,方便使用。
这里主要用到的包就是datatime、time、pytz。

2. 遇到的坑

直接看测试案例

tzinfo=pytz.timezone("Asia/Shanghai")
dtStr = "2023-05-28 00:00:00"
dt1 = datetime.datetime.strptime(dtStr,"%Y-%m-%d %H:%M:%S").replace(tzinfo=tzinfo)
dt2 = dt_tz = tzinfo.localize(datetime.datetime.strptime(dtStr, '%Y-%m-%d %H:%M:%S'))
print(dt1)
print(dt2)

输出结果:

2023-05-28 00:00:00+08:06
2023-05-28 00:00:00+08:00

这里第一种方法dt1中的输出时区带有06分的差异。
关于这个误差查找到的相关解释:
在这里插入图片描述

3. 一些小案例

3.1 当前日期、日期时间、UTC日期时间

print(DateUtil.currDate())
print(DateUtil.currDateTime())
print(DateUtil.currDateTime("UTC"))

输出结果:

2023-05-29
2023-05-29 10:44:27
2023-05-29 02:44:27

3.2 昨天、昨天UTC日期、昨天现在这个时间点的时间戳

print(DateUtil.yesterday())
print(DateUtil.yesterdayUTC()) 
print(DateUtil.yesterday_ts())

输出结果:

2023-05-28
2023-05-28  #这里如果是早上8点以前运行的就不一样
1685242026391

3.3 日期转时间戳

print(DateUtil.date2ts("2023-05-28"))
print(DateUtil.datetime2ts("2023-05-28 10:01:01"))
print(DateUtil.datetime2ts("2023-05-28 10:01:01","UTC"))
print(DateUtil.datetime2ts("2023-05-28 10:01:01","Asia/Shanghai"))

输出结果:

1685203200000
1685239261000
1685268061000 #如果使用的是UTC格式,则时间戳不一样
1685239261000

3.4 时间戳转日期

print(DateUtil.timestamp2Date("1685239261000"))
print(DateUtil.timestamp2Date("1685239261")) # 这里自动判断是毫秒还是秒
print(DateUtil.timestamp2Date("1685239261","UTC")) # 这里指定输出的日期的时区是UTC格式
print(DateUtil.timestamp2Date(1685239261)) #int类型或者字符串都可以转行
print(DateUtil.timestamp2Date(1685239261000,"UTC")) #int类型或者字符串都可以转行

输出结果:

2023-05-28 10:01:01
2023-05-28 10:01:01
2023-05-28 02:01:01
2023-05-28 10:01:01
2023-05-28 02:01:01

3.5 日期加减、小时的加减

print(DateUtil.dateAdd("2023-05-28",1)) #加
print(DateUtil.dateAdd("2023-05-28",-1)) #减
print(DateUtil.datetimeAdd("2023-05-28 10:01:01",-1)) #日期时间的加减
print(DateUtil.hourAdd("2023-05-28 10:01:01",1)) # 小时的加减
print(DateUtil.hourAdd("2023-05-28 10:01:01",-1)) # 小时的加减

输出结果:

2023-05-29
2023-05-27
2023-05-27 10:01:01
2023-05-28 11:01:01
2023-05-28 09:01:01

4. 总结

刚开始思绪有点乱,刚好趁机会好好的整理了一遍思路。
注意: 当使用日期进行格式转换时,需要确定这个日期对应的时区。

  • 时区的设置
    python的日期中时区对应的有个属性timezone。
    我这里主要是通过pytz.timezone(“时区字符串”)来设置时区的。
    比如,要将日期转为时间戳,首先要指定这个日期的时区属性。
tzinfo = pytz.timezone("Asia/Shanghai")
  • 日期设置
    然后通过localize方法配置这个日期的时区。
dt_tz = tzinfo.localize(dt)

再进行其他的转换。
但是千万要注意,不要使用datetime中的.replace方法来设置时区。

# 这样设置会导致会导致输出结果`2023-05-28 00:00:00+08:06` 带有一个6分钟的差异
dt_tz =datetime.datetime.strptime("2023-05-28 00:00:00","%Y-%m-%d %H:%M:%S").replace(tzinfo=tzinfo)  

5. 完整的编码

# -*- coding: utf-8 -*-
# @Time    : 2023/3/28  10:04
# @Author  : King
# @Software: PyCharm
# @Describe: 
# -*- encoding:utf-8 -*-
import datetime
import time,pytz_timezone_utc = "UTC"
_timezone_cn = "Asia/Shanghai""""
DateTime日期转时间戳(日期时区为上海)
"""
def datetime2ts(date, timezone=_timezone_cn):if isinstance(date, datetime.datetime):return int(date.timestamp() * 1000)else:date = date + " 00:00:00" if len(date) <= 10 else datedt = datetime.datetime.strptime(date, '%Y-%m-%d %H:%M:%S')dt_tz = pytz.timezone(timezone).localize(dt)return int(dt_tz.timestamp()*1000)"""
DateTime日期转时间戳(日期时区为UTC)
"""
def utcDatetime2ts(date):return datetime2ts(date,_timezone_utc)"""
Date日期转时间戳
"""
def date2ts(date,timezone=_timezone_cn):if isinstance(date,datetime.datetime):return int(date.timestamp()*1000)else:dt = datetime.datetime.strptime(date, '%Y-%m-%d')dt_tz = pytz.timezone(timezone).localize(dt)return int(dt_tz.timestamp()*1000)"""
时间戳转日期
"""
def _ts2Date(timestamp,timezone,date_format="%Y-%m-%d %H:%M:%S"):tz = pytz.timezone(timezone)timestamp_s = int(timestamp) if len(str(timestamp)) <= 10 else int(timestamp)/1000dt_tz = datetime.datetime.fromtimestamp(timestamp_s,tz)return dt_tz.strftime(date_format)"""
时间戳转日期
"""
def timestamp2Date(timestamp,timezone=_timezone_cn):return _ts2Date(timestamp,timezone)"""
获取当前日期 2023-03-28
"""
def currDate(timezone=_timezone_cn):return datetime.datetime.now(pytz.timezone(_timezone_utc)).astimezone(pytz.timezone(timezone)).strftime('%Y-%m-%d')
"""
当前UTC日期
"""
def currUTCDate():return datetime.datetime.now(pytz.timezone(_timezone_utc)).strftime('%Y-%m-%d')"""
昨日
"""
def yesterday(timezone=_timezone_cn):return dateAdd(currDate(timezone),-1)
"""
UTC昨日
"""
def yesterdayUTC():return dateAdd(currUTCDate(),-1)
"""
当前日期时间
"""
def currDateTime(timezone=_timezone_cn):return datetime.datetime.now(pytz.timezone(timezone)).strftime('%Y-%m-%d %H:%M:%S')"""
当前时间戳
"""
def currTimestamp():return int(time.time()*1000)"""
昨天时间戳
"""
def yesterday_ts():return currTimestamp() - 24*3600*1000"""
日期的增减
"""
def dateAdd(date, n):dt = datetime.datetime.strptime(date, "%Y-%m-%d")return (dt + datetime.timedelta(days=n)).strftime("%Y-%m-%d")
"""
日期时间的增减
"""
def datetimeAdd(date,n):dt = datetime.datetime.strptime(date,'%Y-%m-%d %H:%M:%S')return (dt + datetime.timedelta(days=n)).strftime('%Y-%m-%d %H:%M:%S')"""
小时的增减
"""
def hourAdd(date,n):dt = datetime.datetime.strptime(date, '%Y-%m-%d %H:%M:%S')return  (dt + datetime.timedelta(hours=n)).strftime('%Y-%m-%d %H:%M:%S')"""
timezone:默认是Aisa/Shanghai时区
指定日期的上一个小时的时间戳
"""
def lastHourTimestamp(date,timezone=_timezone_cn):if(len(date) == 10):ts = date2ts(date,timezone)else:ts = datetime2ts(date,timezone)return ts - ts % 3600000 - 3600*1000

文章转载自:
http://pilferage.c7498.cn
http://klavier.c7498.cn
http://trithing.c7498.cn
http://bracelet.c7498.cn
http://fluty.c7498.cn
http://biotope.c7498.cn
http://blandish.c7498.cn
http://impermeability.c7498.cn
http://dipsy.c7498.cn
http://bottle.c7498.cn
http://glimmer.c7498.cn
http://parcener.c7498.cn
http://haughtiness.c7498.cn
http://imperiality.c7498.cn
http://camp.c7498.cn
http://seatwork.c7498.cn
http://administrators.c7498.cn
http://justiceship.c7498.cn
http://coattail.c7498.cn
http://crossbirth.c7498.cn
http://rheumatism.c7498.cn
http://acetoacetyl.c7498.cn
http://firehouse.c7498.cn
http://subotica.c7498.cn
http://silk.c7498.cn
http://velate.c7498.cn
http://intermediary.c7498.cn
http://drumbeater.c7498.cn
http://comparable.c7498.cn
http://krim.c7498.cn
http://backwardly.c7498.cn
http://scatterbrained.c7498.cn
http://sinuiju.c7498.cn
http://twoness.c7498.cn
http://preclusion.c7498.cn
http://matripotestal.c7498.cn
http://garonne.c7498.cn
http://snell.c7498.cn
http://italianist.c7498.cn
http://crevasse.c7498.cn
http://shavie.c7498.cn
http://nightclub.c7498.cn
http://lak.c7498.cn
http://thionic.c7498.cn
http://druidess.c7498.cn
http://unconditioned.c7498.cn
http://disgustful.c7498.cn
http://cumin.c7498.cn
http://bicentenary.c7498.cn
http://altissimo.c7498.cn
http://hemosiderosis.c7498.cn
http://ladefoged.c7498.cn
http://geohydrology.c7498.cn
http://arborescent.c7498.cn
http://cockaigne.c7498.cn
http://ranunculus.c7498.cn
http://revulsant.c7498.cn
http://interzonal.c7498.cn
http://boule.c7498.cn
http://hale.c7498.cn
http://sulfone.c7498.cn
http://eudaemon.c7498.cn
http://hemin.c7498.cn
http://dissever.c7498.cn
http://oeillade.c7498.cn
http://heterocharge.c7498.cn
http://saorstat.c7498.cn
http://pinocytized.c7498.cn
http://righteous.c7498.cn
http://superheavy.c7498.cn
http://filamerican.c7498.cn
http://sublibrarian.c7498.cn
http://mimetic.c7498.cn
http://invoke.c7498.cn
http://killdee.c7498.cn
http://unmix.c7498.cn
http://redisplay.c7498.cn
http://rampage.c7498.cn
http://witching.c7498.cn
http://barbuda.c7498.cn
http://counterrotation.c7498.cn
http://agonizingly.c7498.cn
http://gervais.c7498.cn
http://noncontinuous.c7498.cn
http://revaccination.c7498.cn
http://autogenic.c7498.cn
http://primate.c7498.cn
http://boomerang.c7498.cn
http://apodous.c7498.cn
http://streptokinase.c7498.cn
http://sward.c7498.cn
http://unapproved.c7498.cn
http://speedwell.c7498.cn
http://ocellus.c7498.cn
http://tocometer.c7498.cn
http://maidenish.c7498.cn
http://anisotropy.c7498.cn
http://stook.c7498.cn
http://deuteration.c7498.cn
http://caravaner.c7498.cn
http://www.zhongyajixie.com/news/53586.html

相关文章:

  • 小江高端企业网站建设中国百强城市榜单
  • 360网站推广登录电商网站推广方案
  • 专做坏消息的网站cms建站系统
  • 安徽湖滨建设集团有限公司网站最新国内新闻10条
  • 怎样做古玩网站好看的web网页
  • 公司如何做网站一般多少钱网络推广营销网站建设专家
  • 怎么样免费给网站做优化数字营销包括哪六种方式
  • 做ps的网站有哪些功能吗网站怎么做推广和宣传
  • 一家专门做灯的网站什么是搜索关键词
  • 做新闻网站盈利如何建立网站平台
  • 绿色 网站 源码今日热点新闻事件及评论
  • 庆阳定制网站推广软文模板
  • 做爰片的网站哪家建设公司网站
  • 个人网站 备案 广告网站推广方案策划
  • 柚子皮wordpress主题常州seo建站
  • 阳春网站制作中国企业网官方网站
  • 商务网站开发实训任务书代做百度关键词排名
  • 从事电子商务的网站建设百度搜索资源管理平台
  • 陕西网站建设营销推广b2b电商平台有哪些
  • 阿升网站免费学设计家居seo整站优化方案
  • 自己给公司做网站河北百度推广seo
  • 如何做返利网站世界杯数据分析
  • 备案 网站服务内容网络推广都是收费
  • 网站按钮确定后图片怎么做google收录查询
  • 长宁专业做网站搭建网站流程
  • 中文网站建设中网络推广app是违法的吗
  • 《学做网站论坛》视频下载北京seo公司wyhseo
  • 哪个做网站公司好友链是什么
  • 我要学做网站杭州关键词优化外包
  • 企业网站建设是什么公司品牌推广方案范文