Skip to content

Commit

Permalink
date <=> base60 and tests
Browse files Browse the repository at this point in the history
  • Loading branch information
veganstraightedge committed Jun 11, 2010
1 parent 4268336 commit beba483
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 14 deletions.
30 changes: 18 additions & 12 deletions lib/newbase60.rb
@@ -1,12 +1,16 @@
class Newbase60
require "time"
require "date"
VERSION = '0.0.1'
VERSION = '1.0.0'

def initialize(str)
@base_60 = str
end


def to_s
@base_60
end

# converts NewBase60 into base10 integer
def to_i
num = 0
Expand Down Expand Up @@ -42,10 +46,14 @@ def to_i
num
end

# converts NewBase60 characters into a Time object
def to_time
# days since epoch * seconds * minutes * hours + timezone
Time.at(Newbase60.new(@base_60).to_i * 60 * 60 * 24 + Time.now.gmtoff.abs)
# converts NewBase60 characters into a Date object
def to_date
# HACK this is smelly

# days since epoch * seconds * minutes * hours + timezone
time = Time.at(Newbase60.new(@base_60).to_i * 60 * 60 * 24 + Time.now.gmtoff.abs)
# parse the Time object into a Date object
Date.parse(time.strftime("%Y/%m/%d"))
# 14:48 on 2010-06-04
end
end
Expand Down Expand Up @@ -87,11 +95,9 @@ def to_sxgf(padding)
end
end

# converts Time object into NewBase60
class Time
# converts Date object into NewBase60
class Date
def to_sxg
the_when = self + Time.now.gmtoff.abs
epoch_days = the_when.to_i / 60 / 60 / 24
epoch_days.to_sxg
(self - Date.parse("1970/01/01")).to_i.to_sxg
end
end
8 changes: 6 additions & 2 deletions test/test_newbase60.rb
Expand Up @@ -8,10 +8,14 @@ def test_base60_to_base10
assert_not_equal Newbase60.new("464").to_i, 12345
end

def test_base60_to_time
def test_base60_to_date
assert_equal Newbase60.new("464").to_date, Date.parse("2010/06/04")
assert_not_equal Newbase60.new("464").to_date, Date.parse("2010/06/05")
end

def test_time_to_base60
def test_date_to_base60
assert_equal Date.parse("2010/06/04").to_sxg, Newbase60.new("464").to_s
assert_not_equal Date.parse("2010/06/05").to_sxg, Newbase60.new("464")
end

def test_base10_to_base60
Expand Down

0 comments on commit beba483

Please sign in to comment.