carray 3.0.7 → 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 -1940
- data/README.md +38 -32
- 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 +6 -9
- data/ext/ca_bincmp_dispatch.h +16 -38
- data/ext/ca_binop_dispatch.c +27 -0
- data/ext/ca_binop_dispatch.h +22 -47
- 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 -1506
- 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 +90 -163
- data/ext/ca_obj_bincmp.c +47 -46
- data/ext/ca_obj_binop.c +38 -38
- data/ext/ca_obj_bitarray.c +5 -17
- data/ext/ca_obj_bitfield.c +15 -30
- data/ext/ca_obj_block.c +11 -12
- data/ext/ca_obj_byte_swap.c +32 -30
- data/ext/ca_obj_const_string.c +57 -62
- data/ext/ca_obj_face.c +99 -91
- data/ext/ca_obj_face.h +32 -170
- data/ext/ca_obj_fake.c +15 -21
- data/ext/ca_obj_farray.c +7 -9
- data/ext/ca_obj_field.c +17 -13
- data/ext/ca_obj_fixlen_string.c +8 -19
- data/ext/ca_obj_grid.c +137 -106
- data/ext/ca_obj_meld.c +23 -21
- data/ext/ca_obj_moncmp.c +10 -21
- data/ext/ca_obj_monop.c +24 -35
- data/ext/ca_obj_object.c +51 -37
- data/ext/ca_obj_record.c +74 -22
- data/ext/ca_obj_reduce.c +8 -10
- data/ext/ca_obj_refer.c +42 -29
- data/ext/ca_obj_remap.c +11 -29
- data/ext/ca_obj_repeat.c +41 -23
- data/ext/ca_obj_roll.c +18 -24
- data/ext/ca_obj_select.c +10 -22
- data/ext/ca_obj_select_axis.c +27 -36
- data/ext/ca_obj_shift.c +22 -42
- data/ext/ca_obj_source.c +1 -5
- data/ext/ca_obj_stack.c +28 -24
- data/ext/ca_obj_stride.c +53 -54
- data/ext/ca_obj_string.c +6 -18
- data/ext/ca_obj_tile.c +12 -12
- 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 +35 -39
- data/ext/ca_obj_window.c +67 -86
- data/ext/ca_op_cmplx64.h +4 -0
- data/ext/ca_op_ipower.c +34 -12
- data/ext/ca_op_powi.h +4 -0
- 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 +20 -26
- data/ext/ca_sweep_engine.c +2 -3
- data/ext/ca_sweep_engine.h +17 -9
- data/ext/ca_transform_common.c +3 -8
- data/ext/ca_triop_dispatch.h +20 -32
- data/ext/caframe_csv_reader.c +11 -11
- data/ext/carray.h +300 -1011
- data/ext/carray_access.c +234 -180
- data/ext/carray_address_basis.c +33 -38
- data/ext/carray_attribute.c +81 -104
- data/ext/carray_bincount.c +12 -14
- data/ext/carray_broadcast.c +30 -26
- data/ext/carray_call_cfunc.c +169 -277
- data/ext/carray_call_cfunc.h +36 -28
- data/ext/carray_cast.c +912 -346
- data/ext/carray_cast_func.rb +31 -3
- data/ext/carray_class.c +6 -11
- data/ext/carray_conversion.c +43 -38
- data/ext/carray_copy.c +28 -26
- data/ext/carray_core.c +217 -126
- data/ext/carray_count.c +27 -15
- data/ext/carray_element.c +40 -25
- data/ext/carray_factorize.c +53 -52
- data/ext/carray_generate.c +26 -19
- data/ext/carray_histogram.c +8 -10
- data/ext/carray_hold.c +7 -9
- data/ext/carray_index_classifier.c +123 -228
- data/ext/carray_index_classifier.h +11 -10
- data/ext/carray_integer_total.c +523 -0
- data/ext/carray_internal.h +1274 -160
- data/ext/carray_key_windows.c +3 -3
- data/ext/carray_lazy.c +21 -36
- data/ext/carray_loop.c +19 -22
- data/ext/carray_mask.c +123 -145
- data/ext/carray_math_kernel.h +23 -47
- data/ext/carray_mathfunc.c +10 -3
- data/ext/carray_median_percentile.c +95 -94
- data/ext/carray_memory_view.c +42 -47
- data/ext/carray_operator.c +423 -122
- data/ext/carray_order.c +121 -85
- data/ext/carray_partition.c +13 -15
- data/ext/carray_random.c +47 -41
- data/ext/carray_round.c +9 -4
- data/ext/carray_scatter.c +54 -40
- data/ext/carray_segment.c +41 -45
- data/ext/carray_slab.c +64 -47
- data/ext/carray_slab.h +44 -33
- data/ext/carray_sort.c +32 -29
- data/ext/carray_sort_kernel.c +12 -10
- data/ext/carray_struct.c +38 -43
- data/ext/carray_test.c +47 -43
- data/ext/carray_time_text.c +24 -31
- data/ext/carray_undef.c +50 -7
- data/ext/carray_utils.c +258 -94
- data/ext/extconf.rb +24 -17
- data/ext/mk_call_cfunc.rb +19 -20
- data/ext/mk_pow5_table.rb +2 -1
- data/ext/mkkernel.rb +782 -461
- 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 +264 -180
- data/lib/carray/basics.rb +272 -48
- data/lib/carray/bincount_nd.rb +33 -16
- data/lib/carray/block_iterator.rb +123 -102
- data/lib/carray/boolean_reduce.rb +7 -4
- data/lib/carray/categorical.rb +63 -51
- data/lib/carray/categorical_iterator.rb +89 -67
- data/lib/carray/complex.rb +61 -41
- data/lib/carray/conditional.rb +49 -22
- data/lib/carray/const_string.rb +54 -62
- data/lib/carray/core_extensions.rb +1 -1
- data/lib/carray/distance_limit.rb +7 -5
- data/lib/carray/factories.rb +269 -0
- data/lib/carray/fixlen_string.rb +25 -24
- data/lib/carray/frame/column_verbs.rb +20 -8
- data/lib/carray/frame/concat.rb +3 -3
- data/lib/carray/frame/convert.rb +2 -1
- data/lib/carray/frame/csv_parser.rb +7 -1
- data/lib/carray/frame/frame.rb +35 -25
- data/lib/carray/frame/group.rb +183 -150
- data/lib/carray/frame/io.rb +26 -7
- data/lib/carray/frame/join.rb +37 -12
- data/lib/carray/frame/records.rb +1 -1
- data/lib/carray/frame/reshape.rb +3 -3
- data/lib/carray/frame/sort.rb +10 -8
- data/lib/carray/frame/verbs.rb +148 -118
- data/lib/carray/frame/windows.rb +232 -21
- data/lib/carray/frame.rb +1 -2
- data/lib/carray/fuse_source.rb +24 -21
- 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 +97 -24
- data/lib/carray/key_windows.rb +83 -57
- data/lib/carray/lazy.rb +196 -94
- 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 +5 -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 +72 -71
- data/lib/carray/methods/{index.rb → coordinate.rb} +13 -13
- data/lib/carray/methods/discovery_along.rb +3 -3
- data/lib/carray/methods/gather_nd.rb +106 -106
- 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 +165 -145
- data/lib/carray/methods/join.rb +2 -1
- data/lib/carray/methods/lag.rb +6 -2
- data/lib/carray/methods/locate_addr.rb +150 -138
- 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 +12 -12
- 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 +18 -17
- data/lib/carray/rng.rb +1 -0
- data/lib/carray/runtime.rb +190 -22
- data/lib/carray/segment_iterator.rb +251 -129
- data/lib/carray/serialize.rb +618 -571
- data/lib/carray/slab_iterator.rb +114 -114
- data/lib/carray/stack.rb +11 -12
- data/lib/carray/string.rb +16 -22
- data/lib/carray/string_operation_extension.rb +76 -52
- data/lib/carray/struct.rb +122 -164
- data/lib/carray/struct_builder.rb +243 -72
- data/lib/carray/time.rb +496 -342
- data/lib/carray/window_iterator.rb +191 -150
- 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 +4 -3
- 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 +7 -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 +26 -15
- data/yard-stubs/carray_access.rb +51 -4
- data/yard-stubs/carray_attribute.rb +26 -38
- data/yard-stubs/carray_cast.rb +118 -165
- 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 +5 -3
- 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 +157 -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 +67 -34
- 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 +10 -2
- data/yard-stubs/carray_slab.rb +7 -4
- data/yard-stubs/carray_sort.rb +7 -6
- data/yard-stubs/carray_undef.rb +44 -5
- data/yard-stubs/ruby_carray.rb +75 -40
- metadata +62 -24
- 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 -25337
- 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 -262
- 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
|
|
@@ -634,8 +680,8 @@ module CATimeOperandValues
|
|
|
634
680
|
# @param other [CATime, CATimedelta, CATime::Element, CATimedelta::Element, Time, Date, String]
|
|
635
681
|
# @return [CATime, CATimedelta]
|
|
636
682
|
# @raise [TypeError] when `other` is not of this kind.
|
|
637
|
-
def pmax
|
|
638
|
-
pick_by_order(other) { |a, b| a < b }
|
|
683
|
+
def pmax(other)
|
|
684
|
+
pick_by_order(__callee__, other) { |a, b| a < b }
|
|
639
685
|
end
|
|
640
686
|
|
|
641
687
|
# @overload pmin(other)
|
|
@@ -643,22 +689,21 @@ module CATimeOperandValues
|
|
|
643
689
|
# @param other [CATime, CATimedelta, CATime::Element, CATimedelta::Element, Time, Date, String]
|
|
644
690
|
# @return [CATime, CATimedelta]
|
|
645
691
|
# @raise [TypeError] when `other` is not of this kind.
|
|
646
|
-
def pmin
|
|
647
|
-
pick_by_order(other) { |a, b| a > b }
|
|
692
|
+
def pmin(other)
|
|
693
|
+
pick_by_order(__callee__, other) { |a, b| a > b }
|
|
648
694
|
end
|
|
649
695
|
|
|
650
696
|
# A time or a duration has no NaN, so maximum / minimum (which let a NaN
|
|
651
|
-
# win) are pmax / pmin
|
|
697
|
+
# win) are pmax / pmin.
|
|
652
698
|
alias_method :maximum, :pmax
|
|
653
699
|
alias_method :minimum, :pmin
|
|
654
|
-
alias_method :fmax, :pmax
|
|
655
|
-
alias_method :fmin, :pmin
|
|
656
700
|
|
|
657
701
|
private
|
|
658
702
|
|
|
659
703
|
# `other` where the block says self's cell loses to it, else self's cell.
|
|
660
|
-
|
|
661
|
-
|
|
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)
|
|
662
707
|
a, b = comparable_pair(other)
|
|
663
708
|
b = b[0] unless other.is_a?(CArray) || other.is_a?(Array)
|
|
664
709
|
yield(a, b).then_else(b, a)
|
|
@@ -666,10 +711,10 @@ module CATimeOperandValues
|
|
|
666
711
|
|
|
667
712
|
# Both branches of a selection in the unit a comparison meets them in, so
|
|
668
713
|
# the finer unit wins whichever branch it is.
|
|
669
|
-
def branches_in_common
|
|
714
|
+
def branches_in_common(x, y)
|
|
670
715
|
other, base = y.is_a?(CArray) && !y.scalar? ? [x, y] : [y, x]
|
|
671
716
|
return [x, y] if other.nil? || UNDEF.equal?(other)
|
|
672
|
-
a, b = base.comparable_pair
|
|
717
|
+
a, b = base.send(:comparable_pair, other)
|
|
673
718
|
b = b[0] unless other.is_a?(CArray) && !other.scalar?
|
|
674
719
|
base.equal?(y) ? [b, a] : [a, b]
|
|
675
720
|
end
|
|
@@ -677,34 +722,37 @@ module CATimeOperandValues
|
|
|
677
722
|
# The operand as an array of this kind in its own unit: one value read as
|
|
678
723
|
# it is written, an Array or an array of another kind read value by value
|
|
679
724
|
# in the unit they share, an UNDEF or a masked cell kept masked.
|
|
680
|
-
def lift_operand
|
|
725
|
+
def lift_operand(operand, lookup = false)
|
|
681
726
|
return operand if operand.is_a?(self.class)
|
|
682
727
|
case operand
|
|
683
728
|
when Array
|
|
684
729
|
# A nested Array keeps its shape; reading it row by row would land
|
|
685
730
|
# each row as one value.
|
|
686
|
-
return lift_operand(CA_OBJECT(operand)) if operand.any?(Array)
|
|
687
|
-
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
|
+
}
|
|
688
735
|
units = lifted.compact.map(&:unit)
|
|
689
736
|
u = units.empty? ? unit : units.reduce { |a, b| common_unit(a, b) }
|
|
690
737
|
self.class.wrap(CA_INT64(lifted.map { |x| x ? x.to_unit(u).parent[0] : UNDEF }),
|
|
691
738
|
unit: u)
|
|
692
739
|
when CArray
|
|
693
|
-
lift_operand(operand.to_a.flatten).reshape(*operand.shape)
|
|
740
|
+
lift_operand(operand.to_a.flatten, lookup).reshape(*operand.shape)
|
|
694
741
|
else
|
|
742
|
+
return lift_operand([operand], true) if lookup && foreign_value?(operand)
|
|
695
743
|
lift_value(operand) || to_comparable(operand)
|
|
696
744
|
end
|
|
697
745
|
end
|
|
698
746
|
|
|
699
|
-
def reconcile_values
|
|
747
|
+
def reconcile_values(operand)
|
|
700
748
|
to_comparable(lift_operand(operand))
|
|
701
749
|
end
|
|
702
750
|
|
|
703
751
|
end
|
|
704
752
|
|
|
705
753
|
class CATime
|
|
706
|
-
include
|
|
707
|
-
include
|
|
754
|
+
include CATime::SurfaceFill
|
|
755
|
+
include CATime::OperandValues
|
|
708
756
|
# @overload new(*shape, unit: :ns)
|
|
709
757
|
# Allocates a new int64 storage CArray of the given `shape` and
|
|
710
758
|
# wraps it as a CATime Face with the given `unit`. The
|
|
@@ -797,7 +845,7 @@ class CATime
|
|
|
797
845
|
|
|
798
846
|
# A year past four digits carries its sign, which is what tells it from a
|
|
799
847
|
# date written without separators when the text is read back.
|
|
800
|
-
private def signed_past_four_digits
|
|
848
|
+
private def signed_past_four_digits(text)
|
|
801
849
|
text.start_with?("-") || to_date.year < 10000 ? text : "+#{text}"
|
|
802
850
|
end
|
|
803
851
|
|
|
@@ -816,7 +864,7 @@ class CATime
|
|
|
816
864
|
def <=>(other)
|
|
817
865
|
case other
|
|
818
866
|
when Element
|
|
819
|
-
u =
|
|
867
|
+
u = CATime::UnitAlgebra.diff_unit(unit, other.unit)
|
|
820
868
|
instant_in(u) <=> other.instant_in(u)
|
|
821
869
|
when Time
|
|
822
870
|
to_time <=> other.getutc
|
|
@@ -825,7 +873,7 @@ class CATime
|
|
|
825
873
|
when (defined?(Date) ? Date : nil)
|
|
826
874
|
# a Date names the midnight (UTC) that begins it, as it does when an
|
|
827
875
|
# array is compared with it
|
|
828
|
-
to_time.to_r <=>
|
|
876
|
+
to_time.to_r <=> CATime::Literal.epoch_seconds(other)
|
|
829
877
|
end
|
|
830
878
|
rescue ArgumentError
|
|
831
879
|
nil
|
|
@@ -844,9 +892,9 @@ class CATime
|
|
|
844
892
|
end
|
|
845
893
|
|
|
846
894
|
# Marshal carries the tick and its resolution.
|
|
847
|
-
def marshal_dump = [value, unit.count, unit.base]
|
|
895
|
+
private def marshal_dump = [value, unit.count, unit.base]
|
|
848
896
|
|
|
849
|
-
def marshal_load(data)
|
|
897
|
+
private def marshal_load(data)
|
|
850
898
|
v, count, base = data
|
|
851
899
|
initialize(v, CATime::Resolution.new(count, base))
|
|
852
900
|
end
|
|
@@ -878,9 +926,9 @@ class CATime
|
|
|
878
926
|
def -(other)
|
|
879
927
|
case other
|
|
880
928
|
when Element
|
|
881
|
-
u =
|
|
929
|
+
u = CATime::UnitAlgebra.diff_unit(unit, other.unit)
|
|
882
930
|
v = instant_in(u) - other.instant_in(u)
|
|
883
|
-
|
|
931
|
+
CATime::UnitAlgebra.check_term(v, "-")
|
|
884
932
|
CATimedelta::Element.new(v, u)
|
|
885
933
|
when CATimedelta::Element, CATimedelta
|
|
886
934
|
shifted_by_duration(other, -1)
|
|
@@ -901,7 +949,7 @@ class CATime
|
|
|
901
949
|
# instant does not land on `to`'s grid).
|
|
902
950
|
def instant_in(to)
|
|
903
951
|
return value if to == unit
|
|
904
|
-
|
|
952
|
+
CATime::UnitAlgebra.convert_instant!(CA_INT64([value]), unit, to)[0]
|
|
905
953
|
end
|
|
906
954
|
|
|
907
955
|
private
|
|
@@ -930,16 +978,16 @@ class CATime
|
|
|
930
978
|
"CATime::Element #{sign > 0 ? '+' : '-'} #{td.class} is not " \
|
|
931
979
|
"allowed (add / subtract a CATimedelta)"
|
|
932
980
|
end
|
|
933
|
-
if
|
|
934
|
-
!
|
|
981
|
+
if CATime::UnitAlgebra::CALENDAR.key?(td.unit.base) &&
|
|
982
|
+
!CATime::UnitAlgebra::CALENDAR.key?(unit.base)
|
|
935
983
|
# months on a fixed grid move the date as Date#>> does, as the
|
|
936
984
|
# array does
|
|
937
|
-
months = sign * td.value *
|
|
938
|
-
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)
|
|
939
987
|
end
|
|
940
|
-
tv =
|
|
988
|
+
tv = CATime::UnitAlgebra.duration_on_grid(CA_INT64([td.value]), td.unit, unit)[0]
|
|
941
989
|
v = value + sign * tv
|
|
942
|
-
|
|
990
|
+
CATime::UnitAlgebra.check_term(v, sign > 0 ? "+" : "-")
|
|
943
991
|
Element.new(v, unit)
|
|
944
992
|
end
|
|
945
993
|
|
|
@@ -968,8 +1016,8 @@ class CATime
|
|
|
968
1016
|
end
|
|
969
1017
|
|
|
970
1018
|
class CATimedelta
|
|
971
|
-
include
|
|
972
|
-
include
|
|
1019
|
+
include CATime::SurfaceFill
|
|
1020
|
+
include CATime::OperandValues
|
|
973
1021
|
# @overload new(*shape, unit: :ns)
|
|
974
1022
|
# Allocates a new int64 storage CArray of the given `shape` and
|
|
975
1023
|
# wraps it as a CATimedelta Face with the given `unit`.
|
|
@@ -1007,7 +1055,7 @@ class CATimedelta
|
|
|
1007
1055
|
# use {#to_seconds_approx} when an estimate is acceptable.
|
|
1008
1056
|
# @raise [ArgumentError] for a :Y / :M unit.
|
|
1009
1057
|
def to_seconds
|
|
1010
|
-
unless
|
|
1058
|
+
unless CATime::UnitAlgebra::FIXED.key?(unit.base)
|
|
1011
1059
|
raise ArgumentError,
|
|
1012
1060
|
"CATimedelta #{unit} has no exact seconds (a :#{unit.base} is " \
|
|
1013
1061
|
"calendar-variable); use to_seconds_approx for an estimate"
|
|
@@ -1038,8 +1086,8 @@ class CATimedelta
|
|
|
1038
1086
|
# false). No explicit `==` is defined; Comparable derives it.
|
|
1039
1087
|
def <=>(other)
|
|
1040
1088
|
return nil unless other.is_a?(Element)
|
|
1041
|
-
return nil unless
|
|
1042
|
-
u =
|
|
1089
|
+
return nil unless CATime::UnitAlgebra.same_group?(unit, other.unit)
|
|
1090
|
+
u = CATime::UnitAlgebra.finer(unit, other.unit)
|
|
1043
1091
|
duration_in(u) <=> other.duration_in(u)
|
|
1044
1092
|
rescue ArgumentError
|
|
1045
1093
|
nil
|
|
@@ -1055,9 +1103,9 @@ class CATimedelta
|
|
|
1055
1103
|
end
|
|
1056
1104
|
|
|
1057
1105
|
# Marshal carries the tick and its resolution.
|
|
1058
|
-
def marshal_dump = [value, unit.count, unit.base]
|
|
1106
|
+
private def marshal_dump = [value, unit.count, unit.base]
|
|
1059
1107
|
|
|
1060
|
-
def marshal_load(data)
|
|
1108
|
+
private def marshal_load(data)
|
|
1061
1109
|
v, count, base = data
|
|
1062
1110
|
initialize(v, CATime::Resolution.new(count, base))
|
|
1063
1111
|
end
|
|
@@ -1086,7 +1134,7 @@ class CATimedelta
|
|
|
1086
1134
|
# Lets a duration stand on the right of a number, so `2 * d` is `d * 2`.
|
|
1087
1135
|
# @param number [Numeric]
|
|
1088
1136
|
# @return [Array]
|
|
1089
|
-
def coerce
|
|
1137
|
+
def coerce(number)
|
|
1090
1138
|
[LeftFactor.new(number), self]
|
|
1091
1139
|
end
|
|
1092
1140
|
|
|
@@ -1097,7 +1145,7 @@ class CATimedelta
|
|
|
1097
1145
|
def *(n)
|
|
1098
1146
|
raise TypeError, "CATimedelta::Element * #{n.class} is not allowed (Integer only)" unless n.is_a?(Integer)
|
|
1099
1147
|
v = value * n
|
|
1100
|
-
|
|
1148
|
+
CATime::UnitAlgebra.check_term(v, "*")
|
|
1101
1149
|
Element.new(v, unit)
|
|
1102
1150
|
end
|
|
1103
1151
|
|
|
@@ -1114,7 +1162,7 @@ class CATimedelta
|
|
|
1114
1162
|
case other
|
|
1115
1163
|
when Integer then Element.new(Rational(value, other).truncate, unit)
|
|
1116
1164
|
when Element
|
|
1117
|
-
u =
|
|
1165
|
+
u = CATime::UnitAlgebra.finer(unit, other.unit)
|
|
1118
1166
|
Rational(duration_in(u), other.duration_in(u))
|
|
1119
1167
|
else
|
|
1120
1168
|
raise TypeError, "CATimedelta::Element / #{other.class} is not allowed"
|
|
@@ -1127,7 +1175,7 @@ class CATimedelta
|
|
|
1127
1175
|
# cross-group or non-whole conversion).
|
|
1128
1176
|
def duration_in(to)
|
|
1129
1177
|
return value if to == unit
|
|
1130
|
-
|
|
1178
|
+
CATime::UnitAlgebra.convert_scale!(CA_INT64([value]), unit, to)[0]
|
|
1131
1179
|
end
|
|
1132
1180
|
|
|
1133
1181
|
private
|
|
@@ -1144,9 +1192,9 @@ class CATimedelta
|
|
|
1144
1192
|
raise TypeError,
|
|
1145
1193
|
"CATimedelta::Element #{sign > 0 ? '+' : '-'} #{other.class} is not allowed"
|
|
1146
1194
|
end
|
|
1147
|
-
u =
|
|
1195
|
+
u = CATime::UnitAlgebra.finer(unit, other.unit)
|
|
1148
1196
|
v = duration_in(u) + sign * other.duration_in(u)
|
|
1149
|
-
|
|
1197
|
+
CATime::UnitAlgebra.check_term(v, sign > 0 ? "+" : "-")
|
|
1150
1198
|
Element.new(v, u)
|
|
1151
1199
|
end
|
|
1152
1200
|
end
|
|
@@ -1174,7 +1222,9 @@ class CATime
|
|
|
1174
1222
|
# @overload ticks
|
|
1175
1223
|
# Returns the underlying int64 CArray of tick counts since the
|
|
1176
1224
|
# Unix epoch (1970-01-01 UTC) — the k-th tick of this array's
|
|
1177
|
-
# 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.
|
|
1178
1228
|
# @return [CArray]
|
|
1179
1229
|
def ticks
|
|
1180
1230
|
face_storage
|
|
@@ -1210,7 +1260,7 @@ class CATime
|
|
|
1210
1260
|
# The conversion hands back the storage itself when the unit does not
|
|
1211
1261
|
# change; the result is a new array whichever unit is asked for.
|
|
1212
1262
|
return face_storage.copy.time(unit: to) if to == self.unit
|
|
1213
|
-
|
|
1263
|
+
CATime::UnitAlgebra.convert_instant_floor(face_storage, self.unit, to).time(unit: to)
|
|
1214
1264
|
end
|
|
1215
1265
|
|
|
1216
1266
|
# @overload +(other)
|
|
@@ -1236,19 +1286,19 @@ class CATime
|
|
|
1236
1286
|
# @raise [ArgumentError] when the duration does not land on `self`'s grid.
|
|
1237
1287
|
# @raise [RangeError] when a result leaves the range of int64 ticks.
|
|
1238
1288
|
def +(other)
|
|
1239
|
-
given, other = other,
|
|
1240
|
-
other =
|
|
1241
|
-
|
|
1289
|
+
given, other = other, CATime::UnitAlgebra.operand(other)
|
|
1290
|
+
other = CATime::UnitAlgebra.step_operand(other) ||
|
|
1291
|
+
CATime::UnitAlgebra.scalar_operand(other, unit) || other
|
|
1242
1292
|
case other
|
|
1243
1293
|
when CATimedelta
|
|
1244
1294
|
return shifted_by_months(other, 1) if moved_by_months?(other)
|
|
1245
|
-
d =
|
|
1295
|
+
d = CATime::UnitAlgebra.duration_on_grid(other.parent, other.unit, unit)
|
|
1246
1296
|
d = d[0] if other.scalar?
|
|
1247
|
-
|
|
1297
|
+
CATime::UnitAlgebra.checked_add(face_storage, d).time(unit: unit)
|
|
1248
1298
|
when CATime
|
|
1249
1299
|
raise TypeError, "CATime + CATime is ill-defined"
|
|
1250
1300
|
else
|
|
1251
|
-
raise TypeError, "CATime + #{
|
|
1301
|
+
raise TypeError, "CATime + #{CATime::UnitAlgebra.operand_name(given)} is not allowed: add a duration " \
|
|
1252
1302
|
"(a CATimedelta, a CATimedelta::Element or a String such as \"3 days\")"
|
|
1253
1303
|
end
|
|
1254
1304
|
end
|
|
@@ -1266,23 +1316,23 @@ class CATime
|
|
|
1266
1316
|
# @return [CATime, CATimedelta]
|
|
1267
1317
|
# @raise [TypeError] on an unsupported operand.
|
|
1268
1318
|
def -(other)
|
|
1269
|
-
given, other = other,
|
|
1270
|
-
other =
|
|
1271
|
-
|
|
1319
|
+
given, other = other, CATime::UnitAlgebra.operand(other)
|
|
1320
|
+
other = CATime::UnitAlgebra.step_operand(other) ||
|
|
1321
|
+
CATime::UnitAlgebra.scalar_operand(other, unit) || other
|
|
1272
1322
|
case other
|
|
1273
1323
|
when CATimedelta
|
|
1274
1324
|
return shifted_by_months(other, -1) if moved_by_months?(other)
|
|
1275
|
-
d =
|
|
1325
|
+
d = CATime::UnitAlgebra.duration_on_grid(other.parent, other.unit, unit)
|
|
1276
1326
|
d = d[0] if other.scalar?
|
|
1277
|
-
|
|
1327
|
+
CATime::UnitAlgebra.checked_sub(face_storage, d).time(unit: unit)
|
|
1278
1328
|
when CATime
|
|
1279
|
-
u =
|
|
1280
|
-
a =
|
|
1281
|
-
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)
|
|
1282
1332
|
b = b[0] if other.scalar?
|
|
1283
|
-
|
|
1333
|
+
CATime::UnitAlgebra.checked_sub(a, b).timedelta(unit: u)
|
|
1284
1334
|
else
|
|
1285
|
-
raise TypeError, "CATime - #{
|
|
1335
|
+
raise TypeError, "CATime - #{CATime::UnitAlgebra.operand_name(given)} is not allowed: subtract a duration " \
|
|
1286
1336
|
"(a CATimedelta, a CATimedelta::Element or a String such as \"3 days\") " \
|
|
1287
1337
|
"or a time (a CATime, a CATime::Element, a Time, a Date or a String)"
|
|
1288
1338
|
end
|
|
@@ -1299,17 +1349,17 @@ class CATime
|
|
|
1299
1349
|
# duration on a time whose grid is fixed-length. A time on a month or year
|
|
1300
1350
|
# grid adds it as months already, and a fixed duration on a calendar grid
|
|
1301
1351
|
# is refused by the scale conversion.
|
|
1302
|
-
private def moved_by_months?
|
|
1303
|
-
|
|
1304
|
-
!
|
|
1352
|
+
private def moved_by_months?(td)
|
|
1353
|
+
CATime::UnitAlgebra::CALENDAR.key?(td.unit.base) &&
|
|
1354
|
+
!CATime::UnitAlgebra::CALENDAR.key?(unit.base)
|
|
1305
1355
|
end
|
|
1306
1356
|
|
|
1307
1357
|
# self moved by the months of +td+ (+sign+ 1 to add, -1 to subtract), as
|
|
1308
1358
|
# Ruby's Date#>> moves a date: the day is kept unless the month is shorter.
|
|
1309
|
-
private def shifted_by_months
|
|
1310
|
-
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))
|
|
1311
1361
|
months = months[0] if td.scalar?
|
|
1312
|
-
|
|
1362
|
+
CATime::UnitAlgebra.shift_months(face_storage, unit, months).time(unit: unit)
|
|
1313
1363
|
end
|
|
1314
1364
|
|
|
1315
1365
|
# Element-wise comparison (< <= > >= <=>) rides the core comparison Face
|
|
@@ -1361,8 +1411,9 @@ class CATime
|
|
|
1361
1411
|
# @overload percentile(*p, axis: nil, **opts)
|
|
1362
1412
|
# Returns the percentile instants on `self`'s unit, in the shapes the
|
|
1363
1413
|
# plain {CArray#percentile} uses: one `p` reduces to a single value
|
|
1364
|
-
# (an {Element}, or a {CATime} with `axis:`),
|
|
1365
|
-
# 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.
|
|
1366
1417
|
# @return [Element, CATime, Array<Element>, Array<CATime>]
|
|
1367
1418
|
def percentile(*args, **opts)
|
|
1368
1419
|
reduce_on_own_grid(:percentile, :time, args, opts)
|
|
@@ -1370,7 +1421,7 @@ class CATime
|
|
|
1370
1421
|
|
|
1371
1422
|
# @overload quantile(axis: nil, **opts)
|
|
1372
1423
|
# Returns the five quartile instants `[p0, p25, p50, p75, p100]` on
|
|
1373
|
-
# `self`'s unit (shorthand for `percentile(0, 25, 50, 75, 100)`).
|
|
1424
|
+
# `self`'s unit (shorthand for `percentile([0, 25, 50, 75, 100])`).
|
|
1374
1425
|
# @return [Array<Element>, Array<CATime>]
|
|
1375
1426
|
def quantile(*args, **opts)
|
|
1376
1427
|
reduce_on_own_grid(:quantile, :time, args, opts)
|
|
@@ -1478,37 +1529,50 @@ class CATime
|
|
|
1478
1529
|
# the joins on them) meets there, so it does not depend on which array is
|
|
1479
1530
|
# the receiver.
|
|
1480
1531
|
# @api private
|
|
1481
|
-
def in_common_unit_with
|
|
1532
|
+
private def in_common_unit_with(other)
|
|
1482
1533
|
return [self, other] if other.unit == unit
|
|
1483
|
-
u =
|
|
1534
|
+
u = CATime::UnitAlgebra.diff_unit(unit, other.unit)
|
|
1484
1535
|
[to_unit(u), other.to_unit(u)]
|
|
1485
1536
|
end
|
|
1486
1537
|
|
|
1487
|
-
private def common_unit
|
|
1488
|
-
|
|
1538
|
+
private def common_unit(a, b)
|
|
1539
|
+
CATime::UnitAlgebra.diff_unit(a, b)
|
|
1489
1540
|
end
|
|
1490
1541
|
|
|
1491
1542
|
# One instant as a length-1 time array in its own unit, or nil.
|
|
1492
|
-
private def lift_value
|
|
1543
|
+
private def lift_value(operand)
|
|
1493
1544
|
case operand
|
|
1494
1545
|
when CATime::Element
|
|
1495
1546
|
CATime.wrap(CA_INT64([operand.value]), unit: operand.unit)
|
|
1496
1547
|
when Time, String, Date
|
|
1497
|
-
|
|
1548
|
+
CATime::Literal.exact_instant(operand)
|
|
1498
1549
|
end
|
|
1499
1550
|
end
|
|
1500
1551
|
|
|
1501
|
-
|
|
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)
|
|
1502
1566
|
case operand
|
|
1503
1567
|
when CATime
|
|
1504
1568
|
return operand if operand.unit == unit
|
|
1505
|
-
|
|
1569
|
+
CATime::UnitAlgebra.convert_instant!(operand.parent, operand.unit, unit)
|
|
1506
1570
|
.time(unit: unit)
|
|
1507
1571
|
when CATime::Element
|
|
1508
1572
|
lifted = CATime.wrap(CA_INT64([operand.value]), unit: operand.unit)
|
|
1509
1573
|
to_comparable(lifted)
|
|
1510
1574
|
when Time, String, Date
|
|
1511
|
-
to_comparable(
|
|
1575
|
+
to_comparable(CATime::Literal.exact_instant(operand))
|
|
1512
1576
|
when Array, CArray
|
|
1513
1577
|
# A set of instants written one by one (is_in(["2024-01-02", ...])),
|
|
1514
1578
|
# or an array that is not a time array (an object column of Time
|
|
@@ -1528,16 +1592,18 @@ class CATime
|
|
|
1528
1592
|
# store round-trips with a fetch. A {Element} / `Time` / `Date` /
|
|
1529
1593
|
# `DateTime` / `String` is reconciled to self's unit via {#to_comparable}
|
|
1530
1594
|
# (same lossless discipline: a cross-group unit or a non-exact
|
|
1531
|
-
# finer->coarser cast raises).
|
|
1532
|
-
#
|
|
1533
|
-
# @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]
|
|
1534
1598
|
# @return [Integer, Object] the storage-domain value, or `surface`
|
|
1535
1599
|
# unchanged for a pass-through type.
|
|
1536
1600
|
# @raise [TypeError, ArgumentError] on an unreconcilable surface / unit.
|
|
1537
|
-
def element_to_storage
|
|
1601
|
+
private def element_to_storage(surface)
|
|
1538
1602
|
case surface
|
|
1539
1603
|
when Integer
|
|
1540
|
-
|
|
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"
|
|
1541
1607
|
else
|
|
1542
1608
|
to_comparable(surface).parent[0]
|
|
1543
1609
|
end
|
|
@@ -1574,7 +1640,7 @@ class CATime
|
|
|
1574
1640
|
# @param axis [Integer, nil]
|
|
1575
1641
|
# @return [Element, CATime] an {Element} for a scalar `addr`, `nil` when
|
|
1576
1642
|
# that single answer is undetermined.
|
|
1577
|
-
def linear_fetch
|
|
1643
|
+
def linear_fetch(addr, **opts)
|
|
1578
1644
|
r = face_storage.float64.linear_fetch(addr, **opts)
|
|
1579
1645
|
case r
|
|
1580
1646
|
when CArray
|
|
@@ -1604,7 +1670,7 @@ class CATime
|
|
|
1604
1670
|
# CArray. Array-level counterpart to {Element#to_time}.
|
|
1605
1671
|
# @return [CArray]
|
|
1606
1672
|
def to_time
|
|
1607
|
-
if
|
|
1673
|
+
if CATime::UnitAlgebra::FIXED.key?(unit.base)
|
|
1608
1674
|
f = unit.tick_ratio # exact seconds / tick (Rational)
|
|
1609
1675
|
face_storage.convert(:object) {|v| Time.at(v * f, in: 'UTC')}
|
|
1610
1676
|
else # calendar: exact granule midnight
|
|
@@ -1642,7 +1708,7 @@ class CATime
|
|
|
1642
1708
|
case unit.base
|
|
1643
1709
|
when :Y then face_storage * unit.count + 1970
|
|
1644
1710
|
when :M then (face_storage * unit.count + 1970 * 12) / 12
|
|
1645
|
-
else
|
|
1711
|
+
else CATime::Civil.civil_from_days(days_since_epoch)[0]
|
|
1646
1712
|
end
|
|
1647
1713
|
end
|
|
1648
1714
|
|
|
@@ -1655,7 +1721,7 @@ class CATime
|
|
|
1655
1721
|
when :M
|
|
1656
1722
|
mo = face_storage * unit.count + 1970 * 12
|
|
1657
1723
|
mo % 12 + 1
|
|
1658
|
-
else
|
|
1724
|
+
else CATime::Civil.civil_from_days(days_since_epoch)[1]
|
|
1659
1725
|
end
|
|
1660
1726
|
end
|
|
1661
1727
|
|
|
@@ -1665,7 +1731,7 @@ class CATime
|
|
|
1665
1731
|
def day
|
|
1666
1732
|
case unit.base
|
|
1667
1733
|
when :Y, :M then face_storage * 0 + 1
|
|
1668
|
-
else
|
|
1734
|
+
else CATime::Civil.civil_from_days(days_since_epoch)[2]
|
|
1669
1735
|
end
|
|
1670
1736
|
end
|
|
1671
1737
|
|
|
@@ -1694,7 +1760,7 @@ class CATime
|
|
|
1694
1760
|
def iso_week
|
|
1695
1761
|
thursday, year = iso_thursday
|
|
1696
1762
|
ones = CArray.int64(*shape) { 1 }
|
|
1697
|
-
(thursday -
|
|
1763
|
+
(thursday - CATime::Civil.days_from_civil(year, ones, ones)) / 7 + 1
|
|
1698
1764
|
end
|
|
1699
1765
|
|
|
1700
1766
|
# The ISO 8601 year of each time's week (see {#iso_week}): the calendar
|
|
@@ -1711,7 +1777,7 @@ class CATime
|
|
|
1711
1777
|
days = days_since_epoch
|
|
1712
1778
|
iso_weekday = (days + 3) % 7 + 1 # Monday 1 .. Sunday 7; 1970-01-01 is a Thursday
|
|
1713
1779
|
thursday = days - iso_weekday + 4
|
|
1714
|
-
[thursday,
|
|
1780
|
+
[thursday, CATime::Civil.civil_from_days(thursday)[0]]
|
|
1715
1781
|
end
|
|
1716
1782
|
|
|
1717
1783
|
# Number of days in the month, 28..31 (January's for `:Y` storage, which
|
|
@@ -1719,12 +1785,12 @@ class CATime
|
|
|
1719
1785
|
# the month.
|
|
1720
1786
|
# @return [CArray] integer.
|
|
1721
1787
|
def days_in_month
|
|
1722
|
-
y, m, =
|
|
1788
|
+
y, m, = CATime::Civil.civil_from_days(days_since_epoch)
|
|
1723
1789
|
ones = CArray.int64(*shape) { 1 }
|
|
1724
1790
|
ord = y * 12 + m # the next month's ordinal (m is 1..12)
|
|
1725
1791
|
ny = ord / 12
|
|
1726
|
-
|
|
1727
|
-
|
|
1792
|
+
CATime::Civil.days_from_civil(ny, ord - ny * 12 + 1, ones) -
|
|
1793
|
+
CATime::Civil.days_from_civil(y, m, ones)
|
|
1728
1794
|
end
|
|
1729
1795
|
|
|
1730
1796
|
# Day of the week, Sunday = 0 .. Saturday = 6.
|
|
@@ -1739,9 +1805,9 @@ class CATime
|
|
|
1739
1805
|
# @return [CArray] integer.
|
|
1740
1806
|
def yday
|
|
1741
1807
|
d = days_since_epoch
|
|
1742
|
-
y =
|
|
1808
|
+
y = CATime::Civil.civil_from_days(d)[0]
|
|
1743
1809
|
ones = CArray.int64(*shape) { 1 }
|
|
1744
|
-
d -
|
|
1810
|
+
d - CATime::Civil.days_from_civil(y, ones, ones) + 1
|
|
1745
1811
|
end
|
|
1746
1812
|
# @!endgroup
|
|
1747
1813
|
|
|
@@ -1749,7 +1815,7 @@ class CATime
|
|
|
1749
1815
|
# Returns the Julian Day Number for each element.
|
|
1750
1816
|
# @return [CArray]
|
|
1751
1817
|
def jd
|
|
1752
|
-
to_time.convert(:
|
|
1818
|
+
to_time.convert(:int32) {|t| t.to_date.jd}
|
|
1753
1819
|
end
|
|
1754
1820
|
|
|
1755
1821
|
# @overload ajd
|
|
@@ -1757,7 +1823,7 @@ class CATime
|
|
|
1757
1823
|
# day) for each element.
|
|
1758
1824
|
# @return [CArray]
|
|
1759
1825
|
def ajd
|
|
1760
|
-
to_time.convert(:
|
|
1826
|
+
to_time.convert(:float64) {|t| t.to_datetime.ajd.to_f}
|
|
1761
1827
|
end
|
|
1762
1828
|
|
|
1763
1829
|
# @overload is_leap
|
|
@@ -1889,7 +1955,7 @@ class CATimedelta
|
|
|
1889
1955
|
to = CATime::Resolution.parse(unit)
|
|
1890
1956
|
# As CATime#to_unit: a new array even when the unit does not change.
|
|
1891
1957
|
return face_storage.copy.timedelta(unit: to) if to == self.unit
|
|
1892
|
-
|
|
1958
|
+
CATime::UnitAlgebra.convert_scale_trunc(face_storage, self.unit, to).timedelta(unit: to)
|
|
1893
1959
|
end
|
|
1894
1960
|
|
|
1895
1961
|
# @overload +(other)
|
|
@@ -1902,21 +1968,21 @@ class CATimedelta
|
|
|
1902
1968
|
# @return [CATimedelta, CATime]
|
|
1903
1969
|
# @raise [TypeError] on incompatible operands.
|
|
1904
1970
|
def +(other)
|
|
1905
|
-
given, other = other,
|
|
1906
|
-
other =
|
|
1971
|
+
given, other = other, CATime::UnitAlgebra.operand(other)
|
|
1972
|
+
other = CATime::UnitAlgebra.scalar_operand(other, unit) || other
|
|
1907
1973
|
case other
|
|
1908
1974
|
when CATimedelta
|
|
1909
1975
|
u = common_duration_unit(other.unit)
|
|
1910
|
-
a =
|
|
1911
|
-
b =
|
|
1976
|
+
a = CATime::UnitAlgebra.convert_scale!(face_storage, unit, u)
|
|
1977
|
+
b = CATime::UnitAlgebra.convert_scale!(other.parent, other.unit, u)
|
|
1912
1978
|
b = b[0] if other.scalar?
|
|
1913
|
-
|
|
1979
|
+
CATime::UnitAlgebra.checked_add(a, b).timedelta(unit: u)
|
|
1914
1980
|
when CATime
|
|
1915
1981
|
return other + self unless other.scalar? # commutative -> time's unit
|
|
1916
|
-
d =
|
|
1917
|
-
|
|
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)
|
|
1918
1984
|
else
|
|
1919
|
-
raise TypeError, "CATimedelta + #{
|
|
1985
|
+
raise TypeError, "CATimedelta + #{CATime::UnitAlgebra.operand_name(given)} is not allowed"
|
|
1920
1986
|
end
|
|
1921
1987
|
end
|
|
1922
1988
|
|
|
@@ -1929,17 +1995,17 @@ class CATimedelta
|
|
|
1929
1995
|
# @return [CATimedelta]
|
|
1930
1996
|
# @raise [TypeError] on a non-timedelta operand.
|
|
1931
1997
|
def -(other)
|
|
1932
|
-
given, other = other,
|
|
1933
|
-
other =
|
|
1998
|
+
given, other = other, CATime::UnitAlgebra.operand(other)
|
|
1999
|
+
other = CATime::UnitAlgebra.scalar_operand(other, unit) || other
|
|
1934
2000
|
case other
|
|
1935
2001
|
when CATimedelta
|
|
1936
2002
|
u = common_duration_unit(other.unit)
|
|
1937
|
-
a =
|
|
1938
|
-
b =
|
|
2003
|
+
a = CATime::UnitAlgebra.convert_scale!(face_storage, unit, u)
|
|
2004
|
+
b = CATime::UnitAlgebra.convert_scale!(other.parent, other.unit, u)
|
|
1939
2005
|
b = b[0] if other.scalar?
|
|
1940
|
-
|
|
2006
|
+
CATime::UnitAlgebra.checked_sub(a, b).timedelta(unit: u)
|
|
1941
2007
|
else
|
|
1942
|
-
raise TypeError, "CATimedelta - #{
|
|
2008
|
+
raise TypeError, "CATimedelta - #{CATime::UnitAlgebra.operand_name(given)} is not allowed"
|
|
1943
2009
|
end
|
|
1944
2010
|
end
|
|
1945
2011
|
|
|
@@ -1965,8 +2031,8 @@ class CATimedelta
|
|
|
1965
2031
|
# @raise [TypeError] when `other` is not an Integer.
|
|
1966
2032
|
def *(other)
|
|
1967
2033
|
case other
|
|
1968
|
-
when Integer then
|
|
1969
|
-
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)"
|
|
1970
2036
|
end
|
|
1971
2037
|
end
|
|
1972
2038
|
|
|
@@ -1981,35 +2047,35 @@ class CATimedelta
|
|
|
1981
2047
|
# @return [CATimedelta, CArray]
|
|
1982
2048
|
# @raise [TypeError] on incompatible operands.
|
|
1983
2049
|
def /(other)
|
|
1984
|
-
given, other = other,
|
|
1985
|
-
other =
|
|
2050
|
+
given, other = other, CATime::UnitAlgebra.operand(other)
|
|
2051
|
+
other = CATime::UnitAlgebra.scalar_operand(other, unit) || other
|
|
1986
2052
|
case other
|
|
1987
2053
|
when Integer
|
|
1988
|
-
|
|
2054
|
+
CATime::UnitAlgebra.trunc_divide(face_storage, other).timedelta(unit: unit)
|
|
1989
2055
|
when CATimedelta
|
|
1990
2056
|
# A ratio of durations is a number, not a count of whole ticks: 90 s /
|
|
1991
2057
|
# 60 s is 1.5. Both are brought to one unit first, so the ratio does
|
|
1992
2058
|
# not depend on the units they are held in.
|
|
1993
2059
|
u = common_duration_unit(other.unit)
|
|
1994
|
-
|
|
1995
|
-
|
|
2060
|
+
CATime::UnitAlgebra.convert_scale!(face_storage, unit, u).to_type(:float64) /
|
|
2061
|
+
CATime::UnitAlgebra.convert_scale!(other.parent, other.unit, u)
|
|
1996
2062
|
else raise TypeError, "CATimedelta / #{given.class} is not allowed"
|
|
1997
2063
|
end
|
|
1998
2064
|
end
|
|
1999
2065
|
|
|
2000
2066
|
# A number on the left of a duration: only `n * duration` has an answer.
|
|
2001
2067
|
class LeftFactor
|
|
2002
|
-
def initialize
|
|
2068
|
+
def initialize(factor)
|
|
2003
2069
|
@factor = factor
|
|
2004
2070
|
end
|
|
2005
2071
|
|
|
2006
|
-
def *
|
|
2072
|
+
def *(duration) = duration * @factor
|
|
2007
2073
|
|
|
2008
|
-
def method_missing
|
|
2074
|
+
def method_missing(op, duration, *)
|
|
2009
2075
|
raise TypeError, "#{@factor.class} #{op} #{duration.class} is not allowed"
|
|
2010
2076
|
end
|
|
2011
2077
|
|
|
2012
|
-
def respond_to_missing?
|
|
2078
|
+
def respond_to_missing?(*) = true
|
|
2013
2079
|
end
|
|
2014
2080
|
private_constant :LeftFactor
|
|
2015
2081
|
|
|
@@ -2017,7 +2083,7 @@ class CATimedelta
|
|
|
2017
2083
|
# Lets a duration stand on the right of a number, so `2 * td` is `td * 2`.
|
|
2018
2084
|
# @param number [Numeric]
|
|
2019
2085
|
# @return [Array]
|
|
2020
|
-
def coerce
|
|
2086
|
+
def coerce(number)
|
|
2021
2087
|
number.is_a?(Numeric) ? [LeftFactor.new(number), self] : super
|
|
2022
2088
|
end
|
|
2023
2089
|
|
|
@@ -2026,7 +2092,7 @@ class CATimedelta
|
|
|
2026
2092
|
# @return [CArray] an object array of Rational.
|
|
2027
2093
|
# @raise [ArgumentError] for a :Y / :M unit, which has no exact seconds.
|
|
2028
2094
|
def to_seconds
|
|
2029
|
-
unless
|
|
2095
|
+
unless CATime::UnitAlgebra::FIXED.key?(unit.base)
|
|
2030
2096
|
raise ArgumentError,
|
|
2031
2097
|
"CATimedelta #{unit} has no exact seconds (a :#{unit.base} is " \
|
|
2032
2098
|
"calendar-variable); use to_seconds_approx for an estimate"
|
|
@@ -2083,7 +2149,7 @@ class CATimedelta
|
|
|
2083
2149
|
# whenever the true sum fits int64, whatever order the cells were added
|
|
2084
2150
|
# in. Whether it fits is read from the float64 sum, whose error is far
|
|
2085
2151
|
# inside the factor of 2 between 2**62 and 2**63.
|
|
2086
|
-
def exact_sum_relift
|
|
2152
|
+
def exact_sum_relift(exact, approx)
|
|
2087
2153
|
past = case approx
|
|
2088
2154
|
when Numeric then approx.abs > 2.0**62
|
|
2089
2155
|
when CArray then (approx.abs > 2.0**62).any
|
|
@@ -2152,7 +2218,8 @@ class CATimedelta
|
|
|
2152
2218
|
# @overload percentile(*p, axis: nil, **opts)
|
|
2153
2219
|
# Returns the percentile durations on `self`'s unit, in the shapes the
|
|
2154
2220
|
# plain {CArray#percentile} uses (one `p` reduces to a single value,
|
|
2155
|
-
#
|
|
2221
|
+
# an Array of `p` gives an Array, and several `p` one {CATimedelta}
|
|
2222
|
+
# with the `p` axes in front).
|
|
2156
2223
|
# @return [Element, CATimedelta, Array<Element>, Array<CATimedelta>]
|
|
2157
2224
|
def percentile(*args, **opts)
|
|
2158
2225
|
with_surface_fill(opts) { |o| round_and_relift(face_storage.percentile(*args, **o)) }
|
|
@@ -2238,23 +2305,23 @@ class CATimedelta
|
|
|
2238
2305
|
# array is the receiver. A calendar and a fixed-length duration have no
|
|
2239
2306
|
# common unit and raise.
|
|
2240
2307
|
# @api private
|
|
2241
|
-
def in_common_unit_with
|
|
2308
|
+
private def in_common_unit_with(other)
|
|
2242
2309
|
return [self, other] if other.unit == unit
|
|
2243
|
-
unless
|
|
2310
|
+
unless CATime::UnitAlgebra.same_group?(unit, other.unit)
|
|
2244
2311
|
raise ArgumentError,
|
|
2245
2312
|
"cannot compare a #{unit} duration with a #{other.unit} duration: " \
|
|
2246
2313
|
"a calendar length (months, years) has no fixed length in days"
|
|
2247
2314
|
end
|
|
2248
|
-
u =
|
|
2315
|
+
u = CATime::UnitAlgebra.finer(unit, other.unit)
|
|
2249
2316
|
[to_unit(u), other.to_unit(u)]
|
|
2250
2317
|
end
|
|
2251
2318
|
|
|
2252
|
-
private def common_unit
|
|
2253
|
-
|
|
2319
|
+
private def common_unit(a, b)
|
|
2320
|
+
CATime::UnitAlgebra.finer(a, b)
|
|
2254
2321
|
end
|
|
2255
2322
|
|
|
2256
2323
|
# One duration as a length-1 duration array in its own unit, or nil.
|
|
2257
|
-
private def lift_value
|
|
2324
|
+
private def lift_value(operand)
|
|
2258
2325
|
case operand
|
|
2259
2326
|
when CATimedelta::Element
|
|
2260
2327
|
CATimedelta.wrap(CA_INT64([operand.value]), unit: operand.unit)
|
|
@@ -2264,11 +2331,23 @@ class CATimedelta
|
|
|
2264
2331
|
end
|
|
2265
2332
|
end
|
|
2266
2333
|
|
|
2267
|
-
|
|
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)
|
|
2268
2347
|
case operand
|
|
2269
2348
|
when CATimedelta
|
|
2270
2349
|
return operand if operand.unit == unit
|
|
2271
|
-
|
|
2350
|
+
CATime::UnitAlgebra.convert_scale!(operand.parent, operand.unit, unit)
|
|
2272
2351
|
.timedelta(unit: unit)
|
|
2273
2352
|
when CATimedelta::Element
|
|
2274
2353
|
lifted = CATimedelta.wrap(CA_INT64([operand.value]), unit: operand.unit)
|
|
@@ -2292,23 +2371,25 @@ class CATimedelta
|
|
|
2292
2371
|
# scalar store round-trips with a fetch. A {Element} is reconciled to
|
|
2293
2372
|
# self's unit via {#to_comparable} (lossless discipline; a `Time` /
|
|
2294
2373
|
# `DateTime` is an absolute instant, not a duration, so it raises there).
|
|
2295
|
-
# A String that names a duration (`"3 days"`) is read as one.
|
|
2296
|
-
# Integer
|
|
2297
|
-
#
|
|
2298
|
-
# @param surface [Element,
|
|
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]
|
|
2299
2378
|
# @return [Integer, Object] the storage-domain value, or `surface`
|
|
2300
2379
|
# unchanged for a pass-through type.
|
|
2301
2380
|
# @raise [TypeError, ArgumentError] on an unreconcilable surface / unit.
|
|
2302
|
-
def element_to_storage
|
|
2381
|
+
private def element_to_storage(surface)
|
|
2303
2382
|
case surface
|
|
2304
2383
|
when Integer
|
|
2305
|
-
|
|
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"
|
|
2306
2387
|
when String
|
|
2307
2388
|
begin
|
|
2308
2389
|
to_comparable(surface).parent[0]
|
|
2309
2390
|
rescue ArgumentError => e
|
|
2310
2391
|
raise unless e.message.start_with?("invalid unit spec")
|
|
2311
|
-
surface
|
|
2392
|
+
raise ArgumentError, "#{CATime::UnitAlgebra.method_called_by_user}: #{surface.inspect} cannot be read as a time difference"
|
|
2312
2393
|
end
|
|
2313
2394
|
else
|
|
2314
2395
|
to_comparable(surface).parent[0]
|
|
@@ -2330,7 +2411,7 @@ class CATimedelta
|
|
|
2330
2411
|
# @param axis [Integer, nil]
|
|
2331
2412
|
# @return [Element, CATimedelta] an {Element} for a scalar `addr`, `nil`
|
|
2332
2413
|
# when that single answer is undetermined.
|
|
2333
|
-
def linear_fetch
|
|
2414
|
+
def linear_fetch(addr, **opts)
|
|
2334
2415
|
r = face_storage.float64.linear_fetch(addr, **opts)
|
|
2335
2416
|
case r
|
|
2336
2417
|
when CArray then r.mask_invalid.round.int64.timedelta(unit: unit)
|
|
@@ -2345,12 +2426,12 @@ class CATimedelta
|
|
|
2345
2426
|
# cross-group pair (a month vs a second) has no common duration grid, so it
|
|
2346
2427
|
# raises rather than mixing calendar and fixed durations.
|
|
2347
2428
|
def common_duration_unit(ou)
|
|
2348
|
-
unless
|
|
2429
|
+
unless CATime::UnitAlgebra.same_group?(unit, ou)
|
|
2349
2430
|
raise ArgumentError,
|
|
2350
2431
|
"cannot combine a #{unit} duration with a #{ou} duration " \
|
|
2351
2432
|
"across the calendar/fixed boundary"
|
|
2352
2433
|
end
|
|
2353
|
-
|
|
2434
|
+
CATime::UnitAlgebra.finer(unit, ou)
|
|
2354
2435
|
end
|
|
2355
2436
|
|
|
2356
2437
|
# inspect's name for the array: the class and the tick, which the storage
|
|
@@ -2365,12 +2446,13 @@ class CATimedelta
|
|
|
2365
2446
|
end
|
|
2366
2447
|
end
|
|
2367
2448
|
|
|
2368
|
-
# Reads a time literal (Time / DateTime /
|
|
2449
|
+
# Reads a time literal (Time / Date / DateTime / String / element) into
|
|
2369
2450
|
# the pieces the CATime constructors need. All parsing is UTC (an explicit
|
|
2370
2451
|
# offset is honoured; otherwise UTC) and DateTime-independent (Date._parse +
|
|
2371
2452
|
# a civil-date kernel). Internal to the time surface: nothing here is part
|
|
2372
2453
|
# of the public API.
|
|
2373
|
-
|
|
2454
|
+
# @api private
|
|
2455
|
+
module CATime::Literal
|
|
2374
2456
|
module_function
|
|
2375
2457
|
|
|
2376
2458
|
# Date fields of a String literal, UTC. Ruby's Date._parse does not read
|
|
@@ -2533,26 +2615,30 @@ module CATimeLiteral
|
|
|
2533
2615
|
end
|
|
2534
2616
|
|
|
2535
2617
|
# Exact Rational seconds since the Unix epoch for a start literal (Time /
|
|
2536
|
-
# 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.
|
|
2537
2620
|
def epoch_seconds(spec, format = nil)
|
|
2538
2621
|
case spec
|
|
2539
2622
|
when Time then spec.to_r
|
|
2540
|
-
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)"
|
|
2541
2627
|
when CATime::Element
|
|
2542
2628
|
# A time element is already an instant, so it needs no parsing: a
|
|
2543
2629
|
# fixed unit counts seconds directly, and a calendar one names the
|
|
2544
2630
|
# first midnight of its granule (the same instant convert_instant!
|
|
2545
2631
|
# gives it). This is what lets floor / ceil / to_unit output feed
|
|
2546
2632
|
# straight back into time_range / time_series / .time.
|
|
2547
|
-
if
|
|
2633
|
+
if CATime::UnitAlgebra::FIXED.key?(spec.unit.base)
|
|
2548
2634
|
Rational(spec.value) * spec.unit.tick_ratio
|
|
2549
2635
|
else
|
|
2550
|
-
Rational(
|
|
2636
|
+
Rational(CATime::UnitAlgebra.calendar_days_since_epoch(CA_INT64([spec.value]),
|
|
2551
2637
|
spec.unit)[0] * 86400)
|
|
2552
2638
|
end
|
|
2553
2639
|
when String
|
|
2554
2640
|
h = parse_date_fields(spec, format)
|
|
2555
|
-
days =
|
|
2641
|
+
days = CATime::Civil.days_from_civil_1(h[:year], h[:mon] || 1, h[:mday] || 1)
|
|
2556
2642
|
sec = Rational(days * 86400 + (h[:hour] || 0) * 3600 +
|
|
2557
2643
|
(h[:min] || 0) * 60 + (h[:sec] || 0))
|
|
2558
2644
|
sec += h[:sec_fraction] if h[:sec_fraction]
|
|
@@ -2562,7 +2648,7 @@ module CATimeLiteral
|
|
|
2562
2648
|
if defined?(DateTime) && spec.is_a?(DateTime)
|
|
2563
2649
|
spec.to_time.to_r
|
|
2564
2650
|
elsif defined?(Date) && spec.is_a?(Date)
|
|
2565
|
-
Rational(
|
|
2651
|
+
Rational(CATime::Civil.days_from_civil_1(spec.year, spec.month, spec.day) * 86400)
|
|
2566
2652
|
else
|
|
2567
2653
|
raise ArgumentError, "cannot parse time #{spec.class}"
|
|
2568
2654
|
end
|
|
@@ -2573,7 +2659,9 @@ module CATimeLiteral
|
|
|
2573
2659
|
def year_month(spec, format = nil)
|
|
2574
2660
|
case spec
|
|
2575
2661
|
when Time then t = spec.utc; [t.year, t.month]
|
|
2576
|
-
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)"
|
|
2577
2665
|
when CATime::Element
|
|
2578
2666
|
# A calendar element is a month ordinal already, so read it as one
|
|
2579
2667
|
# rather than going out through Time (which a :Y / :M element would
|
|
@@ -2599,7 +2687,7 @@ module CATimeLiteral
|
|
|
2599
2687
|
|
|
2600
2688
|
# Tick index of `spec`'s instant on the `res` grid (floor toward the past).
|
|
2601
2689
|
def tick_index(spec, res, format = nil)
|
|
2602
|
-
if
|
|
2690
|
+
if CATime::UnitAlgebra::FIXED.key?(res.base)
|
|
2603
2691
|
(epoch_seconds(spec, format) / res.tick_ratio).floor
|
|
2604
2692
|
else
|
|
2605
2693
|
y, m = year_month(spec, format)
|
|
@@ -2703,7 +2791,7 @@ module CATimeLiteral
|
|
|
2703
2791
|
# (:fixed) or months (:calendar), or nil when `res` is finer than a
|
|
2704
2792
|
# nanosecond.
|
|
2705
2793
|
def tick_kind(res)
|
|
2706
|
-
if
|
|
2794
|
+
if CATime::UnitAlgebra::FIXED.key?(res.base)
|
|
2707
2795
|
per = res.tick_ratio * 1_000_000_000
|
|
2708
2796
|
kind = :fixed
|
|
2709
2797
|
else
|
|
@@ -2720,7 +2808,7 @@ module CATimeLiteral
|
|
|
2720
2808
|
# otherwise each String is parsed into fields here. Either way the fields
|
|
2721
2809
|
# become ticks for the whole array at once, and a cell that is not a
|
|
2722
2810
|
# String goes through tick_index.
|
|
2723
|
-
def ticks_from_cells(x, res, format, on_error
|
|
2811
|
+
def ticks_from_cells(x, res, format, on_error)
|
|
2724
2812
|
kind, per = tick_kind(res)
|
|
2725
2813
|
return nil unless kind
|
|
2726
2814
|
if format.is_a?(String)
|
|
@@ -2740,6 +2828,8 @@ module CATimeLiteral
|
|
|
2740
2828
|
direct = {}
|
|
2741
2829
|
cells.each_with_index do |s, i|
|
|
2742
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?
|
|
2743
2833
|
begin
|
|
2744
2834
|
if s.is_a?(String)
|
|
2745
2835
|
h = parse_date_fields(s, format)
|
|
@@ -2750,10 +2840,6 @@ module CATimeLiteral
|
|
|
2750
2840
|
frac[i] = (h[:sec_fraction] * 1_000_000_000).floor if h[:sec_fraction]
|
|
2751
2841
|
off[i] = h[:offset] if h[:offset]
|
|
2752
2842
|
present[i] = 1
|
|
2753
|
-
elsif s.is_a?(Integer) && format.nil?
|
|
2754
|
-
# an Integer counts ticks of the unit given, or seconds when the
|
|
2755
|
-
# unit is chosen from the values
|
|
2756
|
-
direct[i] = seconds ? tick_index(s, res) : s
|
|
2757
2843
|
else
|
|
2758
2844
|
direct[i] = tick_index(s, res, format)
|
|
2759
2845
|
end
|
|
@@ -2822,17 +2908,16 @@ module CATimeLiteral
|
|
|
2822
2908
|
end
|
|
2823
2909
|
cells.each do |v|
|
|
2824
2910
|
case v
|
|
2825
|
-
when Integer then units << :s
|
|
2826
2911
|
when CATime::Element then units << v.unit
|
|
2827
|
-
when Time then units <<
|
|
2912
|
+
when Time then units << CATime::Literal.exact_instant(v).unit
|
|
2828
2913
|
when Date
|
|
2829
|
-
units << (defined?(DateTime) && v.is_a?(DateTime) ?
|
|
2914
|
+
units << (defined?(DateTime) && v.is_a?(DateTime) ? CATime::Literal.exact_instant(v.to_time).unit : :D)
|
|
2830
2915
|
end
|
|
2831
2916
|
end
|
|
2832
2917
|
# A month beside a day is read on the fixed grid both instants reach,
|
|
2833
2918
|
# as two elements are compared (diff_unit); finer covers one group only.
|
|
2834
2919
|
units.map { |u| CATime::Resolution.parse(u) }
|
|
2835
|
-
.reduce { |a, b|
|
|
2920
|
+
.reduce { |a, b| CATime::UnitAlgebra.diff_unit(a, b) } || CATime::Resolution.parse(:s)
|
|
2836
2921
|
end
|
|
2837
2922
|
|
|
2838
2923
|
# The unit a String read with Date._parse shows, or nil when it does not
|
|
@@ -2894,7 +2979,7 @@ class CArray
|
|
|
2894
2979
|
# `unit` (= a {CATime::Resolution}, tick = count * base). The value
|
|
2895
2980
|
# is the k-th tick since the Unix epoch (1970-01-01 UTC): e.g. unit
|
|
2896
2981
|
# `"10 minutes"` value 3 = 1970-01-01T00:30:00Z. Literals are read by
|
|
2897
|
-
# {
|
|
2982
|
+
# {CATime::Literal} (UTC).
|
|
2898
2983
|
|
|
2899
2984
|
# @overload time_range(start, last, unit:, step: nil, format: nil)
|
|
2900
2985
|
# Returns a {CATime} from `start` to `last` inclusive on the `unit`
|
|
@@ -2905,9 +2990,10 @@ class CArray
|
|
|
2905
2990
|
# bucket head (toward the past); the phase is anchored at `start`, and
|
|
2906
2991
|
# `last` is a bound rather than a member -- the series stops at the last
|
|
2907
2992
|
# step at or before it.
|
|
2908
|
-
# @param start [Time, String,
|
|
2909
|
-
# instant.
|
|
2910
|
-
#
|
|
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
|
|
2911
2997
|
# instant (inclusive).
|
|
2912
2998
|
# @param unit [Resolution, Symbol, String] grid resolution (tick).
|
|
2913
2999
|
# @param step [Resolution, Symbol, String, nil] spacing between elements
|
|
@@ -2920,9 +3006,9 @@ class CArray
|
|
|
2920
3006
|
def self.time_range(start, last, unit:, step: nil, format: nil)
|
|
2921
3007
|
res = CATime::Resolution.parse(unit)
|
|
2922
3008
|
stride = step.nil? ? 1 :
|
|
2923
|
-
|
|
2924
|
-
s =
|
|
2925
|
-
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)
|
|
2926
3012
|
n = e < s ? 0 : (e - s) / stride + 1
|
|
2927
3013
|
CArray.int64(n) {|i| s + i * stride }.time(unit: res)
|
|
2928
3014
|
end
|
|
@@ -2933,8 +3019,9 @@ class CArray
|
|
|
2933
3019
|
# is stored on and `step` is the spacing, so an hourly grid sampled once
|
|
2934
3020
|
# a day is `unit: :h, step: "1 day"`. With no `step` the spacing is one
|
|
2935
3021
|
# `unit` tick (consecutive ticks, as before).
|
|
2936
|
-
# @param start [Time, String,
|
|
2937
|
-
# 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.
|
|
2938
3025
|
# @param count [Integer] number of elements.
|
|
2939
3026
|
# @param unit [Resolution, Symbol, String] grid resolution (tick).
|
|
2940
3027
|
# @param step [Resolution, Symbol, String, nil] spacing between elements
|
|
@@ -2947,33 +3034,33 @@ class CArray
|
|
|
2947
3034
|
def self.time_series(start, count:, unit:, step: nil, format: nil)
|
|
2948
3035
|
res = CATime::Resolution.parse(unit)
|
|
2949
3036
|
stride = step.nil? ? 1 :
|
|
2950
|
-
|
|
2951
|
-
s =
|
|
3037
|
+
CATime::UnitAlgebra.multiple_factor(CATime::Resolution.parse(step), res)
|
|
3038
|
+
s = CATime::Literal.tick_index(start, res, format)
|
|
2952
3039
|
CArray.int64(count) {|i| s + i * stride }.time(unit: res)
|
|
2953
3040
|
end
|
|
2954
3041
|
|
|
2955
|
-
# @overload time(x, unit:
|
|
3042
|
+
# @overload time(x, unit: nil, format: nil, on_error: :raise)
|
|
2956
3043
|
# Builds a {CATime} on the `unit` grid from time value(s). `x`
|
|
2957
|
-
# is either a single literal (Time / ISO 8601 String /
|
|
2958
|
-
#
|
|
3044
|
+
# is either a single literal (Time / ISO 8601 String / DateTime /
|
|
3045
|
+
# element) — giving a 1-element result — or a CArray of such
|
|
2959
3046
|
# literals — giving a same-shape result parsed per cell. Parsing is UTC
|
|
2960
|
-
# and DateTime-independent. An Integer
|
|
2961
|
-
#
|
|
2962
|
-
#
|
|
2963
|
-
# `
|
|
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.
|
|
2964
3051
|
#
|
|
2965
3052
|
# A value that cannot be parsed raises by default (`on_error: :raise`);
|
|
2966
3053
|
# pass `on_error: :mask` to make it an UNDEF cell instead. A masked /
|
|
2967
3054
|
# `nil` input cell is a *missing* value (not a parse failure) and always
|
|
2968
3055
|
# becomes UNDEF, regardless of `on_error`.
|
|
2969
|
-
# @param x [Time, String,
|
|
3056
|
+
# @param x [Time, String, DateTime, CATime::Element, Array, CArray] a literal, a
|
|
2970
3057
|
# Ruby Array of literals, or a CArray of literals.
|
|
2971
3058
|
# @param unit [Resolution, Symbol, String, nil] target grid resolution;
|
|
2972
3059
|
# by default the finest the values show, so nothing they hold is cut
|
|
2973
3060
|
# off: text written to the day is `:D`, to the second `:s`, with a
|
|
2974
3061
|
# fraction `:ms` / `:us` / `:ns` by its digits (as
|
|
2975
|
-
# `CAFrame#
|
|
2976
|
-
# the format writes;
|
|
3062
|
+
# `CAFrame#parse_time` reads text); with a format, the finest field
|
|
3063
|
+
# the format writes; a Time the finest
|
|
2977
3064
|
# its value needs; a Date `:D`; an element its own unit.
|
|
2978
3065
|
# @param format [String, nil] optional strptime format for String input.
|
|
2979
3066
|
# With a format, an Integer is read as its decimal digits
|
|
@@ -2981,6 +3068,7 @@ class CArray
|
|
|
2981
3068
|
# @param on_error [:raise, :mask] parse-failure policy (default `:raise`).
|
|
2982
3069
|
# @return [CATime] shape `[1]` for a literal, else `x`'s shape.
|
|
2983
3070
|
# @raise [ArgumentError] on an unparseable value when `on_error: :raise`.
|
|
3071
|
+
# @raise [TypeError] on an Integer without a `format:`, whatever `on_error:`.
|
|
2984
3072
|
#
|
|
2985
3073
|
# @example
|
|
2986
3074
|
# CArray.time("2024-06-15", unit: :D) # 1-element
|
|
@@ -2993,18 +3081,17 @@ class CArray
|
|
|
2993
3081
|
x = CA_OBJECT(x) if x.is_a?(Array) # Ruby Array of literals -> object CArray
|
|
2994
3082
|
# With a format, an Integer is the digits it is written with (20240101
|
|
2995
3083
|
# read as "%Y%m%d"), not a count of ticks: a format describes text.
|
|
2996
|
-
x =
|
|
2997
|
-
res = unit ? CATime::Resolution.parse(unit) :
|
|
2998
|
-
# Without a format, an Integer
|
|
2999
|
-
#
|
|
3000
|
-
if format.nil?
|
|
3001
|
-
|
|
3002
|
-
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"
|
|
3003
3090
|
end
|
|
3004
3091
|
unless x.is_a?(CArray)
|
|
3005
|
-
return
|
|
3092
|
+
return CATime::Literal.to_time_array(x, res, format, on_error)
|
|
3006
3093
|
end
|
|
3007
|
-
raw =
|
|
3094
|
+
raw = CATime::Literal.ticks_from_cells(x, res, format, on_error)
|
|
3008
3095
|
return raw.time(unit: res) if raw
|
|
3009
3096
|
raw = CArray.int64(*x.shape)
|
|
3010
3097
|
x.each_index do |*idx|
|
|
@@ -3013,8 +3100,9 @@ class CArray
|
|
|
3013
3100
|
raw[*idx] = UNDEF # missing input -> missing output (no phantom epoch)
|
|
3014
3101
|
next
|
|
3015
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?
|
|
3016
3104
|
begin
|
|
3017
|
-
raw[*idx] =
|
|
3105
|
+
raw[*idx] = CATime::Literal.tick_index(s, res, format)
|
|
3018
3106
|
rescue ArgumentError, TypeError
|
|
3019
3107
|
raise if on_error == :raise
|
|
3020
3108
|
raw[*idx] = UNDEF # opt-in parse-mask
|
|
@@ -3023,22 +3111,24 @@ class CArray
|
|
|
3023
3111
|
raw.time(unit: res)
|
|
3024
3112
|
end
|
|
3025
3113
|
|
|
3026
|
-
# @overload time(unit: :
|
|
3027
|
-
# 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`
|
|
3028
3117
|
# nil, `self`'s int64 values are taken as tick indices already anchored
|
|
3029
3118
|
# to the Unix epoch (zero-copy Face wrap). With `origin` given, `self`'s
|
|
3030
3119
|
# indices are relative to `origin` and are rebased to the epoch (a new
|
|
3031
3120
|
# int64 array is built; the origin is not stored).
|
|
3032
3121
|
# @param unit [Resolution, Symbol, String] grid resolution.
|
|
3033
|
-
# @param origin [Time, String,
|
|
3034
|
-
# `self`'s indices are counted from (default: the Unix epoch)
|
|
3035
|
-
#
|
|
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.
|
|
3036
3125
|
# @return [CATime]
|
|
3037
3126
|
# @raise [ArgumentError] when `origin` falls between two ticks of `unit`.
|
|
3127
|
+
# @raise [TypeError] when `origin` is an Integer (it is not an instant).
|
|
3038
3128
|
# @note An `int64` receiver is wrapped zero-copy. A narrower integer type
|
|
3039
3129
|
# is widened to `int64` first (a copy); a `Float` / non-integer type
|
|
3040
3130
|
# raises (cast it explicitly if the truncation is intended).
|
|
3041
|
-
def time(unit: :
|
|
3131
|
+
def time(unit: :s, origin: nil)
|
|
3042
3132
|
res = CATime::Resolution.parse(unit)
|
|
3043
3133
|
src = as_int64_time_storage
|
|
3044
3134
|
if origin.nil?
|
|
@@ -3046,20 +3136,19 @@ class CArray
|
|
|
3046
3136
|
else
|
|
3047
3137
|
# The origin is a point of the grid: one between two ticks has no
|
|
3048
3138
|
# index, so it raises rather than moving to the tick before it.
|
|
3049
|
-
|
|
3050
|
-
|
|
3051
|
-
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)
|
|
3052
3141
|
end
|
|
3053
3142
|
end
|
|
3054
3143
|
|
|
3055
|
-
# @overload timedelta(unit: :
|
|
3144
|
+
# @overload timedelta(unit: :s)
|
|
3056
3145
|
# Returns `self` re-wrapped as a zero-copy {CATimedelta} view
|
|
3057
|
-
# with the given `unit
|
|
3146
|
+
# with the given `unit` (by default seconds).
|
|
3058
3147
|
# @param unit [Resolution, Symbol, String] duration resolution.
|
|
3059
3148
|
# @return [CATimedelta]
|
|
3060
3149
|
# @note An `int64` receiver is wrapped zero-copy; a narrower integer type
|
|
3061
3150
|
# is widened to `int64` first (a copy); a `Float` / non-integer raises.
|
|
3062
|
-
def timedelta(unit: :
|
|
3151
|
+
def timedelta(unit: :s)
|
|
3063
3152
|
CATimedelta.wrap(as_int64_time_storage, unit: unit)
|
|
3064
3153
|
end
|
|
3065
3154
|
|
|
@@ -3087,26 +3176,26 @@ class CArray
|
|
|
3087
3176
|
end
|
|
3088
3177
|
|
|
3089
3178
|
# ============================================================================
|
|
3090
|
-
# timestep system
|
|
3179
|
+
# timestep system
|
|
3091
3180
|
#
|
|
3092
3181
|
# Projects an absolute time onto an integer "step index" -- the k-th
|
|
3093
3182
|
# fixed-width bucket of `step` counted from `origin` -- and the inverse /
|
|
3094
3183
|
# rounding / on-grid operators built on it. All storage-domain int64 and
|
|
3095
|
-
# vectorized; the
|
|
3184
|
+
# vectorized; the main benefit is that bucketing, matching, and positional
|
|
3096
3185
|
# addressing across differently-scaled series all become integer arithmetic.
|
|
3097
3186
|
#
|
|
3098
3187
|
# Integer path covers: fixed-length step on fixed-length storage, and Y/M
|
|
3099
3188
|
# step on Y/M storage (:M storage already encodes a linear month ordinal, so
|
|
3100
|
-
# it floor-divides identically).
|
|
3101
|
-
#
|
|
3102
|
-
# 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.
|
|
3103
3191
|
# ============================================================================
|
|
3104
3192
|
|
|
3105
3193
|
# Places a bucket grid on a storage resolution: resolves a timestep into whole
|
|
3106
3194
|
# storage ticks, resolves an origin into a tick count (or a month ordinal on
|
|
3107
3195
|
# the calendar path), and guards both against int64 overflow. Internal to the
|
|
3108
3196
|
# time surface: nothing here is part of the public API.
|
|
3109
|
-
|
|
3197
|
+
# @api private
|
|
3198
|
+
module CATime::Timestep
|
|
3110
3199
|
module_function
|
|
3111
3200
|
|
|
3112
3201
|
# Base units that are day-or-finer (a period head is representable exactly;
|
|
@@ -3144,7 +3233,7 @@ module CATimeGrid
|
|
|
3144
3233
|
# Storage ticks per day of `storage_res` (must be a whole number, else the
|
|
3145
3234
|
# storage grid does not tile a day -- a calendar bucket is unrepresentable).
|
|
3146
3235
|
def ticks_per_day(storage_res)
|
|
3147
|
-
r =
|
|
3236
|
+
r = CATime::UnitAlgebra.ratio(CATime::Resolution.new(1, :D), storage_res)
|
|
3148
3237
|
unless r.denominator == 1
|
|
3149
3238
|
raise ArgumentError,
|
|
3150
3239
|
"cannot place a calendar bucket on storage resolution " \
|
|
@@ -3162,7 +3251,19 @@ module CATimeGrid
|
|
|
3162
3251
|
ymk = ym0 + k * count
|
|
3163
3252
|
yy = ymk / 12
|
|
3164
3253
|
mm = ymk - yy * 12 + 1
|
|
3165
|
-
|
|
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
|
|
3166
3267
|
end
|
|
3167
3268
|
|
|
3168
3269
|
# Month ordinal (year*12 + month-1) of origin; day / time ignored.
|
|
@@ -3184,13 +3285,13 @@ module CATimeGrid
|
|
|
3184
3285
|
# of the bucket tick; a partial multiple ("90 minutes" storage against
|
|
3185
3286
|
# an :h bucket) has no integer timestep and raises.
|
|
3186
3287
|
def resolve_step_scale(step_res, storage_res)
|
|
3187
|
-
r =
|
|
3288
|
+
r = CATime::UnitAlgebra.ratio(step_res, storage_res) # storage ticks / step tick
|
|
3188
3289
|
if r
|
|
3189
3290
|
what = "bucket #{step_res} in ticks of #{storage_res}"
|
|
3190
3291
|
return [1, check_int64_range(r.numerator, what)] if r.denominator == 1
|
|
3191
3292
|
return [check_int64_range(r.denominator, what), 1] if r.numerator == 1
|
|
3192
3293
|
end
|
|
3193
|
-
if
|
|
3294
|
+
if CATime::UnitAlgebra::CALENDAR.key?(step_res.base) && SU_LE_DAY.include?(storage_res.base)
|
|
3194
3295
|
return :civil
|
|
3195
3296
|
end
|
|
3196
3297
|
raise ArgumentError,
|
|
@@ -3200,8 +3301,8 @@ module CATimeGrid
|
|
|
3200
3301
|
|
|
3201
3302
|
# origin -> integer tick count in storage ticks. Default (nil) is the
|
|
3202
3303
|
# epoch, except a week bucket defaults to ISO Monday (1970-01-05). A lossy
|
|
3203
|
-
# conversion (origin not landing exactly on the storage grid) raises;
|
|
3204
|
-
#
|
|
3304
|
+
# conversion (origin not landing exactly on the storage grid) raises; an
|
|
3305
|
+
# Integer is not an instant and raises TypeError.
|
|
3205
3306
|
def resolve_origin_ticks(origin, step_res, storage_res)
|
|
3206
3307
|
if origin.nil?
|
|
3207
3308
|
if step_res.base == :W && SU_LE_DAY.include?(storage_res.base)
|
|
@@ -3210,7 +3311,7 @@ module CATimeGrid
|
|
|
3210
3311
|
end
|
|
3211
3312
|
return 0
|
|
3212
3313
|
end
|
|
3213
|
-
if
|
|
3314
|
+
if CATime::UnitAlgebra::FIXED.key?(storage_res.base)
|
|
3214
3315
|
secs = origin_seconds_exact(origin)
|
|
3215
3316
|
tick = secs / storage_res.tick_ratio # Rational
|
|
3216
3317
|
unless tick.denominator == 1
|
|
@@ -3233,18 +3334,10 @@ module CATimeGrid
|
|
|
3233
3334
|
end
|
|
3234
3335
|
end
|
|
3235
3336
|
|
|
3236
|
-
# Exact Rational seconds since the Unix epoch for a fixed-storage origin
|
|
3237
|
-
#
|
|
3238
|
-
# 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).
|
|
3239
3339
|
def origin_seconds_exact(origin)
|
|
3240
|
-
|
|
3241
|
-
when Integer
|
|
3242
|
-
raise ArgumentError,
|
|
3243
|
-
"origin: a bare Integer is ambiguous (epoch-dependent); pass a " \
|
|
3244
|
-
"Time / String / CATime scalar"
|
|
3245
|
-
else
|
|
3246
|
-
CATimeLiteral.epoch_seconds(origin) # Time / String / DateTime / Element
|
|
3247
|
-
end
|
|
3340
|
+
CATime::Literal.epoch_seconds(origin)
|
|
3248
3341
|
end
|
|
3249
3342
|
|
|
3250
3343
|
# Whether `origin` sits exactly on the head of its month (the 1st at
|
|
@@ -3255,9 +3348,9 @@ module CATimeGrid
|
|
|
3255
3348
|
# A calendar element is a month head by construction, and the shared
|
|
3256
3349
|
# entry point says so by handing back that midnight -- no branch here
|
|
3257
3350
|
# has to know it.
|
|
3258
|
-
secs =
|
|
3351
|
+
secs = CATime::Literal.epoch_seconds(origin)
|
|
3259
3352
|
y, m = origin_year_month(origin)
|
|
3260
|
-
head =
|
|
3353
|
+
head = CATime::Civil.days_from_civil(CA_INT64([y]), CA_INT64([m]), CA_INT64([1]))[0]
|
|
3261
3354
|
secs == head * 86400
|
|
3262
3355
|
end
|
|
3263
3356
|
|
|
@@ -3283,14 +3376,7 @@ module CATimeGrid
|
|
|
3283
3376
|
|
|
3284
3377
|
# [year, month] of a calendar-storage origin (finer fields ignored).
|
|
3285
3378
|
def origin_year_month(origin)
|
|
3286
|
-
|
|
3287
|
-
when Integer
|
|
3288
|
-
raise ArgumentError,
|
|
3289
|
-
"origin: a bare Integer is ambiguous (epoch-dependent); pass a " \
|
|
3290
|
-
"Time / String / CATime scalar"
|
|
3291
|
-
else
|
|
3292
|
-
CATimeLiteral.year_month(origin) # Time / String / DateTime / Element
|
|
3293
|
-
end
|
|
3379
|
+
CATime::Literal.year_month(origin)
|
|
3294
3380
|
end
|
|
3295
3381
|
end
|
|
3296
3382
|
|
|
@@ -3367,7 +3453,7 @@ class CATime
|
|
|
3367
3453
|
|
|
3368
3454
|
# seconds- (fixed base) or months- (calendar base) per tick (Rational).
|
|
3369
3455
|
def tick_ratio
|
|
3370
|
-
count *
|
|
3456
|
+
count * CATime::UnitAlgebra.base_ratio(base)
|
|
3371
3457
|
end
|
|
3372
3458
|
|
|
3373
3459
|
# @return [Boolean] whether `other` is the same count and base unit.
|
|
@@ -3391,12 +3477,12 @@ class CATime
|
|
|
3391
3477
|
# representation, and the reference-time epoch stays out of storage.
|
|
3392
3478
|
#
|
|
3393
3479
|
# g = CATime::Grid.parse("12 hours since 2017-11-30 09:00")
|
|
3394
|
-
# t.snap(g, direction: :floor) # the pair, passed once
|
|
3395
|
-
# 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
|
|
3396
3482
|
# g.at(CA_INT64([0, 1, 2])) # and back
|
|
3397
3483
|
#
|
|
3398
|
-
# Proleptic Gregorian, UTC.
|
|
3399
|
-
#
|
|
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
|
|
3400
3486
|
# benefit does not follow.
|
|
3401
3487
|
class Grid
|
|
3402
3488
|
|
|
@@ -3433,26 +3519,28 @@ class CATime
|
|
|
3433
3519
|
end
|
|
3434
3520
|
end
|
|
3435
3521
|
|
|
3436
|
-
# Normalizes the
|
|
3437
|
-
# `unit:`, or
|
|
3438
|
-
#
|
|
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.
|
|
3439
3527
|
# @return [Array(Object, Object)] (unit, origin).
|
|
3440
|
-
def self.resolve(
|
|
3441
|
-
return [
|
|
3442
|
-
|
|
3443
|
-
unless grid.nil?
|
|
3528
|
+
def self.resolve(unit, origin, name)
|
|
3529
|
+
return [unit, origin] unless unit.is_a?(Grid)
|
|
3530
|
+
unless origin.nil?
|
|
3444
3531
|
raise ArgumentError,
|
|
3445
|
-
"
|
|
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))"
|
|
3446
3535
|
end
|
|
3447
|
-
[unit, origin]
|
|
3536
|
+
[unit.unit, unit.origin]
|
|
3448
3537
|
end
|
|
3449
3538
|
|
|
3450
3539
|
def initialize(unit, origin = nil)
|
|
3451
3540
|
@unit = Resolution.parse(unit)
|
|
3452
3541
|
@origin = origin
|
|
3453
3542
|
@storage = resolve_storage
|
|
3454
|
-
|
|
3455
|
-
@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
|
|
3456
3544
|
freeze
|
|
3457
3545
|
end
|
|
3458
3546
|
|
|
@@ -3478,7 +3566,7 @@ class CATime
|
|
|
3478
3566
|
|
|
3479
3567
|
# @return [Integer] storage ticks from the epoch to the origin.
|
|
3480
3568
|
def origin_ticks
|
|
3481
|
-
@origin ? Integer(
|
|
3569
|
+
@origin ? Integer(CATime::Literal.epoch_seconds(@origin) / @storage.tick_ratio) : 0
|
|
3482
3570
|
end
|
|
3483
3571
|
|
|
3484
3572
|
# Tick indices -> instants: the {CATime.from_timesteps} direction, but
|
|
@@ -3487,17 +3575,22 @@ class CATime
|
|
|
3487
3575
|
# @return [CATime, Element]
|
|
3488
3576
|
def at(k)
|
|
3489
3577
|
return at(CArray.int64(1) { Integer(k) })[0] unless k.is_a?(CArray)
|
|
3490
|
-
|
|
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
|
|
3491
3584
|
(origin_ticks + k.int64 * step_ticks).time(unit: @storage)
|
|
3492
3585
|
end
|
|
3493
3586
|
|
|
3494
3587
|
# Instants -> tick indices: the {CATime#timesteps} direction. Off-grid
|
|
3495
3588
|
# elements floor toward the past; ask {#on} when that would be a lie.
|
|
3496
3589
|
# @return [CArray] int64.
|
|
3497
|
-
def index(time) = time.timesteps(self)
|
|
3590
|
+
def index(time) = time.timesteps(unit: self)
|
|
3498
3591
|
|
|
3499
3592
|
# @return [CArray] boolean, flagging elements that land on a tick.
|
|
3500
|
-
def on(time) = time.is_righttime(self)
|
|
3593
|
+
def on(time) = time.is_righttime(unit: self)
|
|
3501
3594
|
|
|
3502
3595
|
# @return [Boolean] whether every element lands on a tick.
|
|
3503
3596
|
def on?(time) = on(time).all
|
|
@@ -3532,15 +3625,21 @@ class CATime
|
|
|
3532
3625
|
private
|
|
3533
3626
|
|
|
3534
3627
|
# The coarsest storage holding both the unit's tick and the origin's
|
|
3535
|
-
# phase without truncation. A calendar grid is addressed by month
|
|
3536
|
-
# 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).
|
|
3537
3631
|
def resolve_storage
|
|
3538
|
-
|
|
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?
|
|
3539
3638
|
candidates = [@unit, *FALLBACK_BASES.map {|base| Resolution.new(1, base) }]
|
|
3540
3639
|
candidates.each do |res|
|
|
3541
3640
|
next unless (@unit.tick_ratio / res.tick_ratio).denominator == 1
|
|
3542
3641
|
next unless @origin.nil? ||
|
|
3543
|
-
(
|
|
3642
|
+
(CATime::Literal.epoch_seconds(@origin) / res.tick_ratio).denominator == 1
|
|
3544
3643
|
return res
|
|
3545
3644
|
end
|
|
3546
3645
|
raise ArgumentError,
|
|
@@ -3551,12 +3650,17 @@ class CATime
|
|
|
3551
3650
|
|
|
3552
3651
|
# Rounding rules {#snap} accepts, the same three CArray#snap takes.
|
|
3553
3652
|
SNAP_DIRECTIONS = %i[round floor ceil].freeze
|
|
3653
|
+
private_constant :SNAP_DIRECTIONS
|
|
3554
3654
|
|
|
3555
3655
|
# The grid this array is stored on: its own tick, anchored at the epoch.
|
|
3556
|
-
# `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
|
|
3557
3657
|
# has always meant.
|
|
3558
3658
|
# @return [Grid]
|
|
3559
|
-
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
|
|
3560
3664
|
|
|
3561
3665
|
# @overload timesteps(unit: self.unit, origin: nil)
|
|
3562
3666
|
# Returns the integer timestep of every element: the k-th `unit`-wide
|
|
@@ -3570,16 +3674,24 @@ class CATime
|
|
|
3570
3674
|
# (default: this array's own storage resolution).
|
|
3571
3675
|
# @param origin [Time, String, CATime::Element, DateTime, nil] head of
|
|
3572
3676
|
# bucket 0 (default: the Unix epoch, or ISO Monday for a week bucket
|
|
3573
|
-
#
|
|
3574
|
-
#
|
|
3677
|
+
# -- a week array counts its ticks from the epoch Thursday, so a
|
|
3678
|
+
# week bucket on it is answered on days).
|
|
3575
3679
|
# It has to be a bucket head itself: on a calendar grid, the 1st at
|
|
3576
3680
|
# 00:00.
|
|
3577
3681
|
# @return [CArray] int64 timesteps.
|
|
3578
3682
|
# @raise [ArgumentError] on a sub-resolution / unrepresentable (unit,
|
|
3579
3683
|
# storage-resolution) pair or a lossy origin.
|
|
3580
|
-
def timesteps(
|
|
3581
|
-
unit, origin = Grid.resolve(
|
|
3582
|
-
|
|
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
|
|
3583
3695
|
g = resolve_timestep_grid(unit, origin)
|
|
3584
3696
|
return calendar_bucket(:index, g[1], origin) if g[0] == :civil
|
|
3585
3697
|
su, mul, step_ticks, o = g
|
|
@@ -3595,38 +3707,46 @@ class CATime
|
|
|
3595
3707
|
# @param unit [String, Symbol, Resolution] bucket resolution.
|
|
3596
3708
|
# @param origin [Time, String, CATime::Element, DateTime, nil] head of
|
|
3597
3709
|
# bucket 0 (default: the Unix epoch, or ISO Monday for a week bucket
|
|
3598
|
-
#
|
|
3599
|
-
#
|
|
3710
|
+
# -- a week array counts its ticks from the epoch Thursday, so a
|
|
3711
|
+
# week bucket on it is answered on days).
|
|
3600
3712
|
# It has to be a bucket head itself: on a calendar grid, the 1st at
|
|
3601
3713
|
# 00:00.
|
|
3602
3714
|
# @return [CATime]
|
|
3603
|
-
# @overload snap(
|
|
3715
|
+
# @overload snap(unit:, origin: nil, direction: :round)
|
|
3604
3716
|
# Returns each element snapped to a point on the bucket grid, as a
|
|
3605
3717
|
# {CATime} in the same storage resolution. This is the time-domain twin
|
|
3606
|
-
# of {CArray#snap}: there, `(step, offset:, direction:)`; here
|
|
3607
|
-
# (= tick + origin)
|
|
3608
|
-
#
|
|
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.
|
|
3609
3721
|
#
|
|
3610
3722
|
# `:round` picks the nearest bucket head, ties toward the future;
|
|
3611
3723
|
# `:floor` the head at or before; `:ceil` the head at or after.
|
|
3612
3724
|
# {#floor}, {#ceil} and {#round} are this method with `direction:` fixed.
|
|
3613
|
-
# @param grid [Grid, nil] the bucket grid, passed as one value.
|
|
3614
3725
|
# @param unit [String, Symbol, Resolution, Grid] bucket resolution.
|
|
3615
3726
|
# @param origin [Time, String, CATime::Element, DateTime, nil] head of
|
|
3616
3727
|
# bucket 0 (default: the Unix epoch, or ISO Monday for a week bucket
|
|
3617
|
-
#
|
|
3618
|
-
#
|
|
3728
|
+
# -- a week array counts its ticks from the epoch Thursday, so a
|
|
3729
|
+
# week bucket on it is answered on days).
|
|
3619
3730
|
# It has to be a bucket head itself: on a calendar grid, the 1st at
|
|
3620
3731
|
# 00:00.
|
|
3621
3732
|
# @param direction [:round, :floor, :ceil] rounding rule.
|
|
3622
3733
|
# @return [CATime]
|
|
3623
|
-
def snap(
|
|
3734
|
+
def snap(unit: nil, origin: nil, direction: :round)
|
|
3624
3735
|
unless SNAP_DIRECTIONS.include?(direction)
|
|
3625
3736
|
raise ArgumentError,
|
|
3626
3737
|
"snap: direction must be :round / :floor / :ceil " \
|
|
3627
3738
|
"(got #{direction.inspect})"
|
|
3628
3739
|
end
|
|
3629
|
-
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
|
|
3630
3750
|
g = resolve_timestep_grid(unit, origin)
|
|
3631
3751
|
return calendar_bucket(direction, g[1], origin) if g[0] == :civil
|
|
3632
3752
|
su, mul, step_ticks, o = g
|
|
@@ -3656,15 +3776,15 @@ class CATime
|
|
|
3656
3776
|
bucket_head_as_time(o + q * step_ticks, mul, su)
|
|
3657
3777
|
end
|
|
3658
3778
|
|
|
3659
|
-
# @overload floor(
|
|
3779
|
+
# @overload floor(unit:, origin: nil)
|
|
3660
3780
|
# {#snap} with `direction: :floor` -- each element at its bucket head
|
|
3661
3781
|
# (toward the past), as a {CATime} in the same storage resolution.
|
|
3662
3782
|
# @return [CATime]
|
|
3663
|
-
def floor(
|
|
3664
|
-
|
|
3783
|
+
def floor(unit: nil, origin: nil)
|
|
3784
|
+
snap_to(unit, origin, :floor, "floor")
|
|
3665
3785
|
end
|
|
3666
3786
|
|
|
3667
|
-
# @overload end_of(
|
|
3787
|
+
# @overload end_of(unit:, origin: nil)
|
|
3668
3788
|
# Each element at the last tick of its bucket, on the array's own grid:
|
|
3669
3789
|
# the bucket {#floor} gives the head of, one tick before the next head.
|
|
3670
3790
|
# A day array's month ends on the month's last day, a second array's at
|
|
@@ -3678,29 +3798,37 @@ class CATime
|
|
|
3678
3798
|
# @return [CATime]
|
|
3679
3799
|
# @raise [ArgumentError] when the bucket is finer than the grid or not a
|
|
3680
3800
|
# whole number of its ticks.
|
|
3681
|
-
def end_of(
|
|
3682
|
-
step, origin = Grid.resolve(
|
|
3801
|
+
def end_of(unit: nil, origin: nil)
|
|
3802
|
+
step, origin = Grid.resolve(unit, origin, "end_of")
|
|
3683
3803
|
step = CATime::Resolution.parse(step)
|
|
3684
|
-
per =
|
|
3804
|
+
per = CATime::UnitAlgebra.ratio(step, self.unit) # ticks of the grid per bucket
|
|
3685
3805
|
if per && !(per.denominator == 1 && per >= 1)
|
|
3686
3806
|
raise ArgumentError,
|
|
3687
3807
|
"end_of: a bucket of #{step} is not a whole number of the #{self.unit} " \
|
|
3688
3808
|
"grid's ticks, so its last tick is not on the grid"
|
|
3689
3809
|
end
|
|
3690
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
|
|
3691
3819
|
head + CATimedelta::Element.new(step.count, step.base) -
|
|
3692
3820
|
CATimedelta::Element.new(self.unit.count, self.unit.base)
|
|
3693
3821
|
end
|
|
3694
3822
|
|
|
3695
|
-
# @overload ceil(
|
|
3823
|
+
# @overload ceil(unit:, origin: nil)
|
|
3696
3824
|
# {#snap} with `direction: :ceil` -- each element at the bucket head at
|
|
3697
3825
|
# or after it (an element already on a boundary maps to itself).
|
|
3698
3826
|
# @return [CATime]
|
|
3699
|
-
def ceil(
|
|
3700
|
-
|
|
3827
|
+
def ceil(unit: nil, origin: nil)
|
|
3828
|
+
snap_to(unit, origin, :ceil, "ceil")
|
|
3701
3829
|
end
|
|
3702
3830
|
|
|
3703
|
-
# @overload round(
|
|
3831
|
+
# @overload round(unit:, origin: nil)
|
|
3704
3832
|
# {#snap} with `direction: :round` -- each element at its nearest bucket
|
|
3705
3833
|
# head, ties toward the future. Exact for odd `step_ticks` (no half-tick
|
|
3706
3834
|
# loss). For a calendar bucket the nearest head is by the instant (month
|
|
@@ -3708,8 +3836,8 @@ class CATime
|
|
|
3708
3836
|
# stands for the start of its month or year, so a `:M` array rounds as
|
|
3709
3837
|
# the same dates on `:D` do: 1889-07 to the year is 1889.
|
|
3710
3838
|
# @return [CATime]
|
|
3711
|
-
def round(
|
|
3712
|
-
|
|
3839
|
+
def round(unit: nil, origin: nil)
|
|
3840
|
+
snap_to(unit, origin, :round, "round")
|
|
3713
3841
|
end
|
|
3714
3842
|
|
|
3715
3843
|
# @overload is_righttime(unit:, origin: nil)
|
|
@@ -3719,13 +3847,16 @@ class CATime
|
|
|
3719
3847
|
# @param unit [String, Symbol, Resolution] bucket resolution.
|
|
3720
3848
|
# @param origin [Time, String, CATime::Element, DateTime, nil] head of
|
|
3721
3849
|
# bucket 0 (default: the Unix epoch, or ISO Monday for a week bucket
|
|
3722
|
-
#
|
|
3723
|
-
#
|
|
3850
|
+
# -- a week array counts its ticks from the epoch Thursday, so a
|
|
3851
|
+
# week bucket on it is answered on days).
|
|
3724
3852
|
# It has to be a bucket head itself: on a calendar grid, the 1st at
|
|
3725
3853
|
# 00:00.
|
|
3726
3854
|
# @return [CArray] boolean.
|
|
3727
|
-
def is_righttime(
|
|
3728
|
-
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
|
|
3729
3860
|
g = resolve_timestep_grid(unit, origin)
|
|
3730
3861
|
return calendar_bucket(:on, g[1], origin) if g[0] == :civil
|
|
3731
3862
|
su, mul, step_ticks, o = g
|
|
@@ -3755,23 +3886,45 @@ class CATime
|
|
|
3755
3886
|
# It has to be a bucket head itself: on a calendar grid, the 1st at
|
|
3756
3887
|
# 00:00.
|
|
3757
3888
|
# @return [Element, CATime] on the `unit` grid (`:D` for a week bucket).
|
|
3758
|
-
def self.from_timesteps(k,
|
|
3759
|
-
grid = unit if unit.is_a?(Grid)
|
|
3889
|
+
def self.from_timesteps(k, unit: nil, origin: nil)
|
|
3760
3890
|
# A Grid answers on its own storage, so an origin off the unit grid
|
|
3761
3891
|
# ("12 hours since ... 09:00") survives; the keyword form keeps the
|
|
3762
3892
|
# narrower rule it always had, where the origin sits on the unit itself.
|
|
3763
|
-
|
|
3764
|
-
|
|
3893
|
+
if unit.is_a?(Grid)
|
|
3894
|
+
Grid.resolve(unit, origin, "from_timesteps")
|
|
3895
|
+
return unit.at(k)
|
|
3896
|
+
end
|
|
3765
3897
|
res = Resolution.parse(unit)
|
|
3766
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
|
|
3767
3905
|
kk = k.is_a?(CArray) ? k.int64 : CArray.int64(1) { Integer(k) }
|
|
3768
|
-
_mul, step_ticks, o =
|
|
3906
|
+
_mul, step_ticks, o = CATime::Timestep.resolve_integer_grid(res, out, origin)
|
|
3769
3907
|
raw = o + kk * step_ticks
|
|
3770
3908
|
k.is_a?(CArray) ? raw.time(unit: out) : Element.new(raw[0], out)
|
|
3771
3909
|
end
|
|
3772
3910
|
|
|
3773
3911
|
private
|
|
3774
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
|
+
|
|
3775
3928
|
# Resolve the grid for an instance: [storage_res, mul, step_ticks,
|
|
3776
3929
|
# origin_ticks] for the integer path, or [:civil, bucket_res] for the
|
|
3777
3930
|
# calendar path. Bucket arithmetic runs in the *numerator* grid -- the
|
|
@@ -3782,17 +3935,17 @@ class CATime
|
|
|
3782
3935
|
def resolve_timestep_grid(step, origin)
|
|
3783
3936
|
storage_res = unit
|
|
3784
3937
|
step_res = Resolution.parse(step)
|
|
3785
|
-
scale =
|
|
3938
|
+
scale = CATime::Timestep.resolve_step_scale(step_res, storage_res)
|
|
3786
3939
|
return [:civil, step_res] if scale == :civil
|
|
3787
3940
|
mul, step_ticks = scale
|
|
3788
3941
|
num_res = mul == 1 ? storage_res : step_res
|
|
3789
|
-
o =
|
|
3942
|
+
o = CATime::Timestep.resolve_origin_ticks(origin, step_res, num_res)
|
|
3790
3943
|
[storage_res, mul, step_ticks, o]
|
|
3791
3944
|
end
|
|
3792
3945
|
|
|
3793
3946
|
# Storage ticks lifted into the numerator grid (see {#resolve_timestep_grid}).
|
|
3794
3947
|
def storage_ticks_in_numerator_grid(mul)
|
|
3795
|
-
mul == 1 ? face_storage :
|
|
3948
|
+
mul == 1 ? face_storage : CATime::UnitAlgebra.widen(face_storage, mul)
|
|
3796
3949
|
end
|
|
3797
3950
|
|
|
3798
3951
|
# A bucket head computed in numerator ticks, back as a {CATime} in the
|
|
@@ -3808,6 +3961,7 @@ class CATime
|
|
|
3808
3961
|
# bases cannot, so the range guard is skipped for them (zero cost on the
|
|
3809
3962
|
# common :s / :h / :D / :M path).
|
|
3810
3963
|
FINE_UNITS = %i[us ns ps fs as].freeze
|
|
3964
|
+
private_constant :FINE_UNITS
|
|
3811
3965
|
|
|
3812
3966
|
# Raise (rather than silently wrap) if the integer-path arithmetic would
|
|
3813
3967
|
# overflow int64. Checks `parent - o`, and -- when `kind` is a bucket op
|
|
@@ -3830,7 +3984,7 @@ class CATime
|
|
|
3830
3984
|
return if min == UNDEF
|
|
3831
3985
|
lo = Integer(min) * mul
|
|
3832
3986
|
hi = Integer(face_storage.max) * mul
|
|
3833
|
-
chk = ->(v, w) {
|
|
3987
|
+
chk = ->(v, w) { CATime::Timestep.check_int64_range(v, w) }
|
|
3834
3988
|
chk.(lo, "ticks in bucket resolution")
|
|
3835
3989
|
chk.(hi, "ticks in bucket resolution")
|
|
3836
3990
|
chk.(lo - o, "face_storage - origin")
|
|
@@ -3862,17 +4016,17 @@ class CATime
|
|
|
3862
4016
|
case res.base
|
|
3863
4017
|
when :Y
|
|
3864
4018
|
ones = CArray.int64(*shape) { 1 }
|
|
3865
|
-
|
|
4019
|
+
CATime::Civil.days_from_civil(face_storage * res.count + 1970, ones, ones)
|
|
3866
4020
|
when :M
|
|
3867
4021
|
mo = face_storage * res.count + 1970 * 12
|
|
3868
4022
|
y = mo / 12
|
|
3869
4023
|
m = mo - y * 12 + 1
|
|
3870
|
-
|
|
4024
|
+
CATime::Civil.days_from_civil(y, m, CArray.int64(*shape) { 1 })
|
|
3871
4025
|
when :W then face_storage * res.count * 7
|
|
3872
4026
|
when :D then face_storage * res.count
|
|
3873
4027
|
else # fixed sub-day storage
|
|
3874
4028
|
day = CATime::Resolution.new(1, :D)
|
|
3875
|
-
r =
|
|
4029
|
+
r = CATime::UnitAlgebra.ratio(day, res) # storage ticks per day
|
|
3876
4030
|
if r.denominator == 1
|
|
3877
4031
|
face_storage / r.numerator
|
|
3878
4032
|
else # non-day-aligned tick
|
|
@@ -3889,7 +4043,7 @@ class CATime
|
|
|
3889
4043
|
# 10 / 20 / ...; a day grid collapses hour / minute / second to 0). A
|
|
3890
4044
|
# calendar-storage element has no intra-day field, so it collapses to 0.
|
|
3891
4045
|
def clock_field(fu)
|
|
3892
|
-
r =
|
|
4046
|
+
r = CATime::UnitAlgebra.ratio(unit, CATime::Resolution.new(1, fu)) # fu ticks / storage tick
|
|
3893
4047
|
return face_storage * 0 if r.nil? # calendar storage: no clock field
|
|
3894
4048
|
fi = r.denominator == 1 ? face_storage * r.numerator # epoch-relative fu index
|
|
3895
4049
|
: face_storage * r.numerator / r.denominator
|
|
@@ -3905,16 +4059,16 @@ class CATime
|
|
|
3905
4059
|
def calendar_bucket(kind, st, origin)
|
|
3906
4060
|
su = unit # storage Resolution
|
|
3907
4061
|
count = st.count * (st.base == :Y ? 12 : 1) # bucket in months
|
|
3908
|
-
ym0 =
|
|
4062
|
+
ym0 = CATime::Timestep.origin_month_ordinal(origin)
|
|
3909
4063
|
days = days_since_epoch # count-folded day index
|
|
3910
|
-
y, m =
|
|
4064
|
+
y, m = CATime::Civil.civil_from_days(days)
|
|
3911
4065
|
ym = y * 12 + (m - 1)
|
|
3912
4066
|
k = (ym - ym0) / count
|
|
3913
4067
|
return k if kind == :index
|
|
3914
|
-
head =
|
|
4068
|
+
head = CATime::Timestep.calendar_bucket_head_ticks(k, st, su, origin)
|
|
3915
4069
|
return head.time(unit: su) if kind == :floor
|
|
3916
4070
|
return face_storage.eq(head) if kind == :on
|
|
3917
|
-
head_next =
|
|
4071
|
+
head_next = CATime::Timestep.calendar_bucket_head_ticks(k + 1, st, su, origin)
|
|
3918
4072
|
sel = # boolean 0/1: pick head_next?
|
|
3919
4073
|
case kind
|
|
3920
4074
|
when :ceil then face_storage.ne(head) # on-boundary keeps head
|