Permalink
Browse files

added string formatting

  • Loading branch information...
1 parent 62e52c6 commit 04e48bc33bc264ec2c0d0a01d22f66c9f721e721 @progrium committed Dec 4, 2009
Showing with 27 additions and 3 deletions.
  1. +7 −0 README
  2. +17 −3 timeapi.rb
  3. +3 −0 views/index.erb
View
@@ -23,3 +23,10 @@ API (just one method) is available on URL http://chronic.heroku.com/pdt/{your_st
For example:
http://chronic.heroku.com/pdt/in+two+hours
+
+You can also add a format string based on Ruby Time strftime, replacing % with \
+For example:
+
+ http://chronic.heroku.com/pdt/in+two+hours?\a \b \d \I:\M:\S \Z \Y
+
+See http://corelib.rubyonrails.org/classes/Time.html#M000264 for more info.
View
@@ -21,7 +21,13 @@ class App < Sinatra::Default
set :sessions, false
set :run, false
set :environment, ENV['RACK_ENV']
-
+
+ def format
+ request.query_string \
+ .gsub('%20', ' ') \
+ .gsub('\\', '%') \
+ end
+
get '/' do
erb :index
end
@@ -34,7 +40,7 @@ class App < Sinatra::Default
zone = params[:zone].upcase
offset = TimeAPI::const_get(zone)
- Time.new.utc.to_datetime.new_offset(Rational(offset,24)).to_s
+ Time.new.utc.to_datetime.new_offset(Rational(offset,24)).to_s(format)
end
get '/:zone/:time' do
@@ -50,7 +56,7 @@ class App < Sinatra::Default
Chronic.parse(
time, :now=>Time.new.utc.set_timezone(offset)
- ).to_datetime.to_s
+ ).to_datetime.to_s(format)
end
end
@@ -76,4 +82,12 @@ class DateTime
def to_datetime
self
end
+
+ def to_s(format='')
+ unless format.empty?
+ strftime(format)
+ else
+ strftime
+ end
+ end
end
View
@@ -16,6 +16,9 @@
<li><a href="/pdt/7+hours+before+tomorrow+at+noon">/pdt/7+hours+before+tomorrow+at+noon</a> &rarr; I think you get the point.</li>
</ul>
<p>See <a href="http://chronic.rubyforge.org/">Chronic documentation</a> for more examples.</p>
+ <p>You can also add a formatting string with the query parameter based on modified Ruby <a href="http://corelib.rubyonrails.org/classes/Time.html#M000264">Time#strftime</a>:<br />
+ <a href="/utc/now?\a \b \d \I:\M:\S \Z \Y" style="margin-left: 40px;">/utc/now?\a \b \d \I:\M:\S \Z \Y</a>
+ </p>
<p>
by <a href="http://zh.soup.io/">Stoyan Zhekov</a>, <a href="http://chipt.com/">Matt Pennig</a>, and <a href="http://progrium.com">Jeff Lindsay</a>. <a href="http://github.com/progrium/timeapi/tree/master">Source code</a> is on GitHub. Hosted on <a href="http://heroku.com/">Heroku</a>.
</p>

0 comments on commit 04e48bc

Please sign in to comment.