w3c_datetime 0.0.3 → 0.0.4

Sign up to get free protection for your applications and to get access to all the features.
data/lib/time.rb ADDED
@@ -0,0 +1,5 @@
1
+ class Time
2
+ def to_w3c_datetime
3
+ strftime '%Y-%m-%dT%H:%M:%S%:z'
4
+ end
5
+ end
data/lib/w3c_datetime.rb CHANGED
@@ -1,14 +1,2 @@
1
- class W3cDatetime
2
- def self.parse(date_str)
3
- date_regexp = /(?<year>\d{4})-(?<month>\d{2})-(?<day>\d{2})(?:T(?<hour>\d{2}):(?<minute>\d{2}):?(?<second>\d{2})?\.?(?<milisecond>\d{1,2})?(?<timezone>.+)?)?/
4
- parsed = date_regexp.match(date_str)
5
- begin
6
- Time.new(parsed[:year].to_i, parsed[:month].to_i, parsed[:day].to_i, parsed[:hour].to_i, parsed[:minute].to_i, parsed[:second].to_i, self.get_timezone(parsed[:timezone])) unless parsed.nil?
7
- rescue ArgumentError, TypeError
8
- end
9
- end
10
-
11
- def self.get_timezone(timezone)
12
- timezone || "+00:00"
13
- end
14
- end
1
+ require File.dirname(__FILE__) + '/w3c_datetime/w3c_datetime'
2
+ require File.dirname(__FILE__) + '/time'
@@ -0,0 +1,40 @@
1
+ # Handle W3 Date and Time formats
2
+ #
3
+ # Handle W3 Date and Time formats as specified in http://www.w3.org/TR/NOTE-datetime
4
+ #
5
+ # @see http://www.w3.org/TR/NOTE-datetime
6
+ #
7
+ # @example
8
+ # W3cDateTime::parse('2013-02-10')
9
+ # W3cDateTime::parse('2013-02-10T10:10Z')
10
+ # W3cDateTime::parse('2013-02-10T10:10:20+10:00')
11
+ #
12
+ # @author Michal Pawlowski <misza222@gmail.com>
13
+ class W3cDatetime
14
+
15
+ # Parse date string in http://www.w3.org/TR/NOTE-datetime format
16
+ #
17
+ # @param [String] date_str date represented as string in http://www.w3.org/TR/NOTE-datetime format
18
+ # @return [Time] Time object
19
+ def self.parse(date_str)
20
+ date_regexp = /^(?<year>\d{4})-(?<month>\d{2})-(?<day>\d{2})(?:T(?<hour>\d{2}):(?<minute>\d{2}):?(?<second>\d{2})?\.?(?<milisecond>\d{1,2})?(?:(?<timezone>.+)|Z)?)?$/
21
+ parsed = date_regexp.match(date_str)
22
+ begin
23
+ Time.new(parsed[:year].to_i, parsed[:month].to_i, parsed[:day].to_i, parsed[:hour].to_i, parsed[:minute].to_i, parsed[:second].to_i, get_timezone(parsed[:timezone])) unless parsed.nil?
24
+ rescue ArgumentError, TypeError
25
+ end
26
+ end
27
+
28
+ class << self
29
+ private
30
+ def get_timezone(timezone)
31
+ if timezone == 'Z'
32
+ timezone = '+00:00'
33
+ elsif timezone == ''
34
+ timezone = nil
35
+ end
36
+
37
+ timezone
38
+ end
39
+ end
40
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: w3c_datetime
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.3
4
+ version: 0.0.4
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-09-03 00:00:00.000000000 Z
12
+ date: 2013-09-04 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: bundler
@@ -50,6 +50,8 @@ extensions: []
50
50
  extra_rdoc_files: []
51
51
  files:
52
52
  - lib/w3c_datetime.rb
53
+ - lib/time.rb
54
+ - lib/w3c_datetime/w3c_datetime.rb
53
55
  homepage: https://github.com/itsudo/w3c_datetime
54
56
  licenses:
55
57
  - MIT
@@ -76,3 +78,4 @@ signing_key:
76
78
  specification_version: 3
77
79
  summary: Deal with date and time in w3c format
78
80
  test_files: []
81
+ has_rdoc: