时间管理的三件工具
无论是手机的android系统还是Linux系统,都提供两项基本的时间管理工具日历(calendar) and 时钟(clock).Calendar是date地图,提供全景式的鸟瞰图; clock是指南针,指导具体的每一步应该迈向何处。二者的结合形成第三个工具datetime。
工具虽然简单,但如果不能抽象到认知的层面,则不能为己所用。试问,谁手机里没个日历,没个钟表呢。认知层面的结论是需要且仅需要这三件工具O(∩_∩)O。
1.Calendar日历提供全景式鸟瞰
ncal -B 3 #ncal = new calendar
2.Clock时钟实施具体的测量
$ time sleep 10real 0m10.011suser 0m0.002ssys 0m0.001s
3.datetime整合二者,提供更加实用的功能
$ TZ="America/New_York" date #美东夏季时间, 第十个变量时区。Wed 10 Jun 2020 09:46:35 PM EDT #Eatern Daylight Timer
Python中的时间管理
1.Calendar日历
Python中的calendar是个无关紧要的工具,没有人会费力不讨好的到这里查看日期和规划日程。
2. Time钟表
2.1.与epoch time的相互转换
人类以minutes计时, 机器以second计时。
In [4]: import timeIn [5]: time.localtime() #local time Out[5]: time.struct_time(tm_year=2020, tm_mon=6, tm_mday=11, tm_hour=9, tm_min=55, tm_sec=7, tm_wday=3, tm_yday=163, tm_isdst=0)In [6]: time.gmtime(100) #UTC time Out[6]: time.struct_time(tm_year=1970, tm_mon=1, tm_mday=1, tm_hour=0, tm_min=1, tm_sec=40, tm_wday=3, tm_yday=1, tm_isdst=0)In [7]: time.gmtime() #UTC time Out[7]: time.struct_time(tm_year=2020, tm_mon=6, tm_mday=11, tm_hour=1, tm_min=55, tm_sec=41, tm_wday=3, tm_yday=163, tm_isdst=0)In [8]: calendar.timegm(time.gmtime()) #UTC time Out[8]: 1591840586In [9]: time.mktime(time.localtime()) #local time Out[9]: 1591840609.0
2.2 变量 tm_isdst daylight saving time夏季时间
In [10]: time.gmtime? Docstring:gmtime([seconds]) -> (tm_year, tm_mon, tm_mday, tm_hour, tm_min, tm_sec, tm_wday, tm_yday, tm_isdst)Convert seconds since the Epoch to a time tuple expressing UTC (a.k.a.GMT). When 'seconds' is not passed in, convert the current time instead.If the platform supports the tm_gmtoff and tm_zone, they are available asattributes only.Type: builtin_function_or_method
2.3.处理器的计时
time.process_time() #cpu time of kernel and user-spacetime.process_time_ns() ->int #not inclue sleep time time.perf_counter() ->floattime.perf_counter_ns() -> inttime.thread_time() ->floattime.thread_time_ns() ->inttime.time()time.time_ns()time.sleeptime.monotonic()time.monotonic_ns()
2.4.时区常量
In [9]: time.daylightOut[9]: 0In [10]: time.tznameOut[10]: ('CST', 'CST')In [11]: time.altzoneOut[11]: -28800
2.5.两个最重要的方法
time.strptime(string, format)time.strftime(format)
2.6.ctime
In [11]: time.ctime() Out[11]: 'Thu Jun 11 10:01:51 2020'In [12]: time.asctime(time.localtime())Out[12]: 'Thu Jun 11 10:02:04 2020'
3. Datetime日历与钟表相结合
复习calendar(date), clock(time)的逻辑,先看两个没用的函数。
3.1 datetime.date()
class date(builtins.object) 就是符号%x返回的内容 | date(year, month, day) --> date objectIn [14]: import datetime In [15]: datetime.date.today().year Out[15]: 2020In [16]: datetime.date.today().month Out[16]: 6In [17]: datetime.date.today().day
3.2 datetime.time()
class time(builtins.object) 符号%X返回的内容 | time([hour[, minute[, second[, microsecond[, tzinfo]]]]]) --> a time object #5个参数,由大到小排列In [104]: t = datetime.time(6, 15, 30, 999999, dateutil.tz.tzutc()) In [105]: t.strftime("%f:%S:%M:%H %Z") Out[105]: '999999:30:15:06 UTC'In [106]: t.min Out[106]: datetime.time(0, 0)In [107]: t.max Out[107]: datetime.time(23, 59, 59, 999999)In 108]: t.resolution Out[108]: datetime.timedelta(microseconds=1) #精确度
前面两个函数datetime.date与datetime.time并不经常使用。
3.3 datetime.datetime()
经常使用的是函数datetime.datetime以及datetime.timedelta。
lass datetime(date) | datetime(year, month, day[, hour[, minute[, second[, microsecond[,tzinfo]]]]])Help on built-in function weekday:weekday(...) method of datetime.datetime instance Return the day of the week represented by the date. Monday == 0 ... Sunday == 6 In [1]: import datetime In [2]: datetime.datetime.utcnow().timetuple() Out[2]: time.struct_time(tm_year=2020, tm_mon=6, tm_mday=11, tm_hour=2, tm_min=15, tm_sec=42, tm_wday=3, tm_yday=163, tm_isdst=-1)
3.4.datetime.timedelta
In [3]: datetime.datetime.now() + datetime.timedelta(days=3) Out[3]: datetime.datetime(2020, 6, 14, 10, 18, 26, 689863)In [7]: !date -d '3 days' Sun 14 Jun 2020 10:20:57 AM CST #转换为seconds的另外一种写法In [25]: str(datetime.timedelta(seconds=100))Out[25]: '0:01:40'In [29]: time.strftime("%H:%M:%S", time.gmtime(3000))Out[29]: '00:50:00'
Shell的时间管理工具
1.Calendar日历
$ ncal -1$ ncal -M #Monday as the first day $ ncal -w #weeknumber$ncal -y -m; $ncal -d yyyy-mm $ncal yyyy-mm-dd#highlight the current date $ ncal -A3 -B4#就只有这么多操作
2.Clock(Time)钟表
2.1 hwclock
In [31]: !sudo hwclock2019-05-15 21:25:29.514803+08:00
2.2 time (processing profile)
$ time (tree /usr/share/zoneinfo | grep -i "prc")│ ├── Chongqing -> ../PRC│ ├── Chungking -> ../PRC│ ├── Harbin -> ../PRC│ ├── Shanghai -> ../PRC│ │ ├── Chongqing -> ../../PRC│ │ ├── Chungking -> ../../PRC│ │ ├── Harbin -> ../../PRC│ │ ├── Shanghai -> ../../PRC│ ├── PRC -> ../PRC├── PRC│ │ ├── Chongqing -> ../PRC│ │ ├── Chungking -> ../PRC│ │ ├── Harbin -> ../PRC│ │ ├── Shanghai -> ../PRC│ ├── PRCreal 0m0.017suser 0m0.015ssys 0m0.004s
3.Datetime日历加钟表
3.1 timedatectl
In [8]: !timedatectl Local time: Thu 2020-06-11 10:29:37 CST Universal time: Thu 2020-06-11 02:29:37 UTC RTC time: Thu 2020-06-11 02:29:37 Time zone: Asia/Shanghai (CST, +0800)System clock synchronized: yes NTP service: inactive RTC in local TZ: no - Check the current system clock time: timedatectl - Set the local time of the system clock directly: timedatectl set-time {{"yyyy-MM-dd hh:mm:ss"}} - List available timezones: timedatectl list-timezones - Set the system timezone: timedatectl set-timezone {{timezone}} - Enable Network Time Protocol (NTP) synchronization: timedatectl set-ntp on
3.2 date (最趁手的一个工具)
日常应用date作为思考工具。
In [12]: time.time() Out[12]: 1591842959.6268835In [13]: !date +%s 1591842966In [2]: !date +%s 1591843394In [3]: !date -d @$(date +%s) Thu 11 Jun 2020 10:43:33 AM CST
基本操作:
$ date -u +"%Y-%m-%dT%H:%M:%SZ"2020-06-11T02:44:20Z
未来的时间
$ date -d '2 weeks' # 两周之后Thu 25 Jun 2020 10:45:29 AM CST$ date -d 'next friday' # 下周五Fri 12 Jun 2020 12:00:00 AM CST
过去的时间
$ date -d "last thursday"Thu 04 Jun 2020 12:00:00 AM CST$ date -d "2 days ago"Tue 09 Jun 2020 10:47:06 AM CST$ date -d "last month"Mon 11 May 2020 10:47:13 AM CST
总结
时间管理的三个工具为宏观的日历,微观的钟表,以及二者结合的datetime.