-
Notifications
You must be signed in to change notification settings - Fork 1
/
dates_times.py
executable file
·128 lines (99 loc) · 3.55 KB
/
dates_times.py
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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
# -*- coding: utf-8 -*-
"""
Script with examples of using Python date and time functions
https://stackoverflow.com/questions/7479777/difference-between-python-datetime-vs-time-modules
https://stackoverflow.com/questions/13031559/how-to-change-a-struct-time-object
Comment:
The function strptime() creates a date-time structure from a string.
The function strftime() formats a date-time structure into a string.
"""
## Import package for time commands
# Module time for time functions
import time
# Get current time in seconds since epoch
seconds = time.time()
print("Seconds since epoch =", seconds)
# Get date and local clock time object from seconds since epoch
local_time = time.ctime(seconds)
print("Local date and clock time:", local_time)
# Create date and local clock time struct_time structure from seconds since epoch
date_time = time.localtime(seconds)
# Create Greenwich date-time structure from seconds since epoch
gm_time = time.gmtime(seconds)
print("Greenwich date and time structure:", gm_time)
# Get time in seconds from date-time structure
seconds = time.mktime(date_time)
print("Seconds since epoch:", seconds)
# Get date and local clock time as struct_time structure
date_time = time.localtime()
print("Date and local clock time structure:", date_time)
# Get year
print("Year:", date_time.tm_year)
# Get hour
print("Hour:", date_time.tm_hour)
# Get date and time string from date-time structure
local_time = time.asctime(date_time)
print("Local date and clock time:", local_time)
# Get date and time string from date-time structure
local_time = time.strftime("%d.%m.%Y %H:%M:%S", date_time)
# Get date string from date-time structure
local_date = time.strftime("%Y-%m-%d", date_time)
print("Local date and clock time:", local_time)
# Create date-time structure from string
date_time = time.strptime("23.10.2012", "%d.%m.%Y")
print("Date and time object:", date_time)
## Module datetime for date and time functions
# https://www.w3schools.com/python/python_datetime.asp
"""
Comment:
Importing whole datetime module later requires
using the double datetime syntax:
datetime.datetime.strptime()
"""
# import datetime
from datetime import date, datetime, timedelta
# Get date and local clock time object from seconds since epoch
local_time = datetime.fromtimestamp(seconds)
# Coerce datetime to date
local_time.date()
# Coerce date to string
str(local_time.date())
# Get today's date as datetime structure
todayd = date.today()
print(todayd)
yesterday = (todayd - timedelta(days = 1))
# Format datetime structures into strings
todayd = str(todayd)
print(todayd)
yesterday = str(yesterday)
# Remove hyphens
m_date = todayd.replace("-", "")
# Create a datetime structure from integers
date_time = datetime(2020, 5, 17)
print(date_time)
# Get year
print(date_time.year)
# Create datetime structure from string
date_time = datetime.strptime("23.10.2012", "%d.%m.%Y")
print("Date and time object:", date_time)
# Format datetime structure into string
date_time.strftime("%Y-%m-%d")
# Get day of week
date_time.strftime("%A")
# Get name of month
date_time.strftime("%B")
# Get current date and time as datetime structure
date_time = datetime.now()
# Print with seconds
date_time.strftime("%d.%m.%Y %H:%M:%S")
# Print with microseconds
print(date_time)
# Get date from datetime structure
dat_e = date_time.date()
# Add 30 days to date
dat_e + timedelta(days=30)
# Add 1 month to date 2020-01-31 (use proper calendar)
date_time = datetime.strptime("2020-01-31", "%Y-%m-%d")
dat_e = date_time.date()
from dateutil.relativedelta import relativedelta
dat_e + relativedelta(months=1)