|
| 1 | +""" Example showing how to use a custom template function for osxphotos {function} template |
| 2 | + Example use: osxphotos query --quiet --print "{function:date_delta.py::months_since(2021-01-01)}" |
| 3 | +""" |
| 4 | + |
| 5 | +import datetime |
| 6 | +import pathlib |
| 7 | +from typing import List, Optional, Union |
| 8 | + |
| 9 | +from osxphotos import PhotoInfo |
| 10 | +from osxphotos.datetime_utils import datetime_naive_to_local |
| 11 | +from osxphotos.phototemplate import RenderOptions |
| 12 | + |
| 13 | + |
| 14 | +def years_since( |
| 15 | + photo: PhotoInfo, options: RenderOptions, args: Optional[str] = None, **kwargs |
| 16 | +) -> Union[List, str]: |
| 17 | + """Return the number of years between the photo date and the date passed as an argument in format YYYY-MM-DD""" |
| 18 | + |
| 19 | + if not args: |
| 20 | + raise ValueError( |
| 21 | + "months_since function requires an argument in the form of a date string in the format YYYY-MM-DD" |
| 22 | + ) |
| 23 | + # if args doesn't match the expected format, raise an error |
| 24 | + try: |
| 25 | + date_arg = datetime_naive_to_local(datetime.datetime.strptime(args, "%Y-%m-%d")) |
| 26 | + except ValueError: |
| 27 | + raise ValueError( |
| 28 | + "months_since function requires an argument in the form of a date string in the format YYYY-MM-DD" |
| 29 | + ) |
| 30 | + |
| 31 | + return str(_years_since(photo.date, date_arg)) |
| 32 | + |
| 33 | + |
| 34 | +def months_since( |
| 35 | + photo: PhotoInfo, options: RenderOptions, args: Optional[str] = None, **kwargs |
| 36 | +) -> Union[List, str]: |
| 37 | + """Return the number of months between the photo date and the date passed as an argument in format YYYY-MM-DD""" |
| 38 | + |
| 39 | + if not args: |
| 40 | + raise ValueError( |
| 41 | + "months_since function requires an argument in the form of a date string in the format YYYY-MM-DD" |
| 42 | + ) |
| 43 | + # if args doesn't match the expected format, raise an error |
| 44 | + try: |
| 45 | + date_arg = datetime_naive_to_local(datetime.datetime.strptime(args, "%Y-%m-%d")) |
| 46 | + except ValueError: |
| 47 | + raise ValueError( |
| 48 | + "months_since function requires an argument in the form of a date string in the format YYYY-MM-DD" |
| 49 | + ) |
| 50 | + |
| 51 | + return str(_months_since(photo.date, date_arg)) |
| 52 | + |
| 53 | + |
| 54 | +def days_since( |
| 55 | + photo: PhotoInfo, options: RenderOptions, args: Optional[str] = None, **kwargs |
| 56 | +) -> Union[List, str]: |
| 57 | + """Return the number of days between the photo date and the date passed as an argument in format YYYY-MM-DD""" |
| 58 | + |
| 59 | + if not args: |
| 60 | + raise ValueError( |
| 61 | + "months_since function requires an argument in the form of a date string in the format YYYY-MM-DD" |
| 62 | + ) |
| 63 | + # if args doesn't match the expected format, raise an error |
| 64 | + try: |
| 65 | + date_arg = datetime_naive_to_local(datetime.datetime.strptime(args, "%Y-%m-%d")) |
| 66 | + except ValueError: |
| 67 | + raise ValueError( |
| 68 | + "months_since function requires an argument in the form of a date string in the format YYYY-MM-DD" |
| 69 | + ) |
| 70 | + |
| 71 | + return str(_days_since(photo.date, date_arg)) |
| 72 | + |
| 73 | + |
| 74 | +def _months_since(start_date: datetime, end_date: datetime) -> int: |
| 75 | + if start_date > end_date: |
| 76 | + start_date, end_date = end_date, start_date |
| 77 | + years_difference = end_date.year - start_date.year |
| 78 | + months_difference = end_date.month - start_date.month |
| 79 | + return years_difference * 12 + months_difference |
| 80 | + |
| 81 | + |
| 82 | +def _years_since(start_date: datetime, end_date: datetime) -> int: |
| 83 | + if start_date > end_date: |
| 84 | + start_date, end_date = end_date, start_date |
| 85 | + years_difference = end_date.year - start_date.year |
| 86 | + if (end_date.month, end_date.day) < (start_date.month, start_date.day): |
| 87 | + years_difference -= 1 |
| 88 | + return years_difference |
| 89 | + |
| 90 | + |
| 91 | +def _days_since(start_date: datetime, end_date: datetime) -> int: |
| 92 | + return abs((end_date - start_date).days) |
0 commit comments