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

Day20【time模块】

time模块

1.time.time()

time.time()会返回当前时间的时间戳,时间戳是指格林威治时间1970年01月01日00时00分00秒(北京时间1970年01月01日08时00分00秒)起至现在的总秒数。注意time.time()的括号内没有参数:

import timecurrent_time = time.time()
print(current_time)  # 输出当前时间的时间戳,以秒为单位

2.import time

current_time = time.time()
print(current_time) # 输出当前时间的时间戳,以秒为单位

import timecurrent_time = time.localtime()  # 获取当前时间的结构化时间对象
formatted_time = time.strftime("%Y-%m-%d %H:%M:%S", current_time)
print(formatted_time)  # 格式化时间输出
# 2023-12-17 18:57:29

3.time.ctime

time.ctime([sec])可以将时间戳的秒数转换字符串的时间格式:

import timetimestamp = 1639699861
converted_time = time.ctime(timestamp)
print(converted_time)  # 将时间戳转换为可读形式的时间字符串
# Fri Dec 17 08:11:01 2021

4.time.sleep()

import timeprint("Start")
time.sleep(2)  # 暂停程序执行 2 秒钟
print("End")

利用time模块制作简易定时启动项目

# 时间戳
now_w = time.localtime().tm_wday + 1  # 周
now_h = time.localtime().tm_hour  # 时
now_m = time.localtime().tm_min  # 分# 一个简易可视化倒计时模块
def countdown_boss(s):for i in range(s, 0, -1):s = '\r距离开始boss还有:%d秒' % iprint(s.ljust(3), end='', flush=True)time.sleep(1)print("开始boss活动")# 计算距离t时间还有多少秒
def wait_boss(t):now = datetime.now()print(now)mark = now.replace(hour=t, minute=5, second=0)spt = mark - nowreturn int(spt.total_seconds())# 判断今天是周几并且等待一定时间后自动启动任务
def daily_boss():# 门派bossif now_w in [2, 4, 6]:now_h = time.localtime().tm_hour  # 时if now_h >= 12 and now_m >= 5:spt = wait_boss(20)# time.sleep(spt)countdown_boss(spt)

相关文章:

  • 在Flutter中使用PhotoViewGallery指南
  • Docker与K8s的区别
  • 数字化转型导师坚鹏:中国工商银行人工智能与金融数字化转型培训
  • vscode 文件目录栏缩进
  • 基于Springboot的高校教学评价系统的设计与实现(源码+调试)
  • FreeRDP WebConnect Url 任意文件读取漏洞复现
  • acwing算法提高之动态规划--状态压缩DP
  • 网络安全—学习溯源和日志分析
  • Axure元件的介绍使用以及登录界面
  • HarmonyOS给应用添加消息通知
  • Logcat日志记录的使用
  • HPM6750系列--第十篇 时钟系统
  • 小程序静默登录-登录拦截实现方案【全局loginPromis加页面拦截】
  • 【shell脚本实战案例】使用sed提取文件中某一行或几行数据的方法
  • Python基础入门:语法与数据类型
  • Android 架构优化~MVP 架构改造
  • Apache Spark Streaming 使用实例
  • C++回声服务器_9-epoll边缘触发模式版本服务器
  • co.js - 让异步代码同步化
  • emacs初体验
  • iBatis和MyBatis在使用ResultMap对应关系时的区别
  • laravel 用artisan创建自己的模板
  • leetcode98. Validate Binary Search Tree
  • Promise面试题,控制异步流程
  • Service Worker
  • vue2.0项目引入element-ui
  • -- 查询加强-- 使用如何where子句进行筛选,% _ like的使用
  • 大整数乘法-表格法
  • 构建工具 - 收藏集 - 掘金
  • 项目实战-Api的解决方案
  • 一起来学SpringBoot | 第三篇:SpringBoot日志配置
  • 栈实现走出迷宫(C++)
  • ​LeetCode解法汇总2304. 网格中的最小路径代价
  • ​linux启动进程的方式
  • ​ssh-keyscan命令--Linux命令应用大词典729个命令解读
  • ​你们这样子,耽误我的工作进度怎么办?
  • ​一些不规范的GTID使用场景
  • $(function(){})与(function($){....})(jQuery)的区别
  • (6)【Python/机器学习/深度学习】Machine-Learning模型与算法应用—使用Adaboost建模及工作环境下的数据分析整理
  • (iPhone/iPad开发)在UIWebView中自定义菜单栏
  • (原创)攻击方式学习之(4) - 拒绝服务(DOS/DDOS/DRDOS)
  • (转)大型网站的系统架构
  • ... 是什么 ?... 有什么用处?
  • .FileZilla的使用和主动模式被动模式介绍
  • .NET “底层”异步编程模式——异步编程模型(Asynchronous Programming Model,APM)...
  • .net6+aspose.words导出word并转pdf
  • .NET分布式缓存Memcached从入门到实战
  • .NET开发不可不知、不可不用的辅助类(一)
  • .NET企业级应用架构设计系列之开场白
  • ?.的用法
  • @selector(..)警告提示
  • [100天算法】-每个元音包含偶数次的最长子字符串(day 53)
  • [20190401]关于semtimedop函数调用.txt
  • [Android] 240204批量生成联系人,短信,通话记录的APK
  • [Android]使用Retrofit进行网络请求