date 3.3.1 → 3.3.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (4) hide show
  1. checksums.yaml +4 -4
  2. data/ext/date/date_core.c +147 -0
  3. data/lib/date.rb +1 -1
  4. metadata +4 -4
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: d3f6bf4ad3ef248851b47ae7c04b9e2378f699d6d93c59bd369284f681da476c
4
- data.tar.gz: 47680dd596954c5bd32176e047a7f7709e69e45067a5dd7019174e6779105492
3
+ metadata.gz: 75e3a4cb744689e80e55cc75831e2b6f13961ed10b2b0867a8a01d2ce33c28fc
4
+ data.tar.gz: 2391f862cb91c7aed10383225fd6b6c0d8fe37cbd82cac038420ff2e6a6292c1
5
5
  SHA512:
6
- metadata.gz: 31b0f01bbec9b877fd40f02806290120920b8fa327f27f513bca9faa7a358f9f3d423c2ca6f3e0c67212fc1350cdaeedb483000974de74d622ce24e9c6e208d1
7
- data.tar.gz: 639bd03636d459e671b472031999e14917c817804cdd65df9ef95a4070823a6a2dfa906e9c55fb12bc5ac5033f325b5117f9af92a421cf491f876802958a41a5
6
+ metadata.gz: e1e7086f78e0951e84ba1a7dc349868d32a74dee4afc8bf5202e5d9d48f275feaa986b7968684b9bfafa027f25b6bee2e3b9fc68f6c6eaf261eeaac5565961fd
7
+ data.tar.gz: 7e027f42df681fccf94500a543f19ca23a332f322db4d7ee10f533e1ff47d27ff28fb024131e93039d61eaca91f5386f3fc63edd6d2e00d3a25a10ca935dab98
data/ext/date/date_core.c CHANGED
@@ -27,6 +27,10 @@ static VALUE eDateError;
27
27
  static VALUE half_days_in_day, day_in_nanoseconds;
28
28
  static double positive_inf, negative_inf;
29
29
 
30
+ // used by deconstruct_keys
31
+ static VALUE sym_year, sym_month, sym_day, sym_yday, sym_wday;
32
+ static VALUE sym_hour, sym_min, sym_sec, sym_sec_fraction, sym_zone;
33
+
30
34
  #define f_boolcast(x) ((x) ? Qtrue : Qfalse)
31
35
 
32
36
  #define f_abs(x) rb_funcall(x, rb_intern("abs"), 0)
@@ -7432,6 +7436,94 @@ d_lite_jisx0301(VALUE self)
7432
7436
  return strftimev(fmt, self, set_tmx);
7433
7437
  }
7434
7438
 
7439
+ static VALUE
7440
+ deconstruct_keys(VALUE self, VALUE keys, int is_datetime) {
7441
+ VALUE h = rb_hash_new();
7442
+ long i;
7443
+
7444
+ get_d1(self);
7445
+
7446
+ if (NIL_P(keys)) {
7447
+ rb_hash_aset(h, sym_year, m_real_year(dat));
7448
+ rb_hash_aset(h, sym_month, INT2FIX(m_mon(dat)));
7449
+ rb_hash_aset(h, sym_day, INT2FIX(m_mday(dat)));
7450
+ rb_hash_aset(h, sym_yday, INT2FIX(m_yday(dat)));
7451
+ rb_hash_aset(h, sym_wday, INT2FIX(m_wday(dat)));
7452
+ if (is_datetime) {
7453
+ rb_hash_aset(h, sym_hour, INT2FIX(m_hour(dat)));
7454
+ rb_hash_aset(h, sym_min, INT2FIX(m_min(dat)));
7455
+ rb_hash_aset(h, sym_sec, INT2FIX(m_sec(dat)));
7456
+ rb_hash_aset(h, sym_sec_fraction, m_sf_in_sec(dat));
7457
+ rb_hash_aset(h, sym_zone, m_zone(dat));
7458
+ }
7459
+
7460
+ return h;
7461
+ }
7462
+ if (!RB_TYPE_P(keys, T_ARRAY)) {
7463
+ rb_raise(rb_eTypeError,
7464
+ "wrong argument type %"PRIsVALUE" (expected Array or nil)",
7465
+ rb_obj_class(keys));
7466
+
7467
+ }
7468
+
7469
+ for (i=0; i<RARRAY_LEN(keys); i++) {
7470
+ VALUE key = RARRAY_AREF(keys, i);
7471
+
7472
+ if (sym_year == key) rb_hash_aset(h, key, m_real_year(dat));
7473
+ if (sym_month == key) rb_hash_aset(h, key, INT2FIX(m_mon(dat)));
7474
+ if (sym_day == key) rb_hash_aset(h, key, INT2FIX(m_mday(dat)));
7475
+ if (sym_yday == key) rb_hash_aset(h, key, INT2FIX(m_yday(dat)));
7476
+ if (sym_wday == key) rb_hash_aset(h, key, INT2FIX(m_wday(dat)));
7477
+ if (is_datetime) {
7478
+ if (sym_hour == key) rb_hash_aset(h, key, INT2FIX(m_hour(dat)));
7479
+ if (sym_min == key) rb_hash_aset(h, key, INT2FIX(m_min(dat)));
7480
+ if (sym_sec == key) rb_hash_aset(h, key, INT2FIX(m_sec(dat)));
7481
+ if (sym_sec_fraction == key) rb_hash_aset(h, key, m_sf_in_sec(dat));
7482
+ if (sym_zone == key) rb_hash_aset(h, key, m_zone(dat));
7483
+ }
7484
+ }
7485
+ return h;
7486
+ }
7487
+
7488
+ /*
7489
+ * call-seq:
7490
+ * deconstruct_keys(array_of_names_or_nil) -> hash
7491
+ *
7492
+ * Returns a hash of the name/value pairs, to use in pattern matching.
7493
+ * Possible keys are: <tt>:year</tt>, <tt>:month</tt>, <tt>:day</tt>,
7494
+ * <tt>:wday</tt>, <tt>:yday</tt>.
7495
+ *
7496
+ * Possible usages:
7497
+ *
7498
+ * d = Date.new(2022, 10, 5)
7499
+ *
7500
+ * if d in wday: 3, day: ..7 # uses deconstruct_keys underneath
7501
+ * puts "first Wednesday of the month"
7502
+ * end
7503
+ * #=> prints "first Wednesday of the month"
7504
+ *
7505
+ * case d
7506
+ * in year: ...2022
7507
+ * puts "too old"
7508
+ * in month: ..9
7509
+ * puts "quarter 1-3"
7510
+ * in wday: 1..5, month:
7511
+ * puts "working day in month #{month}"
7512
+ * end
7513
+ * #=> prints "working day in month 10"
7514
+ *
7515
+ * Note that deconstruction by pattern can also be combined with class check:
7516
+ *
7517
+ * if d in Date(wday: 3, day: ..7)
7518
+ * puts "first Wednesday of the month"
7519
+ * end
7520
+ *
7521
+ */
7522
+ static VALUE
7523
+ d_lite_deconstruct_keys(VALUE self, VALUE keys) {
7524
+ return deconstruct_keys(self, keys, /* is_datetime=false */ 0);
7525
+ }
7526
+
7435
7527
  #ifndef NDEBUG
7436
7528
  /* :nodoc: */
7437
7529
  static VALUE
@@ -8740,6 +8832,46 @@ dt_lite_jisx0301(int argc, VALUE *argv, VALUE self)
8740
8832
  iso8601_timediv(self, n));
8741
8833
  }
8742
8834
 
8835
+ /*
8836
+ * call-seq:
8837
+ * deconstruct_keys(array_of_names_or_nil) -> hash
8838
+ *
8839
+ * Returns a hash of the name/value pairs, to use in pattern matching.
8840
+ * Possible keys are: <tt>:year</tt>, <tt>:month</tt>, <tt>:day</tt>,
8841
+ * <tt>:wday</tt>, <tt>:yday</tt>, <tt>:hour</tt>, <tt>:min</tt>,
8842
+ * <tt>:sec</tt>, <tt>:sec_fraction</tt>, <tt>:zone</tt>.
8843
+ *
8844
+ * Possible usages:
8845
+ *
8846
+ * dt = DateTime.new(2022, 10, 5, 13, 30)
8847
+ *
8848
+ * if d in wday: 1..5, hour: 10..18 # uses deconstruct_keys underneath
8849
+ * puts "Working time"
8850
+ * end
8851
+ * #=> prints "Working time"
8852
+ *
8853
+ * case dt
8854
+ * in year: ...2022
8855
+ * puts "too old"
8856
+ * in month: ..9
8857
+ * puts "quarter 1-3"
8858
+ * in wday: 1..5, month:
8859
+ * puts "working day in month #{month}"
8860
+ * end
8861
+ * #=> prints "working day in month 10"
8862
+ *
8863
+ * Note that deconstruction by pattern can also be combined with class check:
8864
+ *
8865
+ * if d in DateTime(wday: 1..5, hour: 10..18, day: ..7)
8866
+ * puts "Working time, first week of the month"
8867
+ * end
8868
+ *
8869
+ */
8870
+ static VALUE
8871
+ dt_lite_deconstruct_keys(VALUE self, VALUE keys) {
8872
+ return deconstruct_keys(self, keys, /* is_datetime=true */ 1);
8873
+ }
8874
+
8743
8875
  /* conversions */
8744
8876
 
8745
8877
  #define f_subsec(x) rb_funcall(x, rb_intern("subsec"), 0)
@@ -9370,6 +9502,17 @@ Init_date_core(void)
9370
9502
  id_ge_p = rb_intern_const(">=");
9371
9503
  id_eqeq_p = rb_intern_const("==");
9372
9504
 
9505
+ sym_year = ID2SYM(rb_intern_const("year"));
9506
+ sym_month = ID2SYM(rb_intern_const("month"));
9507
+ sym_yday = ID2SYM(rb_intern_const("yday"));
9508
+ sym_wday = ID2SYM(rb_intern_const("wday"));
9509
+ sym_day = ID2SYM(rb_intern_const("day"));
9510
+ sym_hour = ID2SYM(rb_intern_const("hour"));
9511
+ sym_min = ID2SYM(rb_intern_const("min"));
9512
+ sym_sec = ID2SYM(rb_intern_const("sec"));
9513
+ sym_sec_fraction = ID2SYM(rb_intern_const("sec_fraction"));
9514
+ sym_zone = ID2SYM(rb_intern_const("zone"));
9515
+
9373
9516
  half_days_in_day = rb_rational_new2(INT2FIX(1), INT2FIX(2));
9374
9517
 
9375
9518
  #if (LONG_MAX / DAY_IN_SECONDS) > SECOND_IN_NANOSECONDS
@@ -9691,6 +9834,8 @@ Init_date_core(void)
9691
9834
  rb_define_method(cDate, "httpdate", d_lite_httpdate, 0);
9692
9835
  rb_define_method(cDate, "jisx0301", d_lite_jisx0301, 0);
9693
9836
 
9837
+ rb_define_method(cDate, "deconstruct_keys", d_lite_deconstruct_keys, 1);
9838
+
9694
9839
  #ifndef NDEBUG
9695
9840
  rb_define_method(cDate, "marshal_dump_old", d_lite_marshal_dump_old, 0);
9696
9841
  #endif
@@ -9901,6 +10046,8 @@ Init_date_core(void)
9901
10046
  rb_define_method(cDateTime, "rfc3339", dt_lite_rfc3339, -1);
9902
10047
  rb_define_method(cDateTime, "jisx0301", dt_lite_jisx0301, -1);
9903
10048
 
10049
+ rb_define_method(cDateTime, "deconstruct_keys", dt_lite_deconstruct_keys, 1);
10050
+
9904
10051
  /* conversions */
9905
10052
 
9906
10053
  rb_define_method(rb_cTime, "to_time", time_to_time, 0);
data/lib/date.rb CHANGED
@@ -4,7 +4,7 @@
4
4
  require 'date_core'
5
5
 
6
6
  class Date
7
- VERSION = "3.3.1" # :nodoc:
7
+ VERSION = "3.3.2" # :nodoc:
8
8
 
9
9
  # call-seq:
10
10
  # infinite? -> false
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: date
3
3
  version: !ruby/object:Gem::Version
4
- version: 3.3.1
4
+ version: 3.3.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Tadayoshi Funaba
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2022-12-09 00:00:00.000000000 Z
11
+ date: 2022-12-16 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: A subclass of Object includes Comparable module for handling dates.
14
14
  email:
@@ -42,14 +42,14 @@ required_ruby_version: !ruby/object:Gem::Requirement
42
42
  requirements:
43
43
  - - ">="
44
44
  - !ruby/object:Gem::Version
45
- version: 2.4.0
45
+ version: 2.6.0
46
46
  required_rubygems_version: !ruby/object:Gem::Requirement
47
47
  requirements:
48
48
  - - ">="
49
49
  - !ruby/object:Gem::Version
50
50
  version: '0'
51
51
  requirements: []
52
- rubygems_version: 3.4.0.dev
52
+ rubygems_version: 3.3.26
53
53
  signing_key:
54
54
  specification_version: 4
55
55
  summary: A subclass of Object includes Comparable module for handling dates.