一、說明
時間的獲取及時間各格式間的轉換是比較常用的操作,但一是多種語言經常容易弄混,二是同一種語言同一個功能可能有不同的實現函數,導致每次處理時間經常要百度所以來記錄一下。
另外個人真不喜歡同樣功能有多種寫法的形式,從理想角度說多種實現方式讓不同的人都能以其喜歡的方式進行編寫;但實際上當你忘記的時候,你就總要懷疑是不是這么寫、可不可以這么寫、到底怎么寫,然后到網上看又是五花八門的寫法,這是個很耗費精力的事情。這也是我不喜歡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() # 獲取昨天時間對象。當前時間減去一天的時間差即可 # 接受參數weeks,days,hours,seconds,minutes,microseconds,milliseconds,且可同時使用 # 返回形如datetime.datetime(2020, 2, 28, 10, 37, 31, 470867) datetime.datetime.now() - datetime.timedelta(days=1) # 獲取明天時間對象。當天的時間加上一天的時間差即可 # 接受參數weeks,days,hours,seconds,minutes,microseconds,milliseconds,且可同時使用 # 返回形如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時間操作
2.1 基礎操作
# 獲取當前時間,並輸出成指定格式 # 有引號是因為有空格,沒有空格用不用引號都一樣 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" # 將給定日期輸出成指定格式 date -d "2020-07-22 09:09:09" +"%H:%M:%S %Y-%m-%d"
2.2 date月份設置成中/英文問題【20210813更新】
%b表示以系統所設置語言的月份簡寫,如"Aug";%B當地語言全稱,如"August"。
但這個“系統設置語言”到底是由哪個配置設置呢?之前以為是LANG,LANG設置成"zh_CN.UTF-8"就是中文,date +"%b"就會輸出“8月”;設置成"en_US.UTF-8"就是英文,date +"%b"就會輸出“Aug”。
之前也收到反饋說LANG設置成了"en_US.UTF-8",date +"%b"仍輸出中文“8月”,並不太上心,但今天自己也遇到了這個情況。
通過查找資料和實驗,發現環境變量的優先級是LC_ALL > LC_*(包括決定月分顯示語言的LC_TIME)> LANG。所以當LC_ALL設置成"zh_CN.UTF-8"時去設置LANG為"en_US.UTF-8",date +"%b"仍輸出中文。
# 月份以英文形式輸出。%b縮寫,%B全稱 LC_ALL=C date -d "2020-07-22 09:09:09" +"%b" # 查看各影響編碼的環境變量的當前值 locale # 查看系統支持的全部編碼 locale -a
三、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和Shell是"%H",但MySQL是"%i"。
| Directive |
Meaning |
Example |
| %a |
Weekday as locale’s abbreviated name. |
Sun, Mon, …, Sat (en_US); |
| So, Mo, …, Sa (de_DE) |
||
| %A |
Weekday as locale’s full name. |
Sunday, Monday, …, Saturday (en_US); |
| Sonntag, Montag, …, Samstag (de_DE) |
||
| %w |
Weekday as a decimal number, where 0 is Sunday and 6 is Saturday. |
0, 1, …, 6 |
| %d |
Day of the month as a zero-padded decimal number. |
01, 02, …, 31 |
| %b |
Month as locale’s abbreviated name. |
Jan, Feb, …, Dec (en_US); |
| Jan, Feb, …, Dez (de_DE) |
||
| %B |
Month as locale’s full name. |
January, February, …, December (en_US); |
| Januar, Februar, …, Dezember (de_DE) |
||
| %m |
Month as a zero-padded decimal number. |
01, 02, …, 12 |
| %y |
Year without century as a zero-padded decimal number. |
00, 01, …, 99 |
| %Y |
Year with century as a decimal number. |
0001, 0002, …, 2013, 2014, …, 9998, 9999 |
| %H |
Hour (24-hour clock) as a zero-padded decimal number. |
00, 01, …, 23 |
| %I |
Hour (12-hour clock) as a zero-padded decimal number. |
01, 02, …, 12 |
| %p |
Locale’s equivalent of either AM or PM. |
AM, PM (en_US); |
| am, pm (de_DE) |
||
| %M |
Minute as a zero-padded decimal number. |
00, 01, …, 59 |
| %S |
Second as a zero-padded decimal number. |
00, 01, …, 59 |
| %f |
Microsecond as a decimal number, zero-padded on the left. |
000000, 000001, …, 999999 |
| %z |
UTC offset in the form ±HHMM[SS[.ffffff]] (empty string if the object is naive). |
(empty), +0000, -0400, +1030, +063415, -030712.345216 |
| %Z |
Time zone name (empty string if the object is naive). |
(empty), UTC, EST, CST |
| %j |
Day of the year as a zero-padded decimal number. |
001, 002, …, 366 |
| %U |
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 |
| %W |
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 |
| %c |
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) |
||
| %x |
Locale’s appropriate date representation. |
08/16/88 (None); |
| 08/16/1988 (en_US); |
||
| 16.08.1988 (de_DE) |
||
| %X |
Locale’s appropriate time representation. |
21:30:00 (en_US); |
| 21:30:00 (de_DE) |
||
| %% |
A literal '%' character. |
% |
參考:
https://docs.python.org/3/library/datetime.html#examples-of-usage-datetime
