Skip to content

Commit

Permalink
added string formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
progrium committed Dec 4, 2009
1 parent 62e52c6 commit 04e48bc
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 3 deletions.
7 changes: 7 additions & 0 deletions README
Expand Up @@ -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.
20 changes: 17 additions & 3 deletions timeapi.rb
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
3 changes: 3 additions & 0 deletions views/index.erb
Expand Up @@ -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>
Expand Down

0 comments on commit 04e48bc

Please sign in to comment.