datet 0.0.0 → 0.0.1

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 (5) hide show
  1. data/VERSION +1 -1
  2. data/datet.gemspec +63 -0
  3. data/lib/datet.rb +57 -40
  4. data/spec/datet_spec.rb +25 -0
  5. metadata +4 -3
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.0.0
1
+ 0.0.1
data/datet.gemspec ADDED
@@ -0,0 +1,63 @@
1
+ # Generated by jeweler
2
+ # DO NOT EDIT THIS FILE DIRECTLY
3
+ # Instead, edit Jeweler::Tasks in Rakefile, and run 'rake gemspec'
4
+ # -*- encoding: utf-8 -*-
5
+
6
+ Gem::Specification.new do |s|
7
+ s.name = %q{datet}
8
+ s.version = "0.0.1"
9
+
10
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
+ s.authors = ["Kasper Johansen"]
12
+ s.date = %q{2012-07-13}
13
+ s.description = %q{A framework for handeling date- and time-related stuff in Ruby.}
14
+ s.email = %q{k@spernj.org}
15
+ s.extra_rdoc_files = [
16
+ "LICENSE.txt",
17
+ "README.rdoc"
18
+ ]
19
+ s.files = [
20
+ ".document",
21
+ ".rspec",
22
+ "Gemfile",
23
+ "Gemfile.lock",
24
+ "LICENSE.txt",
25
+ "README.rdoc",
26
+ "Rakefile",
27
+ "VERSION",
28
+ "datet.gemspec",
29
+ "lib/datet.rb",
30
+ "spec/datet_spec.rb",
31
+ "spec/spec_helper.rb"
32
+ ]
33
+ s.homepage = %q{http://github.com/kaspernj/datet}
34
+ s.licenses = ["MIT"]
35
+ s.require_paths = ["lib"]
36
+ s.rubygems_version = %q{1.6.2}
37
+ s.summary = %q{A framework for handeling date- and time-related stuff in Ruby.}
38
+
39
+ if s.respond_to? :specification_version then
40
+ s.specification_version = 3
41
+
42
+ if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
43
+ s.add_development_dependency(%q<rspec>, ["~> 2.8.0"])
44
+ s.add_development_dependency(%q<rdoc>, ["~> 3.12"])
45
+ s.add_development_dependency(%q<bundler>, [">= 1.0.0"])
46
+ s.add_development_dependency(%q<jeweler>, ["~> 1.8.4"])
47
+ s.add_development_dependency(%q<rcov>, [">= 0"])
48
+ else
49
+ s.add_dependency(%q<rspec>, ["~> 2.8.0"])
50
+ s.add_dependency(%q<rdoc>, ["~> 3.12"])
51
+ s.add_dependency(%q<bundler>, [">= 1.0.0"])
52
+ s.add_dependency(%q<jeweler>, ["~> 1.8.4"])
53
+ s.add_dependency(%q<rcov>, [">= 0"])
54
+ end
55
+ else
56
+ s.add_dependency(%q<rspec>, ["~> 2.8.0"])
57
+ s.add_dependency(%q<rdoc>, ["~> 3.12"])
58
+ s.add_dependency(%q<bundler>, [">= 1.0.0"])
59
+ s.add_dependency(%q<jeweler>, ["~> 1.8.4"])
60
+ s.add_dependency(%q<rcov>, [">= 0"])
61
+ end
62
+ end
63
+
data/lib/datet.rb CHANGED
@@ -31,6 +31,20 @@ class Datet
31
31
  "december" => 12
32
32
  }
33
33
 
34
+ @@days_lcase = {
35
+ "monday" => 1,
36
+ "tuesday" => 2,
37
+ "wednesday" => 3,
38
+ "thursday" => 4,
39
+ "friday" => 5,
40
+ "saturday" => 6,
41
+ "sunday" => 0
42
+ }
43
+ @@days_lcase.clone.each do |key, val|
44
+ @@days_lcase[key[0, 3]] = val
45
+ end
46
+
47
+
34
48
  #Initializes the object. Default is the current time. A time-object can be given.
35
49
  def initialize(time = Time.now, *args)
36
50
  if time.is_a?(Time)
@@ -665,26 +679,6 @@ class Datet
665
679
  return str
666
680
  end
667
681
 
668
- #Parses the date from a database-format.
669
- #===Examples
670
- # datet = Datet.from_dbstr("2011-08-01 22:51:11")
671
- # datet.time #=> 2011-08-01 22:51:11 +0200
672
- def self.from_dbstr(date_string)
673
- if date_string.is_a?(Time)
674
- return Datet.new(date_string)
675
- elsif date_string.is_a?(Date)
676
- return Datet.new(date_string.to_time)
677
- end
678
-
679
- return false if Datet.is_nullstamp?(date_string)
680
- return Datet.new(Time.local(*Date.parse(date_string.to_s)))
681
- end
682
-
683
- #Alias for 'from_dbstr'.
684
- def self.parse(str)
685
- return Datet.from_dbstr(str)
686
- end
687
-
688
682
  #Returns true of the given stamp is a 'nullstamp'.
689
683
  #===Examples
690
684
  # Datet.is_nullstamp?("0000-00-00") #=> true
@@ -792,9 +786,13 @@ class Datet
792
786
  return Datet.new(timestr.to_time)
793
787
  elsif timestr.is_a?(Datet)
794
788
  return timestr
789
+ elsif Datet.is_nullstamp?(timestr)
790
+ return false
795
791
  end
796
792
 
797
- if match = timestr.to_s.match(/^(\d+)\/(\d+) (\d+)/)
793
+ timestr_t = timestr.to_s.downcase.strip
794
+
795
+ if match = timestr_t.match(/^(\d+)\/(\d+) (\d+)/)
798
796
  #MySQL date format
799
797
  timestr = timestr.gsub(match[0], "")
800
798
  date = match[1]
@@ -808,13 +806,13 @@ class Datet
808
806
  minute = match[2]
809
807
  end
810
808
 
811
- return Datet.new(Time.local(year, month, date, hour, minute))
812
- elsif match = timestr.to_s.match(/^(\d{1,2})\/(\d{1,2})\/(\d{4})$/)
813
- return Datet.new(Time.local(match[3], match[2], match[1]))
814
- elsif match = timestr.to_s.match(/^(\d{4})(\d{2})(\d{2})(\d{2})(\d{2})(\d{2})(\d{5,6})$/)
809
+ return Datet.new(year, month, date, hour, minute)
810
+ elsif match = timestr_t.match(/^(\d{1,2})\/(\d{1,2})\/(\d{4})$/)
811
+ return Datet.new(match[3], match[2], match[1])
812
+ elsif match = timestr_t.match(/^(\d{4})(\d{2})(\d{2})(\d{2})(\d{2})(\d{2})(\d{5,6})$/)
815
813
  #Datet.code format
816
- return Datet.new(Time.local(match[1], match[2], match[3], match[4], match[5], match[6], match[7]))
817
- elsif match = timestr.to_s.match(/^\s*(\d{4})-(\d{1,2})-(\d{1,2})(|\s+(\d{2}):(\d{2}):(\d{2})(|\.\d+)\s*)(|\s+(UTC))(|\s+(\+|\-)(\d{2})(\d{2}))$/)
814
+ return Datet.new(match[1], match[2], match[3], match[4], match[5], match[6], match[7])
815
+ elsif match = timestr_t.match(/^\s*(\d{4})-(\d{1,2})-(\d{1,2})(|\s+(\d{2}):(\d{2}):(\d{2})(|\.\d+)\s*)(|\s+(utc))(|\s+(\+|\-)(\d{2})(\d{2}))$/)
818
816
  #Database date format (with possibility of .0 in the end - microseconds? -knj.
819
817
 
820
818
  if match[11] and match[13] and match[14]
@@ -831,14 +829,19 @@ class Datet
831
829
  utc_str = nil
832
830
  end
833
831
 
834
- time = Time.local(match[1].to_i, match[2].to_i, match[3].to_i, match[5].to_i, match[6].to_i, match[7].to_i, utc_str)
835
- return Datet.new(time)
836
- elsif match = timestr.to_s.match(/^\s*(\d{2,4})-(\d{1,2})-(\d{1,2})(|\s+(\d{1,2}):(\d{1,2}):(\d{1,2})(:(\d{1,2})|)\s*)$/)
837
- time = Time.local(match[1].to_i, match[2].to_i, match[3].to_i, match[5].to_i, match[6].to_i, match[7].to_i)
838
- return Datet.new(time)
832
+ if utc_str
833
+ time = Time.local(match[1].to_i, match[2].to_i, match[3].to_i, match[5].to_i, match[6].to_i, match[7].to_i, utc_str)
834
+ return Datet.new(time)
835
+ else
836
+ return Datet.new(match[1].to_i, match[2].to_i, match[3].to_i, match[5].to_i, match[6].to_i, match[7].to_i)
837
+ end
838
+ elsif match = timestr_t.match(/^\s*(\d{2,4})-(\d{1,2})-(\d{1,2})(|\s+(\d{1,2}):(\d{1,2}):(\d{1,2})(:(\d{1,2})|)\s*)$/)
839
+ return Datet.new(match[1].to_i, match[2].to_i, match[3].to_i, match[5].to_i, match[6].to_i, match[7].to_i)
840
+ elsif match = timestr_t.match(/^([A-z]+),\s*(\d+)\s+([A-z]+)\s+(\d+)\s+(\d+):(\d+):(\d+)\s*([A-z]+)$/)
841
+ return Datet.new(match[4].to_i, Datet.month_str_to_no(match[3]), match[2].to_i, match[5].to_i, match[6].to_i, match[7].to_i)
839
842
  end
840
843
 
841
- raise Knj::Errors::InvalidData.new("Wrong format: '#{timestr}', class: '#{timestr.class.name}'")
844
+ raise ArgumentError, "Wrong format: '#{timestr}', class: '#{timestr.class.name}'"
842
845
  end
843
846
 
844
847
  #Returns a hash with the month-no as key and month-name as value. It uses the method "_" to translate the months names. So GetText or another method has to be defined.
@@ -894,6 +897,19 @@ class Datet
894
897
  return ret
895
898
  end
896
899
 
900
+ #Converts a given day-name to the right day number.
901
+ #===Examples
902
+ # Datet.day_str_to_no('wed') #=> 3
903
+ def self.day_str_to_no(day_str)
904
+ day_str = day_str.to_s.strip[0, 3]
905
+
906
+ if no = @@days_lcase[day_str]
907
+ return no
908
+ end
909
+
910
+ raise ArgumentError, "Invalid day-string: '#{day_str}'."
911
+ end
912
+
897
913
  #Returns the month-number for a given string (starting with 1 for january).
898
914
  #===Examples
899
915
  # Datet.month_str_to_no("JaNuArY") #=> 1
@@ -917,10 +933,6 @@ class Datet
917
933
  return _(self.time.strftime("%B"))
918
934
  end
919
935
 
920
- def to_s
921
- return self.time.to_s
922
- end
923
-
924
936
  #This returns a code-string that can be used to recreate the Datet-object.
925
937
  #===Examples
926
938
  # code = datet.code #=> "1985061710000000000"
@@ -931,13 +943,18 @@ class Datet
931
943
 
932
944
  #Returns the unix timestamp for this object.
933
945
  #===Examples
934
- # datet.unixt #=> 487843200
935
946
  # datet.to_i #=> 487843200
936
- def unixt
947
+ def to_i
937
948
  return self.time.to_i
938
949
  end
939
950
 
940
- alias :to_i :unixt
951
+ def to_f
952
+ return self.time_to_f
953
+ end
954
+
955
+ def to_s
956
+ return self.time.to_s
957
+ end
941
958
 
942
959
  #Returns the HTTP-date that can be used in headers and such.
943
960
  #===Examples
data/spec/datet_spec.rb CHANGED
@@ -35,6 +35,7 @@ describe "Datet" do
35
35
 
36
36
  #From "knjrbfw_spec.rb".
37
37
  it "should be able to parse various date formats." do
38
+ date = Datet.in("Wed, 13 Jul 2011 16:08:51 GMT")
38
39
  date = Datet.in("2011-07-09 00:00:00 UTC")
39
40
  date = Datet.in("1985-06-17 01:00:00")
40
41
  date = Datet.in("1985-06-17")
@@ -100,4 +101,28 @@ describe "Datet" do
100
101
  datet = Datet.new(1985, 6, 17, 28, 68, 68, 68)
101
102
  raise "Expected dbstr to be '1985-06-18 05:09:09' but it wasnt: '#{datet.dbstr}'." if datet.dbstr != "1985-06-18 05:09:09"
102
103
  end
104
+
105
+ it "should be able to convert day-strings into numbers" do
106
+ tests = {
107
+ "mon" => 1,
108
+ "tue" => 2,
109
+ "wed" => 3,
110
+ "thu" => 4,
111
+ "fri" => 5,
112
+ "sat" => 6,
113
+ "sun" => 0,
114
+ "monda" => 1,
115
+ "tuesday" => 2,
116
+ "wednes" => 3,
117
+ "thursd" => 4,
118
+ "frida" => 5,
119
+ "satur" => 6,
120
+ "sunday" => 0
121
+ }
122
+
123
+ tests.each do |test_str, right_res|
124
+ res = Datet.day_str_to_no(test_str)
125
+ raise "Expected result: '#{right_res}' but got: '#{res}'." if res != right_res
126
+ end
127
+ end
103
128
  end
metadata CHANGED
@@ -2,7 +2,7 @@
2
2
  name: datet
3
3
  version: !ruby/object:Gem::Version
4
4
  prerelease:
5
- version: 0.0.0
5
+ version: 0.0.1
6
6
  platform: ruby
7
7
  authors:
8
8
  - Kasper Johansen
@@ -10,7 +10,7 @@ autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
12
 
13
- date: 2012-07-12 00:00:00 +02:00
13
+ date: 2012-07-13 00:00:00 +02:00
14
14
  default_executable:
15
15
  dependencies:
16
16
  - !ruby/object:Gem::Dependency
@@ -86,6 +86,7 @@ files:
86
86
  - README.rdoc
87
87
  - Rakefile
88
88
  - VERSION
89
+ - datet.gemspec
89
90
  - lib/datet.rb
90
91
  - spec/datet_spec.rb
91
92
  - spec/spec_helper.rb
@@ -103,7 +104,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
103
104
  requirements:
104
105
  - - ">="
105
106
  - !ruby/object:Gem::Version
106
- hash: 2695017267078872396
107
+ hash: 1674875228102623763
107
108
  segments:
108
109
  - 0
109
110
  version: "0"