import time
import datetime
import locale
import random class TimeUtil: def __init__(self, curtime=None):
self.curtime = curtime def get_timestemp(self):
return time.time() def get_date(self):
return time.strftime("%Y-%m-%d") def get_time(self):
return time.strftime("%H:%M:%S") def get_datetime(self):
return time.strftime("%Y-%m-%d %H:%M:%S") def get_chinesedate(self):
locale.setlocale(locale.LC_ALL, 'en')
locale.setlocale(locale.LC_CTYPE, 'chinese')
strTime = time.strftime("%Y年%m月%d日", time.localtime())
return strTime def get_chinesetime(self):
locale.setlocale(locale.LC_ALL, 'en')
locale.setlocale(locale.LC_CTYPE, 'chinese')
strTime = time.strftime("%H时%M分%S秒", time.localtime())
return strTime def get_chinesedatetime(self):
locale.setlocale(locale.LC_ALL, 'en')
locale.setlocale(locale.LC_CTYPE, 'chinese') strTime = time.strftime("%Y年%m月%d日%H时%M分%S秒", time.localtime())
return strTime def compute_date(self, day_interval):
# 获取今天的日期
today = datetime.date.today()
# 在今天的日期上再减10天
if isinstance(day_interval, int) and day_interval >= 0:
return today + datetime.timedelta(days=day_interval)
elif isinstance(day_interval, int) and day_interval < 0:
return today - datetime.timedelta(days=abs(day_interval)) def timestamp_to_date(self, timestamp):
if not isinstance(timestamp, (int, float)):
return None
locale.setlocale(locale.LC_CTYPE, 'chinese')
time_tuple = time.localtime(timestamp) return str(time_tuple[0]) + "年" + str(time_tuple[1]) + "月" + str(time_tuple[2]) + "日" def timestamp_to_time(self, timestamp):
if not isinstance(timestamp, (int, float)):
return None
locale.setlocale(locale.LC_CTYPE, 'chinese')
time_tuple = time.localtime(timestamp)
return str(time_tuple[3]) + "时" + str(time_tuple[4]) + "分" + str(time_tuple[5]) + "秒" def timestamp_to_datetime(self, timestamp):
return self.timestamp_to_date(timestamp) + self.timestamp_to_time(timestamp) if __name__ == "__main__":
t = TimeUtil()
print(t.get_timestemp())
print(t.get_date())
print(t.get_time())
print(t.get_datetime())
print(t.get_chinesedate())
print(t.get_chinesetime())
print(t.get_chinesedatetime())
print(t.compute_date(10))
print(t.compute_date(-10))
print(t.timestamp_to_date(1333333333))
print(t.timestamp_to_time(1333333333))
print(t.timestamp_to_datetime(1333333333))

  打印效果

最新文章

  1. linux 查看php-fpm 进程数
  2. 关于Dagger 2 的使用方式
  3. mysql之对索引的操作
  4. js笔记--json
  5. nginx缓存优先级(缓存问题者必看)
  6. 分布式文件系统FastDFS安装与配置(单机)
  7. Linux环境下使用JFS文件系统
  8. JavaMail邮件发送不成功的那些坑人情况及分析说明
  9. 前端工程之node基础
  10. Ansible自动化运维笔记3(playbook)
  11. 易语言Dns缓存
  12. 单片机的基本构成、工作原理 LET′S TRY“嵌入式编程”: 1 of 6
  13. WCF各个Service之间共享数据
  14. BZOJ.4515.[SDOI2016]游戏(树链剖分 李超线段树)
  15. 查看进程:ps
  16. UVA-11584 Partitioning by Palindromes (简单线性DP)
  17. ansible的安装过程 和基本使用
  18. Python Env
  19. HPUX系统启动后主机名为unknown的解决办法
  20. 6/11 sprint2 看板和燃尽图的更新

热门文章

  1. asp.net中的参数传递:Context.Handler 的用法
  2. js--同步运动json下
  3. 项目Beta冲刺(团队)——05.27(5/7)
  4. scala简单的功能实现~weektwo
  5. appium报错:An unknown server-side error occurred while processing the command. Original error: Could not proxy command to remote server. Original error: Error: read ECONNRESET
  6. hive创建表
  7. ImageMagick 的安装及使用
  8. I2c理解
  9. 比较两个CSV的方法的探索
  10. expr算术运算