time 0.4.1 → 0.5.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.
Files changed (6) hide show
  1. checksums.yaml +4 -4
  2. data/.document +4 -0
  3. data/.rdoc_options +2 -0
  4. data/README.md +7 -5
  5. data/lib/time.rb +65 -14
  6. metadata +6 -6
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: c211ffd223b56fae14493d2d7fb187ac2e7fa4f089943fba05174f0ee9549c4c
4
- data.tar.gz: df4434890036e502a795ac24cfb93920ace49539b4ef2f092942cdef049ba141
3
+ metadata.gz: 6ad6ef6fa9d67bc974c6b57e3885ddbf4950f1467be228eee70a9104f905a5ec
4
+ data.tar.gz: de3739295b2c8923b7e76b1a895235052dcb4b49a3fff8725aede781568cbea3
5
5
  SHA512:
6
- metadata.gz: 247084d1b3bbdda9a0f21652daf9d729a2f32d3aa89a16c3e4b78d305c49fa487c75386a80ebcd88b9bf0ccec224ae1aa1989baa4a96bbdb88e580b97812521f
7
- data.tar.gz: 578f214835d4259e3a4cd9aff080806b29a04c886b90f8acd4f95527af581698722423fd85a4ef3e0d911b7f8f797adfdddfb3239c4cba20e4970299271b3a70
6
+ metadata.gz: afdc920a6bb5caf7081e7d3019a0162559f9ed8fce7bd884c0c83f51caa88647d78ef0b1588fc1691f209aebae5a306341e2a155ed56a6a18005e191a52f5b56
7
+ data.tar.gz: cb6f69028390588f8986d32e4d3c9322fc5cd3ef96e5c161415d46562b6179158c9efb8490b920c59e4adcb3ccae16866ab370b8b15c2fc5b92540d1033a48b7
data/.document ADDED
@@ -0,0 +1,4 @@
1
+ BSDL
2
+ COPYING
3
+ README.md
4
+ lib/
data/.rdoc_options ADDED
@@ -0,0 +1,2 @@
1
+ ---
2
+ main_page: README.md
data/README.md CHANGED
@@ -8,13 +8,16 @@ and converting Times.
8
8
  This library extends the Time class with the following conversions between
9
9
  date strings and Time objects:
10
10
 
11
- * date-time defined by {RFC 2822}[http://www.ietf.org/rfc/rfc2822.txt]
12
- * HTTP-date defined by {RFC 2616}[http://www.ietf.org/rfc/rfc2616.txt]
13
- * dateTime defined by XML Schema Part 2: Datatypes ({ISO
14
- 8601}[http://www.iso.org/iso/date_and_time_format])
11
+ * date-time defined by [RFC 2822]
12
+ * HTTP-date defined by [RFC 2616]
13
+ * dateTime defined by XML Schema Part 2: Datatypes ([ISO 8601])
15
14
  * various formats handled by Date._parse
16
15
  * custom formats handled by Date._strptime
17
16
 
17
+ [RFC 2822]: http://www.ietf.org/rfc/rfc2822.txt
18
+ [RFC 2616]: http://www.ietf.org/rfc/rfc2616.txt
19
+ [ISO 8601]: http://www.iso.org/iso/date_and_time_format
20
+
18
21
  ## Installation
19
22
 
20
23
  Add this line to your application's Gemfile:
@@ -51,4 +54,3 @@ To install this gem onto your local machine, run `bundle exec rake install`. To
51
54
  ## Contributing
52
55
 
53
56
  Bug reports and pull requests are welcome on GitHub at https://github.com/ruby/time.
54
-
data/lib/time.rb CHANGED
@@ -24,9 +24,10 @@ require 'date'
24
24
 
25
25
  # :startdoc:
26
26
 
27
+ # #
27
28
  class Time
28
29
 
29
- VERSION = "0.4.1" # :nodoc:
30
+ VERSION = "0.5.0" # :nodoc:
30
31
 
31
32
  class << Time
32
33
 
@@ -79,7 +80,7 @@ class Time
79
80
  #
80
81
  # You must require 'time' to use this method.
81
82
  #
82
- def zone_offset(zone, year=self.now.year)
83
+ def zone_offset(zone, year=nil)
83
84
  off = nil
84
85
  zone = zone.upcase
85
86
  if /\A([+-])(\d\d)(:?)(\d\d)(?:\3(\d\d))?\z/ =~ zone
@@ -88,10 +89,13 @@ class Time
88
89
  off = zone.to_i * 3600
89
90
  elsif ZoneOffset.include?(zone)
90
91
  off = ZoneOffset[zone] * 3600
91
- elsif ((t = self.local(year, 1, 1)).zone.upcase == zone rescue false)
92
- off = t.utc_offset
93
- elsif ((t = self.local(year, 7, 1)).zone.upcase == zone rescue false)
94
- off = t.utc_offset
92
+ else
93
+ year ||= self.now.year
94
+ if ((t = self.local(year, 1, 1)).zone.upcase == zone rescue false)
95
+ off = t.utc_offset
96
+ elsif ((t = self.local(year, 7, 1)).zone.upcase == zone rescue false)
97
+ off = t.utc_offset
98
+ end
95
99
  end
96
100
  off
97
101
  end
@@ -365,6 +369,9 @@ class Time
365
369
  # If the extracted time zone abbreviation does not match any of them,
366
370
  # it is ignored and the given time is regarded as a local time.
367
371
  #
372
+ # A +zone+ argument can be provided to specify the zone for the given
373
+ # +date+, if the +date+ does not include a time zone or offset.
374
+ #
368
375
  # ArgumentError is raised if Date._parse cannot extract information from
369
376
  # +date+ or if the Time class cannot represent specified date.
370
377
  #
@@ -378,12 +385,12 @@ class Time
378
385
  #
379
386
  # You must require 'time' to use this method.
380
387
  #
381
- def parse(date, now=self.now)
388
+ def parse(date, now=self.now, zone: nil)
382
389
  comp = !block_given?
383
390
  d = Date._parse(date, comp)
384
391
  year = d[:year]
385
392
  year = yield(year) if year && !comp
386
- make_time(date, year, d[:yday], d[:mon], d[:mday], d[:hour], d[:min], d[:sec], d[:sec_fraction], d[:zone], now)
393
+ make_time(date, year, d[:yday], d[:mon], d[:mday], d[:hour], d[:min], d[:sec], d[:sec_fraction], d[:zone] || zone, now)
387
394
  end
388
395
 
389
396
  #
@@ -503,7 +510,7 @@ class Time
503
510
  # require 'time'
504
511
  #
505
512
  # Time.rfc2822("Wed, 05 Oct 2011 22:26:12 -0400")
506
- # #=> 2010-10-05 22:26:12 -0400
513
+ # #=> 2011-10-05 22:26:12 -0400
507
514
  #
508
515
  # You must require 'time' to use this method.
509
516
  #
@@ -620,13 +627,58 @@ class Time
620
627
  # You must require 'time' to use this method.
621
628
  #
622
629
  def xmlschema(time)
623
- if /\A\s*
630
+ pattern = /\A\s*
624
631
  (-?\d+)-(\d\d)-(\d\d)
625
632
  T
626
633
  (\d\d):(\d\d):(\d\d)
627
634
  (\.\d+)?
628
635
  (Z|[+-]\d\d(?::?\d\d)?)?
629
- \s*\z/ix =~ time
636
+ \s*\z/ix
637
+ _xmlschema(pattern, time) or
638
+ raise ArgumentError.new("invalid xmlschema format: #{time.inspect}")
639
+ end
640
+ alias iso8601 xmlschema
641
+
642
+ #
643
+ # Parses +time+ as a dateTime defined by RFC3339 and converts it to
644
+ # a Time object.
645
+ #
646
+ # ArgumentError is raised if +time+ is not compliant with the format or if
647
+ # the Time class cannot represent the specified time.
648
+ #
649
+ # See #xmlschema for more information on this format.
650
+ #
651
+ # require 'time'
652
+ #
653
+ # Time.rfc3339("2011-10-05T22:26:12-04:00")
654
+ # #=> 2011-10-05 22:26:12-04:00
655
+ #
656
+ # You must require 'time' to use this method.
657
+ #
658
+ def rfc3339(time)
659
+ pattern = /\A\s*
660
+ (-?\d{4})-(\d\d)-(\d\d)
661
+ [T ]
662
+ (\d\d):(\d\d):(\d\d)
663
+ (\.\d+)?
664
+ (Z|[+-]\d\d:\d\d)
665
+ \s*\z/ix
666
+ _xmlschema(pattern, time) or
667
+ raise ArgumentError.new("invalid rfc3339 format: #{time.inspect}")
668
+ end
669
+
670
+ private
671
+
672
+ if RUBY_VERSION >= "3.2"
673
+ def _xmlschema(pattern, time) # :nodoc:
674
+ return unless pattern.match?(time)
675
+ time = time.strip
676
+ time.upcase!
677
+ new(time)
678
+ end
679
+ else
680
+ def _xmlschema(pattern, time) # :nodoc:
681
+ return unless pattern =~ time
630
682
  year = $1.to_i
631
683
  mon = $2.to_i
632
684
  day = $3.to_i
@@ -648,11 +700,9 @@ class Time
648
700
  else
649
701
  self.local(year, mon, day, hour, min, sec, usec)
650
702
  end
651
- else
652
- raise ArgumentError.new("invalid xmlschema format: #{time.inspect}")
653
703
  end
654
704
  end
655
- alias iso8601 xmlschema
705
+
656
706
  end # class << self
657
707
 
658
708
  #
@@ -727,4 +777,5 @@ class Time
727
777
  end
728
778
  end
729
779
  alias iso8601 xmlschema unless method_defined?(:iso8601)
780
+ alias rfc3339 xmlschema
730
781
  end
metadata CHANGED
@@ -1,14 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: time
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.1
4
+ version: 0.5.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Tanaka Akira
8
- autorequire:
9
8
  bindir: exe
10
9
  cert_chain: []
11
- date: 2024-11-08 00:00:00.000000000 Z
10
+ date: 2026-10-08 00:00:00.000000000 Z
12
11
  dependencies:
13
12
  - !ruby/object:Gem::Dependency
14
13
  name: date
@@ -31,6 +30,8 @@ executables: []
31
30
  extensions: []
32
31
  extra_rdoc_files: []
33
32
  files:
33
+ - ".document"
34
+ - ".rdoc_options"
34
35
  - BSDL
35
36
  - COPYING
36
37
  - README.md
@@ -42,7 +43,7 @@ licenses:
42
43
  metadata:
43
44
  homepage_uri: https://github.com/ruby/time
44
45
  source_code_uri: https://github.com/ruby/time
45
- post_install_message:
46
+ changelog_uri: https://github.com/ruby/time/releases
46
47
  rdoc_options: []
47
48
  require_paths:
48
49
  - lib
@@ -57,8 +58,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
57
58
  - !ruby/object:Gem::Version
58
59
  version: '0'
59
60
  requirements: []
60
- rubygems_version: 3.5.11
61
- signing_key:
61
+ rubygems_version: 3.6.9
62
62
  specification_version: 4
63
63
  summary: Extends the Time class with methods for parsing and conversion.
64
64
  test_files: []