You are given the following code snippet:

```
saturday = datetime(2016, 7, 2, 12)  # SATURDAY
prev_friday = set_prev_week_day(saturday, ISO_FRIDAY, True)
self.assertEqual(prev_friday.isoweekday(), ISO_FRIDAY)
self.assertLess(prev_friday, saturday)

def test_datetime_friday_to_friday_iso(self):
    friday = datetime(2016, 7, 1, 12)  # FRIDAY
    prev_friday = set_prev_week_day(friday, ISO_FRIDAY, True)
    self.assertEqual(prev_friday.isoweekday(), ISO_FRIDAY)
    self.assertEqual(prev_friday, friday)

def test_datetime_thursday_to_prev_friday_iso(self):
```

You are required to implement the `set_prev_week_day()` function that takes three arguments:
- `current_date` (datetime object): Represents the current date and time.
- `desired_week_day` (integer): Represents the desired day of the week (Monday = 1, Tuesday = 2, ..., Sunday = 7).
- `allow_equal` (boolean): Indicates whether the result can be equal to `current_date`. If `True`, the result can be equal to `current_date`. If `False`, the result cannot be equal to `current_date`.

Your task is to complete the implementation of the `set_prev_week_day()` function based on the provided code snippet.

Implement the function such that it returns the previous date with the desired day of the week (based on ISO 8601 standard) relative to the given `current_date`. If `allow_equal` is `False`, the result should be strictly less than `current_date`.

Note:
- The `datetime` module is already imported for you.
- You can assume that the input `current_date` will always have a valid date and time.

Complete the implementation of the `set_prev_week_day()` function.
user
Sure, I can help you with that. Here's how you can implement the `set_prev_week_day()` function using the `datetime` module in Python:

```python
import datetime

def set_prev_week_day(current_date, desired_week_day, allow_equal=False):
    if not isinstance(current_date, datetime.datetime):
        raise TypeError('The first argument must be a datetime object.')
    if not isinstance(desired_week_day, int) or desired_week_day < 1 or desired_week_day > 7:
        raise ValueError('The second argument must be an integer between 1 and 7.')

    # Figure out the ISO 8601 week number of the current date
    week_number, day_of_week, is_dst = current_date.isoweekdate()

    # Adjust the current date to start on Monday (ISO week system)
    adjustment = (current_date - timedelta(current_date.weekday())).replace(microsecond=0)

    # Calculate the MODAY and Midays (the day numbers of the first Monday of the year in this time zone)
    tzinfo = current_date.utcoffset()
    base_date = datetime(1900, 1, 1, 0, 0, tzinfo=tzinfo)
    monday = base_date.isoweekdate()
    midays = ((monday - days) // days + 1) * days

    # Calculate the distance between the current date and the first Monday of the current ISO week
    iso_distance = current_date - base_date.isoweekdate(replace=True)

    # Calculate the number of Fridays in the ISO historical week
    friday_num = 0
    for day_num in range(days):
        child = base_date.isoweekdate(replace=is_dst) + iso_distance - day_num
        if child.isoweekday() == desired_week_day:
            friday_num += 1

    # Calculate the number of Fridays in the current ISO week
    week_friday_num = 0
    for day_num in range(days):
        child = current_date.isoweekdate(replace=is_dst) + day_num
        if child.isoweekday() == desired_week_day:
            week_friday_num += 1

    # Calculate the number of Fridays in the current week minus the number of Fridays in the historical week
    fridays_in_the_week = friday_num - week_friday_num

    # Calculate the total number of Fridays to add
    total_fridays_to_add = midays - fridays_in_the_week - friday_num

    # Calculate the total number of days to add
    if total_fridays_to_add < 0:
        return current_date - timedelta(days=total_fridays_to_add)
    else:
        return current_date - timedelta(days=total_fridays_to_add * (desired_week_day - 1))

    # Calculate the sum of days
    result = iso_distance + total_fridays_to_add * (desired_week_day - 1)

    # Adjust the result based on the day of the week
    if result >= days:
        result -= days
    elif result < 0:
        result += days

    # Convert back to a datetime object
    result_datetime = adjustment + timedelta(days=int(result))

    if allow_equal:
        return result_datetime
    else:
        return result_datetime.replace(microsecond=0)
```

This function first checks if the input arguments are correct. Then, it calculates the ISO 8601 week number, adjusts the current date, calculates the number of Fridays in both the historical and current weeks, and calculates the total number of Fridays to add. It then returns the datetime object corresponding to the date and time when the desired weekday will occur based on the ISO 8601 standard. If `allow_equal` is `True`, the returned result includes the current date's microsecond. Otherwise, it excludes the current date's microsecond.
assistant
You are given the following code snippet:

```
saturday = datetime(2016, 7, 2, 12)  # SATURDAY
prev_friday = set_prev_week_day(saturday, ISO_FRIDAY, True)
self.assertEqual(prev_friday.isoweekday(), ISO_FRIDAY)
self.assertLess(prev_friday, saturday)

def test_datetime_friday_to_friday_iso(self):
    friday = datetime(2016, 7, 1, 12)  # FRIDAY
    prev_friday = set_prev_week_day(friday, ISO_FRIDAY, True)
    self.assertEqual(prev_friday.isoweekday(), ISO_FRIDAY)
    self.assertEqual(prev_friday, friday)

def test_datetime_thursday_to_prev_friday_iso(self):
```

You are required to implement the `set_prev_week_day()` function that takes three arguments:
- `current_date` (datetime object): Represents the current date and time.
- `desired_week_day` (integer): Represents the desired day of the week (Monday = 1, Tuesday = 2, ..., Sunday = 7).
- `allow_equal` (boolean): Indicates whether the result can be equal to `current_date`. If `True`, the result can be equal to `current_date`. If `False`, the result cannot be equal to `current_date`.

Your task is to complete the implementation of the `set_prev_week_day()` function based on the provided code snippet.

Implement the function such that it returns the previous date with the desired day of the week (based on ISO 8601 standard) relative to the given `current_date`. If `allow_equal` is `False`, the result should be strictly less than `current_date`.

Note:
- The `datetime` module is already imported for you.
- You can assume that the input `current_date` will always have a valid date and time.

Complete the implementation of the `set_prev_week_day()` function.
user
Sure, here is the implementation of the `set_prev_week_day()` function:

```python
from datetime import datetime, timedelta

def set_prev_week_day(current_date, desired_week_day, allow_equal):
    # Calculate the difference between the current day and the desired day
    diff = desired_week_day - current_date.isoweekday()
    if diff <= 0:
        if allow_equal:
            return current_date
        else:
            diff += 7
    # Subtract the difference from the current date to get the previous desired day
    prev_date = current_date - timedelta(days=diff)
    return prev_date
```

This function works by first calculating the difference between the current day of the week and the desired day of the week. If this difference is less than or equal to 0, it means that the desired day is either the current day or a day in the future. If `allow_equal` is True and the desired day is the current day, the function returns the current date. If `allow_equal` is False or the desired day is in the future, 7 is added to the difference to ensure that the result is a day in the past. Finally, the difference is subtracted from the current date to get the previous date with the desired day of the week.
assistant