🦋
Butterfly 用户手册
  • Introduction
  • 一 前言
  • 二 开始
    • 安装部署
    • 五分钟体验指南
    • 单机使用手册
    • 应用规范
      • handler specs
      • middleware specs
      • xingqiao_plugin specs
      • yiqiu_program specs
  • 三 客户端功能
    • MySQL 原生协议
    • MySQL ORM
    • Redis 原生协议
      • redis_config
      • redis_tls
    • Redis ORM
    • Redis mcpack
    • Localcache
    • Kazoo
  • 四 应用(通用服务)
    • API JSON 规范
    • 异步任务 BaiChuan(百川)
    • 任务调度 RuQi(如期)
    • 任务编排 XingQiao(星桥)
    • 配置管理 WuXing(五行)
    • 运筹决策 BaiCe(百策)
  • 五 部署运维
    • 单机容器化部署
    • 监控
    • 异常排查
      • CPU Load spike every 7 hours
    • 升级
    • 安全
    • 其他
  • 六 前端
    • butterfly_template
    • butterfly_fe
    • butterfly-admin(json2web)
      • amis
      • sso
      • pangu
    • NoahV
    • PyWebIO
  • 七 潘多拉魔盒
    • 装饰器
      • localcache_decorator
      • retry_decorator
      • custom_decorator
      • command2http_decorator
    • 算法
      • 算法-分位数
      • 算法-变异系数
    • 实用工具
      • host_util
      • shell_util
      • http_util
      • time_util
      • random_util
      • concurrent
      • jsonschema
      • blinker
      • toml
      • command_util
      • config_util
      • picobox
      • 对称加密
        • des
        • aes
      • ascii_art
        • ttable
        • chart
      • business_rules
      • python-mysql-replication
      • dict_util
    • 中间件
      • middleware_status
      • middleware_whitelist
    • test_handler.py
  • 八 最佳实践
    • 分布式架构
    • Code practice
    • Log practice
    • Daemon process
  • 附录
Powered by GitBook
On this page
  • 1 三种时间格式转换图
  • 2 timestamp
  • 2.1 当前时间戳
  • 2.2 时间戳转为格式化的字符串
  • 2.3 字符串转换为时间戳
  • 3 ISO 8601 格式的 UTC 时间
  • 3.1 当前时间转为 ISO 格式 UTC 时间
  • 3.2 ISO 格式 UTC 时间转为时间戳(毫秒)
  1. 七 潘多拉魔盒
  2. 实用工具

time_util

1 三种时间格式转换图

               +-----------+
               |struct_time| 结构化的时间
               +---^-------^
                / /       \ \ localtime(本地时区的 struct_time ---- time.localtime() # time.struct_time(tm_year=2020, tm_mon=2, tm_mday=22, tm_hour=12, tm_min=12, tm_sec=54, tm_wday=5, tm_yday=53, tm_isdst=0))
      strftime/ /     mktime\ \  gmtime (UTC 时区的 struct_time)
            / / strptime      \ \
    +-------V--+              +V--------+
    |format_str|              |timestamp| (time.time() # 时间戳:1582344742.146092)
    +----------+              +---------+
  格式化的字符串时间            时间戳

2 timestamp

2.1 当前时间戳

code

from xlib.util import time_util
print time_util.get_current_time()

1627539468

2.2 时间戳转为格式化的字符串

code

from xlib.util import time_util
print time_util.timestamp2time_str_with_format(1627479727, "%Y%m%d%H%M%S")

20210728214207

2.3 字符串转换为时间戳

code

# 2022年第10周的周一
time_util.time2timestamp_with_format("202210-1", "%Y%W-%w")
1646582400

# 2022年第10周的周日
time_util.time2timestamp_with_format("202210-0", "%Y%W-%w")
1647100800

%W:  周在当年的周数(是当年的第几周),星期一作为周的第一天
%w:  今天在这周的天数

3 ISO 8601 格式的 UTC 时间

%Y-%m-%dT%H:%M:%SZ
eg: 2023-06-16T17:08:50Z

3.1 当前时间转为 ISO 格式 UTC 时间

ef get_current_utctime_iso_format():
    """
    get cur utctime format
    """
    return time.strftime("%Y-%m-%dT%H:%M:%SZ", time.gmtime())

3.2 ISO 格式 UTC 时间转为时间戳(毫秒)

from dateutil import parser
import calendar

# 原始时间字符串
time_str = "2023-06-16T17:08:50Z"

# 使用 dateutil.parser 解析时间字符串
utc_time = parser.parse(time_str)

# 确保时间是 UTC 的(dateutil.parser 通常能够正确处理带 Z 的时间字符串)
# 将 datetime 对象转换为 Unix 时间戳(秒,UTC)
unix_timestamp = calendar.timegm(utc_time.utctimetuple())

# 将 Unix 时间戳(秒)转换为毫秒时间戳
milliseconds_timestamp = int(unix_timestamp * 1000)

print("Original Time String:", time_str)
print("Milliseconds Timestamp:", milliseconds_timestamp)
Previoushttp_utilNextrandom_util

Last updated 2 months ago