phpf 1.0.0 → 1.1.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.
@@ -0,0 +1,25 @@
1
+ # phpf
2
+
3
+ A Ruby port of [PHP's date function](http://www.php.net/date)
4
+
5
+ ## Installation
6
+
7
+ ```
8
+ $ gem install phpf
9
+ ```
10
+
11
+ ## Usage
12
+
13
+ Either use the `Time#phpf` method or the `Kernel#date` method, for example:
14
+
15
+ ```ruby
16
+ require 'phpf'
17
+
18
+ time = Time.parse('1/1/2008 12:34:56')
19
+
20
+ time.phpf('jS F Y H:i') # => "1st January 2008 12:34"
21
+
22
+ time.to_i # => 1199190896
23
+
24
+ date('jS F Y H:i', 1199190896) # => "1st January 2008 12:34"
25
+ ```
@@ -1,40 +1,7 @@
1
- require 'rake'
2
1
  require 'rake/testtask'
3
- require 'rake/gempackagetask'
4
2
 
3
+ task :default => :spec
5
4
 
6
- GEMSPEC = Gem::Specification.new do |s|
7
- s.name = 'phpf'
8
- s.version = '1.0.0'
9
- s.platform = Gem::Platform::RUBY
10
- s.has_rdoc = false
11
- s.summary = "Port of PHP's date function."
12
- s.description = s.summary
13
- s.author = 'Tim Fletcher'
14
- s.email = 'twoggle@gmail.com'
15
- s.homepage = 'http://phpf.rubyforge.org/'
16
- s.files = Dir.glob('lib/**/*') + %w( COPYING.txt Rakefile.rb README.html )
17
- s.require_paths = ['lib']
5
+ Rake::TestTask.new(:spec) do |t|
6
+ t.test_files = FileList['spec/*_spec.rb']
18
7
  end
19
-
20
- Rake::GemPackageTask.new(GEMSPEC) { }
21
-
22
- Rake::PackageTask.new(GEMSPEC.name, GEMSPEC.version) do |p|
23
- p.need_tar_gz = true
24
- p.package_files.include GEMSPEC.files
25
- end
26
-
27
- desc 'Run all the tests'
28
- task :test do
29
- if ARGV.length == 2 && ARGV.last =~ /SYM=(.)/
30
- TEST_SYMBOL = $1.to_sym
31
- end
32
- require 'test/unit'
33
- Test::Unit.run = false
34
- $:.unshift 'lib'
35
- require 'phpf/tests'
36
- Test::Unit::AutoRunner.run
37
- $SKIPPED_PHP_CHECKS.each do |format, version|
38
- puts "- skipped check for %s (requires PHP %s)" % [format, version]
39
- end
40
- end
@@ -1,42 +1,17 @@
1
1
  require 'phpf/tokenizing'
2
2
  require 'phpf/evaluation'
3
3
  require 'phpf/translations'
4
+ require 'phpf/constants'
4
5
  require 'time'
5
6
 
6
- module PHPF
7
- def self.format(string)
8
- Symbol === string ? StandardFormats.const_get(string.to_s.upcase) : string
9
- end
10
-
11
- # cf. http://uk3.php.net/manual/en/ref.datetime.php#datetime.constants
12
- #
13
- # DATE_RFC822/DATE_COOKIE/DATE_RFC1123/DATE_RSS, DATE_ISO8601,
14
- # and DATE_RFC2822 all have equivalent Ruby methods.
15
- #
16
- module StandardFormats
17
- RFC850, RFC1036 = "l, d-M-y H:i:s T".freeze
18
-
19
- RFC3339, W3C, ATOM = "Y-m-d\TH:i:sP".freeze
20
- end
21
- end
22
-
23
7
  class Time
24
- def self.epoch
25
- @@epoch ||= parse('1st Jan 1970 00:00:00 UTC').freeze
26
- end
27
- def phpf(string)
28
- PHPF.evaluate(PHPF.tokenize(PHPF.format(string)), self).join
29
- end
30
- end
31
-
32
- class Integer
33
- def to_time
34
- Time.epoch + self
8
+ def phpf(format_string)
9
+ PHPF.evaluate(PHPF.tokenize(format_string), self).join
35
10
  end
36
11
  end
37
12
 
38
13
  module Kernel
39
14
  def date(format_string, time = Time.now)
40
- time.to_i.to_time.phpf(format_string)
15
+ Time.at(time.to_i).phpf(format_string)
41
16
  end
42
- end
17
+ end
@@ -0,0 +1,12 @@
1
+ module PHPF
2
+ # cf. http://php.net/manual/en/class.datetime.php#datetime.constants.types
3
+ #
4
+ # DATE_RFC822/DATE_COOKIE/DATE_RFC1123/DATE_RSS, DATE_ISO8601,
5
+ # and DATE_RFC2822 all have equivalent Ruby methods.
6
+
7
+ RFC850 = "l, d-M-y H:i:s T".freeze
8
+
9
+ RFC1036 = "D, d M y H:i:s O".freeze
10
+
11
+ RFC3339, W3C, ATOM = "Y-m-d\TH:i:sP".freeze
12
+ end
@@ -6,15 +6,11 @@ module PHPF
6
6
  end
7
7
 
8
8
  private
9
-
9
+
10
10
  def self.translate(symbol, &rule)
11
11
  translations[@last_symbol = symbol] = rule
12
12
  end
13
13
 
14
- def self.only(php_version)
15
- php_version_requirements[@last_symbol] = php_version
16
- end
17
-
18
14
  def self.test(time_string, expected_output)
19
15
  test_cases[@last_symbol] << [time_string, expected_output]
20
16
  end
@@ -31,25 +27,13 @@ module PHPF
31
27
  @test_cases ||= Hash.new { |h, k| h[k] = [] }
32
28
  end
33
29
 
34
- def self.php_version_requirements
35
- @php_version_requirements ||= {}
36
- end
37
-
38
- def self.epoch
39
- '1st Jan 1970 00:00:00 UTC'
40
- end
41
-
42
30
  def self.ordinal(n)
43
31
  return 'th' if (11..13).include?(n % 100)
44
32
  case n % 10
45
- when 1: 'st'
46
- when 2: 'nd'
47
- when 3: 'rd'
48
- else 'th'
33
+ when 1 then 'st'
34
+ when 2 then 'nd'
35
+ when 3 then 'rd'
36
+ else 'th'
49
37
  end
50
38
  end
51
-
52
- def self.date(time)
53
- Date.new(time.year, time.month, time.mday)
54
- end
55
- end
39
+ end
@@ -1,6 +1,6 @@
1
1
  module PHPF
2
2
  FormatCharacters = 'dDjlNSwzWFmMntLoYyaABgGhHisueIOPTZcrU'
3
-
3
+
4
4
  def self.tokenize(format_string)
5
5
  tokens = []
6
6
  until format_string.empty?
@@ -20,13 +20,13 @@ module PHPF
20
20
  end
21
21
  return tokens
22
22
  end
23
-
23
+
24
24
  def self.untokenize(tokens)
25
25
  tokens.map { |token| untok(token, token.to_s) }.join
26
26
  end
27
27
 
28
28
  private
29
-
29
+
30
30
  def self.untok(token, string)
31
31
  if Symbol === token
32
32
  return string
@@ -34,4 +34,4 @@ module PHPF
34
34
  FormatCharacters.include?(string) ? '\\' + string : string
35
35
  end
36
36
  end
37
- end
37
+ end
@@ -1,15 +1,12 @@
1
1
  require 'date'
2
2
 
3
- # cf. http://php.net/date
4
- #
5
- module PHPF
6
-
3
+ module PHPF
7
4
  # d: Day of the month, 2 digits with leading zeros (e.g. 01 to 31)
8
5
  #
9
6
  translate(:d) { '%02d' % mday }
10
7
  test '1st', '01'
11
8
  test '23rd', '23'
12
-
9
+
13
10
  # D: A textual representation of a day, three letters (e.g. Mon through Sun)
14
11
  #
15
12
  translate(:D) { Time::RFC2822_DAY_NAME[wday] }
@@ -20,7 +17,7 @@ module PHPF
20
17
  translate(:j) { mday }
21
18
  test '1st', '1'
22
19
  test '23rd', '23'
23
-
20
+
24
21
  # l: A full textual representation of the day of the week
25
22
  # e.g. Sunday through Saturday
26
23
  #
@@ -30,11 +27,10 @@ module PHPF
30
27
  # N: ISO-8601 numeric representation of the day of the week
31
28
  # e.g. 1 [for Monday] through 7 [for Sunday]
32
29
  #
33
- translate(:N) { |time| date(time).cwday }
34
- only '5.1.0'
30
+ translate(:N) { |time| time.to_date.cwday }
35
31
  test '9th Dec 2007', '7'
36
32
  test '10th Dec 2007', '1'
37
-
33
+
38
34
  # S: English ordinal suffix for the day of the month
39
35
  # e.g. st, nd, rd or th
40
36
  #
@@ -43,14 +39,14 @@ module PHPF
43
39
  test '2nd', 'nd'
44
40
  test '3rd', 'rd'
45
41
  test '4th', 'th'
46
-
42
+
47
43
  # w: Numeric representation of the day of the week
48
44
  # e.g. 0 [for Sunday] through 6 [for Saturday]
49
45
  #
50
46
  translate(:w) { wday }
51
47
  test '9th Dec 2007', '0'
52
48
  test '10th Dec 2007', '1'
53
-
49
+
54
50
  # z: The day of the year (starting from 0)
55
51
  # e.g. 0 through 365
56
52
  #
@@ -58,11 +54,11 @@ module PHPF
58
54
  test '1st Jan 2007', '0'
59
55
  test '31st Dec 2007', '364'
60
56
  test '31st Dec 2008', '365'
61
-
57
+
62
58
  # W: ISO-8601 week number of year, weeks starting on Monday
63
59
  # e.g. 42 [for the 42nd week in the year]
64
60
  #
65
- translate(:W) { |time| '%02d' % date(time).cweek }
61
+ translate(:W) { |time| '%02d' % time.to_date.cweek }
66
62
  test '24th Dec 2007 12:00', '52'
67
63
  test '31st Dec 2007 12:00', '01'
68
64
  test ' 1st Jan 2008 12:00', '01'
@@ -73,47 +69,46 @@ module PHPF
73
69
  translate(:F) { strftime('%B') }
74
70
  test 'Jan', 'January'
75
71
  test 'Dec', 'December'
76
-
72
+
77
73
  # m: Numeric representation of a month, with leading zeros
78
74
  # e.g. 01 through 12
79
75
  #
80
76
  translate(:m) { '%02d' % month }
81
77
  test 'Jan', '01'
82
78
  test 'Dec', '12'
83
-
79
+
84
80
  # M: A short textual representation of a month
85
81
  # e.g. Jan through Dec
86
82
  #
87
83
  translate(:M) { Time::RFC2822_MONTH_NAME[month - 1] }
88
84
  test 'Jan', 'Jan'
89
85
  test 'Dec', 'Dec'
90
-
86
+
91
87
  # n: Numeric representation of a month, without leading zeros
92
88
  # e.g. 1 through 12
93
89
  #
94
90
  translate(:n) { month }
95
91
  test 'Jan', '1'
96
92
  test 'Dec', '12'
97
-
93
+
98
94
  # t: Number of days in the given month (28 through 31)
99
95
  #
100
- translate(:t) { |time| ((date(time) >> 1) - time.mday).day }
96
+ translate(:t) { |time| ((time.to_date >> 1) - time.mday).day }
101
97
  test 'Jan 2007', '31'
102
98
  test 'Feb 2007', '28'
103
99
  test 'Feb 2008', '29'
104
-
100
+
105
101
  # L: 1 if it is a leap year, 0 otherwise
106
102
  #
107
103
  translate(:L) { Date.leap?(year) ? 1 : 0 }
108
104
  test 'Jan 2007', '0'
109
105
  test 'Jan 2008', '1'
110
-
106
+
111
107
  # o: ISO-8601 year number. This has the same value as Y,
112
108
  # except that if the ISO week number (W) belongs to the previous
113
109
  # or next year, that year is used instead.
114
110
  #
115
- translate(:o) { |time| date(time).cwyear }
116
- only '5.1.0'
111
+ translate(:o) { |time| time.to_date.cwyear }
117
112
  test '24th Dec 2007 12:00', '2007'
118
113
  test '31st Dec 2007 12:00', '2008'
119
114
  test ' 1st Jan 2008 12:00', '2008'
@@ -124,7 +119,7 @@ module PHPF
124
119
  #
125
120
  translate(:Y) { year }
126
121
  test 'Jan 1999', '1999'
127
-
122
+
128
123
  # y: A two digit representation of a year
129
124
  # e.g. 99 or 03
130
125
  #
@@ -136,7 +131,7 @@ module PHPF
136
131
  translate(:a) { strftime('%p').downcase }
137
132
  test '00:42', 'am'
138
133
  test '12:42', 'pm'
139
-
134
+
140
135
  # A: Uppercase Ante meridiem and Post meridiem (AM or PM)
141
136
  #
142
137
  translate(:A) { strftime('%p') }
@@ -148,7 +143,7 @@ module PHPF
148
143
  #
149
144
  translate(:B) { '%03d' % (((utc.to_i + 3600) % 86400) / 86.4) }
150
145
  test '1st Jan 2008 00:00:00 UTC', '041'
151
-
146
+
152
147
  # g: 12-hour format of an hour without leading zeros (1 through 12)
153
148
  #
154
149
  translate(:g) { strftime('%I').to_i }
@@ -164,7 +159,7 @@ module PHPF
164
159
  test '06:42', '6'
165
160
  test '12:42', '12'
166
161
  test '18:42', '18'
167
-
162
+
168
163
  # h: 12-hour format of an hour with leading zeros (01 through 12)
169
164
  #
170
165
  translate(:h) { strftime('%I') }
@@ -203,11 +198,10 @@ module PHPF
203
198
  #
204
199
  # translate(:O) { '+%03d00' % (gmt_offset / 3600) }
205
200
  # TODO
206
-
201
+
207
202
  # P: Difference to Greenwich time (GMT) with colon between hours and minutes
208
203
  # e.g. +02:00
209
204
  #
210
- # only '5.1.3'
211
205
  # TODO
212
206
 
213
207
  # T: Timezone abbreviation
@@ -220,12 +214,11 @@ module PHPF
220
214
  # e.g. -43200 through 50400
221
215
  #
222
216
  # TODO
223
-
217
+
224
218
  # c: ISO 8601 date
225
219
  # e.g. 2004-02-12T15:19:21+00:00
226
220
  #
227
221
  translate(:c) { iso8601.sub(/Z$/, '+00:00') }
228
- only '5'
229
222
  test '1st Jan 2008 00:00:00 UTC', '2008-01-01T00:00:00+00:00'
230
223
  test '1st Jan 2008 12:34:56 PST', '2008-01-01T20:34:56+00:00'
231
224
 
@@ -233,12 +226,11 @@ module PHPF
233
226
  # e.g. Thu, 21 Dec 2000 16:01:07 +0200
234
227
  #
235
228
  translate(:r) { rfc2822 }
236
- test '1st Jan 2008 00:00:00 GMT', 'Tue, 01 Jan 2008 00:00:00 +0000'
229
+ test '1st Jan 2008 00:00:00 GMT', 'Tue, 01 Jan 2008 00:00:00 +0000'
237
230
 
238
231
  # U: Seconds since the Unix Epoch
239
232
  #
240
233
  translate(:U) { to_i }
241
- test epoch, '0'
234
+ test '1st Jan 1970 00:00:00 UTC', '0'
242
235
  test '1st Jan 2008 00:00:00 UTC', '1199145600'
243
-
244
- end
236
+ end
@@ -0,0 +1,12 @@
1
+ Gem::Specification.new do |s|
2
+ s.name = 'phpf'
3
+ s.version = '1.1.0'
4
+ s.platform = Gem::Platform::RUBY
5
+ s.authors = ['Tim Fletcher']
6
+ s.email = ['mail@tfletcher.com']
7
+ s.homepage = 'http://github.com/tim/ruby-phpf'
8
+ s.description = 'A Ruby port of the PHP date function'
9
+ s.summary = s.description
10
+ s.files = Dir.glob('{lib,spec}/**/*') + %w(README.md Rakefile.rb phpf.gemspec)
11
+ s.require_path = 'lib'
12
+ end
@@ -0,0 +1,76 @@
1
+ require 'minitest/autorun'
2
+
3
+ $:.unshift 'lib'
4
+
5
+ require 'phpf'
6
+
7
+ class PhpShell
8
+ def initialize
9
+ @pipe = IO.popen('php -a', 'w+').tap do |pipe|
10
+ pipe.gets
11
+ pipe.gets
12
+ pipe.puts('date_default_timezone_set("Europe/London");')
13
+ end
14
+ end
15
+
16
+ def echo(expression)
17
+ @pipe.puts("echo #{expression};")
18
+ @pipe.gets.chomp
19
+ end
20
+
21
+ def close
22
+ @pipe.close
23
+ end
24
+ end
25
+
26
+ module PHPF
27
+ def self.each_test_case
28
+ test_cases.each do |format_character, test_cases|
29
+ test_cases.each { |array| yield format_character, *array }
30
+ end
31
+ end
32
+ end
33
+
34
+ describe 'Time' do
35
+ describe 'phpf method' do
36
+ it 'formats the time like the examples in the php documentation' do
37
+ # cf. http://php.net/date (Example #4)
38
+
39
+ time = Time.parse('March 10th, 2001, 5:16:18 pm')
40
+
41
+ time.phpf('F j, Y, g:i a').must_equal('March 10, 2001, 5:16 pm')
42
+ time.phpf('m.d.y').must_equal('03.10.01')
43
+ time.phpf('j, n, Y').must_equal('10, 3, 2001')
44
+ time.phpf('Ymd').must_equal('20010310')
45
+ time.phpf('h-i-s, j-m-y, it is w Day').must_equal('05-16-18, 10-03-01, 1631 1618 6 Satpm01')
46
+ time.phpf('\i\t \i\s \t\h\e jS \d\a\y.').must_equal('it is the 10th day.')
47
+ time.phpf('H:m:s \m \i\s \m\o\n\t\h').must_equal('17:03:18 m is month')
48
+ time.phpf('H:i:s').must_equal('17:16:18')
49
+ end
50
+
51
+ puts 'Comparing against ' + `php -version`[/(PHP \d+\.\d+\.\d+)/, 1]
52
+
53
+ $php_shell = PhpShell.new
54
+
55
+ PHPF.each_test_case do |format_character, time_string, expected_output|
56
+ describe "the #{format_character} format character" do
57
+ it 'formats the time like php' do
58
+ time = time_string =~ /\A\d+\z/ ? Time.at(time_string.to_i) : Time.parse(time_string)
59
+
60
+ time.phpf(format_character.to_s).must_equal(expected_output)
61
+
62
+ output = $php_shell.echo('date(%s, %d)' % [format_character.to_s.inspect, time.to_i])
63
+ output.must_equal(expected_output)
64
+ end
65
+ end
66
+ end
67
+ end
68
+ end
69
+
70
+ describe 'Kernel' do
71
+ describe 'date method' do
72
+ it 'formats the given integer timestamp according to the given format string' do
73
+ date('jS F Y', 1350927682).must_equal('22nd October 2012')
74
+ end
75
+ end
76
+ end
metadata CHANGED
@@ -1,61 +1,55 @@
1
- --- !ruby/object:Gem::Specification
1
+ --- !ruby/object:Gem::Specification
2
2
  name: phpf
3
- version: !ruby/object:Gem::Version
4
- version: 1.0.0
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.1.0
5
+ prerelease:
5
6
  platform: ruby
6
- authors:
7
+ authors:
7
8
  - Tim Fletcher
8
9
  autorequire:
9
10
  bindir: bin
10
11
  cert_chain: []
11
-
12
- date: 2008-02-14 00:00:00 +00:00
13
- default_executable:
12
+ date: 2012-10-22 00:00:00.000000000 Z
14
13
  dependencies: []
15
-
16
- description: Port of PHP's date function.
17
- email: twoggle@gmail.com
14
+ description: A Ruby port of the PHP date function
15
+ email:
16
+ - mail@tfletcher.com
18
17
  executables: []
19
-
20
18
  extensions: []
21
-
22
19
  extra_rdoc_files: []
23
-
24
- files:
25
- - lib/phpf
20
+ files:
21
+ - lib/phpf/constants.rb
26
22
  - lib/phpf/evaluation.rb
27
- - lib/phpf/tests.rb
28
23
  - lib/phpf/tokenizing.rb
29
24
  - lib/phpf/translations.rb
30
25
  - lib/phpf.rb
31
- - COPYING.txt
26
+ - spec/phpf_spec.rb
27
+ - README.md
32
28
  - Rakefile.rb
33
- - README.html
34
- has_rdoc: false
35
- homepage: http://phpf.rubyforge.org/
29
+ - phpf.gemspec
30
+ homepage: http://github.com/tim/ruby-phpf
31
+ licenses: []
36
32
  post_install_message:
37
33
  rdoc_options: []
38
-
39
- require_paths:
34
+ require_paths:
40
35
  - lib
41
- required_ruby_version: !ruby/object:Gem::Requirement
42
- requirements:
43
- - - ">="
44
- - !ruby/object:Gem::Version
45
- version: "0"
46
- version:
47
- required_rubygems_version: !ruby/object:Gem::Requirement
48
- requirements:
49
- - - ">="
50
- - !ruby/object:Gem::Version
51
- version: "0"
52
- version:
36
+ required_ruby_version: !ruby/object:Gem::Requirement
37
+ none: false
38
+ requirements:
39
+ - - ! '>='
40
+ - !ruby/object:Gem::Version
41
+ version: '0'
42
+ required_rubygems_version: !ruby/object:Gem::Requirement
43
+ none: false
44
+ requirements:
45
+ - - ! '>='
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
53
48
  requirements: []
54
-
55
49
  rubyforge_project:
56
- rubygems_version: 1.0.1
50
+ rubygems_version: 1.8.24
57
51
  signing_key:
58
- specification_version: 2
59
- summary: Port of PHP's date function.
52
+ specification_version: 3
53
+ summary: A Ruby port of the PHP date function
60
54
  test_files: []
61
-
55
+ has_rdoc:
@@ -1,22 +0,0 @@
1
- Copyright (c) 2008 Tim Fletcher <tfletcher.com>
2
-
3
- Permission is hereby granted, free of charge, to any person
4
- obtaining a copy of this software and associated documentation
5
- files (the "Software"), to deal in the Software without
6
- restriction, including without limitation the rights to use,
7
- copy, modify, merge, publish, distribute, sublicense, and/or sell
8
- copies of the Software, and to permit persons to whom the
9
- Software is furnished to do so, subject to the following
10
- conditions:
11
-
12
- The above copyright notice and this permission notice shall be
13
- included in all copies or substantial portions of the Software.
14
-
15
- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16
- EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
17
- OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18
- NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
19
- HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
20
- WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21
- FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
22
- OTHER DEALINGS IN THE SOFTWARE.
@@ -1,61 +0,0 @@
1
- <html>
2
- <head>
3
- <title>phpf</title>
4
- <style type="text/css" media="screen">
5
- html {
6
- height: 100%;
7
- margin-bottom: 1px;
8
- }
9
- body {
10
- margin: 0;
11
- padding: 30px;
12
- }
13
- h1, h2 {
14
- font-family: Garamond, Rockwell, serif;
15
- }
16
- h2 {
17
- margin-top: 1.5em;
18
- }
19
- code {
20
- font-family: "Panic Sans", "Bitstream Vera Sans Mono", Consolas, Monaco, monospace;
21
- font-size: 11px;
22
- }
23
- </style>
24
- </head>
25
- <body>
26
-
27
-
28
- <h1>phpf <small>1.0.0</small></h1>
29
-
30
- <p>A <a href="http://www.ruby-lang.org/">Ruby</a> port of <a href="http://www.php.net/date">PHP&rsquo;s date function</a>.</p>
31
-
32
-
33
- <h2>HOWTO get it</h2>
34
-
35
- <p>Use <a href="http://rubygems.org/">RubyGems</a> (<tt>gem install phpf</tt>),
36
- or <a href="http://rubyforge.org/frs/?group_id=5524">download from RubyForge</a>.</p>
37
-
38
-
39
- <h2>HOWTO use it</h2>
40
-
41
- <p>Either use Time#phpf, or Kernel#date, e.g.</p>
42
-
43
- <pre><code>require 'phpf'
44
-
45
- time = Time.parse('1/1/2008 12:34:56')
46
-
47
- time.phpf('jS F Y H:i') # => "1st January 2008 12:34"
48
-
49
- time.to_i # => 1199190896
50
-
51
- date('jS F Y H:i', 1199190896) # => "1st January 2008 12:34"
52
- </code></pre>
53
-
54
-
55
- <h2>See Also</h2>
56
-
57
- <p><a href="http://simonwillison.net/2003/Oct/7/dateInPython/">PHP&rsquo;s date() function in Python</a></p>
58
-
59
-
60
- </body>
61
- </html>
@@ -1,93 +0,0 @@
1
- require 'test/unit'
2
- require 'phpf'
3
-
4
- $SKIPPED_PHP_CHECKS = {}
5
-
6
- module PHPF
7
- class Tests < Test::Unit::TestCase
8
- class VersionString
9
- def initialize(string)
10
- @string = string.to_s
11
- end
12
- def to_s
13
- @string
14
- end
15
- def <=>(v)
16
- @string.split('.') <=> v.to_s.split('.')
17
- end
18
- include Comparable
19
- end
20
-
21
- unless defined?(PHP)
22
- PHP = 'php'
23
- end
24
-
25
- PHP_VERSION = begin
26
- (output = `#{PHP} -version`).empty? ? nil : VersionString.new(output[/(\d\.\d\.\d)/, 1])
27
- end
28
-
29
- def assert_expected_php_output(string, tokens, time)
30
- php_code = 'echo date(%p, %d);' % [ PHPF.untokenize(tokens), time.to_i ]
31
- php_output = `#{PHP} -r #{php_code.inspect}`
32
-
33
- assert_equal string, php_output, "unexpected PHP output: from %p" % php_code
34
- end
35
-
36
- def required_version(tokens)
37
- tokens.inject(nil) do |v, token|
38
- next v unless token.is_a?(Symbol)
39
- next v unless PHPF.php_version_requirements.has_key?(token)
40
- version = VersionString.new(PHPF.php_version_requirements[token])
41
- v.nil?? version : (v > version ? v : version)
42
- end
43
- end
44
-
45
- def assert_evaluates_to(string, format, time_string)
46
- tokens = case format
47
- when String then
48
- tokens = PHPF.tokenize(format)
49
- assert_equal format, PHPF.untokenize(tokens)
50
- tokens
51
- when Symbol then [format]
52
- when Array then format
53
- end
54
-
55
- time = Time.parse(time_string)
56
-
57
- unless PHP_VERSION.nil?
58
- if (required_version = required_version(tokens)).nil?
59
- assert_expected_php_output string, tokens, time
60
- elsif PHP_VERSION >= required_version
61
- assert_expected_php_output string, tokens, time
62
- else
63
- $SKIPPED_PHP_CHECKS[format] = required_version
64
- end
65
- end
66
-
67
- assert_equal string, PHPF.evaluate(tokens, time).to_s
68
- end
69
-
70
- PHPF.test_cases.each do |symbol, test_cases|
71
- if !defined?(TEST_SYMBOL) || TEST_SYMBOL == symbol
72
- define_method("test_evaluate_#{symbol}") do
73
- test_cases.each do |(time_string, expected_output)|
74
- assert_evaluates_to expected_output, symbol.to_s, time_string
75
- end
76
- end
77
- end
78
- end
79
-
80
- def test_php_examples # cf. http://php.net/date (Example #4)
81
- time = Time.parse('March 10th, 2001, 5:16:18 pm')
82
-
83
- assert_equal 'March 10, 2001, 5:16 pm', time.phpf('F j, Y, g:i a')
84
- assert_equal '03.10.01', time.phpf('m.d.y')
85
- assert_equal '10, 3, 2001', time.phpf('j, n, Y')
86
- assert_equal '20010310', time.phpf('Ymd')
87
- assert_equal '05-16-18, 10-03-01, 1631 1618 6 Satpm01', time.phpf('h-i-s, j-m-y, it is w Day')
88
- assert_equal 'it is the 10th day.', time.phpf('\i\t \i\s \t\h\e jS \d\a\y.')
89
- assert_equal '17:03:18 m is month', time.phpf('H:m:s \m \i\s \m\o\n\t\h')
90
- assert_equal '17:16:18', time.phpf('H:i:s')
91
- end
92
- end
93
- end