Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Bug Report] Replace datetime.replace() Usage with relativedelta to Prevent Invalid Date Errors #34

Open
1 task done
ShreyTiwari opened this issue Oct 13, 2024 · 0 comments

Comments

@ShreyTiwari
Copy link

Hi there! 👋

I noticed that the codebase uses the replace() method to modify parts of datetime objects, which can inadvertently create invalid dates under certain conditions. Could we implement a more robust approach for these date manipulations to ensure accuracy and reliability?

CodeQL Alerts

Here are the specific instances CodeQL flagged:

  1. Location:
    TODAY_2AGO = datetime.now().replace(year=datetime.now().year - 2).strftime('%d/%m/%Y')

Explanation

Issue:

  • Utilizing replace() on datetime objects to alter the year can lead to invalid dates.
  • Examples of issues include setting February 29th in a non-leap year, or setting the day to the 31st in a month with only 30 days.

Illustrative Example:
Consider a date of February 29, 2024 (a leap year). Using replace() to set the year to 2023 (a non-leap year) results in an invalid date:

from datetime import datetime

# Potentially problematic use of replace
leap_date = datetime(2024, 2, 29)
invalid_date = leap_date.replace(year=2023)  # Raises ValueError

In contrast, dateutil.relativedelta handles this correctly:

from datetime import datetime
from dateutil.relativedelta import relativedelta

# Using relativedelta to safely subtract years
leap_date = datetime(2024, 2, 29)
valid_date = leap_date - relativedelta(years=1)  # Adjusts to February 28, 2023

Potential Solution:

  • Use dateutil.relativedelta, which safely adjusts dates by accounting for calendar rules, such as varying month lengths and leap years. This eliminates the need for manual error handling due to date validity.

Example Solution:

from datetime import datetime
from dateutil.relativedelta import relativedelta

def GET_df_stock_historical(stockId, Country="united states"):
    TODAY = datetime.today().strftime('%d/%m/%Y')
    two_years_ago = datetime.now() - relativedelta(years=2)
    TODAY_2AGO = two_years_ago.strftime('%d/%m/%Y')
    ...

Action Required:

  • Replace instances of replace() to modify datetime objects with dateutil.relativedelta to perform date arithmetic safely and avoid potential invalid date errors.

References:

Thank you so much for your time and effort in maintaining this project! 🌟

Best,
Shrey

Checklist

  • I have checked that there is no similar issue in the repo (required)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant