timesteps 1.0.3 → 1.0.6

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 7475cc3a4a0af27a77730770cd4c952282ecfd1054d3bdd08a53d1da76ebe5a7
4
- data.tar.gz: fdb95e8bd70ec9f2bcddf3b8c206f8db1330f169b54b531d74e4fc4148be92e3
3
+ metadata.gz: 1e933e2020424ed914b88472cafe4e2c7634dd6afe07475f4b0b915689fe4693
4
+ data.tar.gz: be0df225f53617124a675c374f41f47924da31926043a859f548e3d563bf5f6c
5
5
  SHA512:
6
- metadata.gz: 1c8e36605a8128a20a7bf27af046e44d7f3d37cacb3669a7e28c4fa17aa4f0883bc9b44730756471b73f16afee6ef114b8b7021d2cee7812869914a3c832fdab
7
- data.tar.gz: 3598ecc55200ff8e1aa474c0fd3f5c4c443b973c853557db3003e8489eb69f2630b76e4a2a44106ede8328efec69aca90d5e72e23bb95d2cdaed97d226657bfc
6
+ metadata.gz: 61c5653b8eb7b6423c92abdc89e331aac3f8675732ffc424fb34268bb6b69b24b63d7eaefdd121d4a3e28a3ecf1bd726a5a28639f25ce3f705039b5fb9fd6f32
7
+ data.tar.gz: 17015550f19920f9ce2c80e7a8e5df53f80deb44d7d6b6842f3eacd89d0eaf41cf2627274191767bacfbecd670630bdff1b95d2b8add41894eae34aeb9951289
data/Note.ja.md CHANGED
@@ -6,13 +6,36 @@ topicブランチ -> Feature開発版
6
6
 
7
7
  "XX since YYY" と since: time を同時に指定下場合は、前者が採用される (sinceオプションは無視される)。
8
8
 
9
- ### 時間の単位 (udunits)
10
- * years, year
11
- * months,month
9
+ ### 時間の単位 (udunits + α)
10
+
11
+ Normal cases
12
+
13
+ * years, year, yrs, yr, y
14
+ * months, month, mons, mon
12
15
  * days, day, d
13
- * hours, hour, h
14
- * minutes, minute, min
15
- * seconds, sec, s
16
+ * hours, hour, hrs, hr, h
17
+ * minutes, minute, mins, min, m
18
+ * seconds, second, secs, sec, s
19
+ * milliseconds, millisecond, msecs, msec, ms
20
+ * microseconds, microsecond, usecs, usec, us
21
+ * ayears, ayear
22
+
23
+ Week days (has a one week intervals and a day offset)
24
+
25
+ * weeks-sun, week-sun, w-sun
26
+ * weeks-mon, week-mon, w-mon
27
+ * weeks-tue, week-tue, w-tue
28
+ * weeks-wed, week-wed, w-wed
29
+ * weeks-thu, week-thu, w-thu
30
+ * weeks-fri, week-fri, w-fri
31
+ * weeks-sat, week-sat, w-sat
32
+ * sundays, sunday
33
+ * mondays, monday
34
+ * tuesdays, tuesday
35
+ * wednesdays, wednesday
36
+ * thursdays, thursday
37
+ * fridays, friday
38
+ * saturdays, saturdays
16
39
 
17
40
  ### クラス構成
18
41
 
@@ -54,6 +77,7 @@ topicブランチ -> Feature開発版
54
77
  TimeSteps::Calendar#parse(spec) (initialized with bc: option)
55
78
 
56
79
  ### 暦の種類
80
+
57
81
  #### standard, gregorian
58
82
 
59
83
  * 今日使われているグレゴリオ暦
@@ -86,7 +110,14 @@ topicブランチ -> Feature開発版
86
110
  ts = TimeStep.new("6 hours since 2012-01-10 09:00:00")
87
111
  ts = ts.shift_origin_with_days(-256.quo(24))
88
112
 
113
+ ### 謎仕様(修正求む)
114
+
115
+ #### 1日を1年単位で求めると...
116
+
117
+ ts = TimeStep.new("year since 2022-01-01")
118
+ ts.in("days").inverse(1) ### => (1/2)
89
119
 
120
+ なぜに(1/2)
90
121
 
91
122
 
92
123
 
@@ -49,6 +49,8 @@ class TimeStep
49
49
  'msecs?',
50
50
  'ms',
51
51
  'microseconds?',
52
+ 'usecs?',
53
+ 'us',
52
54
  '(?:weeks?|w)(?:\-(?:sun|mon|tue|wed|thu|fri|sat))?',
53
55
  'sundays?',
54
56
  'mondays?',
@@ -210,13 +212,13 @@ class TimeStep
210
212
  end
211
213
  @interval = @numeric
212
214
  case symbol
213
- when /\Ayears?\z/i
215
+ when /\A(years?|yrs?|y)\z/i
214
216
  unless numeric.denominator == 1
215
217
  raise "numeric factor for year should be an integer"
216
218
  end
217
219
  @symbol = :years
218
220
  @interval *= 356.242*86400
219
- when /\Amonths?\z/i
221
+ when /\A(months?|mons?)\z/i
220
222
  unless numeric.denominator == 1
221
223
  raise "numeric factor for month should be an integer"
222
224
  end
@@ -228,7 +230,7 @@ class TimeStep
228
230
  when /\A(hours?|hrs?|h)\z/i
229
231
  @symbol = :hours
230
232
  @interval *= 3600
231
- when /\A(minutes?|mins?)\z/i
233
+ when /\A(minutes?|mins?|m)\z/i
232
234
  @symbol = :minutes
233
235
  @interval *= 60
234
236
  when /\A(seconds?|secs?|s)\z/i
@@ -236,7 +238,7 @@ class TimeStep
236
238
  when /\A(milliseconds?|msecs?|ms)\z/i
237
239
  @symbol = :seconds
238
240
  @interval *= 1.quo(1000)
239
- when /\Amicroseconds?\z/i
241
+ when /\A(microseconds?|usecs?|us)\z/i
240
242
  @symbol = :seconds
241
243
  @interval *= 1.quo(1000000)
242
244
  when /\A(weeks?|w)\-(sun|mon|tue|wed|thu|fri|sat)\z/i
@@ -247,12 +249,12 @@ class TimeStep
247
249
  when /\A(weeks?|w)\z/i
248
250
  @symbol = :weeks
249
251
  @interval *= 7*86400
250
- when /\Asundays?|mondays?|tuesdays?|wednesdays?|thursday?|fridays?|saturdays?\z/i
252
+ when /\A(sundays?|mondays?|tuesdays?|wednesdays?|thursday?|fridays?|saturdays?)\z/i
251
253
  symbol =~ /\A(\w{3})/i
252
254
  @wday = $1.downcase.intern
253
255
  @symbol = WDAY_NAME[@wday].intern ### :sundays, :mondays, ...
254
256
  @interval *= 7*86400
255
- when /\Aayears?\z/i
257
+ when /\A(ayears?)\z/i
256
258
  @symbol = :days
257
259
  @numeric *= 365.242198781.to_r
258
260
  @interval = @numeric * 86400
@@ -457,6 +459,7 @@ class TimeStep
457
459
  time = times.first
458
460
  time = time.to_datetime if time.is_a?(Time)
459
461
  time = @calendar.parse(time, format: format, offset: @origin.offset) if time.is_a?(String)
462
+ time = time.new_offset(@origin.offset) if time.offset != @origin.offset
460
463
  case @symbol
461
464
  when :years
462
465
  diff = time.difference_in_years(@origin)
@@ -566,7 +569,7 @@ class TimeStep
566
569
  #
567
570
  # @return [Numeric]
568
571
  def next_index_of (time)
569
- time = @calendar.parse(time, offset: @origin.offset) if time.is_a?(String)
572
+ time = @calendar.parse(time) if time.is_a?(String)
570
573
  return index_at(time).floor + 1
571
574
  end
572
575
 
@@ -583,7 +586,7 @@ class TimeStep
583
586
  #
584
587
  # @return [Numeric]
585
588
  def prev_index_of (time)
586
- time = @calendar.parse(time, offset: @origin.offset) if time.is_a?(String)
589
+ time = @calendar.parse(time) if time.is_a?(String)
587
590
  return index_at(time).ceil - 1
588
591
  end
589
592
 
@@ -691,8 +694,8 @@ class TimeStep
691
694
  # ts.range("2001", "2002", ends: "[)")
692
695
  # ts.range("2001", 1)
693
696
  #
694
- # @param start [Numeric, DateTime]
695
- # @param last [Numeric, DateTime]
697
+ # @param start [Numeric, DateTime, String]
698
+ # @param last [Numeric, DateTime, String]
696
699
  # @param count [Integer]
697
700
  # @param ends [String] one of "[]", "()", "[)", "(]"
698
701
  #
@@ -1,6 +1,4 @@
1
1
 
2
- autoload :TZInfo, "tzinfo"
3
-
4
2
  class TimeStep
5
3
 
6
4
  class Calendar
@@ -69,7 +67,10 @@ class TimeStep
69
67
  end
70
68
 
71
69
  def valid_datetime_type? (time)
72
- return false unless time.is_a?(DateTimeType[@calendar])
70
+ if @calendar == :standard and time.is_a?(TZInfo::DateTimeWithOffset)
71
+ return true
72
+ end
73
+ return false unless time.is_a?(DateTimeType[@calendar])
73
74
  case @calendar
74
75
  when :standard
75
76
  return time.start == Date::ITALY
data/lib/timesteps.rb CHANGED
@@ -1,5 +1,6 @@
1
1
 
2
2
  require "date"
3
+ require "tzinfo"
3
4
  require "timesteps/time"
4
5
  require "timesteps/datetimelike"
5
6
  require "timesteps/datetimelike_format"
@@ -50,12 +50,15 @@ describe "TimeStep.new" do
50
50
  ["minute since 2001-02-03 04:05:06 +07:00", 1, :minutes],
51
51
  ["second since 2001-02-03 04:05:06 +07:00", 1, :seconds],
52
52
 
53
+ ["yr since 2001-02-03 04:05:06 +07:00", 1, :years],
54
+ ["mon since 2001-02-03 04:05:06 +07:00", 1, :months],
53
55
  ["d since 2001-02-03 04:05:06 +07:00", 1, :days],
54
56
  ["hr since 2001-02-03 04:05:06 +07:00", 1, :hours],
55
57
  ["h since 2001-02-03 04:05:06 +07:00", 1, :hours],
56
58
  ["min since 2001-02-03 04:05:06 +07:00", 1, :minutes],
57
59
  ["sec since 2001-02-03 04:05:06 +07:00", 1, :seconds],
58
60
  ["s since 2001-02-03 04:05:06 +07:00", 1, :seconds],
61
+ ["ms since 2001-02-03 04:05:06 +07:00", 1, :seconds],
59
62
 
60
63
  ["3 years since 2001-02-03 04:05:06 +07:00", 3, :years],
61
64
  ["3 months since 2001-02-03 04:05:06 +07:00", 3, :months],
@@ -115,12 +118,23 @@ describe "TimeStep.new" do
115
118
 
116
119
  tests.each do |spec, numeric, symbol|
117
120
  ts = TimeStep.new(spec)
118
-
119
121
  is_asserted_by { ts.numeric == numeric }
120
122
  is_asserted_by { ts.symbol == symbol }
121
123
  end
122
124
 
123
125
  end
126
+
127
+ example 'without since' do
128
+
129
+ is_asserted_by { TimeStep.new("year").origin == DateTime.new(0) }
130
+ is_asserted_by { TimeStep.new("month").origin == DateTime.new(0) }
131
+ is_asserted_by { TimeStep.new("week").origin == DateTime.new(0) }
132
+ is_asserted_by { TimeStep.new("day").origin == DateTime.new(0) }
133
+ is_asserted_by { TimeStep.new("hour").origin == DateTime.new(1970,1,1) }
134
+ is_asserted_by { TimeStep.new("min").origin == DateTime.new(1970,1,1) }
135
+ is_asserted_by { TimeStep.new("sec").origin == DateTime.new(1970,1,1) }
136
+
137
+ end
124
138
 
125
139
  example 'since option with format' do
126
140
 
@@ -337,6 +351,17 @@ describe "TimeStep#index_at" do
337
351
 
338
352
  end
339
353
 
354
+ example 'different offset' do
355
+
356
+ ts = TimeStep.new("1 seconds since 1970-01-01 00:00:00")
357
+
358
+ time_utc = DateTime.parse("2001-01-01 00:00:00 +00:00")
359
+ time_jst = DateTime.parse("2001-01-01 09:00:00 +09:00")
360
+
361
+ is_asserted_by { ts.index_at(time_utc) == ts.index_at(time_jst) }
362
+
363
+ end
364
+
340
365
  end
341
366
 
342
367
  describe "TimeStep#duration_at" do
@@ -436,7 +461,6 @@ describe "TimeStep#new_origin" do
436
461
 
437
462
  end
438
463
 
439
-
440
464
  describe "TimeStep#right_time?" do
441
465
 
442
466
  example do
@@ -449,3 +473,33 @@ describe "TimeStep#right_time?" do
449
473
 
450
474
  end
451
475
 
476
+ describe "TimeStep#truncate" do
477
+
478
+ example 'hours' do
479
+
480
+ ts = TimeStep.new("1 hour")
481
+
482
+ time1 = DateTime.parse("2001-01-01 01:30:00 +00:00")
483
+ time1_jst = DateTime.parse("2001-01-01 10:30:00 +09:00")
484
+ time2 = DateTime.parse("2001-01-01 01:00:00 +00:00")
485
+
486
+ is_asserted_by { ts.truncate(time1).offset == 0 }
487
+ is_asserted_by { time2 == ts.truncate(time1) }
488
+ is_asserted_by { time2 == ts.truncate(time1_jst) }
489
+
490
+ ts_jst = TimeStep.new("1 hour since 1970-01-01 00:00 +09:00")
491
+
492
+ time1 = DateTime.parse("2001-01-01 01:30:00 +00:00")
493
+ time1_jst = DateTime.parse("2001-01-01 10:30:00 +09:00")
494
+ time2 = DateTime.parse("2001-01-01 01:00:00 +00:00")
495
+ time2_jst = DateTime.parse("2001-01-01 10:00:00 +09:00")
496
+
497
+ is_asserted_by { ts_jst.truncate(time1).offset == 9.quo(24) }
498
+ is_asserted_by { time2 == ts_jst.truncate(time1) }
499
+ is_asserted_by { time2 == ts_jst.truncate(time1_jst) }
500
+ is_asserted_by { time2_jst == ts_jst.truncate(time1) }
501
+ is_asserted_by { time2_jst == ts_jst.truncate(time1_jst) }
502
+
503
+ end
504
+
505
+ end
data/timesteps.gemspec CHANGED
@@ -1,29 +1,30 @@
1
1
 
2
2
  Gem::Specification::new do |s|
3
- version = "1.0.3"
3
+ version = '1.0.6'
4
4
 
5
- files = Dir.glob("**/*") + [".yardopts"] - [
6
- Dir.glob("timesteps-*.gem"),
7
- Dir.glob("doc/**/*"),
8
- Dir.glob("examples/**/*"),
5
+ files = Dir.glob('**/*') + ['.yardopts'] - [
6
+ Dir.glob('timesteps-*.gem'),
7
+ Dir.glob('doc/**/*'),
8
+ Dir.glob('examples/**/*'),
9
9
  ].flatten
10
10
 
11
11
  s.platform = Gem::Platform::RUBY
12
- s.name = "timesteps"
13
- s.summary = "A library for handling discrete time series in constant increments"
12
+ s.name = 'timesteps'
13
+ s.summary = 'A library for handling discrete time series in constant increments'
14
14
  s.description = <<-HERE
15
15
  A library for time conversion and intercomparison of multiple time series data
16
16
  in the case of handling time series data of the type that specifies the time using
17
17
  indexes of time steps since origin time. It handles time units notation like
18
- "hours since 2001-01-01 00:00:00", which is originate from udunits library and
18
+ 'hours since 2001-01-01 00:00:00', which is originate from udunits library and
19
19
  also is used in CF-convension of NetCDF. The main purpose of this library is to
20
20
  describe the time axis when dealing with time series of observational data and climate data.
21
21
  HERE
22
22
  s.version = version
23
23
  s.license = 'MIT'
24
- s.author = "Hiroki Motoyoshi"
25
- s.email = ""
24
+ s.author = 'Hiroki Motoyoshi'
25
+ s.email = ''
26
26
  s.homepage = 'https://github.com/himotoyoshi/timesteps'
27
27
  s.files = files
28
- s.required_ruby_version = ">= 2.4.1"
28
+ s.required_ruby_version = '>= 2.4.1'
29
+ s.add_runtime_dependency 'tzinfo', '~> 2.0'
29
30
  end
metadata CHANGED
@@ -1,19 +1,33 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: timesteps
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.3
4
+ version: 1.0.6
5
5
  platform: ruby
6
6
  authors:
7
7
  - Hiroki Motoyoshi
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2022-06-21 00:00:00.000000000 Z
12
- dependencies: []
11
+ date: 2022-09-27 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: tzinfo
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '2.0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '2.0'
13
27
  description: " A library for time conversion and intercomparison of multiple time
14
28
  series data \n in the case of handling time series data of the type that specifies
15
29
  the time using \n indexes of time steps since origin time. It handles time units
16
- notation like \n \"hours since 2001-01-01 00:00:00\", which is originate from udunits
30
+ notation like \n 'hours since 2001-01-01 00:00:00', which is originate from udunits
17
31
  library and \n also is used in CF-convension of NetCDF. The main purpose of this
18
32
  library is to \n describe the time axis when dealing with time series of observational
19
33
  data and climate data.\n"