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

ValueError: day is out of range for month #57

Closed
drzel opened this issue Mar 4, 2020 · 12 comments
Closed

ValueError: day is out of range for month #57

drzel opened this issue Mar 4, 2020 · 12 comments
Labels
bug Something isn't working
Milestone

Comments

@drzel
Copy link

drzel commented Mar 4, 2020

I get the following output on Python 3.7.1

[2020-03-05 01:16:18,223] fb2cal INFO (main) A total of 543 birthdays were found.
[2020-03-05 01:16:18,223] fb2cal INFO (main) Creating birthday ICS file...
Traceback (most recent call last):
  File "src/fb2cal.py", line 815, in <module>
    main()
  File "src/fb2cal.py", line 127, in main
    c = populate_birthdays_calendar(birthdays)
  File "src/fb2cal.py", line 800, in populate_birthdays_calendar
    e.begin = f'{year}-{month}-{day} 00:00:00'
  File "/home/sheldon/.pyenv/versions/3.7.1/lib/python3.7/site-packages/ics/event.py", line 155, in begin
    value = get_arrow(value)
  File "/home/sheldon/.pyenv/versions/3.7.1/lib/python3.7/site-packages/ics/utils.py", line 196, in get_arrow
    return arrow.get(value)
  File "/home/sheldon/.pyenv/versions/3.7.1/lib/python3.7/site-packages/arrow/api.py", line 21, in get
    return _factory.get(*args, **kwargs)
  File "/home/sheldon/.pyenv/versions/3.7.1/lib/python3.7/site-packages/arrow/factory.py", line 207, in get
    dt = parser.DateTimeParser(locale).parse_iso(arg)
  File "/home/sheldon/.pyenv/versions/3.7.1/lib/python3.7/site-packages/arrow/parser.py", line 131, in parse_iso
    return self._parse_multiformat(string, formats)
  File "/home/sheldon/.pyenv/versions/3.7.1/lib/python3.7/site-packages/arrow/parser.py", line 312, in _parse_multiformat
    _datetime = self.parse(string, fmt)
  File "/home/sheldon/.pyenv/versions/3.7.1/lib/python3.7/site-packages/arrow/parser.py", line 205, in parse
    return self._build_datetime(parts)
  File "/home/sheldon/.pyenv/versions/3.7.1/lib/python3.7/site-packages/arrow/parser.py", line 301, in _build_datetime
    tzinfo=parts.get("tzinfo"),
ValueError: day is out of range for month
@mobeigi mobeigi added the question Further information is requested label Mar 5, 2020
@mobeigi
Copy link
Owner

mobeigi commented Mar 5, 2020

Wonder if its a leap year issue with a birthdate on 29 Feb. What language is your Facebook in?

@drzel
Copy link
Author

drzel commented Mar 5, 2020

English

@benceszabo
Copy link

Hi, thank you for the script! I had the same error and it seems it has to do with the leap year, the script ran fine when I changed all parts with datetime.now() in cur_date and start_date to datetime(2020, 1, 1, 9, 40, 6, 659499).

@mobeigi
Copy link
Owner

mobeigi commented Mar 5, 2020

Hmm so from the looks of it there is a friend with a birthday on 29 Feb but it tries to add in a date into the calendar with the year of (cur_date + relativedelta(years=1)).year which is not a leap year. So this is a bug that would have also started occurring since we past the 29th.

Wouldn't have been fixed in: 7da11cf

@mobeigi mobeigi added the bug Something isn't working label Mar 5, 2020
@mindyourlifeguide
Copy link

I have a similar behavior.
Although everything worked fine in January.

[2020-03-20 21:56:14,847] fb2cal INFO (main) Creating birthday ICS file...
Traceback (most recent call last):
  File "./fb2cal.py", line 815, in <module>
    main()
  File "./fb2cal.py", line 127, in main
    c = populate_birthdays_calendar(birthdays)
  File "./fb2cal.py", line 800, in populate_birthdays_calendar
    e.begin = f'{year}-{month}-{day} 00:00:00'
  File "/usr/lib/python3.8/site-packages/ics/event.py", line 155, in begin
    value = get_arrow(value)
  File "/usr/lib/python3.8/site-packages/ics/utils.py", line 196, in get_arrow
    return arrow.get(value)
  File "/usr/lib/python3.8/site-packages/arrow/api.py", line 21, in get
    return _factory.get(*args, **kwargs)
  File "/usr/lib/python3.8/site-packages/arrow/factory.py", line 207, in get
    dt = parser.DateTimeParser(locale).parse_iso(arg)
  File "/usr/lib/python3.8/site-packages/arrow/parser.py", line 131, in parse_iso
    return self._parse_multiformat(string, formats)
  File "/usr/lib/python3.8/site-packages/arrow/parser.py", line 312, in _parse_multiformat
    _datetime = self.parse(string, fmt)
  File "/usr/lib/python3.8/site-packages/arrow/parser.py", line 205, in parse
    return self._build_datetime(parts)
  File "/usr/lib/python3.8/site-packages/arrow/parser.py", line 293, in _build_datetime
    datetime(
ValueError: day is out of range for month

@kasnder
Copy link

kasnder commented Mar 29, 2020

Can confirm I had this error with a friend whose birthday is on 29 Feb.

@POIZ87
Copy link

POIZ87 commented Apr 11, 2020

when i delete my friend who has birthday on feb 29. this would be a workaround?

Haha yes deleted the friend and than it worked fine :)

@johnameen
Copy link

So I just looked into this. The issue isn't the fact that there's a person with a leap year in the data, but rather that when there does exist a person with a leap year, the scraper scrapes the dates out of order towards the end of February. The person with the leap year birthday is inserted into the birthdays list in between all the 4/28 birthdays. Confirmed this by first trying a birthdays list with no leap year birthdays, and trying a list without any 4/28 birthdays but with a leap year birthday.

Seems like a pretty easy fix since it doesn't really involve dates, just how the birthdays are being inserted into the birthdays list. A hotfix could be as simple as a sort in the populate_birthdays_calendar function.

Also this could really benefit from an optional caching function. It was painful seeing all the work go away when I hit the error right during export.

Would love to help. Just lmk.

@ghost
Copy link

ghost commented May 9, 2020

Hi, I got this as well today. The console (only) its showing the following at the tail:-

[2020-05-09 13:02:42,399] fb2cal INFO (main) A total of 99 birthdays were found. [2020-05-09 13:02:42,399] fb2cal INFO (main) Creating birthday ICS file... Traceback (most recent call last): File "src/fb2cal.py", line 815, in main() File "src/fb2cal.py", line 127, in main c = populate_birthdays_calendar(birthdays) File "src/fb2cal.py", line 800, in populate_birthdays_calendar e.begin = f'{year}-{month}-{day} 00:00:00' File "/Users/davidwishart/.pyenv/versions/3.7.7/lib/python3.7/site-packages/ics/event.py", line 155, in begin value = get_arrow(value) File "/Users/davidwishart/.pyenv/versions/3.7.7/lib/python3.7/site-packages/ics/utils.py", line 196, in get_arrow return arrow.get(value) File "/Users/davidwishart/.pyenv/versions/3.7.7/lib/python3.7/site-packages/arrow/api.py", line 21, in get return _factory.get(*args, **kwargs) File "/Users/davidwishart/.pyenv/versions/3.7.7/lib/python3.7/site-packages/arrow/factory.py", line 207, in get dt = parser.DateTimeParser(locale).parse_iso(arg) File "/Users/davidwishart/.pyenv/versions/3.7.7/lib/python3.7/site-packages/arrow/parser.py", line 131, in parse_iso return self._parse_multiformat(string, formats) File "/Users/davidwishart/.pyenv/versions/3.7.7/lib/python3.7/site-packages/arrow/parser.py", line 312, in _parse_multiformat _datetime = self.parse(string, fmt) File "/Users/davidwishart/.pyenv/versions/3.7.7/lib/python3.7/site-packages/arrow/parser.py", line 205, in parse return self._build_datetime(parts) File "/Users/davidwishart/.pyenv/versions/3.7.7/lib/python3.7/site-packages/arrow/parser.py", line 301, in _build_datetime tzinfo=parts.get("tzinfo"), ValueError: day is out of range for month

Interestingly (or not) the debug level logging to logs\fb2cal.log has the last line of

[2020-05-09 13:02:42,399] fb2cal INFO (main) Creating birthday ICS file...

It looks like this has already been identified as a bug so I was wondering what if anything I could do so someone could look at resolving?

@johnameen
Copy link

I forked your repo @mobeigi and did a pretty ugly/unoptimized fix and also added an caching function for faster testing of bugs closer towards the end (just saving the birthday list to a pickle file) on the forked repo so you can decide if you want to implement the quick-fix. Thank you for this repo. It's really helped.

@mobeigi mobeigi removed the question Further information is requested label May 17, 2020
@mobeigi
Copy link
Owner

mobeigi commented May 17, 2020

Fixed in #64 . Thanks!

@mobeigi mobeigi closed this as completed May 17, 2020
@mobeigi mobeigi added this to the 1.1.0 milestone Jul 7, 2020
@esambe
Copy link

esambe commented May 30, 2024

Another good way is to write a python script in Django which runs automatically to delete invalid date entries. For example

myApp/management/commands/fix_invalid_dates.py

class Command(BaseCommand):
    help = 'Fix invalid dates in the database'

    def handle(self, *args, **kwargs):
        with connection.cursor() as cursor:
            # Delete existing records with invalid dates
            cursor.execute("""
                DELETE FROM table_name 
                    WHERE date_field LIKE '%-2-30'
                    OR date_field LIKE '%-2-31'
                    OR date_field LIKE '%-4-31'
                    OR date_field LIKE '%-6-31'
                    OR date_field LIKE '%-9-31';
            """)
        self.stdout.write(self.style.SUCCESS('Successfully fixed invalid dates and updated the database'))

In myApp/apps.py

class myAppConfig(AppConfig):
    name = 'myapp'

    def ready(self):
        try:
            call_command('fix_invalid_dates')
        except Exception as e:
            import logging
            logger = logging.getLogger(__name__)
            logger.error(f"Failed to fix invalid dates: {e}")

This command python manage.py fix_invalid_dates will run automatically or can choose to run it manually.
Hope this helps :-)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

8 participants