Python/Shell/MySQL时间获取与格式转换
Posted 诸子流
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Python/Shell/MySQL时间获取与格式转换相关的知识,希望对你有一定的参考价值。
一、说明
时间的获取及时间各格式间的转换是比较常用的操作,但一是多种语言经常容易弄混,二是同一种语言同一个功能可能有不同的实现函数,导致每次处理时间经常要百度所以来记录一下。
另外个人真不喜欢同样功能有多种写法的形式,从理想角度说多种实现方式让不同的人都能以其喜欢的方式进行编写;但实际上当你忘记的时候,你就总要怀疑是不是这么写、可不可以这么写、到底怎么写,然后到网上看又是五花八门的写法,这是个很耗费精力的事情。这也是我不喜欢Ruby的原因。
二、Python时间操作
2.1 获取时间对象
import time import datetime # 获取当前时间对象 # 返回形如datetime.datetime(2020, 2, 29, 10, 34, 36, 799216)对象 # 可通过对象的year、month、day、hour、minute、second、microsecond、tzinfo等属性获取各部分的信 datetime.datetime.now() datetime.datetime.today() # 获取昨天时间对象。当前时间减去一天的时间差即可 # 返回形如datetime.datetime(2020, 2, 28, 10, 37, 31, 470867) datetime.datetime.now() - datetime.timedelta(days=1) # 获取明天时间对象。当天的时间加上一天的时间差即可 # 返回形如datetime.datetime(2020, 3, 1, 10, 37, 31, 470867) datetime.datetime.now() - datetime.timedelta(days=1)
2.2 时间对象与给定格式转换
import time
import datetime
# 时间对象输出成指定格式
# 输出形如2020-02-29 11:03:29(-空格和:等符号可以换成其他任意自己想要的字符)
obj = datetime.datetime.now()
obj.strftime("%Y-%m-%d %H:%M:%S")
obj.__format__("%Y-%m-%d %H:%M:%S")
# 给定时间转成时间对象
datetime.datetime.strptime("2020-02-29 11:03:29", "%Y-%m-%d %H:%M:%S")
2.3 时间对象与时间戳转换
import time import datetime # 时间对象转成时间戳 # 结果形如1582943851.470867 obj = datetime.datetime.now() obj.timestamp() # 时间戳转成时间对象 datetime.datetime.fromtimestamp(time.time())
2.4 时间的比较
import time import datetime # 单纯地比较大小可以直接比较。 # 执行太快了两个对象是一样的,所以睡眠一下 obj_a = datetime.datetime.now() time.sleep(1) obj_b = datetime.datetime.now() if obj_b > obj_a: print("yes") # 要获取具体时间差,可将转成时间戳再相减 obj_a = datetime.datetime.now() time.sleep(1) obj_b = datetime.datetime.now() if obj_b > obj_a: late = obj_b.timestamp() - obj_a.timestamp() print(f"obj_b is late than b: {late}")
二、Shell时间操作
# 获取当前时间,并输出成指定格式 # 有引号是因为有空格,没有空格用不用引号都一样 date +"%Y-%m-%d %H:%M:%S" # 获取昨天时间,并输出成指定格式 date -d "yesterday" +"%Y-%m-%d %H:%M:%S" date -d "last-day" +"%Y-%m-%d %H:%M:%S" date -d "1 day ago" +"%Y-%m-%d %H:%M:%S" date -d "-1 days" +"%Y-%m-%d %H:%M:%S" # 获取明天日期,并输出成指定格式 date -d "tomorrow" +"%Y-%m-%d %H:%M:%S" date -d "next-day" +"%Y-%m-%d %H:%M:%S" date -d "+1 days" +"%Y-%m-%d %H:%M:%S"
三、MySQL时间操作
3.1 MySQL获取时间
# 获取当天日期 select curdate(); 获取昨天日期 select date_sub(curdate(),interval 1 day); 获取明天日期 select date_sub(curdate(),interval -1 day); # 获取当前时间 select now(); # 获取一个小前时间 select date_sub(now(),interval 1 hour); # 获取一个小时后时间 select date_sub(now(),interval -1 hour); # 获取昨天时间 select date_sub(now(),interval 1 day); # 获取明天时间 select date_sub(now(),interval -1 day);
3.2 时间输出成指定格式
# 当前时间输出成给定格式 select date_format(now(),"%Y%m%d %H:%i:%S"); # 给定时间输出成指定格式 select date_format(date_sub(curdate(),interval 1 day),"%Y%m%d"); select date_format(date_sub(now(),interval 1 hour),"%Y%m%d %H:%i:%S");
3.3 获取时间戳
select unix_timestamp(now());
四、附Python时间格式
Python、Shell、MySQL之间格式虽然大多是相同的,但小部分还是有区别,自己使用时要注意。比如分钟Python和Linux是"%H",但MySQL是"%i"。
Directive |
Meaning |
Example |
---|---|---|
|
Weekday as locale’s abbreviated name. |
Sun, Mon, …, Sat (en_US);
So, Mo, …, Sa (de_DE)
|
|
Weekday as locale’s full name. |
Sunday, Monday, …, Saturday (en_US);
Sonntag, Montag, …, Samstag (de_DE)
|
|
Weekday as a decimal number, where 0 is Sunday and 6 is Saturday. |
0, 1, …, 6 |
|
Day of the month as a zero-padded decimal number. |
01, 02, …, 31 |
|
Month as locale’s abbreviated name. |
Jan, Feb, …, Dec (en_US);
Jan, Feb, …, Dez (de_DE)
|
|
Month as locale’s full name. |
January, February, …, December (en_US);
Januar, Februar, …, Dezember (de_DE)
|
|
Month as a zero-padded decimal number. |
01, 02, …, 12 |
|
Year without century as a zero-padded decimal number. |
00, 01, …, 99 |
|
Year with century as a decimal number. |
0001, 0002, …, 2013, 2014, …, 9998, 9999 |
|
Hour (24-hour clock) as a zero-padded decimal number. |
00, 01, …, 23 |
|
Hour (12-hour clock) as a zero-padded decimal number. |
01, 02, …, 12 |
|
Locale’s equivalent of either AM or PM. |
AM, PM (en_US);
am, pm (de_DE)
|
|
Minute as a zero-padded decimal number. |
00, 01, …, 59 |
|
Second as a zero-padded decimal number. |
00, 01, …, 59 |
|
Microsecond as a decimal number, zero-padded on the left. |
000000, 000001, …, 999999 |
|
UTC offset in the form |
(empty), +0000, -0400, +1030, +063415, -030712.345216 |
|
Time zone name (empty string if the object is naive). |
(empty), UTC, EST, CST |
|
Day of the year as a zero-padded decimal number. |
001, 002, …, 366 |
|
Week number of the year (Sunday as the first day of the week) as a zero padded decimal number. All days in a new year preceding the first Sunday are considered to be in week 0. |
00, 01, …, 53 |
|
Week number of the year (Monday as the first day of the week) as a decimal number. All days in a new year preceding the first Monday are considered to be in week 0. |
00, 01, …, 53 |
|
Locale’s appropriate date and time representation. |
Tue Aug 16 21:30:00 1988 (en_US);
Di 16 Aug 21:30:00 1988 (de_DE)
|
|
Locale’s appropriate date representation. |
08/16/88 (None);
08/16/1988 (en_US);
16.08.1988 (de_DE)
|
|
Locale’s appropriate time representation. |
21:30:00 (en_US);
21:30:00 (de_DE)
|
|
A literal |
%
|
参考:
https://docs.python.org/3/library/datetime.html#examples-of-usage-datetime
以上是关于Python/Shell/MySQL时间获取与格式转换的主要内容,如果未能解决你的问题,请参考以下文章
Delphi获取与设置系统时间格式,即GetLocaleInfo和SetLocaleInfo
PHP入门教程之日期与时间操作技巧总结(格式化,验证,获取,转换,计算等)