In many programming lectures, the datetime(Python Document) library often comes up, and blogs frequently reference it, but they usually focus on simple examples. It made me wonder, “Why is it considered so important?”

The reason is simple yet profound: time is central to everything we do. If the goal is to “build something people need,” understanding and managing time effectively is essential.

For instance, when extracting data today and tomorrow, knowing the current time lets you compare and analyze trends over time.

Take weather forecasts as an example: if today’s forecast differs from tomorrow’s, tomorrow’s data might be more accurate because it’s updated with the latest information.

This demonstrates how critical time-based insights are for making informed decisions.

For me, the datetime module is something I use quite often. While I initially thought of it as “DateType Control,” it’s more about date and time calculations.

It lets you check the current time, determine the day of the week, find today’s date, and perform calculations like “1 day from now” or “20 hours from now.” These features make it incredibly versatile and useful for various scenarios where time-based operations are required.

DateType Control Code

import datetime

now = datetime.datetime.now()

print("Current Time :", now.strftime("%Y-%m-%d %H:%M:%S"))

days = dict({1: 'monday', 2: 'tuesday', 3: 'wednesday', 4: 'thursday', 5: 'friday',
             6: 'saturday', 7: 'sunday'})
print("Todays :", days[now.isoweekday()])

#timedelta : weeks, days, hours, minutes, seconds
tomorrow = now + datetime.timedelta(days=1)

if now < tomorrow:
    print("Tomorrow :", tomorrow.strftime("%Y-%m-%d %H:%M:%S"))

dt_string = tomorrow.strftime("%Y-%m-%d %H:%M:%S")
dt_format = "%Y-%m-%d %H:%M:%S"
dt = datetime.datetime.strptime(dt_string, dt_format)
print('type :', type(dt))
print('Timestamp :', dt.timestamp())
print('Timezone :', dt.astimezone(tz=None))

Output

Current Time : 2024-11-11 09:23:02
Todays : monday
Tomorrow : 2024-11-12 09:23:02
type : <class 'datetime.datetime'>
Timestamp : 1731370982.0
Timezone : 2024-11-12 09:23:02+09:00

Here are a few examples of why I frequently use the datetime module:

  1. Data Analysis: When analyzing data, such as calculating a moving average for a stock or cryptocurrency, I often need data from the last 15 or 30 days. While I can write SQL queries to extract this data, having it stored by date simplifies the query process significantly.
  2. Scheduled Data Extraction: I’ve built a personal system to extract, summarize, and send data on a schedule. When extracting data, I always tag it with a date. This helps when analyzing data that changes over time (like weather forecasts), as it allows me to assign a rough weighting to newer data for more accurate results.
  3. Task Alarms: I plan to use datetime to create a system that generates reminders for tasks I need to complete. When I have more time, I’d like to write about this idea in detail.

Because of these use cases, I ensure that most of my code logs and data outputs include timestamps. It keeps everything organized and traceable, making the code more efficient and reliable.

By Mark

-_-

Leave a Reply

Your email address will not be published. Required fields are marked *