-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdatetime
More file actions
executable file
·111 lines (91 loc) · 4.72 KB
/
Copy pathdatetime
File metadata and controls
executable file
·111 lines (91 loc) · 4.72 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
#!/usr/bin/env python
# Re: datetime.UTC vs datetime.timezone.utc
# They're the same object. datetime.UTC is just an alias added in 3.11 for convenience:
# If you want to run in 3.10-, just use datetime.timezone.utc
import sys
from datetime import datetime, timedelta, timezone, tzinfo
import pytz
now = datetime.now() # A naive timestamp
now = datetime.now(timezone.utc) # Timezone aware
m = timezone(timedelta(hours=-8), name='PST')
now = datetime.now(m)
then = now - timedelta(days=5)
# print(then < now)
# print(then <= now)
# print(then > now)
print(f'now = {now}')
print(f'now = {now.strftime("%c.%f")}') # %f gives microseconds
print(f'then = {then}')
three_minutes = timedelta(minutes=3)
print(f'There are {three_minutes.total_seconds()} seconds in {three_minutes}')
a = datetime.fromisoformat('2021-07-02-21:35:05+01:00')
a += timedelta(hours=2)
a += timedelta(days=1)
print(a)
print(f'{a} in isoformat: {a.isoformat()}')
print(f'{a} in default format: {a}')
print(f'{a} in epoch time format: {a.strftime("%s")}')
b = datetime.strptime('sat dec 24 01:03:49 Am MDT 2023', '%a %b %d %H:%M:%S %p %Z %Y')
# b is not timezone aware, despite the presence of %Z in the format string.
# Perhaps use dateutil instead
for fmt in ('%s', '%c.%f', '%C', '%G', '%I %p', '%X', '%+'):
print(f"{fmt} format: ", b.strftime(fmt))
try:
d = int(sys.argv[1])
except:
d = 3601
c = datetime.utcfromtimestamp(d) # NOT timezone aware
print(f'{d} in isoformat: {c.isoformat()} *Not* timezone aware') # WTF??!!
print(f'c {"is" if c.tzinfo else "IS NOT"} timezone aware')
c = datetime.fromtimestamp(d, timezone.utc)
print(f'{d} in isoformat: {c.isoformat()} timezone aware')
print(f'c {"is" if c.tzinfo else "IS NOT"} timezone aware')
print(f'raw: {c}')
c = datetime.fromtimestamp(d, pytz.timezone('US/pacific'))
print(f'{d} in isoformat: {c.isoformat()} US/pacific')
print(f'raw: {c}')
# print epoch time
print(f"epoch time of {b}", b.strftime('%s'), " == ", b.timestamp())
'''
# Common format modifiers
%A full weekday name. (Sunday, Monday, Tuesday, Wednesday, etc.) (not case sensitive)
%a abbreviated weekday name. (Sun, Mon, Tue, Wed, Thu, Fri, Sat) (not case sensitive)
%B full month name. (January, etc.)
%b abbreviated month name. (Jan, Feb, Mar, Apr, May, Jun, Jul, Aug, Sep, Oct, Nov, Dec) (not case sensitive)
%C century(year / 100) as decimal number; single digits are preceded by a zero.
%c time and date (eg Sun Dec 24 11:03:49 2023)
%D equivalent to “%m/%d/%y”.
%d day of month as a decimal number (01-31).
%e day of the month as a decimal number (1-31); single digits are preceded by a blank.
%F equivalent to “%Y-%m-%d”.
%G year as a decimal number with century. This year is the one that contains the greater part of the week (Monday as the first day of the week).
%g the same year as in “%G”, but as a decimal number without century (00-99).
%H hour (24-hour clock) as a decimal number (00-23).
%h the same as %b. (abbreviated month name)
%I the hour (12-hour clock) as a decimal number (01-12); single digits are preceded by a '0'.
%j day of the year as a decimal number (001-366).
%k hour (24-hour clock) as a decimal number (0-23); single digits are preceded by a blank.
%l hour (12-hour clock) as a decimal number (1-12); single digits are preceded by a blank.
%M minute as a decimal number (00-59).
%m month as a decimal number (01-12).
%n newline.
%p am/pm (case insensitive)
%R equivalent to “%H:%M”.
%r equivalent to “%I:%M:%S %p”.
%S second as a decimal number (00-60).
%s number of seconds since the Epoch, UTC
%T equivalent to “%H:%M:%S”.
%t a literal tab.
%U week number of the year (Sunday as the first day of the week) as a decimal number (00-53).
%u weekday (Monday as the first day of the week) as a decimal number (1-7).
%V week number of the year (Monday as the first day of the week) as a decimal number (01-53). If the week containing January 1 has four or more days in the new year, then it is week 1; otherwise it is the last week of the previous year, and the next week is week 1.
%v equivalent to “%e-%b-%Y”.
%W the week number of the year (Monday as the first day of the week) as a decimal number (00-53).
%w the weekday (Sunday as the first day of the week) as a decimal number (0-6).
%X national representation of the time.
%x replaced by national representation of the date.
%Y year with century as a decimal number.
%y year without century as a decimal number (00-99).
%Z time zone name.
%z time zone offset from UTC; a leading plus sign stands for east of UTC, a minus sign for west of UTC, hours and minutes follow with two digits each and no delimiter between them (common form for RFC 822 date headers).
'''