Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added julian timestamp utility methods
- Loading branch information
1 parent
a384283
commit 19a6219
Showing
3 changed files
with
162 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
# | ||
# Julian Time helpers | ||
# | ||
# | ||
# The date conversions are currently easily enough done with the standard ruby | ||
# libraries. Snipe uses Modified Julian Day for day values and fractional day | ||
# in the sense of Julian Day | ||
# | ||
require 'date' | ||
module Snipe | ||
module Julian | ||
module Date | ||
module ClassMethods | ||
# convert a modified julian day to a civil day | ||
def mjd_to_civil( mjd ) | ||
jd = ::Date.mjd_to_jd( mjd ) | ||
d = ::Date.jd_to_civil( jd ) | ||
end | ||
end | ||
::Date.extend( ClassMethods ) | ||
end | ||
|
||
module Time | ||
|
||
SECONDS_PER_DAY = 86400 | ||
|
||
module ClassMethods | ||
|
||
# convert from a modified julian day to a Time | ||
def from_mjd( mjd ) | ||
d = ::Date.mjd_to_civl( mjd ) | ||
::Time.gm( d.year, d.month, d.day ) | ||
end | ||
|
||
# convert from a modified julian day stamp to a Time | ||
def from_mjd_stamp( mjd_stamp ) | ||
mjd, fraction = mjd_stamp.split(".") | ||
d = ::Date.new( *::Date.mjd_to_civil( mjd.to_i ) ) | ||
|
||
fraction = Float( "0.#{fraction}") | ||
amt = (SECONDS_PER_DAY * fraction).round | ||
|
||
ss = amt % 60 | ||
amt = (amt - ss) / 60 | ||
mm = amt % 60 | ||
hh = (amt - mm) / 60 | ||
|
||
::Time.gm( d.year, d.month, d.day, hh, mm, ss ) | ||
end | ||
end | ||
::Time.extend( ClassMethods ) | ||
|
||
module InstanceMethods | ||
def seconds_of_day | ||
((hour * 3600) + (min * 60) + (sec) + (usec/1_000_000)).to_f | ||
end | ||
|
||
def to_date | ||
Date.new( year, month, day ) | ||
end | ||
|
||
# calculate the modified julian day fraction | ||
def mjd_fraction | ||
seconds_of_day / SECONDS_PER_DAY | ||
end | ||
|
||
# the iteger portion of a modified julian day | ||
def mjd | ||
to_date.mjd | ||
end | ||
|
||
# generate a #####.##### stringn that is the modified julian day plus | ||
# the fractional portion of the day. | ||
def mjd_stamp | ||
"%0.5f" % (mjd + mjd_fraction) | ||
end | ||
end | ||
end | ||
end | ||
end | ||
|
||
class Time | ||
include Snipe::Julian::Time::InstanceMethods | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
require File.expand_path( File.join( File.dirname( __FILE__ ),"spec_helper.rb")) | ||
require 'snipe/julian' | ||
|
||
describe Snipe::Julian do | ||
|
||
it "can round trip a time from now to modified julan time and back" do | ||
only_sec = Time.at( Time.now.utc.to_i ).utc | ||
today_mjd = only_sec.mjd_stamp | ||
utc_round = Time.from_mjd_stamp( today_mjd ) | ||
utc_round.should == only_sec | ||
end | ||
end | ||
|