Permalink
Browse files

pulling in pennig's changes from heroku repo

  • Loading branch information...
2 parents adeac0b + 26b1d0f commit 357224d12e27ddce74f90f7d74e05cd272a116da @progrium committed Aug 29, 2009
Showing with 60 additions and 10 deletions.
  1. +1 −0 config.ru
  2. +3 −0 local.rb
  3. +42 −10 timeapi.rb
  4. +14 −0 views/index.erb
View
@@ -1,2 +1,3 @@
require 'timeapi.rb'
run TimeAPI::App
+
View
@@ -0,0 +1,3 @@
+require 'timeapi'
+
+TimeAPI::App.run!
View
@@ -1,29 +1,61 @@
require 'rubygems'
require 'sinatra'
require 'chronic'
+require 'date'
module TimeAPI
-
+ PST = -8
+ MST = -7
+ CST = -6
+ EST = -5
+ PDT = -7
+ MDT = -6
+ CDT = -5
+ EDT = -4
+ UTC = 0
+ GMT = 0
+
class App < Sinatra::Default
set :sessions, false
set :run, false
set :environment, ENV['RACK_ENV']
get '/' do
- erb :form
+ erb :index
end
- # Main hub endpoint for both publisher and subscribers
- post '/' do
- throw :halt, [400, "Bad request, missing 'dt' parameter"] unless params['dt']
- Chronic.parse(params['dt']).to_s
+ get '/favicon.ico' do
+ ''
end
-
- get '/pdt/:dt' do
- throw :halt, [400, "Bad request, missing 'dt' parameter"] unless params['dt']
- Chronic.parse(params['dt']).to_s
+
+ get '/:zone' do
+ zone = params[:zone].upcase
+ offset = TimeAPI::const_get(zone)
+
+ Time.new.utc.to_datetime.new_offset(Rational(offset,24)).to_s
+ end
+
+ get '/:zone/:time' do
+ zone = params[:zone].upcase
+ offset = TimeAPI::const_get(zone)
+
+ Chronic.parse(
+ params[:time], :now=>Time.new.utc
+ ).to_datetime.new_offset(Rational(offset,24)).to_s
end
end
end
+
+class Time
+ def to_datetime
+ # Convert seconds + microseconds into a fractional number of seconds
+ seconds = sec + Rational(usec, 10**6)
+
+ # Convert a UTC offset measured in minutes to one measured in a
+ # fraction of a day.
+ offset = Rational(utc_offset, 60 * 60 * 24)
+ DateTime.new(year, month, day, hour, min, seconds, offset)
+ end
+end
View
@@ -0,0 +1,14 @@
+<html>
+<head>
+<title>timeapi</title>
+<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
+</head>
+<body>
+ <p>
+ <a href="/pdt">"/pdt" &rarr; Get current time in PDT</a>
+ </p>
+ <p>
+ <a href="/pdt">"/pdt/in+two+hours" &rarr; Get time two hours from now in PDT</a>
+ </p>
+</body>
+</html>

0 comments on commit 357224d

Please sign in to comment.