carray 3.0.6 → 3.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/.yardopts +1 -1
- data/CHANGELOG.md +7 -1830
- data/README.md +62 -35
- data/carray.gemspec +16 -4
- data/docs/changelogs/CHANGELOG-3.0.md +2464 -0
- data/ext/ca_array_pool.c +4 -5
- data/ext/ca_axis_descriptor.h +45 -30
- data/ext/ca_axis_dispatch.c +7 -4
- data/ext/ca_axis_group.c +12 -15
- data/ext/ca_bincmp_dispatch.h +16 -38
- data/ext/ca_binop_dispatch.c +35 -0
- data/ext/ca_binop_dispatch.h +22 -46
- data/ext/ca_categorical_iterator.c +36 -34
- data/ext/ca_compare.c +0 -1
- data/ext/ca_compare.h +8 -1
- data/ext/ca_composite_dispatch.h +6 -0
- data/ext/ca_for_buffer.h +6 -1
- data/ext/ca_group_iter.c +17 -18
- data/ext/ca_iter_state_fields.h +196 -0
- data/ext/ca_iter_substrate.h +84 -103
- data/ext/ca_kernel_iterator.c +125 -89
- data/ext/ca_kernel_iterator.h +229 -1504
- data/ext/ca_kernel_iterator_internal.h +1167 -0
- data/ext/ca_moncmp_dispatch.h +17 -24
- data/ext/ca_monop_dispatch.h +25 -74
- data/ext/ca_obj_array.c +92 -165
- data/ext/ca_obj_bincmp.c +65 -48
- data/ext/ca_obj_binop.c +74 -49
- data/ext/ca_obj_bitarray.c +7 -21
- data/ext/ca_obj_bitfield.c +93 -26
- data/ext/ca_obj_block.c +11 -12
- data/ext/ca_obj_byte_swap.c +34 -34
- data/ext/ca_obj_const_string.c +464 -304
- data/ext/ca_obj_face.c +167 -121
- data/ext/ca_obj_face.h +32 -170
- data/ext/ca_obj_fake.c +127 -34
- data/ext/ca_obj_farray.c +13 -9
- data/ext/ca_obj_field.c +17 -13
- data/ext/ca_obj_fixlen_string.c +57 -34
- data/ext/ca_obj_grid.c +149 -113
- data/ext/ca_obj_meld.c +28 -26
- data/ext/ca_obj_moncmp.c +10 -21
- data/ext/ca_obj_monop.c +88 -73
- data/ext/ca_obj_object.c +60 -46
- data/ext/ca_obj_record.c +74 -22
- data/ext/ca_obj_reduce.c +10 -14
- data/ext/ca_obj_refer.c +146 -39
- data/ext/ca_obj_remap.c +11 -29
- data/ext/ca_obj_repeat.c +41 -23
- data/ext/ca_obj_roll.c +20 -28
- data/ext/ca_obj_select.c +17 -27
- data/ext/ca_obj_select_axis.c +35 -42
- data/ext/ca_obj_shift.c +22 -42
- data/ext/ca_obj_source.c +1 -5
- data/ext/ca_obj_stack.c +33 -29
- data/ext/ca_obj_stride.c +99 -62
- data/ext/ca_obj_string.c +8 -20
- data/ext/ca_obj_tile.c +14 -16
- data/ext/ca_obj_time.c +12 -23
- data/ext/ca_obj_timedelta.c +12 -23
- data/ext/ca_obj_transpose.c +8 -10
- data/ext/ca_obj_triop.c +48 -49
- data/ext/ca_obj_window.c +87 -99
- data/ext/ca_op_cmplx64.h +4 -0
- data/ext/ca_op_ipower.c +97 -27
- data/ext/ca_op_powi.h +9 -28
- data/ext/ca_pow5_table.h +2 -1
- data/ext/ca_rng_normal.h +4 -0
- data/ext/ca_rng_xoshiro256pp.h +3 -0
- data/ext/ca_sort_kernels.h +23 -26
- data/ext/ca_sweep_engine.c +92 -26
- data/ext/ca_sweep_engine.h +22 -15
- data/ext/ca_transform_common.c +3 -8
- data/ext/ca_triop_dispatch.h +20 -32
- data/ext/caframe_csv_reader.c +12 -12
- data/ext/carray.h +300 -1009
- data/ext/carray_access.c +320 -207
- data/ext/carray_address_basis.c +128 -59
- data/ext/carray_attribute.c +81 -104
- data/ext/carray_bincount.c +12 -14
- data/ext/carray_broadcast.c +31 -27
- data/ext/carray_call_cfunc.c +174 -285
- data/ext/carray_call_cfunc.h +36 -28
- data/ext/carray_cast.c +1191 -351
- data/ext/carray_cast_func.rb +68 -8
- data/ext/carray_class.c +6 -11
- data/ext/carray_conversion.c +98 -39
- data/ext/carray_copy.c +52 -26
- data/ext/carray_core.c +286 -132
- data/ext/carray_count.c +53 -22
- data/ext/carray_element.c +49 -28
- data/ext/carray_factorize.c +64 -63
- data/ext/carray_generate.c +26 -19
- data/ext/carray_histogram.c +9 -11
- data/ext/carray_hold.c +7 -9
- data/ext/carray_index_classifier.c +127 -230
- data/ext/carray_index_classifier.h +11 -10
- data/ext/carray_integer_total.c +523 -0
- data/ext/carray_internal.h +1364 -156
- data/ext/carray_key_windows.c +3 -3
- data/ext/carray_lazy.c +37 -37
- data/ext/carray_loop.c +27 -23
- data/ext/carray_mask.c +235 -150
- data/ext/carray_math_kernel.h +23 -47
- data/ext/carray_mathfunc.c +10 -3
- data/ext/carray_median_percentile.c +106 -100
- data/ext/carray_memory_view.c +83 -69
- data/ext/carray_operator.c +490 -128
- data/ext/carray_order.c +163 -84
- data/ext/carray_partition.c +14 -16
- data/ext/carray_random.c +48 -42
- data/ext/carray_round.c +33 -5
- data/ext/carray_scatter.c +57 -39
- data/ext/carray_segment.c +42 -46
- data/ext/carray_slab.c +77 -51
- data/ext/carray_slab.h +44 -33
- data/ext/carray_sort.c +37 -31
- data/ext/carray_sort_kernel.c +36 -10
- data/ext/carray_struct.c +44 -34
- data/ext/carray_test.c +131 -81
- data/ext/carray_time_text.c +30 -31
- data/ext/carray_undef.c +50 -7
- data/ext/carray_utils.c +373 -139
- data/ext/extconf.rb +24 -17
- data/ext/mk_call_cfunc.rb +25 -31
- data/ext/mk_pow5_table.rb +2 -1
- data/ext/mkkernel.rb +819 -488
- data/ext/ruby_carray.c +47 -42
- data/ext/version.h +7 -5
- data/lib/carray/attribute.rb +19 -18
- data/lib/carray/autoload_carray.rb +20 -15
- data/lib/carray/autoload_method_extension.rb +64 -57
- data/lib/carray/axis_group.rb +272 -185
- data/lib/carray/basics.rb +298 -56
- data/lib/carray/bincount_nd.rb +41 -18
- data/lib/carray/block_iterator.rb +167 -119
- data/lib/carray/boolean_reduce.rb +7 -4
- data/lib/carray/categorical.rb +84 -59
- data/lib/carray/categorical_iterator.rb +90 -67
- data/lib/carray/complex.rb +61 -41
- data/lib/carray/conditional.rb +98 -23
- data/lib/carray/const_string.rb +112 -46
- data/lib/carray/core_extensions.rb +20 -69
- data/lib/carray/distance_limit.rb +7 -5
- data/lib/carray/factories.rb +269 -0
- data/lib/carray/fixlen_string.rb +95 -12
- data/lib/carray/frame/column_verbs.rb +76 -9
- data/lib/carray/frame/concat.rb +51 -7
- data/lib/carray/frame/convert.rb +2 -1
- data/lib/carray/frame/csv_parser.rb +20 -5
- data/lib/carray/frame/frame.rb +73 -31
- data/lib/carray/frame/group.rb +247 -187
- data/lib/carray/frame/io.rb +36 -12
- data/lib/carray/frame/join.rb +56 -17
- data/lib/carray/frame/records.rb +7 -5
- data/lib/carray/frame/reshape.rb +27 -7
- data/lib/carray/frame/sort.rb +10 -8
- data/lib/carray/frame/verbs.rb +217 -134
- data/lib/carray/frame/windows.rb +254 -19
- data/lib/carray/frame.rb +1 -2
- data/lib/carray/fuse_source.rb +28 -23
- data/lib/carray/fusion.rb +30 -30
- data/lib/carray/histogram.rb +35 -16
- data/lib/carray/inspect.rb +7 -7
- data/lib/carray/iterator.rb +104 -23
- data/lib/carray/key_windows.rb +93 -61
- data/lib/carray/lazy.rb +208 -111
- data/lib/carray/limits.rb +80 -0
- data/lib/carray/mask_gap_fill.rb +21 -16
- data/lib/carray/math.rb +87 -50
- data/lib/carray/meld_reduce.rb +30 -8
- data/lib/carray/methods/align_addr.rb +29 -17
- data/lib/carray/methods/bin.rb +8 -1
- data/lib/carray/methods/bincount.rb +4 -2
- data/lib/carray/methods/bit_string.rb +19 -31
- data/lib/carray/methods/broadcast.rb +8 -3
- data/lib/carray/methods/choose.rb +5 -5
- data/lib/carray/methods/composition.rb +73 -72
- data/lib/carray/methods/{index.rb → coordinate.rb} +13 -13
- data/lib/carray/methods/discovery_along.rb +3 -3
- data/lib/carray/methods/factorize.rb +2 -0
- data/lib/carray/methods/gather_nd.rb +106 -103
- data/lib/carray/methods/insert_block.rb +2 -2
- data/lib/carray/methods/is_between.rb +6 -4
- data/lib/carray/methods/is_in.rb +183 -143
- data/lib/carray/methods/join.rb +2 -1
- data/lib/carray/methods/lag.rb +6 -2
- data/lib/carray/methods/locate_addr.rb +157 -94
- data/lib/carray/methods/mask_duplicates.rb +1 -1
- data/lib/carray/methods/meshgrid.rb +28 -16
- data/lib/carray/methods/mode.rb +67 -67
- data/lib/carray/methods/nunique.rb +9 -7
- data/lib/carray/methods/repeat.rb +23 -13
- data/lib/carray/methods/resize.rb +1 -1
- data/lib/carray/methods/snap.rb +24 -20
- data/lib/carray/methods/string_format.rb +2 -2
- data/lib/carray/methods/unique.rb +4 -2
- data/lib/carray/methods/value_counts.rb +1 -1
- data/lib/carray/mkmf.rb +25 -28
- data/lib/carray/percentile_arguments.rb +82 -0
- data/lib/carray/record_order.rb +50 -20
- data/lib/carray/rng.rb +1 -0
- data/lib/carray/runtime.rb +195 -17
- data/lib/carray/segment_iterator.rb +262 -137
- data/lib/carray/serialize.rb +635 -566
- data/lib/carray/slab_iterator.rb +114 -114
- data/lib/carray/stack.rb +14 -15
- data/lib/carray/string.rb +16 -22
- data/lib/carray/string_operation_extension.rb +97 -52
- data/lib/carray/struct.rb +122 -164
- data/lib/carray/struct_builder.rb +247 -73
- data/lib/carray/time.rb +663 -365
- data/lib/carray/window_iterator.rb +232 -155
- data/lib/carray.rb +6 -11
- data/yard-stubs/ca_obj_array.rb +20 -135
- data/yard-stubs/ca_obj_bitarray.rb +1 -8
- data/yard-stubs/ca_obj_bitfield.rb +6 -2
- data/yard-stubs/ca_obj_byte_swap.rb +21 -7
- data/yard-stubs/ca_obj_fake.rb +3 -1
- data/yard-stubs/ca_obj_farray.rb +1 -1
- data/yard-stubs/ca_obj_field.rb +8 -6
- data/yard-stubs/ca_obj_refer.rb +10 -5
- data/yard-stubs/ca_obj_roll.rb +7 -6
- data/yard-stubs/ca_obj_shift.rb +2 -3
- data/yard-stubs/ca_obj_stride.rb +5 -7
- data/yard-stubs/ca_obj_tile.rb +1 -1
- data/yard-stubs/ca_obj_time.rb +43 -0
- data/yard-stubs/ca_obj_transpose.rb +1 -1
- data/yard-stubs/ca_obj_window.rb +28 -16
- data/yard-stubs/carray_access.rb +55 -6
- data/yard-stubs/carray_attribute.rb +26 -38
- data/yard-stubs/carray_cast.rb +121 -162
- data/yard-stubs/carray_class.rb +3 -4
- data/yard-stubs/carray_conversion.rb +6 -7
- data/yard-stubs/carray_copy.rb +2 -1
- data/yard-stubs/carray_count.rb +10 -5
- data/yard-stubs/carray_element.rb +4 -3
- data/yard-stubs/carray_limits.rb +100 -0
- data/yard-stubs/carray_loop.rb +0 -20
- data/yard-stubs/carray_mask.rb +59 -41
- data/yard-stubs/carray_math.rb +162 -100
- data/yard-stubs/carray_mathfunc.rb +415 -3
- data/yard-stubs/carray_median_percentile.rb +29 -11
- data/yard-stubs/carray_memory_view.rb +2 -2
- data/yard-stubs/carray_order.rb +72 -33
- data/yard-stubs/carray_random.rb +6 -6
- data/yard-stubs/carray_reduce.rb +353 -0
- data/yard-stubs/carray_rng.rb +92 -0
- data/yard-stubs/carray_scatter.rb +12 -3
- data/yard-stubs/carray_slab.rb +7 -4
- data/yard-stubs/carray_sort.rb +7 -6
- data/yard-stubs/carray_test.rb +18 -5
- data/yard-stubs/carray_undef.rb +44 -5
- data/yard-stubs/ruby_carray.rb +75 -40
- metadata +63 -25
- data/CHANGELOG.v1.md +0 -126
- data/ext/carray_build_flags.h +0 -3
- data/ext/carray_config.h +0 -28
- data/ext/carray_data_type.c +0 -90
- data/ext/carray_kernels_bincmp.c +0 -6457
- data/ext/carray_kernels_binop.c +0 -31534
- data/ext/carray_kernels_init.c +0 -1734
- data/ext/carray_kernels_moncmp.c +0 -2104
- data/ext/carray_kernels_monop.c +0 -25387
- data/ext/carray_kernels_reduce_aggregate.c +0 -27545
- data/ext/carray_kernels_reduce_boolean.c +0 -1924
- data/ext/carray_kernels_reduce_cumulative.c +0 -11646
- data/ext/carray_kernels_reduce_extreme.c +0 -21631
- data/ext/carray_kernels_reduce_variance.c +0 -5981
- data/ext/carray_kernels_scan.c +0 -4302
- data/ext/carray_kernels_search.c +0 -37776
- data/ext/carray_kernels_sort.c +0 -10814
- data/ext/carray_kernels_triop.c +0 -1864
- data/lib/carray/construct.rb +0 -360
- data/lib/carray/data_type_extension.rb +0 -261
- data/lib/carray/data_type_limits.rb +0 -91
data/lib/carray/time.rb
CHANGED
|
@@ -27,12 +27,14 @@
|
|
|
27
27
|
# Civil-date arithmetic on int64 CArrays: the calendar kernel every time
|
|
28
28
|
# grid is built on (Howard Hinnant's days<->civil algorithms, vectorized).
|
|
29
29
|
# Internal to the time surface: nothing here is part of the public API.
|
|
30
|
+
#
|
|
30
31
|
# Loaded with the file, not inside the methods: a require is a lock that
|
|
31
32
|
# Ractors running these methods at the same time can deadlock on.
|
|
32
33
|
require 'date'
|
|
33
34
|
require 'time'
|
|
34
35
|
|
|
35
|
-
|
|
36
|
+
# @api private
|
|
37
|
+
module CATime::Civil
|
|
36
38
|
module_function
|
|
37
39
|
|
|
38
40
|
# Vectorized Howard Hinnant civil-date algebra (proleptic Gregorian,
|
|
@@ -69,7 +71,7 @@ module CATimeCivil
|
|
|
69
71
|
# all the time in a bulk column parse used to go (170k rows: 3.5 s, of
|
|
70
72
|
# which 3.1 s was this). Ruby's Integer `/` floors, as CArray's does,
|
|
71
73
|
# so the two forms read alike and agree everywhere.
|
|
72
|
-
def days_from_civil_1
|
|
74
|
+
def days_from_civil_1(y, m, d)
|
|
73
75
|
y -= 1 if m <= 2
|
|
74
76
|
era = y / 400
|
|
75
77
|
yoe = y - era * 400
|
|
@@ -83,7 +85,8 @@ end
|
|
|
83
85
|
# is finer, what a difference is measured in) and how a tick count moves
|
|
84
86
|
# between them -- by scaling for a duration, on the calendar for an instant.
|
|
85
87
|
# Internal to the time surface: nothing here is part of the public API.
|
|
86
|
-
|
|
88
|
+
# @api private
|
|
89
|
+
module CATime::UnitAlgebra
|
|
87
90
|
# seconds per base unit (Rational)
|
|
88
91
|
FIXED = {
|
|
89
92
|
W: 604800r, D: 86400r, h: 3600r, m: 60r, s: 1r,
|
|
@@ -100,6 +103,37 @@ module CATimeUnitAlgebra
|
|
|
100
103
|
|
|
101
104
|
module_function
|
|
102
105
|
|
|
106
|
+
# The method the user called, for a refusal raised from a store hook
|
|
107
|
+
# (element_to_storage). The hook runs under the store, and a store may run
|
|
108
|
+
# under another CArray method (scatter_replace! stores through []=), so the
|
|
109
|
+
# name is that of the outermost frame in the run of CArray methods above
|
|
110
|
+
# the hook. The stub that loads an autoloaded method on its first call
|
|
111
|
+
# is in the run but is not a method the user called, so it is passed over.
|
|
112
|
+
#
|
|
113
|
+
# A frame is in the run when its label names a CArray class (Ruby 3.4 and
|
|
114
|
+
# later write labels as "CArray#[]="), when it runs carray's own Ruby code,
|
|
115
|
+
# or when it is a method written in C: before Ruby 3.4 a label has no class
|
|
116
|
+
# name, and a C method's frame has the path and line of its caller.
|
|
117
|
+
def method_called_by_user
|
|
118
|
+
frames = caller_locations(2)
|
|
119
|
+
run = []
|
|
120
|
+
frames.each_with_index do |f, i|
|
|
121
|
+
outer = frames[i + 1]
|
|
122
|
+
ours = f.label.to_s.match?(/\ACA\w*[#.]/) ||
|
|
123
|
+
f.path.to_s.start_with?(CARRAY_LIB_DIR) ||
|
|
124
|
+
(outer && f.path == outer.path && f.lineno == outer.lineno)
|
|
125
|
+
break unless ours
|
|
126
|
+
run << f unless f.path == AUTOLOAD_STUB_PATH
|
|
127
|
+
end
|
|
128
|
+
(run.last || frames.first).base_label
|
|
129
|
+
end
|
|
130
|
+
|
|
131
|
+
# The directory that holds carray's Ruby code.
|
|
132
|
+
CARRAY_LIB_DIR = (File.expand_path("..", __dir__) + File::SEPARATOR).freeze
|
|
133
|
+
|
|
134
|
+
# The file whose stub loads an autoloaded method on its first call.
|
|
135
|
+
AUTOLOAD_STUB_PATH = File.join(__dir__, "autoload_method_extension.rb").freeze
|
|
136
|
+
|
|
103
137
|
# An operand of a time or duration operator, taken through its to_ca when
|
|
104
138
|
# it exports MemoryView and defines one (an Apache Arrow duration array, given
|
|
105
139
|
# a bridge that adds to_ca), as the core binary operators take it; other
|
|
@@ -137,11 +171,11 @@ module CATimeUnitAlgebra
|
|
|
137
171
|
nil
|
|
138
172
|
end
|
|
139
173
|
return CATimedelta.wrap(CScalar.int64 { res.count }, unit: res.base) if res
|
|
140
|
-
scalar_instant(
|
|
174
|
+
scalar_instant(CATime::Literal.exact_instant(x), unit)
|
|
141
175
|
when CATime::Element
|
|
142
176
|
CATime.wrap(CScalar.int64 { x.value }, unit: x.unit)
|
|
143
177
|
when Time, Date
|
|
144
|
-
scalar_instant(
|
|
178
|
+
scalar_instant(CATime::Literal.exact_instant(x), unit)
|
|
145
179
|
end
|
|
146
180
|
end
|
|
147
181
|
|
|
@@ -437,15 +471,15 @@ module CATimeUnitAlgebra
|
|
|
437
471
|
n = rd.numerator
|
|
438
472
|
days = storage / n # floors toward the past
|
|
439
473
|
rest = storage - days * n # the time of day, in ticks
|
|
440
|
-
y, m, d =
|
|
474
|
+
y, m, d = CATime::Civil.civil_from_days(days)
|
|
441
475
|
ord = y * 12 + (m - 1) + months # month ordinal after the move
|
|
442
476
|
ny = ord / 12
|
|
443
477
|
nm = ord - ny * 12 + 1
|
|
444
478
|
ones = CArray.int64(*storage.shape) { 1 }
|
|
445
|
-
head =
|
|
479
|
+
head = CATime::Civil.days_from_civil(ny, nm, ones)
|
|
446
480
|
ord1 = ord + 1
|
|
447
481
|
ny1 = ord1 / 12
|
|
448
|
-
last =
|
|
482
|
+
last = CATime::Civil.days_from_civil(ny1, ord1 - ny1 * 12 + 1, ones) - head
|
|
449
483
|
widen(head + d.pmin(last) - 1, n) + rest
|
|
450
484
|
end
|
|
451
485
|
|
|
@@ -463,9 +497,9 @@ module CATimeUnitAlgebra
|
|
|
463
497
|
abs = storage * from.count + 1970 * 12 # absolute month ordinal
|
|
464
498
|
y = abs / 12
|
|
465
499
|
m = abs - y * 12 + 1
|
|
466
|
-
|
|
500
|
+
CATime::Civil.days_from_civil(y, m, ones)
|
|
467
501
|
else # :Y
|
|
468
|
-
|
|
502
|
+
CATime::Civil.days_from_civil(storage * from.count + 1970, ones, ones)
|
|
469
503
|
end
|
|
470
504
|
end
|
|
471
505
|
|
|
@@ -505,7 +539,7 @@ module CATimeUnitAlgebra
|
|
|
505
539
|
"cannot convert time #{from} to #{to} without loss " \
|
|
506
540
|
"(instant is not on a day boundary)"
|
|
507
541
|
end
|
|
508
|
-
y, m, d =
|
|
542
|
+
y, m, d = CATime::Civil.civil_from_days(days[:days])
|
|
509
543
|
on_boundary = d.eq(1)
|
|
510
544
|
on_boundary &= m.eq(1) if to.base == :Y
|
|
511
545
|
unless on_boundary.all
|
|
@@ -552,7 +586,7 @@ module CATimeUnitAlgebra
|
|
|
552
586
|
# month, whatever day and time it carries. The flooring counterpart of
|
|
553
587
|
# convert_instant_fixed_to_calendar.
|
|
554
588
|
def convert_instant_fixed_to_calendar_floor(storage, from, to)
|
|
555
|
-
y, m, =
|
|
589
|
+
y, m, = CATime::Civil.civil_from_days(fixed_storage_days(storage, from)[:days])
|
|
556
590
|
ord = to.base == :M ? (y * 12 + (m - 1) - 1970 * 12) : (y - 1970)
|
|
557
591
|
to.count > 1 ? ord / to.count : ord
|
|
558
592
|
end
|
|
@@ -563,11 +597,13 @@ end
|
|
|
563
597
|
# `strip_mask` and `[]=` read it. The reduction runs on the storage, so the
|
|
564
598
|
# fill goes in after the result is a Face again: a whole reduction keeps its
|
|
565
599
|
# axes for that, so the fill lands in a typed cell, and is then taken out.
|
|
566
|
-
|
|
600
|
+
# Internal to the time surface: nothing here is part of the public API.
|
|
601
|
+
# @api private
|
|
602
|
+
module CATime::SurfaceFill
|
|
567
603
|
|
|
568
604
|
private
|
|
569
605
|
|
|
570
|
-
def with_surface_fill
|
|
606
|
+
def with_surface_fill(opts)
|
|
571
607
|
fill = opts[:fill_value]
|
|
572
608
|
return yield(opts) if fill.nil? || fill.equal?(UNDEF)
|
|
573
609
|
o = opts.reject { |k, _| k == :fill_value }
|
|
@@ -583,14 +619,16 @@ module CATimeSurfaceFill
|
|
|
583
619
|
end
|
|
584
620
|
|
|
585
621
|
# `kept` is the shape to return to, or nil to keep the reduced axes.
|
|
586
|
-
def surface_fill
|
|
622
|
+
def surface_fill(r, fill, kept)
|
|
587
623
|
case r
|
|
588
624
|
when Array then r.map { |x| surface_fill(x, fill, kept) }
|
|
589
625
|
when CArray
|
|
590
626
|
r = r.strip_mask(fill) if r.has_mask?
|
|
591
|
-
|
|
592
|
-
|
|
593
|
-
|
|
627
|
+
# Axes in front of the reduced ones (percentile's p axes) stay.
|
|
628
|
+
lead = r.shape[0, r.ndim - ndim]
|
|
629
|
+
if kept.nil? then r
|
|
630
|
+
elsif kept.empty? && lead.empty? then r[*Array.new(r.ndim, 0)]
|
|
631
|
+
else r.reshape(*(lead + kept))
|
|
594
632
|
end
|
|
595
633
|
else r
|
|
596
634
|
end
|
|
@@ -603,7 +641,9 @@ end
|
|
|
603
641
|
# a string column) are each read as a value of this Face by to_comparable, an
|
|
604
642
|
# UNDEF or a masked cell kept masked. Nil for any other operand, which the Face's own
|
|
605
643
|
# to_comparable decides.
|
|
606
|
-
|
|
644
|
+
# Internal to the time surface: nothing here is part of the public API.
|
|
645
|
+
# @api private
|
|
646
|
+
module CATime::OperandValues
|
|
607
647
|
|
|
608
648
|
# The reference and the operand of a comparison (eq, <, count, search,
|
|
609
649
|
# linear_section, pmax ...) brought to one unit: the operand read in its
|
|
@@ -612,9 +652,15 @@ module CATimeOperandValues
|
|
|
612
652
|
# is_in and the set operations meet. A value that is not of this kind is
|
|
613
653
|
# refused as to_comparable refuses it. An assignment, which has to land on
|
|
614
654
|
# the reference's grid, reconciles with to_comparable instead.
|
|
655
|
+
#
|
|
656
|
+
# With `lookup: true` (is_in, locate_addr and the joins built on them) a
|
|
657
|
+
# value no cell of this kind can be (foreign_value?: a Float, a Symbol,
|
|
658
|
+
# nil) is a masked cell, which matches nothing, as such a value matches
|
|
659
|
+
# nothing in a numeric array. An Integer and a String that does not read
|
|
660
|
+
# are refused all the same.
|
|
615
661
|
# @api private
|
|
616
|
-
def comparable_pair
|
|
617
|
-
lifted = lift_operand(operand)
|
|
662
|
+
private def comparable_pair(operand, lookup: false)
|
|
663
|
+
lifted = lift_operand(operand, lookup)
|
|
618
664
|
return [self, lifted] if lifted.unit == unit
|
|
619
665
|
narrowed = lifted.to_unit(unit)
|
|
620
666
|
if narrowed.to_unit(lifted.unit).parent.eq(lifted.parent).all
|
|
@@ -623,39 +669,90 @@ module CATimeOperandValues
|
|
|
623
669
|
in_common_unit_with(lifted)
|
|
624
670
|
end
|
|
625
671
|
|
|
672
|
+
# @overload pmax(other)
|
|
673
|
+
# The later time, or the longer duration, of each cell and `other`, cell
|
|
674
|
+
# by cell: `other` is an array of the same kind and shape, or one value
|
|
675
|
+
# (for a time a {CATime::Element}, a `Time`, a `Date`, `"2024-01-02"`; for
|
|
676
|
+
# a duration a {CATimedelta::Element}, `"3 days"`) for every cell. The
|
|
677
|
+
# two meet as a comparison does, so the result is in `self`'s unit when
|
|
678
|
+
# `other` sits on its grid and in the finer unit the two share otherwise.
|
|
679
|
+
# A masked cell on either side gives a masked cell.
|
|
680
|
+
# @param other [CATime, CATimedelta, CATime::Element, CATimedelta::Element, Time, Date, String]
|
|
681
|
+
# @return [CATime, CATimedelta]
|
|
682
|
+
# @raise [TypeError] when `other` is not of this kind.
|
|
683
|
+
def pmax(other)
|
|
684
|
+
pick_by_order(__callee__, other) { |a, b| a < b }
|
|
685
|
+
end
|
|
686
|
+
|
|
687
|
+
# @overload pmin(other)
|
|
688
|
+
# The earlier time, or the shorter duration; see {#pmax}.
|
|
689
|
+
# @param other [CATime, CATimedelta, CATime::Element, CATimedelta::Element, Time, Date, String]
|
|
690
|
+
# @return [CATime, CATimedelta]
|
|
691
|
+
# @raise [TypeError] when `other` is not of this kind.
|
|
692
|
+
def pmin(other)
|
|
693
|
+
pick_by_order(__callee__, other) { |a, b| a > b }
|
|
694
|
+
end
|
|
695
|
+
|
|
696
|
+
# A time or a duration has no NaN, so maximum / minimum (which let a NaN
|
|
697
|
+
# win) are pmax / pmin.
|
|
698
|
+
alias_method :maximum, :pmax
|
|
699
|
+
alias_method :minimum, :pmin
|
|
700
|
+
|
|
626
701
|
private
|
|
627
702
|
|
|
703
|
+
# `other` where the block says self's cell loses to it, else self's cell.
|
|
704
|
+
# UNDEF is no value to compare with; a masked operand is a masked array.
|
|
705
|
+
def pick_by_order(verb, other)
|
|
706
|
+
raise ArgumentError, "#{verb}: comparison of #{self.class} with UNDEF failed" if UNDEF.equal?(other)
|
|
707
|
+
a, b = comparable_pair(other)
|
|
708
|
+
b = b[0] unless other.is_a?(CArray) || other.is_a?(Array)
|
|
709
|
+
yield(a, b).then_else(b, a)
|
|
710
|
+
end
|
|
711
|
+
|
|
712
|
+
# Both branches of a selection in the unit a comparison meets them in, so
|
|
713
|
+
# the finer unit wins whichever branch it is.
|
|
714
|
+
def branches_in_common(x, y)
|
|
715
|
+
other, base = y.is_a?(CArray) && !y.scalar? ? [x, y] : [y, x]
|
|
716
|
+
return [x, y] if other.nil? || UNDEF.equal?(other)
|
|
717
|
+
a, b = base.send(:comparable_pair, other)
|
|
718
|
+
b = b[0] unless other.is_a?(CArray) && !other.scalar?
|
|
719
|
+
base.equal?(y) ? [b, a] : [a, b]
|
|
720
|
+
end
|
|
721
|
+
|
|
628
722
|
# The operand as an array of this kind in its own unit: one value read as
|
|
629
723
|
# it is written, an Array or an array of another kind read value by value
|
|
630
724
|
# in the unit they share, an UNDEF or a masked cell kept masked.
|
|
631
|
-
def lift_operand
|
|
725
|
+
def lift_operand(operand, lookup = false)
|
|
632
726
|
return operand if operand.is_a?(self.class)
|
|
633
727
|
case operand
|
|
634
728
|
when Array
|
|
635
729
|
# A nested Array keeps its shape; reading it row by row would land
|
|
636
730
|
# each row as one value.
|
|
637
|
-
return lift_operand(CA_OBJECT(operand)) if operand.any?(Array)
|
|
638
|
-
lifted = operand.map { |v|
|
|
731
|
+
return lift_operand(CA_OBJECT(operand), lookup) if operand.any?(Array)
|
|
732
|
+
lifted = operand.map { |v|
|
|
733
|
+
UNDEF.equal?(v) || (lookup && foreign_value?(v)) ? nil : lift_operand(v, lookup)
|
|
734
|
+
}
|
|
639
735
|
units = lifted.compact.map(&:unit)
|
|
640
736
|
u = units.empty? ? unit : units.reduce { |a, b| common_unit(a, b) }
|
|
641
737
|
self.class.wrap(CA_INT64(lifted.map { |x| x ? x.to_unit(u).parent[0] : UNDEF }),
|
|
642
738
|
unit: u)
|
|
643
739
|
when CArray
|
|
644
|
-
lift_operand(operand.to_a.flatten).reshape(*operand.shape)
|
|
740
|
+
lift_operand(operand.to_a.flatten, lookup).reshape(*operand.shape)
|
|
645
741
|
else
|
|
742
|
+
return lift_operand([operand], true) if lookup && foreign_value?(operand)
|
|
646
743
|
lift_value(operand) || to_comparable(operand)
|
|
647
744
|
end
|
|
648
745
|
end
|
|
649
746
|
|
|
650
|
-
def reconcile_values
|
|
747
|
+
def reconcile_values(operand)
|
|
651
748
|
to_comparable(lift_operand(operand))
|
|
652
749
|
end
|
|
653
750
|
|
|
654
751
|
end
|
|
655
752
|
|
|
656
753
|
class CATime
|
|
657
|
-
include
|
|
658
|
-
include
|
|
754
|
+
include CATime::SurfaceFill
|
|
755
|
+
include CATime::OperandValues
|
|
659
756
|
# @overload new(*shape, unit: :ns)
|
|
660
757
|
# Allocates a new int64 storage CArray of the given `shape` and
|
|
661
758
|
# wraps it as a CATime Face with the given `unit`. The
|
|
@@ -739,13 +836,19 @@ class CATime
|
|
|
739
836
|
# @return [String]
|
|
740
837
|
def to_s
|
|
741
838
|
case unit.base
|
|
742
|
-
when :Y then to_date.strftime("%Y")
|
|
743
|
-
when :M then to_date.strftime("%Y-%m")
|
|
744
|
-
when :W, :D then to_date.strftime("%Y-%m-%d")
|
|
745
|
-
else to_time.iso8601(fractional_second_digits) # :h .. :as (time shown)
|
|
839
|
+
when :Y then signed_past_four_digits(to_date.strftime("%Y"))
|
|
840
|
+
when :M then signed_past_four_digits(to_date.strftime("%Y-%m"))
|
|
841
|
+
when :W, :D then signed_past_four_digits(to_date.strftime("%Y-%m-%d"))
|
|
842
|
+
else signed_past_four_digits(to_time.iso8601(fractional_second_digits)) # :h .. :as (time shown)
|
|
746
843
|
end
|
|
747
844
|
end
|
|
748
845
|
|
|
846
|
+
# A year past four digits carries its sign, which is what tells it from a
|
|
847
|
+
# date written without separators when the text is read back.
|
|
848
|
+
private def signed_past_four_digits(text)
|
|
849
|
+
text.start_with?("-") || to_date.year < 10000 ? text : "+#{text}"
|
|
850
|
+
end
|
|
851
|
+
|
|
749
852
|
# @return [String] the instant plus its storage unit.
|
|
750
853
|
def inspect
|
|
751
854
|
tag = unit.count == 1 ? "#{value}#{unit.base}" : "#{value} @ #{unit}"
|
|
@@ -761,7 +864,7 @@ class CATime
|
|
|
761
864
|
def <=>(other)
|
|
762
865
|
case other
|
|
763
866
|
when Element
|
|
764
|
-
u =
|
|
867
|
+
u = CATime::UnitAlgebra.diff_unit(unit, other.unit)
|
|
765
868
|
instant_in(u) <=> other.instant_in(u)
|
|
766
869
|
when Time
|
|
767
870
|
to_time <=> other.getutc
|
|
@@ -770,7 +873,7 @@ class CATime
|
|
|
770
873
|
when (defined?(Date) ? Date : nil)
|
|
771
874
|
# a Date names the midnight (UTC) that begins it, as it does when an
|
|
772
875
|
# array is compared with it
|
|
773
|
-
to_time.to_r <=>
|
|
876
|
+
to_time.to_r <=> CATime::Literal.epoch_seconds(other)
|
|
774
877
|
end
|
|
775
878
|
rescue ArgumentError
|
|
776
879
|
nil
|
|
@@ -789,9 +892,9 @@ class CATime
|
|
|
789
892
|
end
|
|
790
893
|
|
|
791
894
|
# Marshal carries the tick and its resolution.
|
|
792
|
-
def marshal_dump = [value, unit.count, unit.base]
|
|
895
|
+
private def marshal_dump = [value, unit.count, unit.base]
|
|
793
896
|
|
|
794
|
-
def marshal_load(data)
|
|
897
|
+
private def marshal_load(data)
|
|
795
898
|
v, count, base = data
|
|
796
899
|
initialize(v, CATime::Resolution.new(count, base))
|
|
797
900
|
end
|
|
@@ -810,7 +913,7 @@ class CATime
|
|
|
810
913
|
# @raise [TypeError, ArgumentError] on a non-timedelta operand, or a
|
|
811
914
|
# pair that has no answer on self's grid.
|
|
812
915
|
def +(td)
|
|
813
|
-
shifted_by_duration(td, 1)
|
|
916
|
+
shifted_by_duration(one_step_of(td), 1)
|
|
814
917
|
end
|
|
815
918
|
|
|
816
919
|
# @overload -(other)
|
|
@@ -823,14 +926,18 @@ class CATime
|
|
|
823
926
|
def -(other)
|
|
824
927
|
case other
|
|
825
928
|
when Element
|
|
826
|
-
u =
|
|
929
|
+
u = CATime::UnitAlgebra.diff_unit(unit, other.unit)
|
|
827
930
|
v = instant_in(u) - other.instant_in(u)
|
|
828
|
-
|
|
931
|
+
CATime::UnitAlgebra.check_term(v, "-")
|
|
829
932
|
CATimedelta::Element.new(v, u)
|
|
830
933
|
when CATimedelta::Element, CATimedelta
|
|
831
934
|
shifted_by_duration(other, -1)
|
|
832
935
|
when CATime
|
|
833
936
|
-(other - self)
|
|
937
|
+
when String, Time, Date
|
|
938
|
+
step = other.is_a?(String) ? one_step_of(other) : other
|
|
939
|
+
return shifted_by_duration(step, -1) unless step.equal?(other)
|
|
940
|
+
(CATime.wrap(CA_INT64([value]), unit: unit) - other)[0]
|
|
834
941
|
else
|
|
835
942
|
raise TypeError, "CATime::Element - #{other.class} is not allowed"
|
|
836
943
|
end
|
|
@@ -842,7 +949,7 @@ class CATime
|
|
|
842
949
|
# instant does not land on `to`'s grid).
|
|
843
950
|
def instant_in(to)
|
|
844
951
|
return value if to == unit
|
|
845
|
-
|
|
952
|
+
CATime::UnitAlgebra.convert_instant!(CA_INT64([value]), unit, to)[0]
|
|
846
953
|
end
|
|
847
954
|
|
|
848
955
|
private
|
|
@@ -851,6 +958,18 @@ class CATime
|
|
|
851
958
|
# the duration is converted into it (truncated toward zero when finer; a
|
|
852
959
|
# month or year duration on a fixed grid moves the date by months).
|
|
853
960
|
# Mirrors the array CATime +/-.
|
|
961
|
+
# A String naming a duration ("3 days") as one step of it; anything else
|
|
962
|
+
# as it is.
|
|
963
|
+
def one_step_of(x)
|
|
964
|
+
return x unless x.is_a?(String)
|
|
965
|
+
res = begin
|
|
966
|
+
CATime::Resolution.parse(x)
|
|
967
|
+
rescue ArgumentError
|
|
968
|
+
return x
|
|
969
|
+
end
|
|
970
|
+
CATimedelta::Element.new(1, res)
|
|
971
|
+
end
|
|
972
|
+
|
|
854
973
|
def shifted_by_duration(td, sign)
|
|
855
974
|
# An array of durations gives an array of instants, anchored here.
|
|
856
975
|
return sign > 0 ? td + self : (-td) + self if td.is_a?(CATimedelta)
|
|
@@ -859,16 +978,16 @@ class CATime
|
|
|
859
978
|
"CATime::Element #{sign > 0 ? '+' : '-'} #{td.class} is not " \
|
|
860
979
|
"allowed (add / subtract a CATimedelta)"
|
|
861
980
|
end
|
|
862
|
-
if
|
|
863
|
-
!
|
|
981
|
+
if CATime::UnitAlgebra::CALENDAR.key?(td.unit.base) &&
|
|
982
|
+
!CATime::UnitAlgebra::CALENDAR.key?(unit.base)
|
|
864
983
|
# months on a fixed grid move the date as Date#>> does, as the
|
|
865
984
|
# array does
|
|
866
|
-
months = sign * td.value *
|
|
867
|
-
return Element.new(
|
|
985
|
+
months = sign * td.value * CATime::UnitAlgebra.months_per_tick(td.unit)
|
|
986
|
+
return Element.new(CATime::UnitAlgebra.shift_months(CA_INT64([value]), unit, months)[0], unit)
|
|
868
987
|
end
|
|
869
|
-
tv =
|
|
988
|
+
tv = CATime::UnitAlgebra.duration_on_grid(CA_INT64([td.value]), td.unit, unit)[0]
|
|
870
989
|
v = value + sign * tv
|
|
871
|
-
|
|
990
|
+
CATime::UnitAlgebra.check_term(v, sign > 0 ? "+" : "-")
|
|
872
991
|
Element.new(v, unit)
|
|
873
992
|
end
|
|
874
993
|
|
|
@@ -897,8 +1016,8 @@ class CATime
|
|
|
897
1016
|
end
|
|
898
1017
|
|
|
899
1018
|
class CATimedelta
|
|
900
|
-
include
|
|
901
|
-
include
|
|
1019
|
+
include CATime::SurfaceFill
|
|
1020
|
+
include CATime::OperandValues
|
|
902
1021
|
# @overload new(*shape, unit: :ns)
|
|
903
1022
|
# Allocates a new int64 storage CArray of the given `shape` and
|
|
904
1023
|
# wraps it as a CATimedelta Face with the given `unit`.
|
|
@@ -936,7 +1055,7 @@ class CATimedelta
|
|
|
936
1055
|
# use {#to_seconds_approx} when an estimate is acceptable.
|
|
937
1056
|
# @raise [ArgumentError] for a :Y / :M unit.
|
|
938
1057
|
def to_seconds
|
|
939
|
-
unless
|
|
1058
|
+
unless CATime::UnitAlgebra::FIXED.key?(unit.base)
|
|
940
1059
|
raise ArgumentError,
|
|
941
1060
|
"CATimedelta #{unit} has no exact seconds (a :#{unit.base} is " \
|
|
942
1061
|
"calendar-variable); use to_seconds_approx for an estimate"
|
|
@@ -967,8 +1086,8 @@ class CATimedelta
|
|
|
967
1086
|
# false). No explicit `==` is defined; Comparable derives it.
|
|
968
1087
|
def <=>(other)
|
|
969
1088
|
return nil unless other.is_a?(Element)
|
|
970
|
-
return nil unless
|
|
971
|
-
u =
|
|
1089
|
+
return nil unless CATime::UnitAlgebra.same_group?(unit, other.unit)
|
|
1090
|
+
u = CATime::UnitAlgebra.finer(unit, other.unit)
|
|
972
1091
|
duration_in(u) <=> other.duration_in(u)
|
|
973
1092
|
rescue ArgumentError
|
|
974
1093
|
nil
|
|
@@ -984,9 +1103,9 @@ class CATimedelta
|
|
|
984
1103
|
end
|
|
985
1104
|
|
|
986
1105
|
# Marshal carries the tick and its resolution.
|
|
987
|
-
def marshal_dump = [value, unit.count, unit.base]
|
|
1106
|
+
private def marshal_dump = [value, unit.count, unit.base]
|
|
988
1107
|
|
|
989
|
-
def marshal_load(data)
|
|
1108
|
+
private def marshal_load(data)
|
|
990
1109
|
v, count, base = data
|
|
991
1110
|
initialize(v, CATime::Resolution.new(count, base))
|
|
992
1111
|
end
|
|
@@ -1005,6 +1124,20 @@ class CATimedelta
|
|
|
1005
1124
|
# @return [CATimedelta::Element]
|
|
1006
1125
|
def -(other) = combined_with_duration(other, -1)
|
|
1007
1126
|
|
|
1127
|
+
# @return [CATimedelta::Element] this duration with its sign reversed.
|
|
1128
|
+
def -@ = Element.new(-value, unit)
|
|
1129
|
+
|
|
1130
|
+
# @return [CATimedelta::Element] the magnitude of this duration.
|
|
1131
|
+
def abs = Element.new(value.abs, unit)
|
|
1132
|
+
|
|
1133
|
+
# @overload coerce(number)
|
|
1134
|
+
# Lets a duration stand on the right of a number, so `2 * d` is `d * 2`.
|
|
1135
|
+
# @param number [Numeric]
|
|
1136
|
+
# @return [Array]
|
|
1137
|
+
def coerce(number)
|
|
1138
|
+
[LeftFactor.new(number), self]
|
|
1139
|
+
end
|
|
1140
|
+
|
|
1008
1141
|
# @overload *(n)
|
|
1009
1142
|
# Returns this duration scaled by an Integer.
|
|
1010
1143
|
# @param n [Integer]
|
|
@@ -1012,7 +1145,7 @@ class CATimedelta
|
|
|
1012
1145
|
def *(n)
|
|
1013
1146
|
raise TypeError, "CATimedelta::Element * #{n.class} is not allowed (Integer only)" unless n.is_a?(Integer)
|
|
1014
1147
|
v = value * n
|
|
1015
|
-
|
|
1148
|
+
CATime::UnitAlgebra.check_term(v, "*")
|
|
1016
1149
|
Element.new(v, unit)
|
|
1017
1150
|
end
|
|
1018
1151
|
|
|
@@ -1029,7 +1162,7 @@ class CATimedelta
|
|
|
1029
1162
|
case other
|
|
1030
1163
|
when Integer then Element.new(Rational(value, other).truncate, unit)
|
|
1031
1164
|
when Element
|
|
1032
|
-
u =
|
|
1165
|
+
u = CATime::UnitAlgebra.finer(unit, other.unit)
|
|
1033
1166
|
Rational(duration_in(u), other.duration_in(u))
|
|
1034
1167
|
else
|
|
1035
1168
|
raise TypeError, "CATimedelta::Element / #{other.class} is not allowed"
|
|
@@ -1042,7 +1175,7 @@ class CATimedelta
|
|
|
1042
1175
|
# cross-group or non-whole conversion).
|
|
1043
1176
|
def duration_in(to)
|
|
1044
1177
|
return value if to == unit
|
|
1045
|
-
|
|
1178
|
+
CATime::UnitAlgebra.convert_scale!(CA_INT64([value]), unit, to)[0]
|
|
1046
1179
|
end
|
|
1047
1180
|
|
|
1048
1181
|
private
|
|
@@ -1059,9 +1192,9 @@ class CATimedelta
|
|
|
1059
1192
|
raise TypeError,
|
|
1060
1193
|
"CATimedelta::Element #{sign > 0 ? '+' : '-'} #{other.class} is not allowed"
|
|
1061
1194
|
end
|
|
1062
|
-
u =
|
|
1195
|
+
u = CATime::UnitAlgebra.finer(unit, other.unit)
|
|
1063
1196
|
v = duration_in(u) + sign * other.duration_in(u)
|
|
1064
|
-
|
|
1197
|
+
CATime::UnitAlgebra.check_term(v, sign > 0 ? "+" : "-")
|
|
1065
1198
|
Element.new(v, u)
|
|
1066
1199
|
end
|
|
1067
1200
|
end
|
|
@@ -1089,7 +1222,9 @@ class CATime
|
|
|
1089
1222
|
# @overload ticks
|
|
1090
1223
|
# Returns the underlying int64 CArray of tick counts since the
|
|
1091
1224
|
# Unix epoch (1970-01-01 UTC) — the k-th tick of this array's
|
|
1092
|
-
# resolution (see §4 of docs/CATime.md).
|
|
1225
|
+
# resolution (see §4 of docs/CATime.md). It is a view of the
|
|
1226
|
+
# storage: writing it (`t.ticks[0] = 5`) writes the time. A cell of
|
|
1227
|
+
# the time itself takes an instant, not an Integer.
|
|
1093
1228
|
# @return [CArray]
|
|
1094
1229
|
def ticks
|
|
1095
1230
|
face_storage
|
|
@@ -1125,7 +1260,7 @@ class CATime
|
|
|
1125
1260
|
# The conversion hands back the storage itself when the unit does not
|
|
1126
1261
|
# change; the result is a new array whichever unit is asked for.
|
|
1127
1262
|
return face_storage.copy.time(unit: to) if to == self.unit
|
|
1128
|
-
|
|
1263
|
+
CATime::UnitAlgebra.convert_instant_floor(face_storage, self.unit, to).time(unit: to)
|
|
1129
1264
|
end
|
|
1130
1265
|
|
|
1131
1266
|
# @overload +(other)
|
|
@@ -1151,19 +1286,19 @@ class CATime
|
|
|
1151
1286
|
# @raise [ArgumentError] when the duration does not land on `self`'s grid.
|
|
1152
1287
|
# @raise [RangeError] when a result leaves the range of int64 ticks.
|
|
1153
1288
|
def +(other)
|
|
1154
|
-
given, other = other,
|
|
1155
|
-
other =
|
|
1156
|
-
|
|
1289
|
+
given, other = other, CATime::UnitAlgebra.operand(other)
|
|
1290
|
+
other = CATime::UnitAlgebra.step_operand(other) ||
|
|
1291
|
+
CATime::UnitAlgebra.scalar_operand(other, unit) || other
|
|
1157
1292
|
case other
|
|
1158
1293
|
when CATimedelta
|
|
1159
1294
|
return shifted_by_months(other, 1) if moved_by_months?(other)
|
|
1160
|
-
d =
|
|
1295
|
+
d = CATime::UnitAlgebra.duration_on_grid(other.parent, other.unit, unit)
|
|
1161
1296
|
d = d[0] if other.scalar?
|
|
1162
|
-
|
|
1297
|
+
CATime::UnitAlgebra.checked_add(face_storage, d).time(unit: unit)
|
|
1163
1298
|
when CATime
|
|
1164
1299
|
raise TypeError, "CATime + CATime is ill-defined"
|
|
1165
1300
|
else
|
|
1166
|
-
raise TypeError, "CATime + #{
|
|
1301
|
+
raise TypeError, "CATime + #{CATime::UnitAlgebra.operand_name(given)} is not allowed: add a duration " \
|
|
1167
1302
|
"(a CATimedelta, a CATimedelta::Element or a String such as \"3 days\")"
|
|
1168
1303
|
end
|
|
1169
1304
|
end
|
|
@@ -1181,81 +1316,50 @@ class CATime
|
|
|
1181
1316
|
# @return [CATime, CATimedelta]
|
|
1182
1317
|
# @raise [TypeError] on an unsupported operand.
|
|
1183
1318
|
def -(other)
|
|
1184
|
-
given, other = other,
|
|
1185
|
-
other =
|
|
1186
|
-
|
|
1319
|
+
given, other = other, CATime::UnitAlgebra.operand(other)
|
|
1320
|
+
other = CATime::UnitAlgebra.step_operand(other) ||
|
|
1321
|
+
CATime::UnitAlgebra.scalar_operand(other, unit) || other
|
|
1187
1322
|
case other
|
|
1188
1323
|
when CATimedelta
|
|
1189
1324
|
return shifted_by_months(other, -1) if moved_by_months?(other)
|
|
1190
|
-
d =
|
|
1325
|
+
d = CATime::UnitAlgebra.duration_on_grid(other.parent, other.unit, unit)
|
|
1191
1326
|
d = d[0] if other.scalar?
|
|
1192
|
-
|
|
1327
|
+
CATime::UnitAlgebra.checked_sub(face_storage, d).time(unit: unit)
|
|
1193
1328
|
when CATime
|
|
1194
|
-
u =
|
|
1195
|
-
a =
|
|
1196
|
-
b =
|
|
1329
|
+
u = CATime::UnitAlgebra.diff_unit(unit, other.unit)
|
|
1330
|
+
a = CATime::UnitAlgebra.convert_instant!(face_storage, unit, u)
|
|
1331
|
+
b = CATime::UnitAlgebra.convert_instant!(other.parent, other.unit, u)
|
|
1197
1332
|
b = b[0] if other.scalar?
|
|
1198
|
-
|
|
1333
|
+
CATime::UnitAlgebra.checked_sub(a, b).timedelta(unit: u)
|
|
1199
1334
|
else
|
|
1200
|
-
raise TypeError, "CATime - #{
|
|
1335
|
+
raise TypeError, "CATime - #{CATime::UnitAlgebra.operand_name(given)} is not allowed: subtract a duration " \
|
|
1201
1336
|
"(a CATimedelta, a CATimedelta::Element or a String such as \"3 days\") " \
|
|
1202
1337
|
"or a time (a CATime, a CATime::Element, a Time, a Date or a String)"
|
|
1203
1338
|
end
|
|
1204
1339
|
end
|
|
1205
1340
|
|
|
1341
|
+
# The word form of {#+}.
|
|
1342
|
+
# @return [CATime]
|
|
1343
|
+
alias_method :add, :+
|
|
1344
|
+
# The word form of {#-}.
|
|
1345
|
+
# @return [CATime, CATimedelta]
|
|
1346
|
+
alias_method :sub, :-
|
|
1347
|
+
|
|
1206
1348
|
# Whether a duration moves this time by calendar months: a month or year
|
|
1207
1349
|
# duration on a time whose grid is fixed-length. A time on a month or year
|
|
1208
1350
|
# grid adds it as months already, and a fixed duration on a calendar grid
|
|
1209
1351
|
# is refused by the scale conversion.
|
|
1210
|
-
private def moved_by_months?
|
|
1211
|
-
|
|
1212
|
-
!
|
|
1352
|
+
private def moved_by_months?(td)
|
|
1353
|
+
CATime::UnitAlgebra::CALENDAR.key?(td.unit.base) &&
|
|
1354
|
+
!CATime::UnitAlgebra::CALENDAR.key?(unit.base)
|
|
1213
1355
|
end
|
|
1214
1356
|
|
|
1215
1357
|
# self moved by the months of +td+ (+sign+ 1 to add, -1 to subtract), as
|
|
1216
1358
|
# Ruby's Date#>> moves a date: the day is kept unless the month is shorter.
|
|
1217
|
-
private def shifted_by_months
|
|
1218
|
-
months = td.parent.int64 * (sign *
|
|
1359
|
+
private def shifted_by_months(td, sign)
|
|
1360
|
+
months = td.parent.int64 * (sign * CATime::UnitAlgebra.months_per_tick(td.unit))
|
|
1219
1361
|
months = months[0] if td.scalar?
|
|
1220
|
-
|
|
1221
|
-
end
|
|
1222
|
-
|
|
1223
|
-
# @overload pmax(other)
|
|
1224
|
-
# The later of each cell and `other`, cell by cell: `other` is a time
|
|
1225
|
-
# array of the same shape, or one time (a {CATime::Element}, a `Time`, a
|
|
1226
|
-
# `Date`, `"2024-01-02"`) for every cell. The two meet as a comparison
|
|
1227
|
-
# does, so the result is in `self`'s unit when `other` sits on its grid
|
|
1228
|
-
# and in the finer unit the two share otherwise. A masked cell on either
|
|
1229
|
-
# side gives a masked cell.
|
|
1230
|
-
# @param other [CATime, CATime::Element, Time, Date, String]
|
|
1231
|
-
# @return [CATime]
|
|
1232
|
-
# @raise [TypeError] when `other` is not a time.
|
|
1233
|
-
def pmax (other)
|
|
1234
|
-
pick_by_order(other) { |a, b| a < b }
|
|
1235
|
-
end
|
|
1236
|
-
|
|
1237
|
-
# @overload pmin(other)
|
|
1238
|
-
# The earlier of each cell and `other`; see {#pmax}.
|
|
1239
|
-
# @param other [CATime, CATime::Element, Time, Date, String]
|
|
1240
|
-
# @return [CATime]
|
|
1241
|
-
# @raise [TypeError] when `other` is not a time.
|
|
1242
|
-
def pmin (other)
|
|
1243
|
-
pick_by_order(other) { |a, b| a > b }
|
|
1244
|
-
end
|
|
1245
|
-
|
|
1246
|
-
# A time has no NaN, so maximum / minimum (which let a NaN win) are pmax /
|
|
1247
|
-
# pmin, and fmax / fmin are their other names.
|
|
1248
|
-
alias_method :maximum, :pmax
|
|
1249
|
-
alias_method :minimum, :pmin
|
|
1250
|
-
alias_method :fmax, :pmax
|
|
1251
|
-
alias_method :fmin, :pmin
|
|
1252
|
-
|
|
1253
|
-
# `other` where the block says self's cell loses to it, else self's cell.
|
|
1254
|
-
private def pick_by_order (other)
|
|
1255
|
-
return copy.tap { |out| out[] = UNDEF } if UNDEF.equal?(other)
|
|
1256
|
-
a, b = comparable_pair(other)
|
|
1257
|
-
b = b[0] unless other.is_a?(CArray) || other.is_a?(Array)
|
|
1258
|
-
yield(a, b).then_else(b, a)
|
|
1362
|
+
CATime::UnitAlgebra.shift_months(face_storage, unit, months).time(unit: unit)
|
|
1259
1363
|
end
|
|
1260
1364
|
|
|
1261
1365
|
# Element-wise comparison (< <= > >= <=>) rides the core comparison Face
|
|
@@ -1307,8 +1411,9 @@ class CATime
|
|
|
1307
1411
|
# @overload percentile(*p, axis: nil, **opts)
|
|
1308
1412
|
# Returns the percentile instants on `self`'s unit, in the shapes the
|
|
1309
1413
|
# plain {CArray#percentile} uses: one `p` reduces to a single value
|
|
1310
|
-
# (an {Element}, or a {CATime} with `axis:`),
|
|
1311
|
-
# Array of those
|
|
1414
|
+
# (an {Element}, or a {CATime} with `axis:`), an Array of `p` gives an
|
|
1415
|
+
# Array of those, and several `p` (or a CArray of `p`) one {CATime}
|
|
1416
|
+
# with the `p` axes in front.
|
|
1312
1417
|
# @return [Element, CATime, Array<Element>, Array<CATime>]
|
|
1313
1418
|
def percentile(*args, **opts)
|
|
1314
1419
|
reduce_on_own_grid(:percentile, :time, args, opts)
|
|
@@ -1316,7 +1421,7 @@ class CATime
|
|
|
1316
1421
|
|
|
1317
1422
|
# @overload quantile(axis: nil, **opts)
|
|
1318
1423
|
# Returns the five quartile instants `[p0, p25, p50, p75, p100]` on
|
|
1319
|
-
# `self`'s unit (shorthand for `percentile(0, 25, 50, 75, 100)`).
|
|
1424
|
+
# `self`'s unit (shorthand for `percentile([0, 25, 50, 75, 100])`).
|
|
1320
1425
|
# @return [Array<Element>, Array<CATime>]
|
|
1321
1426
|
def quantile(*args, **opts)
|
|
1322
1427
|
reduce_on_own_grid(:quantile, :time, args, opts)
|
|
@@ -1424,37 +1529,50 @@ class CATime
|
|
|
1424
1529
|
# the joins on them) meets there, so it does not depend on which array is
|
|
1425
1530
|
# the receiver.
|
|
1426
1531
|
# @api private
|
|
1427
|
-
def in_common_unit_with
|
|
1532
|
+
private def in_common_unit_with(other)
|
|
1428
1533
|
return [self, other] if other.unit == unit
|
|
1429
|
-
u =
|
|
1534
|
+
u = CATime::UnitAlgebra.diff_unit(unit, other.unit)
|
|
1430
1535
|
[to_unit(u), other.to_unit(u)]
|
|
1431
1536
|
end
|
|
1432
1537
|
|
|
1433
|
-
private def common_unit
|
|
1434
|
-
|
|
1538
|
+
private def common_unit(a, b)
|
|
1539
|
+
CATime::UnitAlgebra.diff_unit(a, b)
|
|
1435
1540
|
end
|
|
1436
1541
|
|
|
1437
1542
|
# One instant as a length-1 time array in its own unit, or nil.
|
|
1438
|
-
private def lift_value
|
|
1543
|
+
private def lift_value(operand)
|
|
1439
1544
|
case operand
|
|
1440
1545
|
when CATime::Element
|
|
1441
1546
|
CATime.wrap(CA_INT64([operand.value]), unit: operand.unit)
|
|
1442
1547
|
when Time, String, Date
|
|
1443
|
-
|
|
1548
|
+
CATime::Literal.exact_instant(operand)
|
|
1444
1549
|
end
|
|
1445
1550
|
end
|
|
1446
1551
|
|
|
1447
|
-
|
|
1552
|
+
# A value no time cell can be: eq, ne, count and search find no cell for
|
|
1553
|
+
# it, as a numeric array finds none for nil. A time, a String (a time
|
|
1554
|
+
# written out, which is read and refused when it does not read) and an
|
|
1555
|
+
# Integer (the raw ticks, refused with a pointer to #ticks) are asked.
|
|
1556
|
+
private def foreign_value?(value)
|
|
1557
|
+
case value
|
|
1558
|
+
when CATime::Element, CATimedelta::Element, Time, Date, String, Integer, Array, CArray
|
|
1559
|
+
false
|
|
1560
|
+
else
|
|
1561
|
+
true
|
|
1562
|
+
end
|
|
1563
|
+
end
|
|
1564
|
+
|
|
1565
|
+
private def to_comparable(operand)
|
|
1448
1566
|
case operand
|
|
1449
1567
|
when CATime
|
|
1450
1568
|
return operand if operand.unit == unit
|
|
1451
|
-
|
|
1569
|
+
CATime::UnitAlgebra.convert_instant!(operand.parent, operand.unit, unit)
|
|
1452
1570
|
.time(unit: unit)
|
|
1453
1571
|
when CATime::Element
|
|
1454
1572
|
lifted = CATime.wrap(CA_INT64([operand.value]), unit: operand.unit)
|
|
1455
1573
|
to_comparable(lifted)
|
|
1456
1574
|
when Time, String, Date
|
|
1457
|
-
to_comparable(
|
|
1575
|
+
to_comparable(CATime::Literal.exact_instant(operand))
|
|
1458
1576
|
when Array, CArray
|
|
1459
1577
|
# A set of instants written one by one (is_in(["2024-01-02", ...])),
|
|
1460
1578
|
# or an array that is not a time array (an object column of Time
|
|
@@ -1474,16 +1592,18 @@ class CATime
|
|
|
1474
1592
|
# store round-trips with a fetch. A {Element} / `Time` / `Date` /
|
|
1475
1593
|
# `DateTime` / `String` is reconciled to self's unit via {#to_comparable}
|
|
1476
1594
|
# (same lossless discipline: a cross-group unit or a non-exact
|
|
1477
|
-
# finer->coarser cast raises).
|
|
1478
|
-
#
|
|
1479
|
-
# @param surface [Element, Time, Date, DateTime,
|
|
1595
|
+
# finer->coarser cast raises). An Integer is not an instant and
|
|
1596
|
+
# raises; the raw tick is written through {#ticks}.
|
|
1597
|
+
# @param surface [Element, Time, Date, DateTime, String]
|
|
1480
1598
|
# @return [Integer, Object] the storage-domain value, or `surface`
|
|
1481
1599
|
# unchanged for a pass-through type.
|
|
1482
1600
|
# @raise [TypeError, ArgumentError] on an unreconcilable surface / unit.
|
|
1483
|
-
def element_to_storage
|
|
1601
|
+
private def element_to_storage(surface)
|
|
1484
1602
|
case surface
|
|
1485
1603
|
when Integer
|
|
1486
|
-
|
|
1604
|
+
raise TypeError, "#{CATime::UnitAlgebra.method_called_by_user}: an Integer is not a time; write an instant (a Time, Date, String or CATime::Element), or the raw tick through #ticks"
|
|
1605
|
+
when nil
|
|
1606
|
+
raise TypeError, "#{CATime::UnitAlgebra.method_called_by_user}: nil is not a value of a time cell; assign UNDEF to mask the cell"
|
|
1487
1607
|
else
|
|
1488
1608
|
to_comparable(surface).parent[0]
|
|
1489
1609
|
end
|
|
@@ -1520,7 +1640,7 @@ class CATime
|
|
|
1520
1640
|
# @param axis [Integer, nil]
|
|
1521
1641
|
# @return [Element, CATime] an {Element} for a scalar `addr`, `nil` when
|
|
1522
1642
|
# that single answer is undetermined.
|
|
1523
|
-
def linear_fetch
|
|
1643
|
+
def linear_fetch(addr, **opts)
|
|
1524
1644
|
r = face_storage.float64.linear_fetch(addr, **opts)
|
|
1525
1645
|
case r
|
|
1526
1646
|
when CArray
|
|
@@ -1550,7 +1670,7 @@ class CATime
|
|
|
1550
1670
|
# CArray. Array-level counterpart to {Element#to_time}.
|
|
1551
1671
|
# @return [CArray]
|
|
1552
1672
|
def to_time
|
|
1553
|
-
if
|
|
1673
|
+
if CATime::UnitAlgebra::FIXED.key?(unit.base)
|
|
1554
1674
|
f = unit.tick_ratio # exact seconds / tick (Rational)
|
|
1555
1675
|
face_storage.convert(:object) {|v| Time.at(v * f, in: 'UTC')}
|
|
1556
1676
|
else # calendar: exact granule midnight
|
|
@@ -1588,7 +1708,7 @@ class CATime
|
|
|
1588
1708
|
case unit.base
|
|
1589
1709
|
when :Y then face_storage * unit.count + 1970
|
|
1590
1710
|
when :M then (face_storage * unit.count + 1970 * 12) / 12
|
|
1591
|
-
else
|
|
1711
|
+
else CATime::Civil.civil_from_days(days_since_epoch)[0]
|
|
1592
1712
|
end
|
|
1593
1713
|
end
|
|
1594
1714
|
|
|
@@ -1601,7 +1721,7 @@ class CATime
|
|
|
1601
1721
|
when :M
|
|
1602
1722
|
mo = face_storage * unit.count + 1970 * 12
|
|
1603
1723
|
mo % 12 + 1
|
|
1604
|
-
else
|
|
1724
|
+
else CATime::Civil.civil_from_days(days_since_epoch)[1]
|
|
1605
1725
|
end
|
|
1606
1726
|
end
|
|
1607
1727
|
|
|
@@ -1611,7 +1731,7 @@ class CATime
|
|
|
1611
1731
|
def day
|
|
1612
1732
|
case unit.base
|
|
1613
1733
|
when :Y, :M then face_storage * 0 + 1
|
|
1614
|
-
else
|
|
1734
|
+
else CATime::Civil.civil_from_days(days_since_epoch)[2]
|
|
1615
1735
|
end
|
|
1616
1736
|
end
|
|
1617
1737
|
|
|
@@ -1640,7 +1760,7 @@ class CATime
|
|
|
1640
1760
|
def iso_week
|
|
1641
1761
|
thursday, year = iso_thursday
|
|
1642
1762
|
ones = CArray.int64(*shape) { 1 }
|
|
1643
|
-
(thursday -
|
|
1763
|
+
(thursday - CATime::Civil.days_from_civil(year, ones, ones)) / 7 + 1
|
|
1644
1764
|
end
|
|
1645
1765
|
|
|
1646
1766
|
# The ISO 8601 year of each time's week (see {#iso_week}): the calendar
|
|
@@ -1657,7 +1777,7 @@ class CATime
|
|
|
1657
1777
|
days = days_since_epoch
|
|
1658
1778
|
iso_weekday = (days + 3) % 7 + 1 # Monday 1 .. Sunday 7; 1970-01-01 is a Thursday
|
|
1659
1779
|
thursday = days - iso_weekday + 4
|
|
1660
|
-
[thursday,
|
|
1780
|
+
[thursday, CATime::Civil.civil_from_days(thursday)[0]]
|
|
1661
1781
|
end
|
|
1662
1782
|
|
|
1663
1783
|
# Number of days in the month, 28..31 (January's for `:Y` storage, which
|
|
@@ -1665,12 +1785,12 @@ class CATime
|
|
|
1665
1785
|
# the month.
|
|
1666
1786
|
# @return [CArray] integer.
|
|
1667
1787
|
def days_in_month
|
|
1668
|
-
y, m, =
|
|
1788
|
+
y, m, = CATime::Civil.civil_from_days(days_since_epoch)
|
|
1669
1789
|
ones = CArray.int64(*shape) { 1 }
|
|
1670
1790
|
ord = y * 12 + m # the next month's ordinal (m is 1..12)
|
|
1671
1791
|
ny = ord / 12
|
|
1672
|
-
|
|
1673
|
-
|
|
1792
|
+
CATime::Civil.days_from_civil(ny, ord - ny * 12 + 1, ones) -
|
|
1793
|
+
CATime::Civil.days_from_civil(y, m, ones)
|
|
1674
1794
|
end
|
|
1675
1795
|
|
|
1676
1796
|
# Day of the week, Sunday = 0 .. Saturday = 6.
|
|
@@ -1685,9 +1805,9 @@ class CATime
|
|
|
1685
1805
|
# @return [CArray] integer.
|
|
1686
1806
|
def yday
|
|
1687
1807
|
d = days_since_epoch
|
|
1688
|
-
y =
|
|
1808
|
+
y = CATime::Civil.civil_from_days(d)[0]
|
|
1689
1809
|
ones = CArray.int64(*shape) { 1 }
|
|
1690
|
-
d -
|
|
1810
|
+
d - CATime::Civil.days_from_civil(y, ones, ones) + 1
|
|
1691
1811
|
end
|
|
1692
1812
|
# @!endgroup
|
|
1693
1813
|
|
|
@@ -1695,7 +1815,7 @@ class CATime
|
|
|
1695
1815
|
# Returns the Julian Day Number for each element.
|
|
1696
1816
|
# @return [CArray]
|
|
1697
1817
|
def jd
|
|
1698
|
-
to_time.convert(:
|
|
1818
|
+
to_time.convert(:int32) {|t| t.to_date.jd}
|
|
1699
1819
|
end
|
|
1700
1820
|
|
|
1701
1821
|
# @overload ajd
|
|
@@ -1703,7 +1823,7 @@ class CATime
|
|
|
1703
1823
|
# day) for each element.
|
|
1704
1824
|
# @return [CArray]
|
|
1705
1825
|
def ajd
|
|
1706
|
-
to_time.convert(:
|
|
1826
|
+
to_time.convert(:float64) {|t| t.to_datetime.ajd.to_f}
|
|
1707
1827
|
end
|
|
1708
1828
|
|
|
1709
1829
|
# @overload is_leap
|
|
@@ -1835,7 +1955,7 @@ class CATimedelta
|
|
|
1835
1955
|
to = CATime::Resolution.parse(unit)
|
|
1836
1956
|
# As CATime#to_unit: a new array even when the unit does not change.
|
|
1837
1957
|
return face_storage.copy.timedelta(unit: to) if to == self.unit
|
|
1838
|
-
|
|
1958
|
+
CATime::UnitAlgebra.convert_scale_trunc(face_storage, self.unit, to).timedelta(unit: to)
|
|
1839
1959
|
end
|
|
1840
1960
|
|
|
1841
1961
|
# @overload +(other)
|
|
@@ -1848,21 +1968,21 @@ class CATimedelta
|
|
|
1848
1968
|
# @return [CATimedelta, CATime]
|
|
1849
1969
|
# @raise [TypeError] on incompatible operands.
|
|
1850
1970
|
def +(other)
|
|
1851
|
-
given, other = other,
|
|
1852
|
-
other =
|
|
1971
|
+
given, other = other, CATime::UnitAlgebra.operand(other)
|
|
1972
|
+
other = CATime::UnitAlgebra.scalar_operand(other, unit) || other
|
|
1853
1973
|
case other
|
|
1854
1974
|
when CATimedelta
|
|
1855
1975
|
u = common_duration_unit(other.unit)
|
|
1856
|
-
a =
|
|
1857
|
-
b =
|
|
1976
|
+
a = CATime::UnitAlgebra.convert_scale!(face_storage, unit, u)
|
|
1977
|
+
b = CATime::UnitAlgebra.convert_scale!(other.parent, other.unit, u)
|
|
1858
1978
|
b = b[0] if other.scalar?
|
|
1859
|
-
|
|
1979
|
+
CATime::UnitAlgebra.checked_add(a, b).timedelta(unit: u)
|
|
1860
1980
|
when CATime
|
|
1861
1981
|
return other + self unless other.scalar? # commutative -> time's unit
|
|
1862
|
-
d =
|
|
1863
|
-
|
|
1982
|
+
d = CATime::UnitAlgebra.duration_on_grid(face_storage, unit, other.unit)
|
|
1983
|
+
CATime::UnitAlgebra.checked_add(d, other.parent[0]).time(unit: other.unit)
|
|
1864
1984
|
else
|
|
1865
|
-
raise TypeError, "CATimedelta + #{
|
|
1985
|
+
raise TypeError, "CATimedelta + #{CATime::UnitAlgebra.operand_name(given)} is not allowed"
|
|
1866
1986
|
end
|
|
1867
1987
|
end
|
|
1868
1988
|
|
|
@@ -1875,17 +1995,17 @@ class CATimedelta
|
|
|
1875
1995
|
# @return [CATimedelta]
|
|
1876
1996
|
# @raise [TypeError] on a non-timedelta operand.
|
|
1877
1997
|
def -(other)
|
|
1878
|
-
given, other = other,
|
|
1879
|
-
other =
|
|
1998
|
+
given, other = other, CATime::UnitAlgebra.operand(other)
|
|
1999
|
+
other = CATime::UnitAlgebra.scalar_operand(other, unit) || other
|
|
1880
2000
|
case other
|
|
1881
2001
|
when CATimedelta
|
|
1882
2002
|
u = common_duration_unit(other.unit)
|
|
1883
|
-
a =
|
|
1884
|
-
b =
|
|
2003
|
+
a = CATime::UnitAlgebra.convert_scale!(face_storage, unit, u)
|
|
2004
|
+
b = CATime::UnitAlgebra.convert_scale!(other.parent, other.unit, u)
|
|
1885
2005
|
b = b[0] if other.scalar?
|
|
1886
|
-
|
|
2006
|
+
CATime::UnitAlgebra.checked_sub(a, b).timedelta(unit: u)
|
|
1887
2007
|
else
|
|
1888
|
-
raise TypeError, "CATimedelta - #{
|
|
2008
|
+
raise TypeError, "CATimedelta - #{CATime::UnitAlgebra.operand_name(given)} is not allowed"
|
|
1889
2009
|
end
|
|
1890
2010
|
end
|
|
1891
2011
|
|
|
@@ -1911,8 +2031,8 @@ class CATimedelta
|
|
|
1911
2031
|
# @raise [TypeError] when `other` is not an Integer.
|
|
1912
2032
|
def *(other)
|
|
1913
2033
|
case other
|
|
1914
|
-
when Integer then
|
|
1915
|
-
else raise TypeError, "CATimedelta * #{
|
|
2034
|
+
when Integer then CATime::UnitAlgebra.checked_mul(face_storage, other).timedelta(unit: unit)
|
|
2035
|
+
else raise TypeError, "CATimedelta * #{CATime::UnitAlgebra.operand_name(other)} is not allowed (Integer only)"
|
|
1916
2036
|
end
|
|
1917
2037
|
end
|
|
1918
2038
|
|
|
@@ -1927,21 +2047,84 @@ class CATimedelta
|
|
|
1927
2047
|
# @return [CATimedelta, CArray]
|
|
1928
2048
|
# @raise [TypeError] on incompatible operands.
|
|
1929
2049
|
def /(other)
|
|
1930
|
-
given, other = other,
|
|
2050
|
+
given, other = other, CATime::UnitAlgebra.operand(other)
|
|
2051
|
+
other = CATime::UnitAlgebra.scalar_operand(other, unit) || other
|
|
1931
2052
|
case other
|
|
1932
2053
|
when Integer
|
|
1933
|
-
|
|
2054
|
+
CATime::UnitAlgebra.trunc_divide(face_storage, other).timedelta(unit: unit)
|
|
1934
2055
|
when CATimedelta
|
|
1935
2056
|
# A ratio of durations is a number, not a count of whole ticks: 90 s /
|
|
1936
2057
|
# 60 s is 1.5. Both are brought to one unit first, so the ratio does
|
|
1937
2058
|
# not depend on the units they are held in.
|
|
1938
2059
|
u = common_duration_unit(other.unit)
|
|
1939
|
-
|
|
1940
|
-
|
|
2060
|
+
CATime::UnitAlgebra.convert_scale!(face_storage, unit, u).to_type(:float64) /
|
|
2061
|
+
CATime::UnitAlgebra.convert_scale!(other.parent, other.unit, u)
|
|
1941
2062
|
else raise TypeError, "CATimedelta / #{given.class} is not allowed"
|
|
1942
2063
|
end
|
|
1943
2064
|
end
|
|
1944
2065
|
|
|
2066
|
+
# A number on the left of a duration: only `n * duration` has an answer.
|
|
2067
|
+
class LeftFactor
|
|
2068
|
+
def initialize(factor)
|
|
2069
|
+
@factor = factor
|
|
2070
|
+
end
|
|
2071
|
+
|
|
2072
|
+
def *(duration) = duration * @factor
|
|
2073
|
+
|
|
2074
|
+
def method_missing(op, duration, *)
|
|
2075
|
+
raise TypeError, "#{@factor.class} #{op} #{duration.class} is not allowed"
|
|
2076
|
+
end
|
|
2077
|
+
|
|
2078
|
+
def respond_to_missing?(*) = true
|
|
2079
|
+
end
|
|
2080
|
+
private_constant :LeftFactor
|
|
2081
|
+
|
|
2082
|
+
# @overload coerce(number)
|
|
2083
|
+
# Lets a duration stand on the right of a number, so `2 * td` is `td * 2`.
|
|
2084
|
+
# @param number [Numeric]
|
|
2085
|
+
# @return [Array]
|
|
2086
|
+
def coerce(number)
|
|
2087
|
+
number.is_a?(Numeric) ? [LeftFactor.new(number), self] : super
|
|
2088
|
+
end
|
|
2089
|
+
|
|
2090
|
+
# @overload to_seconds
|
|
2091
|
+
# Each duration in seconds, exactly, as {Element#to_seconds} answers.
|
|
2092
|
+
# @return [CArray] an object array of Rational.
|
|
2093
|
+
# @raise [ArgumentError] for a :Y / :M unit, which has no exact seconds.
|
|
2094
|
+
def to_seconds
|
|
2095
|
+
unless CATime::UnitAlgebra::FIXED.key?(unit.base)
|
|
2096
|
+
raise ArgumentError,
|
|
2097
|
+
"CATimedelta #{unit} has no exact seconds (a :#{unit.base} is " \
|
|
2098
|
+
"calendar-variable); use to_seconds_approx for an estimate"
|
|
2099
|
+
end
|
|
2100
|
+
ratio = unit.tick_ratio
|
|
2101
|
+
face_storage.convert(:object) { |v| Rational(v) * ratio }
|
|
2102
|
+
end
|
|
2103
|
+
|
|
2104
|
+
# @overload to_seconds_approx
|
|
2105
|
+
# Each duration in seconds by nominal lengths, as
|
|
2106
|
+
# {Element#to_seconds_approx} answers.
|
|
2107
|
+
# @return [CArray] float64.
|
|
2108
|
+
def to_seconds_approx
|
|
2109
|
+
face_storage.to_type(:float64) * (unit.count * (Element::UNIT_TO_SECONDS[unit.base] || 1))
|
|
2110
|
+
end
|
|
2111
|
+
|
|
2112
|
+
# The word form of {#+}.
|
|
2113
|
+
# @return [CATimedelta, CATime]
|
|
2114
|
+
alias_method :add, :+
|
|
2115
|
+
# The word form of {#-}.
|
|
2116
|
+
# @return [CATimedelta]
|
|
2117
|
+
alias_method :sub, :-
|
|
2118
|
+
# The word form of {#*}.
|
|
2119
|
+
# @return [CATimedelta]
|
|
2120
|
+
alias_method :mul, :*
|
|
2121
|
+
# The word form of {#/}.
|
|
2122
|
+
# @return [CATimedelta, CArray]
|
|
2123
|
+
alias_method :div, :/
|
|
2124
|
+
# The word form of {#-@}.
|
|
2125
|
+
# @return [CATimedelta]
|
|
2126
|
+
alias_method :neg, :-@
|
|
2127
|
+
|
|
1945
2128
|
# Element-wise comparison (< <= > >= <=>) rides the core comparison Face
|
|
1946
2129
|
# gate (ca_face_reconcile_comparison): the inherited CArray operators
|
|
1947
2130
|
# descend to storage and reconcile a CATimedelta RHS in a different unit
|
|
@@ -1966,7 +2149,7 @@ class CATimedelta
|
|
|
1966
2149
|
# whenever the true sum fits int64, whatever order the cells were added
|
|
1967
2150
|
# in. Whether it fits is read from the float64 sum, whose error is far
|
|
1968
2151
|
# inside the factor of 2 between 2**62 and 2**63.
|
|
1969
|
-
def exact_sum_relift
|
|
2152
|
+
def exact_sum_relift(exact, approx)
|
|
1970
2153
|
past = case approx
|
|
1971
2154
|
when Numeric then approx.abs > 2.0**62
|
|
1972
2155
|
when CArray then (approx.abs > 2.0**62).any
|
|
@@ -2035,7 +2218,8 @@ class CATimedelta
|
|
|
2035
2218
|
# @overload percentile(*p, axis: nil, **opts)
|
|
2036
2219
|
# Returns the percentile durations on `self`'s unit, in the shapes the
|
|
2037
2220
|
# plain {CArray#percentile} uses (one `p` reduces to a single value,
|
|
2038
|
-
#
|
|
2221
|
+
# an Array of `p` gives an Array, and several `p` one {CATimedelta}
|
|
2222
|
+
# with the `p` axes in front).
|
|
2039
2223
|
# @return [Element, CATimedelta, Array<Element>, Array<CATimedelta>]
|
|
2040
2224
|
def percentile(*args, **opts)
|
|
2041
2225
|
with_surface_fill(opts) { |o| round_and_relift(face_storage.percentile(*args, **o)) }
|
|
@@ -2121,37 +2305,55 @@ class CATimedelta
|
|
|
2121
2305
|
# array is the receiver. A calendar and a fixed-length duration have no
|
|
2122
2306
|
# common unit and raise.
|
|
2123
2307
|
# @api private
|
|
2124
|
-
def in_common_unit_with
|
|
2308
|
+
private def in_common_unit_with(other)
|
|
2125
2309
|
return [self, other] if other.unit == unit
|
|
2126
|
-
unless
|
|
2310
|
+
unless CATime::UnitAlgebra.same_group?(unit, other.unit)
|
|
2127
2311
|
raise ArgumentError,
|
|
2128
2312
|
"cannot compare a #{unit} duration with a #{other.unit} duration: " \
|
|
2129
2313
|
"a calendar length (months, years) has no fixed length in days"
|
|
2130
2314
|
end
|
|
2131
|
-
u =
|
|
2315
|
+
u = CATime::UnitAlgebra.finer(unit, other.unit)
|
|
2132
2316
|
[to_unit(u), other.to_unit(u)]
|
|
2133
2317
|
end
|
|
2134
2318
|
|
|
2135
|
-
private def common_unit
|
|
2136
|
-
|
|
2319
|
+
private def common_unit(a, b)
|
|
2320
|
+
CATime::UnitAlgebra.finer(a, b)
|
|
2137
2321
|
end
|
|
2138
2322
|
|
|
2139
2323
|
# One duration as a length-1 duration array in its own unit, or nil.
|
|
2140
|
-
private def lift_value
|
|
2141
|
-
|
|
2324
|
+
private def lift_value(operand)
|
|
2325
|
+
case operand
|
|
2326
|
+
when CATimedelta::Element
|
|
2142
2327
|
CATimedelta.wrap(CA_INT64([operand.value]), unit: operand.unit)
|
|
2328
|
+
when String
|
|
2329
|
+
res = CATime::Resolution.parse(operand)
|
|
2330
|
+
CATimedelta.wrap(CA_INT64([res.count]), unit: res.base)
|
|
2143
2331
|
end
|
|
2144
2332
|
end
|
|
2145
2333
|
|
|
2146
|
-
|
|
2334
|
+
# A value no time difference cell can be: eq, ne, count and search find
|
|
2335
|
+
# no cell for it. A duration, a String (read as one) and an Integer (the
|
|
2336
|
+
# raw ticks) are asked.
|
|
2337
|
+
private def foreign_value?(value)
|
|
2338
|
+
case value
|
|
2339
|
+
when CATimedelta::Element, String, Integer, Array, CArray
|
|
2340
|
+
false
|
|
2341
|
+
else
|
|
2342
|
+
true
|
|
2343
|
+
end
|
|
2344
|
+
end
|
|
2345
|
+
|
|
2346
|
+
private def to_comparable(operand)
|
|
2147
2347
|
case operand
|
|
2148
2348
|
when CATimedelta
|
|
2149
2349
|
return operand if operand.unit == unit
|
|
2150
|
-
|
|
2350
|
+
CATime::UnitAlgebra.convert_scale!(operand.parent, operand.unit, unit)
|
|
2151
2351
|
.timedelta(unit: unit)
|
|
2152
2352
|
when CATimedelta::Element
|
|
2153
2353
|
lifted = CATimedelta.wrap(CA_INT64([operand.value]), unit: operand.unit)
|
|
2154
2354
|
to_comparable(lifted)
|
|
2355
|
+
when String
|
|
2356
|
+
to_comparable(lift_value(operand))
|
|
2155
2357
|
when Array, CArray
|
|
2156
2358
|
# Durations written one by one, or an array that is not a duration
|
|
2157
2359
|
# array (an object column of elements), read value by value.
|
|
@@ -2169,16 +2371,26 @@ class CATimedelta
|
|
|
2169
2371
|
# scalar store round-trips with a fetch. A {Element} is reconciled to
|
|
2170
2372
|
# self's unit via {#to_comparable} (lossless discipline; a `Time` /
|
|
2171
2373
|
# `DateTime` is an absolute instant, not a duration, so it raises there).
|
|
2172
|
-
# A
|
|
2173
|
-
# through
|
|
2174
|
-
#
|
|
2374
|
+
# A String that names a duration (`"3 days"`) is read as one. An
|
|
2375
|
+
# Integer is not a duration and raises; the raw tick is written through
|
|
2376
|
+
# {#ticks}.
|
|
2377
|
+
# @param surface [Element, String]
|
|
2175
2378
|
# @return [Integer, Object] the storage-domain value, or `surface`
|
|
2176
2379
|
# unchanged for a pass-through type.
|
|
2177
2380
|
# @raise [TypeError, ArgumentError] on an unreconcilable surface / unit.
|
|
2178
|
-
def element_to_storage
|
|
2381
|
+
private def element_to_storage(surface)
|
|
2179
2382
|
case surface
|
|
2180
|
-
when Integer
|
|
2181
|
-
|
|
2383
|
+
when Integer
|
|
2384
|
+
raise TypeError, "#{CATime::UnitAlgebra.method_called_by_user}: an Integer is not a time difference; write a duration (a String such as \"3 days\" or a CATimedelta::Element), or the raw tick through #ticks"
|
|
2385
|
+
when nil
|
|
2386
|
+
raise TypeError, "#{CATime::UnitAlgebra.method_called_by_user}: nil is not a value of a time difference cell; assign UNDEF to mask the cell"
|
|
2387
|
+
when String
|
|
2388
|
+
begin
|
|
2389
|
+
to_comparable(surface).parent[0]
|
|
2390
|
+
rescue ArgumentError => e
|
|
2391
|
+
raise unless e.message.start_with?("invalid unit spec")
|
|
2392
|
+
raise ArgumentError, "#{CATime::UnitAlgebra.method_called_by_user}: #{surface.inspect} cannot be read as a time difference"
|
|
2393
|
+
end
|
|
2182
2394
|
else
|
|
2183
2395
|
to_comparable(surface).parent[0]
|
|
2184
2396
|
end
|
|
@@ -2199,7 +2411,7 @@ class CATimedelta
|
|
|
2199
2411
|
# @param axis [Integer, nil]
|
|
2200
2412
|
# @return [Element, CATimedelta] an {Element} for a scalar `addr`, `nil`
|
|
2201
2413
|
# when that single answer is undetermined.
|
|
2202
|
-
def linear_fetch
|
|
2414
|
+
def linear_fetch(addr, **opts)
|
|
2203
2415
|
r = face_storage.float64.linear_fetch(addr, **opts)
|
|
2204
2416
|
case r
|
|
2205
2417
|
when CArray then r.mask_invalid.round.int64.timedelta(unit: unit)
|
|
@@ -2214,12 +2426,12 @@ class CATimedelta
|
|
|
2214
2426
|
# cross-group pair (a month vs a second) has no common duration grid, so it
|
|
2215
2427
|
# raises rather than mixing calendar and fixed durations.
|
|
2216
2428
|
def common_duration_unit(ou)
|
|
2217
|
-
unless
|
|
2429
|
+
unless CATime::UnitAlgebra.same_group?(unit, ou)
|
|
2218
2430
|
raise ArgumentError,
|
|
2219
2431
|
"cannot combine a #{unit} duration with a #{ou} duration " \
|
|
2220
2432
|
"across the calendar/fixed boundary"
|
|
2221
2433
|
end
|
|
2222
|
-
|
|
2434
|
+
CATime::UnitAlgebra.finer(unit, ou)
|
|
2223
2435
|
end
|
|
2224
2436
|
|
|
2225
2437
|
# inspect's name for the array: the class and the tick, which the storage
|
|
@@ -2234,12 +2446,13 @@ class CATimedelta
|
|
|
2234
2446
|
end
|
|
2235
2447
|
end
|
|
2236
2448
|
|
|
2237
|
-
# Reads a time literal (Time / DateTime /
|
|
2449
|
+
# Reads a time literal (Time / Date / DateTime / String / element) into
|
|
2238
2450
|
# the pieces the CATime constructors need. All parsing is UTC (an explicit
|
|
2239
2451
|
# offset is honoured; otherwise UTC) and DateTime-independent (Date._parse +
|
|
2240
2452
|
# a civil-date kernel). Internal to the time surface: nothing here is part
|
|
2241
2453
|
# of the public API.
|
|
2242
|
-
|
|
2454
|
+
# @api private
|
|
2455
|
+
module CATime::Literal
|
|
2243
2456
|
module_function
|
|
2244
2457
|
|
|
2245
2458
|
# Date fields of a String literal, UTC. Ruby's Date._parse does not read
|
|
@@ -2266,11 +2479,24 @@ module CATimeLiteral
|
|
|
2266
2479
|
sec: t.sec, sec_fraction: frac.zero? ? nil : Rational(frac), leftover: h[:leftover] }.compact
|
|
2267
2480
|
end
|
|
2268
2481
|
|
|
2482
|
+
# Date._parse drops the sign of a year on its own or before a month, a day
|
|
2483
|
+
# of the year or a week, and reads a year below 100 as this century's, so
|
|
2484
|
+
# those are read here.
|
|
2485
|
+
def year_first_fields(spec)
|
|
2486
|
+
case spec
|
|
2487
|
+
when /\A([-+]\d{4,})\z/ then { year: $1.to_i }
|
|
2488
|
+
when /\A([-+]\d{4,})-(\d{1,2})\z/ then { year: $1.to_i, mon: $2.to_i }
|
|
2489
|
+
when /\A([-+]?\d{4,})-(\d{3})\z/ then { year: $1.to_i, yday: $2.to_i }
|
|
2490
|
+
when /\A([-+]?\d{4,})-W(\d{2})-(\d)\z/ then { cwyear: $1.to_i, cweek: $2.to_i, cwday: $3.to_i }
|
|
2491
|
+
end
|
|
2492
|
+
end
|
|
2493
|
+
|
|
2269
2494
|
def parse_date_fields(spec, format)
|
|
2270
2495
|
h =
|
|
2271
2496
|
if format then epoch_fields(Date._strptime(spec, format))
|
|
2272
2497
|
elsif spec =~ /\A(\d{4})-(\d{1,2})\z/ then { year: $1.to_i, mon: $2.to_i }
|
|
2273
2498
|
elsif spec =~ /\A(\d{4})\z/ then { year: $1.to_i }
|
|
2499
|
+
elsif (year_first = year_first_fields(spec)) then year_first
|
|
2274
2500
|
elsif (hour = hour_only_fields(spec)) then hour
|
|
2275
2501
|
else Date._parse(spec)
|
|
2276
2502
|
end
|
|
@@ -2389,26 +2615,30 @@ module CATimeLiteral
|
|
|
2389
2615
|
end
|
|
2390
2616
|
|
|
2391
2617
|
# Exact Rational seconds since the Unix epoch for a start literal (Time /
|
|
2392
|
-
# Date / DateTime /
|
|
2618
|
+
# Date / DateTime / String / element). UTC default. An Integer is not
|
|
2619
|
+
# an instant and raises, except with a format, which reads its digits.
|
|
2393
2620
|
def epoch_seconds(spec, format = nil)
|
|
2394
2621
|
case spec
|
|
2395
2622
|
when Time then spec.to_r
|
|
2396
|
-
when Integer
|
|
2623
|
+
when Integer
|
|
2624
|
+
# With a format an Integer is its digits, as CArray.time reads it.
|
|
2625
|
+
return epoch_seconds(spec.to_s, format) if format
|
|
2626
|
+
raise TypeError, "#{CATime::UnitAlgebra.method_called_by_user}: an Integer is not a time; write an instant (a Time, Date, String or CATime::Element)"
|
|
2397
2627
|
when CATime::Element
|
|
2398
2628
|
# A time element is already an instant, so it needs no parsing: a
|
|
2399
2629
|
# fixed unit counts seconds directly, and a calendar one names the
|
|
2400
2630
|
# first midnight of its granule (the same instant convert_instant!
|
|
2401
2631
|
# gives it). This is what lets floor / ceil / to_unit output feed
|
|
2402
2632
|
# straight back into time_range / time_series / .time.
|
|
2403
|
-
if
|
|
2633
|
+
if CATime::UnitAlgebra::FIXED.key?(spec.unit.base)
|
|
2404
2634
|
Rational(spec.value) * spec.unit.tick_ratio
|
|
2405
2635
|
else
|
|
2406
|
-
Rational(
|
|
2636
|
+
Rational(CATime::UnitAlgebra.calendar_days_since_epoch(CA_INT64([spec.value]),
|
|
2407
2637
|
spec.unit)[0] * 86400)
|
|
2408
2638
|
end
|
|
2409
2639
|
when String
|
|
2410
2640
|
h = parse_date_fields(spec, format)
|
|
2411
|
-
days =
|
|
2641
|
+
days = CATime::Civil.days_from_civil_1(h[:year], h[:mon] || 1, h[:mday] || 1)
|
|
2412
2642
|
sec = Rational(days * 86400 + (h[:hour] || 0) * 3600 +
|
|
2413
2643
|
(h[:min] || 0) * 60 + (h[:sec] || 0))
|
|
2414
2644
|
sec += h[:sec_fraction] if h[:sec_fraction]
|
|
@@ -2418,7 +2648,7 @@ module CATimeLiteral
|
|
|
2418
2648
|
if defined?(DateTime) && spec.is_a?(DateTime)
|
|
2419
2649
|
spec.to_time.to_r
|
|
2420
2650
|
elsif defined?(Date) && spec.is_a?(Date)
|
|
2421
|
-
Rational(
|
|
2651
|
+
Rational(CATime::Civil.days_from_civil_1(spec.year, spec.month, spec.day) * 86400)
|
|
2422
2652
|
else
|
|
2423
2653
|
raise ArgumentError, "cannot parse time #{spec.class}"
|
|
2424
2654
|
end
|
|
@@ -2429,7 +2659,9 @@ module CATimeLiteral
|
|
|
2429
2659
|
def year_month(spec, format = nil)
|
|
2430
2660
|
case spec
|
|
2431
2661
|
when Time then t = spec.utc; [t.year, t.month]
|
|
2432
|
-
when Integer
|
|
2662
|
+
when Integer
|
|
2663
|
+
return year_month(spec.to_s, format) if format
|
|
2664
|
+
raise TypeError, "#{CATime::UnitAlgebra.method_called_by_user}: an Integer is not a time; write an instant (a Time, Date, String or CATime::Element)"
|
|
2433
2665
|
when CATime::Element
|
|
2434
2666
|
# A calendar element is a month ordinal already, so read it as one
|
|
2435
2667
|
# rather than going out through Time (which a :Y / :M element would
|
|
@@ -2455,7 +2687,7 @@ module CATimeLiteral
|
|
|
2455
2687
|
|
|
2456
2688
|
# Tick index of `spec`'s instant on the `res` grid (floor toward the past).
|
|
2457
2689
|
def tick_index(spec, res, format = nil)
|
|
2458
|
-
if
|
|
2690
|
+
if CATime::UnitAlgebra::FIXED.key?(res.base)
|
|
2459
2691
|
(epoch_seconds(spec, format) / res.tick_ratio).floor
|
|
2460
2692
|
else
|
|
2461
2693
|
y, m = year_month(spec, format)
|
|
@@ -2559,7 +2791,7 @@ module CATimeLiteral
|
|
|
2559
2791
|
# (:fixed) or months (:calendar), or nil when `res` is finer than a
|
|
2560
2792
|
# nanosecond.
|
|
2561
2793
|
def tick_kind(res)
|
|
2562
|
-
if
|
|
2794
|
+
if CATime::UnitAlgebra::FIXED.key?(res.base)
|
|
2563
2795
|
per = res.tick_ratio * 1_000_000_000
|
|
2564
2796
|
kind = :fixed
|
|
2565
2797
|
else
|
|
@@ -2576,7 +2808,7 @@ module CATimeLiteral
|
|
|
2576
2808
|
# otherwise each String is parsed into fields here. Either way the fields
|
|
2577
2809
|
# become ticks for the whole array at once, and a cell that is not a
|
|
2578
2810
|
# String goes through tick_index.
|
|
2579
|
-
def ticks_from_cells(x, res, format, on_error
|
|
2811
|
+
def ticks_from_cells(x, res, format, on_error)
|
|
2580
2812
|
kind, per = tick_kind(res)
|
|
2581
2813
|
return nil unless kind
|
|
2582
2814
|
if format.is_a?(String)
|
|
@@ -2596,6 +2828,8 @@ module CATimeLiteral
|
|
|
2596
2828
|
direct = {}
|
|
2597
2829
|
cells.each_with_index do |s, i|
|
|
2598
2830
|
next if s.nil? || UNDEF.equal?(s)
|
|
2831
|
+
# An Integer is not a parse failure, so on_error: :mask does not mask it.
|
|
2832
|
+
raise TypeError, "time: an Integer is not a time; build times from ticks with CArray#time (CA_INT64([...]).time(unit: :s)), or give a format: that reads its digits" if s.is_a?(Integer) && format.nil?
|
|
2599
2833
|
begin
|
|
2600
2834
|
if s.is_a?(String)
|
|
2601
2835
|
h = parse_date_fields(s, format)
|
|
@@ -2606,10 +2840,6 @@ module CATimeLiteral
|
|
|
2606
2840
|
frac[i] = (h[:sec_fraction] * 1_000_000_000).floor if h[:sec_fraction]
|
|
2607
2841
|
off[i] = h[:offset] if h[:offset]
|
|
2608
2842
|
present[i] = 1
|
|
2609
|
-
elsif s.is_a?(Integer) && format.nil?
|
|
2610
|
-
# an Integer counts ticks of the unit given, or seconds when the
|
|
2611
|
-
# unit is chosen from the values
|
|
2612
|
-
direct[i] = seconds ? tick_index(s, res) : s
|
|
2613
2843
|
else
|
|
2614
2844
|
direct[i] = tick_index(s, res, format)
|
|
2615
2845
|
end
|
|
@@ -2678,17 +2908,16 @@ module CATimeLiteral
|
|
|
2678
2908
|
end
|
|
2679
2909
|
cells.each do |v|
|
|
2680
2910
|
case v
|
|
2681
|
-
when Integer then units << :s
|
|
2682
2911
|
when CATime::Element then units << v.unit
|
|
2683
|
-
when Time then units <<
|
|
2912
|
+
when Time then units << CATime::Literal.exact_instant(v).unit
|
|
2684
2913
|
when Date
|
|
2685
|
-
units << (defined?(DateTime) && v.is_a?(DateTime) ?
|
|
2914
|
+
units << (defined?(DateTime) && v.is_a?(DateTime) ? CATime::Literal.exact_instant(v.to_time).unit : :D)
|
|
2686
2915
|
end
|
|
2687
2916
|
end
|
|
2688
2917
|
# A month beside a day is read on the fixed grid both instants reach,
|
|
2689
2918
|
# as two elements are compared (diff_unit); finer covers one group only.
|
|
2690
2919
|
units.map { |u| CATime::Resolution.parse(u) }
|
|
2691
|
-
.reduce { |a, b|
|
|
2920
|
+
.reduce { |a, b| CATime::UnitAlgebra.diff_unit(a, b) } || CATime::Resolution.parse(:s)
|
|
2692
2921
|
end
|
|
2693
2922
|
|
|
2694
2923
|
# The unit a String read with Date._parse shows, or nil when it does not
|
|
@@ -2750,7 +2979,7 @@ class CArray
|
|
|
2750
2979
|
# `unit` (= a {CATime::Resolution}, tick = count * base). The value
|
|
2751
2980
|
# is the k-th tick since the Unix epoch (1970-01-01 UTC): e.g. unit
|
|
2752
2981
|
# `"10 minutes"` value 3 = 1970-01-01T00:30:00Z. Literals are read by
|
|
2753
|
-
# {
|
|
2982
|
+
# {CATime::Literal} (UTC).
|
|
2754
2983
|
|
|
2755
2984
|
# @overload time_range(start, last, unit:, step: nil, format: nil)
|
|
2756
2985
|
# Returns a {CATime} from `start` to `last` inclusive on the `unit`
|
|
@@ -2761,9 +2990,10 @@ class CArray
|
|
|
2761
2990
|
# bucket head (toward the past); the phase is anchored at `start`, and
|
|
2762
2991
|
# `last` is a bound rather than a member -- the series stops at the last
|
|
2763
2992
|
# step at or before it.
|
|
2764
|
-
# @param start [Time, String,
|
|
2765
|
-
# instant.
|
|
2766
|
-
#
|
|
2993
|
+
# @param start [Time, String, DateTime, CATime::Element] first
|
|
2994
|
+
# instant. An Integer raises `TypeError` (it is not an instant)
|
|
2995
|
+
# unless `format:` is given, which reads its digits.
|
|
2996
|
+
# @param last [Time, String, DateTime, CATime::Element] last
|
|
2767
2997
|
# instant (inclusive).
|
|
2768
2998
|
# @param unit [Resolution, Symbol, String] grid resolution (tick).
|
|
2769
2999
|
# @param step [Resolution, Symbol, String, nil] spacing between elements
|
|
@@ -2776,9 +3006,9 @@ class CArray
|
|
|
2776
3006
|
def self.time_range(start, last, unit:, step: nil, format: nil)
|
|
2777
3007
|
res = CATime::Resolution.parse(unit)
|
|
2778
3008
|
stride = step.nil? ? 1 :
|
|
2779
|
-
|
|
2780
|
-
s =
|
|
2781
|
-
e =
|
|
3009
|
+
CATime::UnitAlgebra.multiple_factor(CATime::Resolution.parse(step), res)
|
|
3010
|
+
s = CATime::Literal.tick_index(start, res, format)
|
|
3011
|
+
e = CATime::Literal.tick_index(last, res, format)
|
|
2782
3012
|
n = e < s ? 0 : (e - s) / stride + 1
|
|
2783
3013
|
CArray.int64(n) {|i| s + i * stride }.time(unit: res)
|
|
2784
3014
|
end
|
|
@@ -2789,8 +3019,9 @@ class CArray
|
|
|
2789
3019
|
# is stored on and `step` is the spacing, so an hourly grid sampled once
|
|
2790
3020
|
# a day is `unit: :h, step: "1 day"`. With no `step` the spacing is one
|
|
2791
3021
|
# `unit` tick (consecutive ticks, as before).
|
|
2792
|
-
# @param start [Time, String,
|
|
2793
|
-
# instant.
|
|
3022
|
+
# @param start [Time, String, DateTime, CATime::Element] first
|
|
3023
|
+
# instant. An Integer raises `TypeError` (it is not an instant)
|
|
3024
|
+
# unless `format:` is given, which reads its digits.
|
|
2794
3025
|
# @param count [Integer] number of elements.
|
|
2795
3026
|
# @param unit [Resolution, Symbol, String] grid resolution (tick).
|
|
2796
3027
|
# @param step [Resolution, Symbol, String, nil] spacing between elements
|
|
@@ -2803,33 +3034,33 @@ class CArray
|
|
|
2803
3034
|
def self.time_series(start, count:, unit:, step: nil, format: nil)
|
|
2804
3035
|
res = CATime::Resolution.parse(unit)
|
|
2805
3036
|
stride = step.nil? ? 1 :
|
|
2806
|
-
|
|
2807
|
-
s =
|
|
3037
|
+
CATime::UnitAlgebra.multiple_factor(CATime::Resolution.parse(step), res)
|
|
3038
|
+
s = CATime::Literal.tick_index(start, res, format)
|
|
2808
3039
|
CArray.int64(count) {|i| s + i * stride }.time(unit: res)
|
|
2809
3040
|
end
|
|
2810
3041
|
|
|
2811
|
-
# @overload time(x, unit:
|
|
3042
|
+
# @overload time(x, unit: nil, format: nil, on_error: :raise)
|
|
2812
3043
|
# Builds a {CATime} on the `unit` grid from time value(s). `x`
|
|
2813
|
-
# is either a single literal (Time / ISO 8601 String /
|
|
2814
|
-
#
|
|
3044
|
+
# is either a single literal (Time / ISO 8601 String / DateTime /
|
|
3045
|
+
# element) — giving a 1-element result — or a CArray of such
|
|
2815
3046
|
# literals — giving a same-shape result parsed per cell. Parsing is UTC
|
|
2816
|
-
# and DateTime-independent. An Integer
|
|
2817
|
-
#
|
|
2818
|
-
#
|
|
2819
|
-
# `
|
|
3047
|
+
# and DateTime-independent. An Integer is not a time and raises
|
|
3048
|
+
# `TypeError`; ticks are read by {CArray#time}
|
|
3049
|
+
# (`CA_INT64([0, 10]).time(unit: :m)` is 00:00 and 00:10), and with a
|
|
3050
|
+
# `format:` an Integer is read as its digits.
|
|
2820
3051
|
#
|
|
2821
3052
|
# A value that cannot be parsed raises by default (`on_error: :raise`);
|
|
2822
3053
|
# pass `on_error: :mask` to make it an UNDEF cell instead. A masked /
|
|
2823
3054
|
# `nil` input cell is a *missing* value (not a parse failure) and always
|
|
2824
3055
|
# becomes UNDEF, regardless of `on_error`.
|
|
2825
|
-
# @param x [Time, String,
|
|
3056
|
+
# @param x [Time, String, DateTime, CATime::Element, Array, CArray] a literal, a
|
|
2826
3057
|
# Ruby Array of literals, or a CArray of literals.
|
|
2827
3058
|
# @param unit [Resolution, Symbol, String, nil] target grid resolution;
|
|
2828
3059
|
# by default the finest the values show, so nothing they hold is cut
|
|
2829
3060
|
# off: text written to the day is `:D`, to the second `:s`, with a
|
|
2830
3061
|
# fraction `:ms` / `:us` / `:ns` by its digits (as
|
|
2831
|
-
# `CAFrame#
|
|
2832
|
-
# the format writes;
|
|
3062
|
+
# `CAFrame#parse_time` reads text); with a format, the finest field
|
|
3063
|
+
# the format writes; a Time the finest
|
|
2833
3064
|
# its value needs; a Date `:D`; an element its own unit.
|
|
2834
3065
|
# @param format [String, nil] optional strptime format for String input.
|
|
2835
3066
|
# With a format, an Integer is read as its decimal digits
|
|
@@ -2837,6 +3068,7 @@ class CArray
|
|
|
2837
3068
|
# @param on_error [:raise, :mask] parse-failure policy (default `:raise`).
|
|
2838
3069
|
# @return [CATime] shape `[1]` for a literal, else `x`'s shape.
|
|
2839
3070
|
# @raise [ArgumentError] on an unparseable value when `on_error: :raise`.
|
|
3071
|
+
# @raise [TypeError] on an Integer without a `format:`, whatever `on_error:`.
|
|
2840
3072
|
#
|
|
2841
3073
|
# @example
|
|
2842
3074
|
# CArray.time("2024-06-15", unit: :D) # 1-element
|
|
@@ -2849,18 +3081,17 @@ class CArray
|
|
|
2849
3081
|
x = CA_OBJECT(x) if x.is_a?(Array) # Ruby Array of literals -> object CArray
|
|
2850
3082
|
# With a format, an Integer is the digits it is written with (20240101
|
|
2851
3083
|
# read as "%Y%m%d"), not a count of ticks: a format describes text.
|
|
2852
|
-
x =
|
|
2853
|
-
res = unit ? CATime::Resolution.parse(unit) :
|
|
2854
|
-
# Without a format, an Integer
|
|
2855
|
-
#
|
|
2856
|
-
if format.nil?
|
|
2857
|
-
|
|
2858
|
-
return (x.data_type == CA_INT64 ? x.copy : x).time(unit: res) if x.is_a?(CArray) && !x.face? && x.integer?
|
|
3084
|
+
x = CATime::Literal.integers_as_text(x) if format
|
|
3085
|
+
res = unit ? CATime::Resolution.parse(unit) : CATime::Literal.unit_shown(x, format)
|
|
3086
|
+
# Without a format, an Integer is not a time: ticks are read by
|
|
3087
|
+
# CArray#time, which says that it reads them.
|
|
3088
|
+
if format.nil? and (x.is_a?(Integer) or (x.is_a?(CArray) && !x.face? && x.integer?))
|
|
3089
|
+
raise TypeError, "time: an Integer is not a time; build times from ticks with CArray#time (CA_INT64([...]).time(unit: :s)), or give a format: that reads its digits"
|
|
2859
3090
|
end
|
|
2860
3091
|
unless x.is_a?(CArray)
|
|
2861
|
-
return
|
|
3092
|
+
return CATime::Literal.to_time_array(x, res, format, on_error)
|
|
2862
3093
|
end
|
|
2863
|
-
raw =
|
|
3094
|
+
raw = CATime::Literal.ticks_from_cells(x, res, format, on_error)
|
|
2864
3095
|
return raw.time(unit: res) if raw
|
|
2865
3096
|
raw = CArray.int64(*x.shape)
|
|
2866
3097
|
x.each_index do |*idx|
|
|
@@ -2869,8 +3100,9 @@ class CArray
|
|
|
2869
3100
|
raw[*idx] = UNDEF # missing input -> missing output (no phantom epoch)
|
|
2870
3101
|
next
|
|
2871
3102
|
end
|
|
3103
|
+
raise TypeError, "time: an Integer is not a time; build times from ticks with CArray#time (CA_INT64([...]).time(unit: :s)), or give a format: that reads its digits" if s.is_a?(Integer) && format.nil?
|
|
2872
3104
|
begin
|
|
2873
|
-
raw[*idx] =
|
|
3105
|
+
raw[*idx] = CATime::Literal.tick_index(s, res, format)
|
|
2874
3106
|
rescue ArgumentError, TypeError
|
|
2875
3107
|
raise if on_error == :raise
|
|
2876
3108
|
raw[*idx] = UNDEF # opt-in parse-mask
|
|
@@ -2879,22 +3111,24 @@ class CArray
|
|
|
2879
3111
|
raw.time(unit: res)
|
|
2880
3112
|
end
|
|
2881
3113
|
|
|
2882
|
-
# @overload time(unit: :
|
|
2883
|
-
# Returns `self` as a {CATime} on the `unit` grid
|
|
3114
|
+
# @overload time(unit: :s, origin: nil)
|
|
3115
|
+
# Returns `self` as a {CATime} on the `unit` grid; by default the values
|
|
3116
|
+
# count seconds. With `origin`
|
|
2884
3117
|
# nil, `self`'s int64 values are taken as tick indices already anchored
|
|
2885
3118
|
# to the Unix epoch (zero-copy Face wrap). With `origin` given, `self`'s
|
|
2886
3119
|
# indices are relative to `origin` and are rebased to the epoch (a new
|
|
2887
3120
|
# int64 array is built; the origin is not stored).
|
|
2888
3121
|
# @param unit [Resolution, Symbol, String] grid resolution.
|
|
2889
|
-
# @param origin [Time, String,
|
|
2890
|
-
# `self`'s indices are counted from (default: the Unix epoch)
|
|
2891
|
-
#
|
|
3122
|
+
# @param origin [Time, String, DateTime, CATime::Element, nil] base instant that
|
|
3123
|
+
# `self`'s indices are counted from (default: the Unix epoch). It has
|
|
3124
|
+
# to be a point of the `unit` grid.
|
|
2892
3125
|
# @return [CATime]
|
|
2893
3126
|
# @raise [ArgumentError] when `origin` falls between two ticks of `unit`.
|
|
3127
|
+
# @raise [TypeError] when `origin` is an Integer (it is not an instant).
|
|
2894
3128
|
# @note An `int64` receiver is wrapped zero-copy. A narrower integer type
|
|
2895
3129
|
# is widened to `int64` first (a copy); a `Float` / non-integer type
|
|
2896
3130
|
# raises (cast it explicitly if the truncation is intended).
|
|
2897
|
-
def time(unit: :
|
|
3131
|
+
def time(unit: :s, origin: nil)
|
|
2898
3132
|
res = CATime::Resolution.parse(unit)
|
|
2899
3133
|
src = as_int64_time_storage
|
|
2900
3134
|
if origin.nil?
|
|
@@ -2902,20 +3136,19 @@ class CArray
|
|
|
2902
3136
|
else
|
|
2903
3137
|
# The origin is a point of the grid: one between two ticks has no
|
|
2904
3138
|
# index, so it raises rather than moving to the tick before it.
|
|
2905
|
-
|
|
2906
|
-
|
|
2907
|
-
CATimeUnitAlgebra.checked_add(src, o).time(unit: res)
|
|
3139
|
+
o = CATime::Timestep.resolve_origin_ticks(origin, res, res)
|
|
3140
|
+
CATime::UnitAlgebra.checked_add(src, o).time(unit: res)
|
|
2908
3141
|
end
|
|
2909
3142
|
end
|
|
2910
3143
|
|
|
2911
|
-
# @overload timedelta(unit: :
|
|
3144
|
+
# @overload timedelta(unit: :s)
|
|
2912
3145
|
# Returns `self` re-wrapped as a zero-copy {CATimedelta} view
|
|
2913
|
-
# with the given `unit
|
|
3146
|
+
# with the given `unit` (by default seconds).
|
|
2914
3147
|
# @param unit [Resolution, Symbol, String] duration resolution.
|
|
2915
3148
|
# @return [CATimedelta]
|
|
2916
3149
|
# @note An `int64` receiver is wrapped zero-copy; a narrower integer type
|
|
2917
3150
|
# is widened to `int64` first (a copy); a `Float` / non-integer raises.
|
|
2918
|
-
def timedelta(unit: :
|
|
3151
|
+
def timedelta(unit: :s)
|
|
2919
3152
|
CATimedelta.wrap(as_int64_time_storage, unit: unit)
|
|
2920
3153
|
end
|
|
2921
3154
|
|
|
@@ -2943,26 +3176,26 @@ class CArray
|
|
|
2943
3176
|
end
|
|
2944
3177
|
|
|
2945
3178
|
# ============================================================================
|
|
2946
|
-
# timestep system
|
|
3179
|
+
# timestep system
|
|
2947
3180
|
#
|
|
2948
3181
|
# Projects an absolute time onto an integer "step index" -- the k-th
|
|
2949
3182
|
# fixed-width bucket of `step` counted from `origin` -- and the inverse /
|
|
2950
3183
|
# rounding / on-grid operators built on it. All storage-domain int64 and
|
|
2951
|
-
# vectorized; the
|
|
3184
|
+
# vectorized; the main benefit is that bucketing, matching, and positional
|
|
2952
3185
|
# addressing across differently-scaled series all become integer arithmetic.
|
|
2953
3186
|
#
|
|
2954
3187
|
# Integer path covers: fixed-length step on fixed-length storage, and Y/M
|
|
2955
3188
|
# step on Y/M storage (:M storage already encodes a linear month ordinal, so
|
|
2956
|
-
# it floor-divides identically).
|
|
2957
|
-
#
|
|
2958
|
-
# See devel/PROPOSAL_DATETIME64_STEP_SYSTEM.md.
|
|
3189
|
+
# it floor-divides identically). A Y/M step on sub-day storage takes the
|
|
3190
|
+
# calendar path, through civil-date algebra.
|
|
2959
3191
|
# ============================================================================
|
|
2960
3192
|
|
|
2961
3193
|
# Places a bucket grid on a storage resolution: resolves a timestep into whole
|
|
2962
3194
|
# storage ticks, resolves an origin into a tick count (or a month ordinal on
|
|
2963
3195
|
# the calendar path), and guards both against int64 overflow. Internal to the
|
|
2964
3196
|
# time surface: nothing here is part of the public API.
|
|
2965
|
-
|
|
3197
|
+
# @api private
|
|
3198
|
+
module CATime::Timestep
|
|
2966
3199
|
module_function
|
|
2967
3200
|
|
|
2968
3201
|
# Base units that are day-or-finer (a period head is representable exactly;
|
|
@@ -3000,7 +3233,7 @@ module CATimeGrid
|
|
|
3000
3233
|
# Storage ticks per day of `storage_res` (must be a whole number, else the
|
|
3001
3234
|
# storage grid does not tile a day -- a calendar bucket is unrepresentable).
|
|
3002
3235
|
def ticks_per_day(storage_res)
|
|
3003
|
-
r =
|
|
3236
|
+
r = CATime::UnitAlgebra.ratio(CATime::Resolution.new(1, :D), storage_res)
|
|
3004
3237
|
unless r.denominator == 1
|
|
3005
3238
|
raise ArgumentError,
|
|
3006
3239
|
"cannot place a calendar bucket on storage resolution " \
|
|
@@ -3018,7 +3251,19 @@ module CATimeGrid
|
|
|
3018
3251
|
ymk = ym0 + k * count
|
|
3019
3252
|
yy = ymk / 12
|
|
3020
3253
|
mm = ymk - yy * 12 + 1
|
|
3021
|
-
|
|
3254
|
+
CATime::Civil.days_from_civil(yy, mm, CArray.int64(*k.shape) { 1 }) * tpd
|
|
3255
|
+
end
|
|
3256
|
+
|
|
3257
|
+
# Whether a calendar `origin` is a month head that the ticks of `res`
|
|
3258
|
+
# (a :Y / :M resolution) cannot hold: a bucket of N months whose origin
|
|
3259
|
+
# is not N months from the epoch, or a year bucket that starts in a month
|
|
3260
|
+
# other than January. Such an origin is answered on month storage.
|
|
3261
|
+
def calendar_origin_off_grid?(res, origin)
|
|
3262
|
+
return false if origin.nil? or not %i[Y M].include?(res.base)
|
|
3263
|
+
y, m = check_calendar_origin(origin, nil)
|
|
3264
|
+
ord = y * 12 + (m - 1) - 1970 * 12
|
|
3265
|
+
months = res.count * (res.base == :Y ? 12 : 1)
|
|
3266
|
+
ord % months != 0
|
|
3022
3267
|
end
|
|
3023
3268
|
|
|
3024
3269
|
# Month ordinal (year*12 + month-1) of origin; day / time ignored.
|
|
@@ -3040,13 +3285,13 @@ module CATimeGrid
|
|
|
3040
3285
|
# of the bucket tick; a partial multiple ("90 minutes" storage against
|
|
3041
3286
|
# an :h bucket) has no integer timestep and raises.
|
|
3042
3287
|
def resolve_step_scale(step_res, storage_res)
|
|
3043
|
-
r =
|
|
3288
|
+
r = CATime::UnitAlgebra.ratio(step_res, storage_res) # storage ticks / step tick
|
|
3044
3289
|
if r
|
|
3045
3290
|
what = "bucket #{step_res} in ticks of #{storage_res}"
|
|
3046
3291
|
return [1, check_int64_range(r.numerator, what)] if r.denominator == 1
|
|
3047
3292
|
return [check_int64_range(r.denominator, what), 1] if r.numerator == 1
|
|
3048
3293
|
end
|
|
3049
|
-
if
|
|
3294
|
+
if CATime::UnitAlgebra::CALENDAR.key?(step_res.base) && SU_LE_DAY.include?(storage_res.base)
|
|
3050
3295
|
return :civil
|
|
3051
3296
|
end
|
|
3052
3297
|
raise ArgumentError,
|
|
@@ -3056,8 +3301,8 @@ module CATimeGrid
|
|
|
3056
3301
|
|
|
3057
3302
|
# origin -> integer tick count in storage ticks. Default (nil) is the
|
|
3058
3303
|
# epoch, except a week bucket defaults to ISO Monday (1970-01-05). A lossy
|
|
3059
|
-
# conversion (origin not landing exactly on the storage grid) raises;
|
|
3060
|
-
#
|
|
3304
|
+
# conversion (origin not landing exactly on the storage grid) raises; an
|
|
3305
|
+
# Integer is not an instant and raises TypeError.
|
|
3061
3306
|
def resolve_origin_ticks(origin, step_res, storage_res)
|
|
3062
3307
|
if origin.nil?
|
|
3063
3308
|
if step_res.base == :W && SU_LE_DAY.include?(storage_res.base)
|
|
@@ -3066,7 +3311,7 @@ module CATimeGrid
|
|
|
3066
3311
|
end
|
|
3067
3312
|
return 0
|
|
3068
3313
|
end
|
|
3069
|
-
if
|
|
3314
|
+
if CATime::UnitAlgebra::FIXED.key?(storage_res.base)
|
|
3070
3315
|
secs = origin_seconds_exact(origin)
|
|
3071
3316
|
tick = secs / storage_res.tick_ratio # Rational
|
|
3072
3317
|
unless tick.denominator == 1
|
|
@@ -3089,18 +3334,10 @@ module CATimeGrid
|
|
|
3089
3334
|
end
|
|
3090
3335
|
end
|
|
3091
3336
|
|
|
3092
|
-
# Exact Rational seconds since the Unix epoch for a fixed-storage origin
|
|
3093
|
-
#
|
|
3094
|
-
# an origin reads the same here as it does for a start literal.
|
|
3337
|
+
# Exact Rational seconds since the Unix epoch for a fixed-storage origin,
|
|
3338
|
+
# read as a start literal is (an Integer raises there).
|
|
3095
3339
|
def origin_seconds_exact(origin)
|
|
3096
|
-
|
|
3097
|
-
when Integer
|
|
3098
|
-
raise ArgumentError,
|
|
3099
|
-
"origin: a bare Integer is ambiguous (epoch-dependent); pass a " \
|
|
3100
|
-
"Time / String / CATime scalar"
|
|
3101
|
-
else
|
|
3102
|
-
CATimeLiteral.epoch_seconds(origin) # Time / String / DateTime / Element
|
|
3103
|
-
end
|
|
3340
|
+
CATime::Literal.epoch_seconds(origin)
|
|
3104
3341
|
end
|
|
3105
3342
|
|
|
3106
3343
|
# Whether `origin` sits exactly on the head of its month (the 1st at
|
|
@@ -3111,9 +3348,9 @@ module CATimeGrid
|
|
|
3111
3348
|
# A calendar element is a month head by construction, and the shared
|
|
3112
3349
|
# entry point says so by handing back that midnight -- no branch here
|
|
3113
3350
|
# has to know it.
|
|
3114
|
-
secs =
|
|
3351
|
+
secs = CATime::Literal.epoch_seconds(origin)
|
|
3115
3352
|
y, m = origin_year_month(origin)
|
|
3116
|
-
head =
|
|
3353
|
+
head = CATime::Civil.days_from_civil(CA_INT64([y]), CA_INT64([m]), CA_INT64([1]))[0]
|
|
3117
3354
|
secs == head * 86400
|
|
3118
3355
|
end
|
|
3119
3356
|
|
|
@@ -3139,14 +3376,7 @@ module CATimeGrid
|
|
|
3139
3376
|
|
|
3140
3377
|
# [year, month] of a calendar-storage origin (finer fields ignored).
|
|
3141
3378
|
def origin_year_month(origin)
|
|
3142
|
-
|
|
3143
|
-
when Integer
|
|
3144
|
-
raise ArgumentError,
|
|
3145
|
-
"origin: a bare Integer is ambiguous (epoch-dependent); pass a " \
|
|
3146
|
-
"Time / String / CATime scalar"
|
|
3147
|
-
else
|
|
3148
|
-
CATimeLiteral.year_month(origin) # Time / String / DateTime / Element
|
|
3149
|
-
end
|
|
3379
|
+
CATime::Literal.year_month(origin)
|
|
3150
3380
|
end
|
|
3151
3381
|
end
|
|
3152
3382
|
|
|
@@ -3223,7 +3453,7 @@ class CATime
|
|
|
3223
3453
|
|
|
3224
3454
|
# seconds- (fixed base) or months- (calendar base) per tick (Rational).
|
|
3225
3455
|
def tick_ratio
|
|
3226
|
-
count *
|
|
3456
|
+
count * CATime::UnitAlgebra.base_ratio(base)
|
|
3227
3457
|
end
|
|
3228
3458
|
|
|
3229
3459
|
# @return [Boolean] whether `other` is the same count and base unit.
|
|
@@ -3247,12 +3477,12 @@ class CATime
|
|
|
3247
3477
|
# representation, and the reference-time epoch stays out of storage.
|
|
3248
3478
|
#
|
|
3249
3479
|
# g = CATime::Grid.parse("12 hours since 2017-11-30 09:00")
|
|
3250
|
-
# t.snap(g, direction: :floor) # the pair, passed once
|
|
3251
|
-
# t.timesteps(g) # which tick each element falls in
|
|
3480
|
+
# t.snap(unit: g, direction: :floor) # the pair, passed once
|
|
3481
|
+
# t.timesteps(unit: g) # which tick each element falls in
|
|
3252
3482
|
# g.at(CA_INT64([0, 1, 2])) # and back
|
|
3253
3483
|
#
|
|
3254
|
-
# Proleptic Gregorian, UTC.
|
|
3255
|
-
#
|
|
3484
|
+
# Proleptic Gregorian, UTC. The calendar is not replaceable, on purpose: a
|
|
3485
|
+
# replaceable calendar costs time in every operation that touches a time, and the
|
|
3256
3486
|
# benefit does not follow.
|
|
3257
3487
|
class Grid
|
|
3258
3488
|
|
|
@@ -3289,26 +3519,28 @@ class CATime
|
|
|
3289
3519
|
end
|
|
3290
3520
|
end
|
|
3291
3521
|
|
|
3292
|
-
# Normalizes the
|
|
3293
|
-
# `unit:`, or
|
|
3294
|
-
#
|
|
3522
|
+
# Normalizes the two ways a grid reaches a method -- a Grid as
|
|
3523
|
+
# `unit:`, or the loose `(unit:, origin:)` pair -- into that pair.
|
|
3524
|
+
# A Grid carries its own origin, so `origin:` beside it is refused
|
|
3525
|
+
# rather than dropped. One call at the head of a method is the whole
|
|
3526
|
+
# Grid arm.
|
|
3295
3527
|
# @return [Array(Object, Object)] (unit, origin).
|
|
3296
|
-
def self.resolve(
|
|
3297
|
-
return [
|
|
3298
|
-
|
|
3299
|
-
unless grid.nil?
|
|
3528
|
+
def self.resolve(unit, origin, name)
|
|
3529
|
+
return [unit, origin] unless unit.is_a?(Grid)
|
|
3530
|
+
unless origin.nil?
|
|
3300
3531
|
raise ArgumentError,
|
|
3301
|
-
"
|
|
3532
|
+
"#{name}: origin: cannot be given with a CATime::Grid, which " \
|
|
3533
|
+
"carries its own origin; give it when making the grid " \
|
|
3534
|
+
"(CATime::Grid.new(unit, origin))"
|
|
3302
3535
|
end
|
|
3303
|
-
[unit, origin]
|
|
3536
|
+
[unit.unit, unit.origin]
|
|
3304
3537
|
end
|
|
3305
3538
|
|
|
3306
3539
|
def initialize(unit, origin = nil)
|
|
3307
3540
|
@unit = Resolution.parse(unit)
|
|
3308
3541
|
@origin = origin
|
|
3309
3542
|
@storage = resolve_storage
|
|
3310
|
-
|
|
3311
|
-
@origin = CATimeLiteral.to_time_array(origin, @storage, nil, :raise)[0] if origin
|
|
3543
|
+
@origin = CATime::Literal.to_time_array(origin, @storage, nil, :raise)[0] if origin
|
|
3312
3544
|
freeze
|
|
3313
3545
|
end
|
|
3314
3546
|
|
|
@@ -3334,7 +3566,7 @@ class CATime
|
|
|
3334
3566
|
|
|
3335
3567
|
# @return [Integer] storage ticks from the epoch to the origin.
|
|
3336
3568
|
def origin_ticks
|
|
3337
|
-
@origin ? Integer(
|
|
3569
|
+
@origin ? Integer(CATime::Literal.epoch_seconds(@origin) / @storage.tick_ratio) : 0
|
|
3338
3570
|
end
|
|
3339
3571
|
|
|
3340
3572
|
# Tick indices -> instants: the {CATime.from_timesteps} direction, but
|
|
@@ -3343,17 +3575,22 @@ class CATime
|
|
|
3343
3575
|
# @return [CATime, Element]
|
|
3344
3576
|
def at(k)
|
|
3345
3577
|
return at(CArray.int64(1) { Integer(k) })[0] unless k.is_a?(CArray)
|
|
3346
|
-
|
|
3578
|
+
# A calendar grid counts months, and a week grid with no origin counts
|
|
3579
|
+
# from the ISO Monday, as the keyword pair does; neither is a fixed
|
|
3580
|
+
# number of storage ticks from the epoch.
|
|
3581
|
+
if calendar? or (@unit.base == :W and @origin.nil?)
|
|
3582
|
+
return CATime.from_timesteps(k.int64, unit: @unit, origin: @origin)
|
|
3583
|
+
end
|
|
3347
3584
|
(origin_ticks + k.int64 * step_ticks).time(unit: @storage)
|
|
3348
3585
|
end
|
|
3349
3586
|
|
|
3350
3587
|
# Instants -> tick indices: the {CATime#timesteps} direction. Off-grid
|
|
3351
3588
|
# elements floor toward the past; ask {#on} when that would be a lie.
|
|
3352
3589
|
# @return [CArray] int64.
|
|
3353
|
-
def index(time) = time.timesteps(self)
|
|
3590
|
+
def index(time) = time.timesteps(unit: self)
|
|
3354
3591
|
|
|
3355
3592
|
# @return [CArray] boolean, flagging elements that land on a tick.
|
|
3356
|
-
def on(time) = time.is_righttime(self)
|
|
3593
|
+
def on(time) = time.is_righttime(unit: self)
|
|
3357
3594
|
|
|
3358
3595
|
# @return [Boolean] whether every element lands on a tick.
|
|
3359
3596
|
def on?(time) = on(time).all
|
|
@@ -3388,15 +3625,21 @@ class CATime
|
|
|
3388
3625
|
private
|
|
3389
3626
|
|
|
3390
3627
|
# The coarsest storage holding both the unit's tick and the origin's
|
|
3391
|
-
# phase without truncation. A calendar grid is addressed by month
|
|
3392
|
-
# its origin is
|
|
3628
|
+
# phase without truncation. A calendar grid is addressed by month and
|
|
3629
|
+
# its origin is a month head: the unit serves when its ticks hold the
|
|
3630
|
+
# origin, and months serve when they do not (3 months from February).
|
|
3393
3631
|
def resolve_storage
|
|
3394
|
-
|
|
3632
|
+
if calendar?
|
|
3633
|
+
return CATime::Timestep.calendar_origin_off_grid?(@unit, @origin) ? Resolution.new(1, :M) : @unit
|
|
3634
|
+
end
|
|
3635
|
+
# With no origin a week counts from the ISO Monday, which the epoch
|
|
3636
|
+
# week ticks (Thursdays) cannot hold; days can.
|
|
3637
|
+
return Resolution.new(1, :D) if @unit.base == :W and @origin.nil?
|
|
3395
3638
|
candidates = [@unit, *FALLBACK_BASES.map {|base| Resolution.new(1, base) }]
|
|
3396
3639
|
candidates.each do |res|
|
|
3397
3640
|
next unless (@unit.tick_ratio / res.tick_ratio).denominator == 1
|
|
3398
3641
|
next unless @origin.nil? ||
|
|
3399
|
-
(
|
|
3642
|
+
(CATime::Literal.epoch_seconds(@origin) / res.tick_ratio).denominator == 1
|
|
3400
3643
|
return res
|
|
3401
3644
|
end
|
|
3402
3645
|
raise ArgumentError,
|
|
@@ -3407,12 +3650,17 @@ class CATime
|
|
|
3407
3650
|
|
|
3408
3651
|
# Rounding rules {#snap} accepts, the same three CArray#snap takes.
|
|
3409
3652
|
SNAP_DIRECTIONS = %i[round floor ceil].freeze
|
|
3653
|
+
private_constant :SNAP_DIRECTIONS
|
|
3410
3654
|
|
|
3411
3655
|
# The grid this array is stored on: its own tick, anchored at the epoch.
|
|
3412
|
-
# `t.timesteps` is `t.timesteps(t.grid)`, which is what the `unit:` default
|
|
3656
|
+
# `t.timesteps` is `t.timesteps(unit: t.grid)`, which is what the `unit:` default
|
|
3413
3657
|
# has always meant.
|
|
3414
3658
|
# @return [Grid]
|
|
3415
|
-
def grid
|
|
3659
|
+
def grid
|
|
3660
|
+
# A grid with no origin means the default one, which for a week is the
|
|
3661
|
+
# ISO Monday; week ticks count from the epoch, so say so.
|
|
3662
|
+
unit.base == :W ? Grid.new(unit, "1970-01-01") : Grid.new(unit)
|
|
3663
|
+
end
|
|
3416
3664
|
|
|
3417
3665
|
# @overload timesteps(unit: self.unit, origin: nil)
|
|
3418
3666
|
# Returns the integer timestep of every element: the k-th `unit`-wide
|
|
@@ -3426,16 +3674,24 @@ class CATime
|
|
|
3426
3674
|
# (default: this array's own storage resolution).
|
|
3427
3675
|
# @param origin [Time, String, CATime::Element, DateTime, nil] head of
|
|
3428
3676
|
# bucket 0 (default: the Unix epoch, or ISO Monday for a week bucket
|
|
3429
|
-
#
|
|
3430
|
-
#
|
|
3677
|
+
# -- a week array counts its ticks from the epoch Thursday, so a
|
|
3678
|
+
# week bucket on it is answered on days).
|
|
3431
3679
|
# It has to be a bucket head itself: on a calendar grid, the 1st at
|
|
3432
3680
|
# 00:00.
|
|
3433
3681
|
# @return [CArray] int64 timesteps.
|
|
3434
3682
|
# @raise [ArgumentError] on a sub-resolution / unrepresentable (unit,
|
|
3435
3683
|
# storage-resolution) pair or a lossy origin.
|
|
3436
|
-
def timesteps(
|
|
3437
|
-
unit, origin = Grid.resolve(
|
|
3438
|
-
|
|
3684
|
+
def timesteps(unit: nil, origin: nil)
|
|
3685
|
+
unit, origin = Grid.resolve(unit, origin, "timesteps")
|
|
3686
|
+
# No unit means this array's own grid, whose week ticks count from
|
|
3687
|
+
# the epoch.
|
|
3688
|
+
if unit.nil?
|
|
3689
|
+
unit = self.unit
|
|
3690
|
+
origin = grid.origin if origin.nil?
|
|
3691
|
+
end
|
|
3692
|
+
if (days = week_bucket_on_days(unit, origin))
|
|
3693
|
+
return days.timesteps(unit: unit, origin: origin)
|
|
3694
|
+
end
|
|
3439
3695
|
g = resolve_timestep_grid(unit, origin)
|
|
3440
3696
|
return calendar_bucket(:index, g[1], origin) if g[0] == :civil
|
|
3441
3697
|
su, mul, step_ticks, o = g
|
|
@@ -3451,38 +3707,46 @@ class CATime
|
|
|
3451
3707
|
# @param unit [String, Symbol, Resolution] bucket resolution.
|
|
3452
3708
|
# @param origin [Time, String, CATime::Element, DateTime, nil] head of
|
|
3453
3709
|
# bucket 0 (default: the Unix epoch, or ISO Monday for a week bucket
|
|
3454
|
-
#
|
|
3455
|
-
#
|
|
3710
|
+
# -- a week array counts its ticks from the epoch Thursday, so a
|
|
3711
|
+
# week bucket on it is answered on days).
|
|
3456
3712
|
# It has to be a bucket head itself: on a calendar grid, the 1st at
|
|
3457
3713
|
# 00:00.
|
|
3458
3714
|
# @return [CATime]
|
|
3459
|
-
# @overload snap(
|
|
3715
|
+
# @overload snap(unit:, origin: nil, direction: :round)
|
|
3460
3716
|
# Returns each element snapped to a point on the bucket grid, as a
|
|
3461
3717
|
# {CATime} in the same storage resolution. This is the time-domain twin
|
|
3462
|
-
# of {CArray#snap}: there, `(step, offset:, direction:)`; here
|
|
3463
|
-
# (= tick + origin)
|
|
3464
|
-
#
|
|
3718
|
+
# of {CArray#snap}: there, `(step, offset:, direction:)`; here `unit:`
|
|
3719
|
+
# and `origin:`, or a {Grid} (= tick + origin) as `unit:`, and the same
|
|
3720
|
+
# three directions.
|
|
3465
3721
|
#
|
|
3466
3722
|
# `:round` picks the nearest bucket head, ties toward the future;
|
|
3467
3723
|
# `:floor` the head at or before; `:ceil` the head at or after.
|
|
3468
3724
|
# {#floor}, {#ceil} and {#round} are this method with `direction:` fixed.
|
|
3469
|
-
# @param grid [Grid, nil] the bucket grid, passed as one value.
|
|
3470
3725
|
# @param unit [String, Symbol, Resolution, Grid] bucket resolution.
|
|
3471
3726
|
# @param origin [Time, String, CATime::Element, DateTime, nil] head of
|
|
3472
3727
|
# bucket 0 (default: the Unix epoch, or ISO Monday for a week bucket
|
|
3473
|
-
#
|
|
3474
|
-
#
|
|
3728
|
+
# -- a week array counts its ticks from the epoch Thursday, so a
|
|
3729
|
+
# week bucket on it is answered on days).
|
|
3475
3730
|
# It has to be a bucket head itself: on a calendar grid, the 1st at
|
|
3476
3731
|
# 00:00.
|
|
3477
3732
|
# @param direction [:round, :floor, :ceil] rounding rule.
|
|
3478
3733
|
# @return [CATime]
|
|
3479
|
-
def snap(
|
|
3734
|
+
def snap(unit: nil, origin: nil, direction: :round)
|
|
3480
3735
|
unless SNAP_DIRECTIONS.include?(direction)
|
|
3481
3736
|
raise ArgumentError,
|
|
3482
3737
|
"snap: direction must be :round / :floor / :ceil " \
|
|
3483
3738
|
"(got #{direction.inspect})"
|
|
3484
3739
|
end
|
|
3485
|
-
unit, origin
|
|
3740
|
+
snap_to(unit, origin, direction, "snap")
|
|
3741
|
+
end
|
|
3742
|
+
|
|
3743
|
+
# The body of {#snap}, with the name of the method the user called, so
|
|
3744
|
+
# floor / ceil / round report themselves.
|
|
3745
|
+
private def snap_to(unit, origin, direction, name)
|
|
3746
|
+
unit, origin = Grid.resolve(unit, origin, name)
|
|
3747
|
+
if (days = week_bucket_on_days(unit, origin))
|
|
3748
|
+
return days.send(:snap_to, unit, origin, direction, name)
|
|
3749
|
+
end
|
|
3486
3750
|
g = resolve_timestep_grid(unit, origin)
|
|
3487
3751
|
return calendar_bucket(direction, g[1], origin) if g[0] == :civil
|
|
3488
3752
|
su, mul, step_ticks, o = g
|
|
@@ -3512,15 +3776,15 @@ class CATime
|
|
|
3512
3776
|
bucket_head_as_time(o + q * step_ticks, mul, su)
|
|
3513
3777
|
end
|
|
3514
3778
|
|
|
3515
|
-
# @overload floor(
|
|
3779
|
+
# @overload floor(unit:, origin: nil)
|
|
3516
3780
|
# {#snap} with `direction: :floor` -- each element at its bucket head
|
|
3517
3781
|
# (toward the past), as a {CATime} in the same storage resolution.
|
|
3518
3782
|
# @return [CATime]
|
|
3519
|
-
def floor(
|
|
3520
|
-
|
|
3783
|
+
def floor(unit: nil, origin: nil)
|
|
3784
|
+
snap_to(unit, origin, :floor, "floor")
|
|
3521
3785
|
end
|
|
3522
3786
|
|
|
3523
|
-
# @overload end_of(
|
|
3787
|
+
# @overload end_of(unit:, origin: nil)
|
|
3524
3788
|
# Each element at the last tick of its bucket, on the array's own grid:
|
|
3525
3789
|
# the bucket {#floor} gives the head of, one tick before the next head.
|
|
3526
3790
|
# A day array's month ends on the month's last day, a second array's at
|
|
@@ -3534,29 +3798,37 @@ class CATime
|
|
|
3534
3798
|
# @return [CATime]
|
|
3535
3799
|
# @raise [ArgumentError] when the bucket is finer than the grid or not a
|
|
3536
3800
|
# whole number of its ticks.
|
|
3537
|
-
def end_of(
|
|
3538
|
-
step, origin = Grid.resolve(
|
|
3801
|
+
def end_of(unit: nil, origin: nil)
|
|
3802
|
+
step, origin = Grid.resolve(unit, origin, "end_of")
|
|
3539
3803
|
step = CATime::Resolution.parse(step)
|
|
3540
|
-
per =
|
|
3804
|
+
per = CATime::UnitAlgebra.ratio(step, self.unit) # ticks of the grid per bucket
|
|
3541
3805
|
if per && !(per.denominator == 1 && per >= 1)
|
|
3542
3806
|
raise ArgumentError,
|
|
3543
3807
|
"end_of: a bucket of #{step} is not a whole number of the #{self.unit} " \
|
|
3544
3808
|
"grid's ticks, so its last tick is not on the grid"
|
|
3545
3809
|
end
|
|
3546
3810
|
head = floor(unit: step, origin: origin)
|
|
3811
|
+
# A week bucket on a week array is answered on days (see
|
|
3812
|
+
# #week_bucket_on_days); its last tick on this grid is the one its last
|
|
3813
|
+
# day falls in.
|
|
3814
|
+
if head.unit != self.unit
|
|
3815
|
+
last = head + CATimedelta::Element.new(step.count, step.base) -
|
|
3816
|
+
CATimedelta::Element.new(1, :D)
|
|
3817
|
+
return last.to_unit(self.unit)
|
|
3818
|
+
end
|
|
3547
3819
|
head + CATimedelta::Element.new(step.count, step.base) -
|
|
3548
3820
|
CATimedelta::Element.new(self.unit.count, self.unit.base)
|
|
3549
3821
|
end
|
|
3550
3822
|
|
|
3551
|
-
# @overload ceil(
|
|
3823
|
+
# @overload ceil(unit:, origin: nil)
|
|
3552
3824
|
# {#snap} with `direction: :ceil` -- each element at the bucket head at
|
|
3553
3825
|
# or after it (an element already on a boundary maps to itself).
|
|
3554
3826
|
# @return [CATime]
|
|
3555
|
-
def ceil(
|
|
3556
|
-
|
|
3827
|
+
def ceil(unit: nil, origin: nil)
|
|
3828
|
+
snap_to(unit, origin, :ceil, "ceil")
|
|
3557
3829
|
end
|
|
3558
3830
|
|
|
3559
|
-
# @overload round(
|
|
3831
|
+
# @overload round(unit:, origin: nil)
|
|
3560
3832
|
# {#snap} with `direction: :round` -- each element at its nearest bucket
|
|
3561
3833
|
# head, ties toward the future. Exact for odd `step_ticks` (no half-tick
|
|
3562
3834
|
# loss). For a calendar bucket the nearest head is by the instant (month
|
|
@@ -3564,8 +3836,8 @@ class CATime
|
|
|
3564
3836
|
# stands for the start of its month or year, so a `:M` array rounds as
|
|
3565
3837
|
# the same dates on `:D` do: 1889-07 to the year is 1889.
|
|
3566
3838
|
# @return [CATime]
|
|
3567
|
-
def round(
|
|
3568
|
-
|
|
3839
|
+
def round(unit: nil, origin: nil)
|
|
3840
|
+
snap_to(unit, origin, :round, "round")
|
|
3569
3841
|
end
|
|
3570
3842
|
|
|
3571
3843
|
# @overload is_righttime(unit:, origin: nil)
|
|
@@ -3575,13 +3847,16 @@ class CATime
|
|
|
3575
3847
|
# @param unit [String, Symbol, Resolution] bucket resolution.
|
|
3576
3848
|
# @param origin [Time, String, CATime::Element, DateTime, nil] head of
|
|
3577
3849
|
# bucket 0 (default: the Unix epoch, or ISO Monday for a week bucket
|
|
3578
|
-
#
|
|
3579
|
-
#
|
|
3850
|
+
# -- a week array counts its ticks from the epoch Thursday, so a
|
|
3851
|
+
# week bucket on it is answered on days).
|
|
3580
3852
|
# It has to be a bucket head itself: on a calendar grid, the 1st at
|
|
3581
3853
|
# 00:00.
|
|
3582
3854
|
# @return [CArray] boolean.
|
|
3583
|
-
def is_righttime(
|
|
3584
|
-
unit, origin = Grid.resolve(
|
|
3855
|
+
def is_righttime(unit: nil, origin: nil)
|
|
3856
|
+
unit, origin = Grid.resolve(unit, origin, "is_righttime")
|
|
3857
|
+
if (days = week_bucket_on_days(unit, origin))
|
|
3858
|
+
return days.is_righttime(unit: unit, origin: origin)
|
|
3859
|
+
end
|
|
3585
3860
|
g = resolve_timestep_grid(unit, origin)
|
|
3586
3861
|
return calendar_bucket(:on, g[1], origin) if g[0] == :civil
|
|
3587
3862
|
su, mul, step_ticks, o = g
|
|
@@ -3611,23 +3886,45 @@ class CATime
|
|
|
3611
3886
|
# It has to be a bucket head itself: on a calendar grid, the 1st at
|
|
3612
3887
|
# 00:00.
|
|
3613
3888
|
# @return [Element, CATime] on the `unit` grid (`:D` for a week bucket).
|
|
3614
|
-
def self.from_timesteps(k,
|
|
3615
|
-
grid = unit if unit.is_a?(Grid)
|
|
3889
|
+
def self.from_timesteps(k, unit: nil, origin: nil)
|
|
3616
3890
|
# A Grid answers on its own storage, so an origin off the unit grid
|
|
3617
3891
|
# ("12 hours since ... 09:00") survives; the keyword form keeps the
|
|
3618
3892
|
# narrower rule it always had, where the origin sits on the unit itself.
|
|
3619
|
-
|
|
3620
|
-
|
|
3893
|
+
if unit.is_a?(Grid)
|
|
3894
|
+
Grid.resolve(unit, origin, "from_timesteps")
|
|
3895
|
+
return unit.at(k)
|
|
3896
|
+
end
|
|
3621
3897
|
res = Resolution.parse(unit)
|
|
3622
3898
|
out = res.base == :W ? Resolution.new(1, :D) : res
|
|
3899
|
+
# A calendar origin off the unit's own grid ("3 months since
|
|
3900
|
+
# 2024-02-01", a year from March) is one {#timesteps} and {#floor}
|
|
3901
|
+
# take; the unit's ticks cannot hold it, so answer on months.
|
|
3902
|
+
if CATime::Timestep.calendar_origin_off_grid?(res, origin)
|
|
3903
|
+
out = Resolution.new(1, :M)
|
|
3904
|
+
end
|
|
3623
3905
|
kk = k.is_a?(CArray) ? k.int64 : CArray.int64(1) { Integer(k) }
|
|
3624
|
-
_mul, step_ticks, o =
|
|
3906
|
+
_mul, step_ticks, o = CATime::Timestep.resolve_integer_grid(res, out, origin)
|
|
3625
3907
|
raw = o + kk * step_ticks
|
|
3626
3908
|
k.is_a?(CArray) ? raw.time(unit: out) : Element.new(raw[0], out)
|
|
3627
3909
|
end
|
|
3628
3910
|
|
|
3629
3911
|
private
|
|
3630
3912
|
|
|
3913
|
+
# This array on days, when it is stored in weeks and asked for a week
|
|
3914
|
+
# bucket whose head its ticks cannot hold; nil otherwise. Week ticks
|
|
3915
|
+
# count from the epoch, a Thursday, while a week bucket with no origin
|
|
3916
|
+
# starts on the ISO Monday, as it does on a day array. Days hold every
|
|
3917
|
+
# week head, so the bucket is answered there.
|
|
3918
|
+
def week_bucket_on_days(step, origin)
|
|
3919
|
+
return nil unless unit.base == :W and step
|
|
3920
|
+
return nil unless Resolution.parse(step).base == :W
|
|
3921
|
+
unless origin.nil?
|
|
3922
|
+
tick = CATime::Timestep.origin_seconds_exact(origin) / unit.tick_ratio
|
|
3923
|
+
return nil if tick.denominator == 1
|
|
3924
|
+
end
|
|
3925
|
+
to_unit(Resolution.new(1, :D))
|
|
3926
|
+
end
|
|
3927
|
+
|
|
3631
3928
|
# Resolve the grid for an instance: [storage_res, mul, step_ticks,
|
|
3632
3929
|
# origin_ticks] for the integer path, or [:civil, bucket_res] for the
|
|
3633
3930
|
# calendar path. Bucket arithmetic runs in the *numerator* grid -- the
|
|
@@ -3638,17 +3935,17 @@ class CATime
|
|
|
3638
3935
|
def resolve_timestep_grid(step, origin)
|
|
3639
3936
|
storage_res = unit
|
|
3640
3937
|
step_res = Resolution.parse(step)
|
|
3641
|
-
scale =
|
|
3938
|
+
scale = CATime::Timestep.resolve_step_scale(step_res, storage_res)
|
|
3642
3939
|
return [:civil, step_res] if scale == :civil
|
|
3643
3940
|
mul, step_ticks = scale
|
|
3644
3941
|
num_res = mul == 1 ? storage_res : step_res
|
|
3645
|
-
o =
|
|
3942
|
+
o = CATime::Timestep.resolve_origin_ticks(origin, step_res, num_res)
|
|
3646
3943
|
[storage_res, mul, step_ticks, o]
|
|
3647
3944
|
end
|
|
3648
3945
|
|
|
3649
3946
|
# Storage ticks lifted into the numerator grid (see {#resolve_timestep_grid}).
|
|
3650
3947
|
def storage_ticks_in_numerator_grid(mul)
|
|
3651
|
-
mul == 1 ? face_storage :
|
|
3948
|
+
mul == 1 ? face_storage : CATime::UnitAlgebra.widen(face_storage, mul)
|
|
3652
3949
|
end
|
|
3653
3950
|
|
|
3654
3951
|
# A bucket head computed in numerator ticks, back as a {CATime} in the
|
|
@@ -3664,6 +3961,7 @@ class CATime
|
|
|
3664
3961
|
# bases cannot, so the range guard is skipped for them (zero cost on the
|
|
3665
3962
|
# common :s / :h / :D / :M path).
|
|
3666
3963
|
FINE_UNITS = %i[us ns ps fs as].freeze
|
|
3964
|
+
private_constant :FINE_UNITS
|
|
3667
3965
|
|
|
3668
3966
|
# Raise (rather than silently wrap) if the integer-path arithmetic would
|
|
3669
3967
|
# overflow int64. Checks `parent - o`, and -- when `kind` is a bucket op
|
|
@@ -3686,7 +3984,7 @@ class CATime
|
|
|
3686
3984
|
return if min == UNDEF
|
|
3687
3985
|
lo = Integer(min) * mul
|
|
3688
3986
|
hi = Integer(face_storage.max) * mul
|
|
3689
|
-
chk = ->(v, w) {
|
|
3987
|
+
chk = ->(v, w) { CATime::Timestep.check_int64_range(v, w) }
|
|
3690
3988
|
chk.(lo, "ticks in bucket resolution")
|
|
3691
3989
|
chk.(hi, "ticks in bucket resolution")
|
|
3692
3990
|
chk.(lo - o, "face_storage - origin")
|
|
@@ -3718,17 +4016,17 @@ class CATime
|
|
|
3718
4016
|
case res.base
|
|
3719
4017
|
when :Y
|
|
3720
4018
|
ones = CArray.int64(*shape) { 1 }
|
|
3721
|
-
|
|
4019
|
+
CATime::Civil.days_from_civil(face_storage * res.count + 1970, ones, ones)
|
|
3722
4020
|
when :M
|
|
3723
4021
|
mo = face_storage * res.count + 1970 * 12
|
|
3724
4022
|
y = mo / 12
|
|
3725
4023
|
m = mo - y * 12 + 1
|
|
3726
|
-
|
|
4024
|
+
CATime::Civil.days_from_civil(y, m, CArray.int64(*shape) { 1 })
|
|
3727
4025
|
when :W then face_storage * res.count * 7
|
|
3728
4026
|
when :D then face_storage * res.count
|
|
3729
4027
|
else # fixed sub-day storage
|
|
3730
4028
|
day = CATime::Resolution.new(1, :D)
|
|
3731
|
-
r =
|
|
4029
|
+
r = CATime::UnitAlgebra.ratio(day, res) # storage ticks per day
|
|
3732
4030
|
if r.denominator == 1
|
|
3733
4031
|
face_storage / r.numerator
|
|
3734
4032
|
else # non-day-aligned tick
|
|
@@ -3745,7 +4043,7 @@ class CATime
|
|
|
3745
4043
|
# 10 / 20 / ...; a day grid collapses hour / minute / second to 0). A
|
|
3746
4044
|
# calendar-storage element has no intra-day field, so it collapses to 0.
|
|
3747
4045
|
def clock_field(fu)
|
|
3748
|
-
r =
|
|
4046
|
+
r = CATime::UnitAlgebra.ratio(unit, CATime::Resolution.new(1, fu)) # fu ticks / storage tick
|
|
3749
4047
|
return face_storage * 0 if r.nil? # calendar storage: no clock field
|
|
3750
4048
|
fi = r.denominator == 1 ? face_storage * r.numerator # epoch-relative fu index
|
|
3751
4049
|
: face_storage * r.numerator / r.denominator
|
|
@@ -3761,16 +4059,16 @@ class CATime
|
|
|
3761
4059
|
def calendar_bucket(kind, st, origin)
|
|
3762
4060
|
su = unit # storage Resolution
|
|
3763
4061
|
count = st.count * (st.base == :Y ? 12 : 1) # bucket in months
|
|
3764
|
-
ym0 =
|
|
4062
|
+
ym0 = CATime::Timestep.origin_month_ordinal(origin)
|
|
3765
4063
|
days = days_since_epoch # count-folded day index
|
|
3766
|
-
y, m =
|
|
4064
|
+
y, m = CATime::Civil.civil_from_days(days)
|
|
3767
4065
|
ym = y * 12 + (m - 1)
|
|
3768
4066
|
k = (ym - ym0) / count
|
|
3769
4067
|
return k if kind == :index
|
|
3770
|
-
head =
|
|
4068
|
+
head = CATime::Timestep.calendar_bucket_head_ticks(k, st, su, origin)
|
|
3771
4069
|
return head.time(unit: su) if kind == :floor
|
|
3772
4070
|
return face_storage.eq(head) if kind == :on
|
|
3773
|
-
head_next =
|
|
4071
|
+
head_next = CATime::Timestep.calendar_bucket_head_ticks(k + 1, st, su, origin)
|
|
3774
4072
|
sel = # boolean 0/1: pick head_next?
|
|
3775
4073
|
case kind
|
|
3776
4074
|
when :ceil then face_storage.ne(head) # on-boundary keeps head
|