### Install Development Dependencies Source: https://github.com/dateutil/dateutil/blob/master/CONTRIBUTING.md Install the necessary dependencies for running tests and development. This is typically done after installing the library in development mode. ```bash pip install -r requirements-dev.txt ``` -------------------------------- ### TZSTR Format: Basic Example Source: https://github.com/dateutil/dateutil/blob/master/docs/examples.md Shows a basic example of a recognized TZSTR format string. ```python from dateutil.tz import tzstr tzstr("EST5EDT") ``` -------------------------------- ### Install dateutil in Development Mode Source: https://github.com/dateutil/dateutil/blob/master/CONTRIBUTING.md Install the library in development mode after setting up a virtual environment. This allows local changes to be used directly. ```bash pip install -e . ``` -------------------------------- ### TZSTR Format: Equivalent to time module example Source: https://github.com/dateutil/dateutil/blob/master/docs/examples.md Provides a TZSTR example that is equivalent to one shown in the Python time module documentation. ```python tzstr("EST5EDT,M3.2.0/02:00:00,M11.1.0/02:00:00") ``` -------------------------------- ### Example Changelog Entry for Bugfix Source: https://github.com/dateutil/dateutil/blob/master/CONTRIBUTING.md This is an example of how to format a changelog entry for a bug fix, including reporter and submitter information. ```rst Fixed issue where the tz.tzstr constructor would erroneously succeed if passed an invalid value for tzstr. Reported by @pganssle (gh issue #259). Fixed by @pablogsal (gh pr #581) ``` -------------------------------- ### Get Python Version Source: https://github.com/dateutil/dateutil/blob/master/CONTRIBUTING.md Use this command to retrieve the installed version of Python. ```bash python --version ``` -------------------------------- ### Get dateutil Version Source: https://github.com/dateutil/dateutil/blob/master/CONTRIBUTING.md Use this command to retrieve the installed version of the dateutil library. ```python import dateutil; print(dateutil.__version__) ``` -------------------------------- ### Install dateutil Source: https://github.com/dateutil/dateutil/blob/master/README.rst Install the dateutil package from PyPI using pip. Note that the package name is 'python-dateutil'. ```bash pip install python-dateutil ``` -------------------------------- ### Initialize NOW and TODAY variables Source: https://github.com/dateutil/dateutil/blob/master/docs/relativedelta.md Initializes NOW to the current datetime and TODAY to the current date. These are used in subsequent examples. ```python >>> NOW = datetime.now() >>> TODAY = date.today() >>> NOW datetime.datetime(2003, 9, 17, 20, 54, 47, 282310) >>> TODAY datetime.date(2003, 9, 17) ``` -------------------------------- ### Quick Example: Calculating Time Until Next Easter Source: https://github.com/dateutil/dateutil/blob/master/README.rst This example demonstrates calculating the time remaining until the next Easter, specifically in a year that features a Friday the 13th in August. It utilizes various modules from dateutil for parsing, relative deltas, Easter calculations, and recurrence rules. ```python from dateutil.relativedelta import * from dateutil.easter import * from dateutil.rrule import * from dateutil.parser import * from datetime import * now = parse("Sat Oct 11 17:13:46 UTC 2003") today = now.date() year = rrule(YEARLY,dtstart=now,bymonth=8,bymonthday=13,byweekday=FR)[0].year rdelta = relativedelta(easter(year), today) print("Today is: %s" % today) print("Year with next Aug 13th on a Friday is: %s" % year) print("How far is the Easter of that year: %s" % rdelta) print("And the Easter of that year is: %s" % (today+rdelta)) ``` -------------------------------- ### Date Parsing: Basic Example with Timezone Offset Source: https://github.com/dateutil/dateutil/blob/master/docs/examples.md Parses a date string using a provided timezone offset, demonstrating the use of the ZOFFSET dictionary. ```python from dateutil.parser import parse from dateutil.tz import tzlocal, tzoffset ZOFFSET = { 'BRST': tzoffset(None, -10800, 'BRST') } date_str = "Sun, 10 May 2009 14:10:30 -0300" parsed_date = parse(date_str, tzinfos=ZOFFSET) ``` -------------------------------- ### Date Parsing: Stripping Further Source: https://github.com/dateutil/dateutil/blob/master/docs/examples.md Shows an example of further stripping components from a parsed date string. ```python parse("2010-01-01 10:00:00", default=datetime(2012, 1, 1)).strftime('%Y-%m-%d %H:%M:%S') ``` -------------------------------- ### Example Property Test for rrulestr Source: https://github.com/dateutil/dateutil/wiki/HackIllinois-2019 This example demonstrates how to use Hypothesis to test the round-trip conversion of an rrule object to its string representation and back. It's particularly useful for finite rrules. ```python def test_rrulestr_roundtrip(self): for rrule in self.rrule_list: self.assertEqual(rrule, rrulestr(str(rrule))) ``` -------------------------------- ### Date Parsing: Fuzzy Parsing Source: https://github.com/dateutil/dateutil/blob/master/docs/examples.md Shows an example of fuzzy date parsing, where the parser attempts to interpret less precise date strings. ```python parse("Jan 1, 2010", fuzzy=True) ``` -------------------------------- ### Run dateutil Tests with Tox and Pytest Arguments Source: https://github.com/dateutil/dateutil/blob/master/CONTRIBUTING.md Pass arguments to pytest through tox. This example runs tests marked with 'tzstr' on Python 3.6. ```bash tox -e py36 -- -m tzstr ``` -------------------------------- ### Yearday in a non-leap year to ignore leap day effect Source: https://github.com/dateutil/dateutil/blob/master/docs/relativedelta.md Illustrates how to use 'yearday' to get the same day in a non-leap year, effectively ignoring the leap day. ```python >>> date(2004, 1, 1) + relativedelta(yearday=+261) date.date(2004, 9, 17) ``` -------------------------------- ### VTIMEZONE Definition Example Source: https://github.com/dateutil/dateutil/blob/master/docs/examples.md This snippet shows a sample VTIMEZONE definition for the US-Eastern timezone, extracted from an RFC. It includes standard and daylight saving time rules. ```default BEGIN:VTIMEZONE TZID:US-Eastern LAST-MODIFIED:19870101T000000Z TZURL:http://zones.stds_r_us.net/tz/US-Eastern BEGIN:STANDARD DTSTART:19671029T020000 RRULE:FREQ=YEARLY;BYDAY=-1SU;BYMONTH=10 TZOFFSETFROM:-0400 TZOFFSETTO:-0500 TZNAME:EST END:STANDARD BEGIN:DAYLIGHT DTSTART:19870405T020000 RRULE:FREQ=YEARLY;BYDAY=1SU;BYMONTH=4 TZOFFSETFROM:-0500 TZOFFSETTO:-0400 TZNAME:EDT END:DAYLIGHT END:VTIMEZONE ``` -------------------------------- ### Define MLK Day Rule with dateutil.rrule Source: https://github.com/dateutil/dateutil/blob/master/docs/exercises/solutions/mlk-day-rrule.md Defines the rule for Martin Luther King Day, which occurs annually on the third Monday of January, starting from 1986. Requires importing `rrule` from `dateutil` and `datetime` from `datetime`. ```python # ------- YOUR CODE ------------- from dateutil import rrule from datetime import datetime MLK_DAY = rrule.rrule( dtstart=datetime(1986, 1, 20), # First celebration freq=rrule.YEARLY, # Occurs once per year bymonth=1, # In January byweekday=rrule.MO(+3), # On the 3rd Monday ) # -------------------------------# ``` -------------------------------- ### Create and Activate Virtual Environment with virtualenv Source: https://github.com/dateutil/dateutil/blob/master/CONTRIBUTING.md Use this command to create a virtual environment for development. Activate it using the 'source' command. ```bash python -m virtualenv .venv # Create virtual environment in .venv directory source .venv/bin/activate # Activate the virtual environment ``` -------------------------------- ### Date Parsing: Different Date Orderings Source: https://github.com/dateutil/dateutil/blob/master/docs/examples.md Demonstrates parsing date strings with varying month/day orderings. ```python parse("01/01/2010") parse("01-01-2010") ``` -------------------------------- ### Import necessary modules for relativedelta Source: https://github.com/dateutil/dateutil/blob/master/docs/examples.md Import datetime, relativedelta, and calendar modules to begin using dateutil's relative time calculations. ```default >>> from datetime import *; from dateutil.relativedelta import * >>> import calendar ``` -------------------------------- ### Create and Activate Conda Environment Source: https://github.com/dateutil/dateutil/blob/master/CONTRIBUTING.md Create a conda environment for development. You can optionally specify a Python version. Activate it using the 'source activate' command. ```bash conda create -n dateutil # Create a conda environment # conda create -n dateutil python=3.6 # Or specify a version source activate dateutil # Activate the conda environment ``` -------------------------------- ### TZSTR Format: With Daylight Saving Rules Source: https://github.com/dateutil/dateutil/blob/master/docs/examples.md Demonstrates a TZSTR format string that includes specific rules for daylight saving time transitions. ```python tzstr("EST5EDT4,M4.1.0/02:00:00,M10-5-0/02:00") ``` -------------------------------- ### Calculate next month plus one week at 10am Source: https://github.com/dateutil/dateutil/blob/master/docs/relativedelta.md Adds months, weeks, and sets a specific hour. ```python >>> NOW + relativedelta(months=+1, weeks=+1, hour=10) datetime.datetime(2003, 10, 24, 10, 0, 47, 282310) ``` -------------------------------- ### Calculate next month plus one week Source: https://github.com/dateutil/dateutil/blob/master/docs/relativedelta.md Shows how to add both months and weeks to a date. ```python >>> NOW + relativedelta(months=+1, weeks=+1) datetime.datetime(2003, 10, 24, 20, 54, 47, 282310) ``` -------------------------------- ### Find first day of ISO year week Source: https://github.com/dateutil/dateutil/blob/master/docs/relativedelta.md Calculates the first day of a specific ISO week in a given year. ```python >>> date(1997, 1, 1) + relativedelta(weeks=+14, weekday=MO) date.date(1997, 4, 7) ``` -------------------------------- ### Date Parsing: Ambiguous Dates Source: https://github.com/dateutil/dateutil/blob/master/docs/examples.md Shows how the parser handles potentially ambiguous date formats. ```python parse("01/02/03") ``` -------------------------------- ### Date Parsing: Hours with Letters Source: https://github.com/dateutil/dateutil/blob/master/docs/examples.md Demonstrates parsing time strings that include textual representations of hours. ```python parse("2010-01-01 10am") ``` -------------------------------- ### Calculate next month Source: https://github.com/dateutil/dateutil/blob/master/docs/relativedelta.md Demonstrates adding one month to a date. ```python >>> NOW + relativedelta(months=+1) datetime.datetime(2003, 10, 17, 20, 54, 47, 282310) ``` -------------------------------- ### Import necessary modules Source: https://github.com/dateutil/dateutil/blob/master/docs/relativedelta.md Imports the datetime and relativedelta classes, along with the calendar module for potential date-related operations. ```python >>> from datetime import * >>> from dateutil.relativedelta import * >>> import calendar ``` -------------------------------- ### Run dateutil Tests Directly with Pytest Source: https://github.com/dateutil/dateutil/blob/master/CONTRIBUTING.md Run tests directly using pytest in the root directory. This is often faster for initial checks but may be less thorough than tox. ```bash python -m pytest ``` -------------------------------- ### Absolute relativedelta for year and month replacement Source: https://github.com/dateutil/dateutil/blob/master/docs/relativedelta.md Illustrates using singular 'year' and 'month' in relativedelta to replace existing values rather than performing arithmetic. ```python >>> NOW + relativedelta(year=2004, month=5) datetime.datetime(2004, 5, 17, 20, 54, 47, 282310) ``` -------------------------------- ### TZSTR Format: Checking Daylight Limit Source: https://github.com/dateutil/dateutil/blob/master/docs/examples.md Illustrates checking the daylight saving time limit using a TZSTR definition. ```python tz = tzstr("EST5EDT,M3.2.0/02:00:00,M11.1.0/02:00:00") # Example of checking DST limit (specific code depends on desired check) ``` -------------------------------- ### Handling months with different numbers of days Source: https://github.com/dateutil/dateutil/blob/master/docs/relativedelta.md Shows that adding one month does not cross the month boundary and respects the number of days in the target month. ```python >>> date(2003, 1, 31) + relativedelta(months=+1) date.date(2003, 2, 28) ``` -------------------------------- ### RRULE String Parsing: Using Only the RRULE Value Source: https://github.com/dateutil/dateutil/blob/master/docs/examples.md Demonstrates parsing an RRULE string when only the rule value is provided. This returns an rrule instance. ```python rules = rrulestr("DTSTART:20100101T090000Z\nRRULE:FREQ=DAILY;COUNT=10") ``` -------------------------------- ### Obtain today's date using yearday Source: https://github.com/dateutil/dateutil/blob/master/docs/relativedelta.md Demonstrates using the 'yearday' attribute to specify a date within a year. ```python >>> date(2003, 1, 1) + relativedelta(yearday=+260) date.date(2003, 9, 17) ``` -------------------------------- ### RRULE String Parsing: Automatic RRULESET Usage Source: https://github.com/dateutil/dateutil/blob/master/docs/examples.md Illustrates how rrulestr automatically uses RRULESET when multiple rules or complex recurrence patterns are specified. ```python rules = rrulestr("DTSTART:20100101T090000Z\nRRULE:FREQ=DAILY;COUNT=10\nRRULE:FREQ=WEEKLY;COUNT=5") ``` -------------------------------- ### TZSTR Format: Without Touching System Settings Source: https://github.com/dateutil/dateutil/blob/master/docs/examples.md Demonstrates using tzstr to represent timezone information without altering system-wide timezone settings. ```python from dateutil.utils import today dt = today() dt.replace(tzinfo=tzstr("EST5EDT,M3.2.0/02:00:00,M11.1.0/02:00:00")) ``` -------------------------------- ### TZSTR Format: With Numeric Day of Year for DST Source: https://github.com/dateutil/dateutil/blob/master/docs/examples.md Illustrates a TZSTR format using the day of the year for daylight saving time transitions. ```python tzstr("EST5EDT4,95/02:00:00,298/02:00") ``` -------------------------------- ### Date Parsing: Custom ParserInfo Source: https://github.com/dateutil/dateutil/blob/master/docs/examples.md Demonstrates overriding the default parser information with a custom parserinfo object. ```python from dateutil.parser import parserinfo class CustomParserInfo(parserinfo): def __init__(self): parserinfo.__init__(self, dayfirst=True, yearfirst=False) custom_parser = CustomParserInfo() parse("01/02/2010", parserinfo=custom_parser) ``` -------------------------------- ### Daily Recurring Rule with Weekend Exclusion Source: https://github.com/dateutil/dateutil/blob/master/docs/examples.md Demonstrates creating a daily recurrence rule that skips Saturdays and Sundays. ```python from dateutil.rrule import rrule, DAILY, WEEKLY, MO, TU, WE, TH, FR, SA, SU rrule(DAILY, count=7, byweekday=(MO, TU, WE, TH, FR)) ``` -------------------------------- ### Date Parsing: Other Date Separators Source: https://github.com/dateutil/dateutil/blob/master/docs/examples.md Illustrates parsing dates that use separators other than the standard hyphen or slash. ```python parse("2010.01.01") ``` -------------------------------- ### TZSTR Format: With Julian Day for DST Source: https://github.com/dateutil/dateutil/blob/master/docs/examples.md Shows a TZSTR format using Julian day numbers for daylight saving time transitions. ```python tzstr("EST5EDT4,J96/02:00:00,J299/02:00") ``` -------------------------------- ### RelativeDelta with dates Source: https://github.com/dateutil/dateutil/blob/master/docs/relativedelta.md Shows that RelativeDelta can be used with date objects for calculations. ```python >>> relativedelta(date(2003, 10, 17), date(2003, 9, 17)) relativedelta(months=+1) ``` -------------------------------- ### Run dateutil Tests with Tox for Specific Environments Source: https://github.com/dateutil/dateutil/blob/master/CONTRIBUTING.md Use tox to run tests against specific Python versions. This command runs tests on Python 2.7 and Python 3.6. ```bash tox -e py27,py36 ``` -------------------------------- ### Calculate next Wednesday (today) Source: https://github.com/dateutil/dateutil/blob/master/docs/relativedelta.md Finds the date of the next Wednesday, which in this case is the same day. ```python >>> TODAY + relativedelta(weekday=WE) datetime.date(2003, 9, 17) ``` -------------------------------- ### Calculate Next Monday Meeting Time Source: https://github.com/dateutil/dateutil/blob/master/docs/exercises/index.md Calculates the date and time of the next Monday meeting at 10 AM, given a datetime object. Requires the relativedelta module. ```python3 # --------- YOUR CODE -------------- # from dateutil import relativedelta def next_monday(dt): <> # ---------------------------------- # from datetime import datetime from dateutil import tz NEXT_MONDAY_CASES = [ (datetime(2018, 4, 11, 14, 30, 15, 123456), datetime(2018, 4, 16, 10, 0)), (datetime(2018, 4, 16, 10, 0), datetime(2018, 4, 16, 10, 0)), (datetime(2018, 4, 16, 10, 30), datetime(2018, 4, 23, 10, 0)), (datetime(2018, 4, 14, 9, 30, tzinfo=tz.gettz('America/New_York')), datetime(2018, 4, 16, 10, 0, tzinfo=tz.gettz('America/New_York'))), ] def test_next_monday_1(): for dt_in, dt_out in NEXT_MONDAY_CASES: assert next_monday(dt_in) == dt_out if __name__ == "__main__": test_next_monday_1() print('Success!') ``` -------------------------------- ### Date Parsing: String Without Timezone Source: https://github.com/dateutil/dateutil/blob/master/docs/examples.md Demonstrates parsing a date string that does not contain any timezone information. ```python parse("2010-01-01 10:00:00") ``` -------------------------------- ### Weekly Recurring Rule with Specific Day and Exclusion Source: https://github.com/dateutil/dateutil/blob/master/docs/examples.md Shows how to define a weekly recurrence rule for 4 weeks, including a specific day and excluding another. ```python rrule(WEEKLY, count=4, byweekday=SU, byyearday=7, exclude=(16,)) ``` -------------------------------- ### Date Parsing: ISO Format Source: https://github.com/dateutil/dateutil/blob/master/docs/examples.md Parses a date string in the standard ISO 8601 format. ```python parse("2010-01-01T10:00:00") ``` -------------------------------- ### Date Parsing: With Spaces Source: https://github.com/dateutil/dateutil/blob/master/docs/examples.md Parses date strings where spaces are used as separators. ```python parse("2010 01 01") ``` -------------------------------- ### Parse Timezones (India, Ireland) with Ambiguity Handling Source: https://github.com/dateutil/dateutil/blob/master/docs/exercises/index.md Parses datetime strings from India or Ireland, correctly localizing unambiguous strings and defaulting ambiguous strings to India. Uses dateutil.parser and dateutil.tz. ```python3 # --------- YOUR CODE -------------- # from dateutil.parser import parse from dateutil import tz def parse_func_ind_ire(): <> # ---------------------------------- # ISRAEL = tz.gettz('Asia/Jerusalem') INDIA = tz.gettz('Asia/Kolkata') PARSE_IXT_TEST_CASE = [ ('2018-02-03 12:00 IST+02:00', datetime(2018, 2, 3, 12, tzinfo=ISRAEL)), ('2018-06-14 12:00 IDT+03:00', datetime(2018, 6, 14, 12, tzinfo=ISRAEL)), ('2018-06-14 12:00 IST', datetime(2018, 6, 14, 12, tzinfo=INDIA)), ('2018-06-14 12:00 IST+05:30', datetime(2018, 6, 14, 12, tzinfo=INDIA)), ('2018-02-03 12:00 IST', datetime(2018, 2, 3, 12, tzinfo=INDIA)), ] def test_parse_ixt(): for dtstr, dt_exp in PARSE_IXT_TEST_CASE: dt_act = parse_func_ind_ire(dtstr) assert dt_act == dt_exp, (dt_act, dt_exp) assert dt_act.tzinfo is dt_exp.tzinfo, (dt_act, dt_exp) if __name__ == "__main__": test_parse_ixt() print('Success!') ``` -------------------------------- ### TZSTR Format: Default DST Conventions Source: https://github.com/dateutil/dateutil/blob/master/docs/examples.md Explains the default conventions used by tzstr for daylight saving time when not explicitly specified (e.g., first Sunday of April, last Sunday of October, 2 AM). ```python # tzstr("EST5EDT") implicitly uses default DST rules if not specified. ``` -------------------------------- ### Handling end-of-month adjustments Source: https://github.com/dateutil/dateutil/blob/master/docs/relativedelta.md Illustrates that if a calculated day falls after the last day of a month, the last day of that month is used. ```python >>> date(2003, 1, 31) + relativedelta(months=+2) date.date(2003, 3, 31) ``` -------------------------------- ### Date Parsing: With AM/PM Source: https://github.com/dateutil/dateutil/blob/master/docs/examples.md Parses time strings that use AM/PM notation. ```python parse("2010-01-01 10:00:00 PM") ``` -------------------------------- ### Relative hour and ignored weeks in relativedelta Source: https://github.com/dateutil/dateutil/blob/master/docs/relativedelta.md Demonstrates that 'hour' is relative when used as a difference, and 'weeks' can be ignored if not applicable. ```python >>> NOW + relativedelta(hours=-1, weeks=1) datetime.datetime(2003, 9, 17, 19, 54, 47, 282310) ``` -------------------------------- ### Handling leap years Source: https://github.com/dateutil/dateutil/blob/master/docs/relativedelta.md Demonstrates that relativedelta correctly handles leap years when calculating date differences. ```python >>> date(2004, 1, 31) + relativedelta(months=+1) date.date(2004, 2, 29) ``` -------------------------------- ### Zonefile Metadata Structure Source: https://github.com/dateutil/dateutil/blob/master/docs/zoneinfo.md This JSON object defines the metadata for the timezone database, including version, download URLs, file checksums, and the list of included zone groups. It is used by the updatezinfo.py script. ```json { "metadata_version": 2.0, "releases_url": [ "ftp://ftp.iana.org/tz/releases/" ], "tzdata_file": "tzdata2024a.tar.gz", "tzdata_file_sha512": "1f09f1b2327cc9e1afc7e9045e83ee3377918dafe1bee2f282b6991828d03b3c70a4d3a17f9207dfb1361bb25bc214a8922a756e84fa114e9ba476226db57236", "tzversion": "2024a", "zonegroups": [ "africa", "antarctica", "asia", "australasia", "europe", "northamerica", "southamerica", "etcetera", "factory", "backzone", "backward" ] } ``` -------------------------------- ### Date Parsing: Without Year, Using Default Datetime Source: https://github.com/dateutil/dateutil/blob/master/docs/examples.md Parses a date string without a year, using a default datetime to determine the year. ```python from datetime import datetime default_dt = datetime(2012, 1, 1) parse("01-01 10:00:00", default=default_dt) ``` -------------------------------- ### Parse Local Timezone Abbreviations (US, India, Japan) Source: https://github.com/dateutil/dateutil/blob/master/docs/exercises/index.md Parses a datetime string and returns a timezone-aware datetime with an IANA-style timezone. Assumes input dates are from the continental US, India, or Japan. Uses dateutil.parser and dateutil.tz. ```python3 # --------- YOUR CODE -------------- # from dateutil.parser import parse from dateutil import tz def parse_func_us_jp_ind(): <> # ---------------------------------- # from dateutil import tz from datetime import datetime PARSE_TZ_TEST_DATETIMES = [ datetime(2018, 1, 1, 12, 0), datetime(2018, 3, 20, 2, 0), datetime(2018, 5, 12, 3, 30), datetime(2014, 9, 1, 23) ] PARSE_TZ_TEST_ZONES = [ tz.gettz('America/New_York'), tz.gettz('America/Chicago'), tz.gettz('America/Denver'), tz.gettz('America/Los_Angeles'), tz.gettz('Asia/Kolkata'), tz.gettz('Asia/Tokyo'), ] def test_parse(): for tzi in PARSE_TZ_TEST_ZONES: for dt in PARSE_TZ_TEST_DATETIMES: dt_exp = dt.replace(tzinfo=tzi) dtstr = dt_exp.strftime('%Y-%m-%d %H:%M:%S %Z') dt_act = parse_func_us_jp_ind(dtstr) assert dt_act == dt_exp assert dt_act.tzinfo is dt_exp.tzinfo if __name__ == "__main__": test_parse() print('Success!') ``` -------------------------------- ### Date Parsing: ISO Format Without Separators Source: https://github.com/dateutil/dateutil/blob/master/docs/examples.md Parses an ISO 8601 date string that omits separators like hyphens and colons. ```python parse("20100101T100000") ``` -------------------------------- ### Calculate next Friday Source: https://github.com/dateutil/dateutil/blob/master/docs/relativedelta.md Finds the date of the next Friday from a given date. ```python >>> TODAY + relativedelta(weekday=FR) datetime.date(2003, 9, 19) ``` -------------------------------- ### Calculate one month before one year Source: https://github.com/dateutil/dateutil/blob/master/docs/relativedelta.md Performs a subtraction of one month from a date that is one year in the future. ```python >>> NOW + relativedelta(years=+1, months=-1) datetime.datetime(2004, 8, 17, 20, 54, 47, 282310) ``` -------------------------------- ### Test MLK Day Rule with Test Cases Source: https://github.com/dateutil/dateutil/blob/master/docs/exercises/solutions/mlk-day-rrule.md Includes test cases to validate the MLK_DAY rule. It checks the rule's output against expected dates for various time ranges. This snippet requires the MLK_DAY rule to be defined previously. ```python from datetime import datetime MLK_TEST_CASES = [ ((datetime(1970, 1, 1), datetime(1980, 1, 1)), []), ((datetime(1980, 1, 1), datetime(1989, 1, 1)), [datetime(1986, 1, 20), datetime(1987, 1, 19), datetime(1988, 1, 18)]), ((datetime(2017, 2, 1), datetime(2022, 2, 1)), [ datetime(2018, 1, 15, 0, 0), datetime(2019, 1, 21, 0, 0), datetime(2020, 1, 20, 0, 0), datetime(2021, 1, 18, 0, 0), datetime(2022, 1, 17, 0, 0)] ), ] def test_mlk_day(): for (between_args, expected) in MLK_TEST_CASES: assert MLK_DAY.between(*between_args) == expected if __name__ == "__main__": test_mlk_day() print('Success!') ``` -------------------------------- ### Calculate age Source: https://github.com/dateutil/dateutil/blob/master/docs/relativedelta.md Computes the age of a person given their birth date and the current date. ```python >>> relativedelta(NOW, date(1980, 1, 1)) relativedelta(years=+23, months=+8, days=+16, hours=+20, minutes=+54, seconds=+47, microseconds=+282310) ``` -------------------------------- ### RRULE String Parsing: Every 10 Days for 5 Occurrences Source: https://github.com/dateutil/dateutil/blob/master/docs/examples.md Parses an RRULE string to generate a recurrence rule for 5 occurrences every 10 days. ```python from dateutil.rrule import rrulestr rules = rrulestr("every 10 days, 5 occurrences") ``` -------------------------------- ### Yearday in a leap year Source: https://github.com/dateutil/dateutil/blob/master/docs/relativedelta.md Shows the result of using 'yearday' in a leap year. ```python >>> date(2004, 1, 1) + relativedelta(yearday=+260) date.date(2004, 9, 16) ``` -------------------------------- ### Calculate next Wednesday (not today) Source: https://github.com/dateutil/dateutil/blob/master/docs/relativedelta.md Finds the date of the next Wednesday, ensuring it's not the current day. ```python >>> TODAY + relativedelta(weekday=WE(2)) datetime.date(2003, 9, 24) ``` -------------------------------- ### Date Parsing: RFC822 Format Source: https://github.com/dateutil/dateutil/blob/master/docs/examples.md Parses a date string formatted according to RFC822 (also known as RFC5322). ```python parse("Fri, 01 Jan 2010 10:00:00 -0000") ``` -------------------------------- ### Date Parsing: Ignoring Timezone Information Source: https://github.com/dateutil/dateutil/blob/master/docs/examples.md Parses a date string while explicitly ignoring any timezone information present in the string. ```python parse("2010-01-01T10:00:00", ignoretz=True) ``` -------------------------------- ### Calculate Next Easter on Friday the 13th Source: https://github.com/dateutil/dateutil/blob/master/docs/index.md This snippet calculates the date of the next Easter that occurs on a Friday the 13th in August. It retrieves the current date from the system's 'date' command. ```python from dateutil.easter import easter from datetime import date import subprocess def get_today(): date_str = subprocess.check_output(['date', '+%Y-%m-%d-%H-%M-%S']) return date(*map(int, date_str.strip().split('-'))) today = get_today() year = today.year while True: e = easter(year) if e.month == 8 and e.day == 13 and e.weekday() == 4: print(f'Next easter on friday 13th in august is {e}') break year += 1 ``` -------------------------------- ### Calculate time since millennium change Source: https://github.com/dateutil/dateutil/blob/master/docs/relativedelta.md Determines the duration between the millennium change and a specific date. ```python >>> relativedelta(NOW, date(2000, 1, 1)) relativedelta(years=+3, months=+8, days=+16, hours=+20, minutes=+54, seconds=+47, microseconds=+282310) ``` -------------------------------- ### Calculate last Friday in this month Source: https://github.com/dateutil/dateutil/blob/master/docs/relativedelta.md Determines the date of the last Friday within the current month. ```python >>> TODAY + relativedelta(weekday=FR(-1)) datetime.date(2003, 9, 26) ``` -------------------------------- ### Generate Martin Luther King Day Recurrence Rule Source: https://github.com/dateutil/dateutil/blob/master/docs/exercises/index.md This snippet demonstrates how to define a recurrence rule for Martin Luther King Day, which falls on the third Monday in January. It is intended to be used with the dateutil.rrule module. ```python3 # ------- YOUR CODE ------------- from dateutil import rrule MLK_DAY = <> # ------------------------------- from datetime import datetime MLK_TEST_CASES = [ ((datetime(1970, 1, 1), datetime(1980, 1, 1)), []), ((datetime(1980, 1, 1), datetime(1989, 1, 1)), [datetime(1986, 1, 20), datetime(1987, 1, 19), datetime(1988, 1, 18)]), ((datetime(2017, 2, 1), datetime(2022, 2, 1)), [ datetime(2018, 1, 15, 0, 0), datetime(2019, 1, 21, 0, 0), datetime(2020, 1, 20, 0, 0), datetime(2021, 1, 18, 0, 0), datetime(2022, 1, 17, 0, 0)] ), ] def test_mlk_day(): for (between_args, expected) in MLK_TEST_CASES: assert MLK_DAY.between(*between_args) == expected if __name__ == "__main__": test_mlk_day() print('Success!') ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.