Skip to content

Commit df2908a

Browse files
caitriona-cloudsmithjayvynl
authored andcommitted
Added toYearWeek functionality
1 parent 4966dce commit df2908a

File tree

2 files changed

+42
-0
lines changed

2 files changed

+42
-0
lines changed

clickhouse_backend/models/functions/datetime.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
"toYYYYMM",
1515
"toYYYYMMDD",
1616
"toYYYYMMDDhhmmss",
17+
"toYearWeek",
1718
]
1819

1920

@@ -77,3 +78,21 @@ class toStartOfFifteenMinutes(toStartOfMinute):
7778

7879
class toStartOfHour(toStartOfMinute):
7980
pass
81+
82+
class toYearWeek(Func):
83+
output_field = fields.UInt32Field()
84+
85+
def __init__(self, *expressions):
86+
arity = len(expressions)
87+
if arity < 1 or arity > 3:
88+
raise TypeError(
89+
"'%s' takes between 1 and 3 arguments (%s given)"
90+
% (
91+
self.__class__.__name__,
92+
len(expressions),
93+
)
94+
)
95+
96+
expressions = (expressions[0], *(models.Value(expr) for expr in expressions[1:]))
97+
98+
super().__init__(*expressions)

tests/clickhouse_functions/test_datetime.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,13 @@ def setUpTestData(cls):
2727
datetime(2023, 11, 30, hour=16, minute=59, second=59), is_dst=False
2828
),
2929
)
30+
cls.sarah = Author.objects.create(
31+
name="Sarah Connor",
32+
alias="sconnor",
33+
birthday=pytz.utc.localize(
34+
datetime(2023, 12, 31, hour=23, minute=30, second=00), is_dst=False
35+
),
36+
)
3037

3138
def test_yyyymm(self):
3239
john = Author.objects.annotate(v=models.toYYYYMM("birthday")).get(
@@ -177,3 +184,19 @@ def test_tostartofhour(self):
177184
elena.v,
178185
datetime(2023, 11, 30, hour=10, minute=00, second=00),
179186
)
187+
188+
def test_toyearweek(self):
189+
sarah = Author.objects.annotate(v=models.toYearWeek("birthday")).get(
190+
id=self.sarah.id
191+
)
192+
self.assertEqual(sarah.v, 202353)
193+
194+
sarah = Author.objects.annotate(v=models.toYearWeek("birthday", 1)).get(
195+
id=self.sarah.id
196+
)
197+
self.assertEqual(sarah.v, 202352)
198+
199+
sarah = Author.objects.annotate(v=models.toYearWeek("birthday", 1, "Pacific/Kiritimati")).get(
200+
id=self.sarah.id
201+
)
202+
self.assertEqual(sarah.v, 202401)

0 commit comments

Comments
 (0)