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