/
npdatetime.py
628 lines (539 loc) · 25.6 KB
/
npdatetime.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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
"""
Implementation of operations on numpy timedelta64.
"""
import numpy as np
from llvmlite.llvmpy.core import Type, Constant
import llvmlite.llvmpy.core as lc
from numba import npdatetime, types, cgutils
from .imputils import lower_builtin, lower_constant, impl_ret_untracked
# datetime64 and timedelta64 use the same internal representation
DATETIME64 = TIMEDELTA64 = Type.int(64)
NAT = Constant.int(TIMEDELTA64, npdatetime.NAT)
TIMEDELTA_BINOP_SIG = (types.NPTimedelta,) * 2
def scale_by_constant(builder, val, factor):
"""
Multiply *val* by the constant *factor*.
"""
return builder.mul(val, Constant.int(TIMEDELTA64, factor))
def unscale_by_constant(builder, val, factor):
"""
Divide *val* by the constant *factor*.
"""
return builder.sdiv(val, Constant.int(TIMEDELTA64, factor))
def add_constant(builder, val, const):
"""
Add constant *const* to *val*.
"""
return builder.add(val, Constant.int(TIMEDELTA64, const))
def scale_timedelta(context, builder, val, srcty, destty):
"""
Scale the timedelta64 *val* from *srcty* to *destty*
(both numba.types.NPTimedelta instances)
"""
factor = npdatetime.get_timedelta_conversion_factor(srcty.unit, destty.unit)
if factor is None:
# This can happen when using explicit output in a ufunc.
raise NotImplementedError("cannot convert timedelta64 from %r to %r"
% (srcty.unit, destty.unit))
return scale_by_constant(builder, val, factor)
def normalize_timedeltas(context, builder, left, right, leftty, rightty):
"""
Scale either *left* or *right* to the other's unit, in order to have
homogeneous units.
"""
factor = npdatetime.get_timedelta_conversion_factor(leftty.unit, rightty.unit)
if factor is not None:
return scale_by_constant(builder, left, factor), right
factor = npdatetime.get_timedelta_conversion_factor(rightty.unit, leftty.unit)
if factor is not None:
return left, scale_by_constant(builder, right, factor)
# Typing should not let this happen, except on == and != operators
raise RuntimeError("cannot normalize %r and %r" % (leftty, rightty))
def alloc_timedelta_result(builder, name='ret'):
"""
Allocate a NaT-initialized datetime64 (or timedelta64) result slot.
"""
ret = cgutils.alloca_once(builder, TIMEDELTA64, name=name)
builder.store(NAT, ret)
return ret
def alloc_boolean_result(builder, name='ret'):
"""
Allocate an uninitialized boolean result slot.
"""
ret = cgutils.alloca_once(builder, Type.int(1), name=name)
return ret
def is_not_nat(builder, val):
"""
Return a predicate which is true if *val* is not NaT.
"""
return builder.icmp(lc.ICMP_NE, val, NAT)
def are_not_nat(builder, vals):
"""
Return a predicate which is true if all of *vals* are not NaT.
"""
assert len(vals) >= 1
pred = is_not_nat(builder, vals[0])
for val in vals[1:]:
pred = builder.and_(pred, is_not_nat(builder, val))
return pred
def make_constant_array(vals):
consts = [Constant.int(TIMEDELTA64, v) for v in vals]
return Constant.array(TIMEDELTA64, consts)
normal_year_months = make_constant_array([31, 28, 31, 30, 31, 30,
31, 31, 30, 31, 30, 31])
leap_year_months = make_constant_array([31, 29, 31, 30, 31, 30,
31, 31, 30, 31, 30, 31])
normal_year_months_acc = make_constant_array(
[0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334])
leap_year_months_acc = make_constant_array(
[0, 31, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335])
@lower_constant(types.NPDatetime)
@lower_constant(types.NPTimedelta)
def datetime_constant(context, builder, ty, pyval):
return DATETIME64(pyval.astype(np.int64))
# Arithmetic operators on timedelta64
@lower_builtin('+', types.NPTimedelta)
def timedelta_pos_impl(context, builder, sig, args):
res =args[0]
return impl_ret_untracked(context, builder, sig.return_type, res)
@lower_builtin('-', types.NPTimedelta)
def timedelta_neg_impl(context, builder, sig, args):
res = builder.neg(args[0])
return impl_ret_untracked(context, builder, sig.return_type, res)
@lower_builtin(abs, types.NPTimedelta)
def timedelta_abs_impl(context, builder, sig, args):
val, = args
ret = alloc_timedelta_result(builder)
with builder.if_else(cgutils.is_scalar_neg(builder, val)) as (then, otherwise):
with then:
builder.store(builder.neg(val), ret)
with otherwise:
builder.store(val, ret)
res = builder.load(ret)
return impl_ret_untracked(context, builder, sig.return_type, res)
def timedelta_sign_impl(context, builder, sig, args):
"""
np.sign(timedelta64)
"""
val, = args
ret = alloc_timedelta_result(builder)
zero = Constant.int(TIMEDELTA64, 0)
with builder.if_else(builder.icmp(lc.ICMP_SGT, val, zero)
) as (gt_zero, le_zero):
with gt_zero:
builder.store(Constant.int(TIMEDELTA64, 1), ret)
with le_zero:
with builder.if_else(builder.icmp(lc.ICMP_EQ, val, zero)
) as (eq_zero, lt_zero):
with eq_zero:
builder.store(Constant.int(TIMEDELTA64, 0), ret)
with lt_zero:
builder.store(Constant.int(TIMEDELTA64, -1), ret)
res = builder.load(ret)
return impl_ret_untracked(context, builder, sig.return_type, res)
@lower_builtin('+', *TIMEDELTA_BINOP_SIG)
def timedelta_add_impl(context, builder, sig, args):
[va, vb] = args
[ta, tb] = sig.args
ret = alloc_timedelta_result(builder)
with cgutils.if_likely(builder, are_not_nat(builder, [va, vb])):
va = scale_timedelta(context, builder, va, ta, sig.return_type)
vb = scale_timedelta(context, builder, vb, tb, sig.return_type)
builder.store(builder.add(va, vb), ret)
res = builder.load(ret)
return impl_ret_untracked(context, builder, sig.return_type, res)
@lower_builtin('-', *TIMEDELTA_BINOP_SIG)
def timedelta_sub_impl(context, builder, sig, args):
[va, vb] = args
[ta, tb] = sig.args
ret = alloc_timedelta_result(builder)
with cgutils.if_likely(builder, are_not_nat(builder, [va, vb])):
va = scale_timedelta(context, builder, va, ta, sig.return_type)
vb = scale_timedelta(context, builder, vb, tb, sig.return_type)
builder.store(builder.sub(va, vb), ret)
res = builder.load(ret)
return impl_ret_untracked(context, builder, sig.return_type, res)
def _timedelta_times_number(context, builder, td_arg, td_type,
number_arg, number_type, return_type):
ret = alloc_timedelta_result(builder)
with cgutils.if_likely(builder, is_not_nat(builder, td_arg)):
if isinstance(number_type, types.Float):
val = builder.sitofp(td_arg, number_arg.type)
val = builder.fmul(val, number_arg)
val = builder.fptosi(val, TIMEDELTA64)
else:
val = builder.mul(td_arg, number_arg)
# The scaling is required for ufunc np.multiply() with an explicit
# output in a different unit.
val = scale_timedelta(context, builder, val, td_type, return_type)
builder.store(val, ret)
return builder.load(ret)
@lower_builtin('*', types.NPTimedelta, types.Integer)
@lower_builtin('*', types.NPTimedelta, types.Float)
def timedelta_times_number(context, builder, sig, args):
res = _timedelta_times_number(context, builder,
args[0], sig.args[0], args[1], sig.args[1],
sig.return_type)
return impl_ret_untracked(context, builder, sig.return_type, res)
@lower_builtin('*', types.Integer, types.NPTimedelta)
@lower_builtin('*', types.Float, types.NPTimedelta)
def number_times_timedelta(context, builder, sig, args):
res = _timedelta_times_number(context, builder,
args[1], sig.args[1], args[0], sig.args[0],
sig.return_type)
return impl_ret_untracked(context, builder, sig.return_type, res)
@lower_builtin('/', types.NPTimedelta, types.Integer)
@lower_builtin('//', types.NPTimedelta, types.Integer)
@lower_builtin('/?', types.NPTimedelta, types.Integer)
@lower_builtin('/', types.NPTimedelta, types.Float)
@lower_builtin('//', types.NPTimedelta, types.Float)
@lower_builtin('/?', types.NPTimedelta, types.Float)
def timedelta_over_number(context, builder, sig, args):
td_arg, number_arg = args
number_type = sig.args[1]
ret = alloc_timedelta_result(builder)
ok = builder.and_(is_not_nat(builder, td_arg),
builder.not_(cgutils.is_scalar_zero_or_nan(builder, number_arg)))
with cgutils.if_likely(builder, ok):
# Denominator is non-zero, non-NaN
if isinstance(number_type, types.Float):
val = builder.sitofp(td_arg, number_arg.type)
val = builder.fdiv(val, number_arg)
val = builder.fptosi(val, TIMEDELTA64)
else:
val = builder.sdiv(td_arg, number_arg)
# The scaling is required for ufuncs np.*divide() with an explicit
# output in a different unit.
val = scale_timedelta(context, builder, val, sig.args[0], sig.return_type)
builder.store(val, ret)
res = builder.load(ret)
return impl_ret_untracked(context, builder, sig.return_type, res)
@lower_builtin('/', *TIMEDELTA_BINOP_SIG)
@lower_builtin('/?', *TIMEDELTA_BINOP_SIG)
def timedelta_over_timedelta(context, builder, sig, args):
[va, vb] = args
[ta, tb] = sig.args
not_nan = are_not_nat(builder, [va, vb])
ll_ret_type = context.get_value_type(sig.return_type)
ret = cgutils.alloca_once(builder, ll_ret_type, name='ret')
builder.store(Constant.real(ll_ret_type, float('nan')), ret)
with cgutils.if_likely(builder, not_nan):
va, vb = normalize_timedeltas(context, builder, va, vb, ta, tb)
va = builder.sitofp(va, ll_ret_type)
vb = builder.sitofp(vb, ll_ret_type)
builder.store(builder.fdiv(va, vb), ret)
res = builder.load(ret)
return impl_ret_untracked(context, builder, sig.return_type, res)
# Comparison operators on timedelta64
def _create_timedelta_comparison_impl(ll_op, default_value):
def impl(context, builder, sig, args):
[va, vb] = args
[ta, tb] = sig.args
ret = alloc_boolean_result(builder)
with builder.if_else(are_not_nat(builder, [va, vb])) as (then, otherwise):
with then:
try:
norm_a, norm_b = normalize_timedeltas(context, builder, va, vb, ta, tb)
except RuntimeError:
# Cannot normalize units => the values are unequal (except if NaT)
builder.store(default_value, ret)
else:
builder.store(builder.icmp(ll_op, norm_a, norm_b), ret)
with otherwise:
# No scaling when comparing NaTs
builder.store(builder.icmp(ll_op, va, vb), ret)
res = builder.load(ret)
return impl_ret_untracked(context, builder, sig.return_type, res)
return impl
def _create_timedelta_ordering_impl(ll_op):
def impl(context, builder, sig, args):
[va, vb] = args
[ta, tb] = sig.args
ret = alloc_boolean_result(builder)
with builder.if_else(are_not_nat(builder, [va, vb])) as (then, otherwise):
with then:
norm_a, norm_b = normalize_timedeltas(context, builder, va, vb, ta, tb)
builder.store(builder.icmp(ll_op, norm_a, norm_b), ret)
with otherwise:
# No scaling when comparing NaT with something else
# (i.e. NaT is <= everything else, since it's the smallest
# int64 value)
builder.store(builder.icmp(ll_op, va, vb), ret)
res = builder.load(ret)
return impl_ret_untracked(context, builder, sig.return_type, res)
return impl
timedelta_eq_timedelta_impl = _create_timedelta_comparison_impl(lc.ICMP_EQ, cgutils.false_bit)
timedelta_ne_timedelta_impl = _create_timedelta_comparison_impl(lc.ICMP_NE, cgutils.true_bit)
timedelta_lt_timedelta_impl = _create_timedelta_ordering_impl(lc.ICMP_SLT)
timedelta_le_timedelta_impl = _create_timedelta_ordering_impl(lc.ICMP_SLE)
timedelta_gt_timedelta_impl = _create_timedelta_ordering_impl(lc.ICMP_SGT)
timedelta_ge_timedelta_impl = _create_timedelta_ordering_impl(lc.ICMP_SGE)
for op, func in [('==', timedelta_eq_timedelta_impl),
('!=', timedelta_ne_timedelta_impl),
('<', timedelta_lt_timedelta_impl),
('<=', timedelta_le_timedelta_impl),
('>', timedelta_gt_timedelta_impl),
('>=', timedelta_ge_timedelta_impl)]:
lower_builtin(op, *TIMEDELTA_BINOP_SIG)(func)
# Arithmetic on datetime64
def is_leap_year(builder, year_val):
"""
Return a predicate indicating whether *year_val* (offset by 1970) is a
leap year.
"""
actual_year = builder.add(year_val, Constant.int(DATETIME64, 1970))
multiple_of_4 = cgutils.is_null(
builder, builder.and_(actual_year, Constant.int(DATETIME64, 3)))
not_multiple_of_100 = cgutils.is_not_null(
builder, builder.srem(actual_year, Constant.int(DATETIME64, 100)))
multiple_of_400 = cgutils.is_null(
builder, builder.srem(actual_year, Constant.int(DATETIME64, 400)))
return builder.and_(multiple_of_4,
builder.or_(not_multiple_of_100, multiple_of_400))
def year_to_days(builder, year_val):
"""
Given a year *year_val* (offset to 1970), return the number of days
since the 1970 epoch.
"""
# The algorithm below is copied from Numpy's get_datetimestruct_days()
# (src/multiarray/datetime.c)
ret = cgutils.alloca_once(builder, TIMEDELTA64)
# First approximation
days = scale_by_constant(builder, year_val, 365)
# Adjust for leap years
with builder.if_else(cgutils.is_neg_int(builder, year_val)) \
as (if_neg, if_pos):
with if_pos:
# At or after 1970:
# 1968 is the closest leap year before 1970.
# Exclude the current year, so add 1.
from_1968 = add_constant(builder, year_val, 1)
# Add one day for each 4 years
p_days = builder.add(days,
unscale_by_constant(builder, from_1968, 4))
# 1900 is the closest previous year divisible by 100
from_1900 = add_constant(builder, from_1968, 68)
# Subtract one day for each 100 years
p_days = builder.sub(p_days,
unscale_by_constant(builder, from_1900, 100))
# 1600 is the closest previous year divisible by 400
from_1600 = add_constant(builder, from_1900, 300)
# Add one day for each 400 years
p_days = builder.add(p_days,
unscale_by_constant(builder, from_1600, 400))
builder.store(p_days, ret)
with if_neg:
# Before 1970:
# NOTE `year_val` is negative, and so will be `from_1972` and `from_2000`.
# 1972 is the closest later year after 1970.
# Include the current year, so subtract 2.
from_1972 = add_constant(builder, year_val, -2)
# Subtract one day for each 4 years (`from_1972` is negative)
n_days = builder.add(days,
unscale_by_constant(builder, from_1972, 4))
# 2000 is the closest later year divisible by 100
from_2000 = add_constant(builder, from_1972, -28)
# Add one day for each 100 years
n_days = builder.sub(n_days,
unscale_by_constant(builder, from_2000, 100))
# 2000 is also the closest later year divisible by 400
# Subtract one day for each 400 years
n_days = builder.add(n_days,
unscale_by_constant(builder, from_2000, 400))
builder.store(n_days, ret)
return builder.load(ret)
def reduce_datetime_for_unit(builder, dt_val, src_unit, dest_unit):
dest_unit_code = npdatetime.DATETIME_UNITS[dest_unit]
src_unit_code = npdatetime.DATETIME_UNITS[src_unit]
if dest_unit_code < 2 or src_unit_code >= 2:
return dt_val, src_unit
# Need to compute the day ordinal for *dt_val*
if src_unit_code == 0:
# Years to days
year_val = dt_val
days_val = year_to_days(builder, year_val)
else:
# Months to days
leap_array = cgutils.global_constant(builder, "leap_year_months_acc",
leap_year_months_acc)
normal_array = cgutils.global_constant(builder, "normal_year_months_acc",
normal_year_months_acc)
days = cgutils.alloca_once(builder, TIMEDELTA64)
# First compute year number and month number
year, month = cgutils.divmod_by_constant(builder, dt_val, 12)
# Then deduce the number of days
with builder.if_else(is_leap_year(builder, year)) as (then, otherwise):
with then:
addend = builder.load(cgutils.gep(builder, leap_array,
0, month, inbounds=True))
builder.store(addend, days)
with otherwise:
addend = builder.load(cgutils.gep(builder, normal_array,
0, month, inbounds=True))
builder.store(addend, days)
days_val = year_to_days(builder, year)
days_val = builder.add(days_val, builder.load(days))
if dest_unit_code == 2:
# Need to scale back to weeks
weeks, _ = cgutils.divmod_by_constant(builder, days_val, 7)
return weeks, 'W'
else:
return days_val, 'D'
def convert_datetime_for_arith(builder, dt_val, src_unit, dest_unit):
"""
Convert datetime *dt_val* from *src_unit* to *dest_unit*.
"""
# First partial conversion to days or weeks, if necessary.
dt_val, dt_unit = reduce_datetime_for_unit(builder, dt_val, src_unit, dest_unit)
# Then multiply by the remaining constant factor.
dt_factor = npdatetime.get_timedelta_conversion_factor(dt_unit, dest_unit)
if dt_factor is None:
# This can happen when using explicit output in a ufunc.
raise NotImplementedError("cannot convert datetime64 from %r to %r"
% (src_unit, dest_unit))
return scale_by_constant(builder, dt_val, dt_factor)
def _datetime_timedelta_arith(ll_op_name):
def impl(context, builder, dt_arg, dt_unit,
td_arg, td_unit, ret_unit):
ret = alloc_timedelta_result(builder)
with cgutils.if_likely(builder, are_not_nat(builder, [dt_arg, td_arg])):
dt_arg = convert_datetime_for_arith(builder, dt_arg,
dt_unit, ret_unit)
td_factor = npdatetime.get_timedelta_conversion_factor(td_unit, ret_unit)
td_arg = scale_by_constant(builder, td_arg, td_factor)
ret_val = getattr(builder, ll_op_name)(dt_arg, td_arg)
builder.store(ret_val, ret)
return builder.load(ret)
return impl
_datetime_plus_timedelta = _datetime_timedelta_arith('add')
_datetime_minus_timedelta = _datetime_timedelta_arith('sub')
# datetime64 + timedelta64
@lower_builtin('+', types.NPDatetime, types.NPTimedelta)
def datetime_plus_timedelta(context, builder, sig, args):
dt_arg, td_arg = args
dt_type, td_type = sig.args
res = _datetime_plus_timedelta(context, builder,
dt_arg, dt_type.unit,
td_arg, td_type.unit,
sig.return_type.unit)
return impl_ret_untracked(context, builder, sig.return_type, res)
@lower_builtin('+', types.NPTimedelta, types.NPDatetime)
def timedelta_plus_datetime(context, builder, sig, args):
td_arg, dt_arg = args
td_type, dt_type = sig.args
res = _datetime_plus_timedelta(context, builder,
dt_arg, dt_type.unit,
td_arg, td_type.unit,
sig.return_type.unit)
return impl_ret_untracked(context, builder, sig.return_type, res)
# datetime64 - timedelta64
@lower_builtin('-', types.NPDatetime, types.NPTimedelta)
def datetime_minus_timedelta(context, builder, sig, args):
dt_arg, td_arg = args
dt_type, td_type = sig.args
res = _datetime_minus_timedelta(context, builder,
dt_arg, dt_type.unit,
td_arg, td_type.unit,
sig.return_type.unit)
return impl_ret_untracked(context, builder, sig.return_type, res)
# datetime64 - datetime64
@lower_builtin('-', types.NPDatetime, types.NPDatetime)
def datetime_minus_datetime(context, builder, sig, args):
va, vb = args
ta, tb = sig.args
unit_a = ta.unit
unit_b = tb.unit
ret_unit = sig.return_type.unit
ret = alloc_timedelta_result(builder)
with cgutils.if_likely(builder, are_not_nat(builder, [va, vb])):
va = convert_datetime_for_arith(builder, va, unit_a, ret_unit)
vb = convert_datetime_for_arith(builder, vb, unit_b, ret_unit)
ret_val = builder.sub(va, vb)
builder.store(ret_val, ret)
res = builder.load(ret)
return impl_ret_untracked(context, builder, sig.return_type, res)
# datetime64 comparisons
def _create_datetime_comparison_impl(ll_op):
def impl(context, builder, sig, args):
va, vb = args
ta, tb = sig.args
unit_a = ta.unit
unit_b = tb.unit
ret_unit = npdatetime.get_best_unit(unit_a, unit_b)
ret = alloc_boolean_result(builder)
with builder.if_else(are_not_nat(builder, [va, vb])) as (then, otherwise):
with then:
norm_a = convert_datetime_for_arith(builder, va, unit_a, ret_unit)
norm_b = convert_datetime_for_arith(builder, vb, unit_b, ret_unit)
ret_val = builder.icmp(ll_op, norm_a, norm_b)
builder.store(ret_val, ret)
with otherwise:
# No scaling when comparing NaTs
ret_val = builder.icmp(ll_op, va, vb)
builder.store(ret_val, ret)
res = builder.load(ret)
return impl_ret_untracked(context, builder, sig.return_type, res)
return impl
datetime_eq_datetime_impl = _create_datetime_comparison_impl(lc.ICMP_EQ)
datetime_ne_datetime_impl = _create_datetime_comparison_impl(lc.ICMP_NE)
datetime_lt_datetime_impl = _create_datetime_comparison_impl(lc.ICMP_SLT)
datetime_le_datetime_impl = _create_datetime_comparison_impl(lc.ICMP_SLE)
datetime_gt_datetime_impl = _create_datetime_comparison_impl(lc.ICMP_SGT)
datetime_ge_datetime_impl = _create_datetime_comparison_impl(lc.ICMP_SGE)
for op, func in [('==', datetime_eq_datetime_impl),
('!=', datetime_ne_datetime_impl),
('<', datetime_lt_datetime_impl),
('<=', datetime_le_datetime_impl),
('>', datetime_gt_datetime_impl),
('>=', datetime_ge_datetime_impl)]:
lower_builtin(op, *[types.NPDatetime]*2)(func)
########################################################################
# datetime/timedelta fmax/fmin maximum/minimum support
def datetime_max_impl(context, builder, sig, args):
# just a regular int64 max avoiding nats.
# note this could be optimizing relying on the actual value of NAT
# but as NumPy doesn't rely on this, this seems more resilient
in1, in2 = args
in1_not_nat = is_not_nat(builder, in1)
in2_not_nat = is_not_nat(builder, in2)
in1_ge_in2 = builder.icmp(lc.ICMP_SGE, in1, in2)
res = builder.select(in1_ge_in2, in1, in2)
res = builder.select(in1_not_nat, res, in2)
res = builder.select(in2_not_nat, res, in1)
return impl_ret_untracked(context, builder, sig.return_type, res)
def datetime_min_impl(context, builder, sig, args):
# just a regular int64 min avoiding nats.
# note this could be optimizing relying on the actual value of NAT
# but as NumPy doesn't rely on this, this seems more resilient
in1, in2 = args
in1_not_nat = is_not_nat(builder, in1)
in2_not_nat = is_not_nat(builder, in2)
in1_le_in2 = builder.icmp(lc.ICMP_SLE, in1, in2)
res = builder.select(in1_le_in2, in1, in2)
res = builder.select(in1_not_nat, res, in2)
res = builder.select(in2_not_nat, res, in1)
return impl_ret_untracked(context, builder, sig.return_type, res)
def timedelta_max_impl(context, builder, sig, args):
# just a regular int64 max avoiding nats.
# note this could be optimizing relying on the actual value of NAT
# but as NumPy doesn't rely on this, this seems more resilient
in1, in2 = args
in1_not_nat = is_not_nat(builder, in1)
in2_not_nat = is_not_nat(builder, in2)
in1_ge_in2 = builder.icmp(lc.ICMP_SGE, in1, in2)
res = builder.select(in1_ge_in2, in1, in2)
res = builder.select(in1_not_nat, res, in2)
res = builder.select(in2_not_nat, res, in1)
return impl_ret_untracked(context, builder, sig.return_type, res)
def timedelta_min_impl(context, builder, sig, args):
# just a regular int64 min avoiding nats.
# note this could be optimizing relying on the actual value of NAT
# but as NumPy doesn't rely on this, this seems more resilient
in1, in2 = args
in1_not_nat = is_not_nat(builder, in1)
in2_not_nat = is_not_nat(builder, in2)
in1_le_in2 = builder.icmp(lc.ICMP_SLE, in1, in2)
res = builder.select(in1_le_in2, in1, in2)
res = builder.select(in1_not_nat, res, in2)
res = builder.select(in2_not_nat, res, in1)
return impl_ret_untracked(context, builder, sig.return_type, res)