Permalink
Browse files

Added julian timestamp utility methods

  • Loading branch information...
1 parent a384283 commit 19a62191235b86f2cf33bbf11d15a4c761802152 @copiousfreetime committed Feb 28, 2009
Showing with 162 additions and 0 deletions.
  1. +65 −0 TODO.taskpaper
  2. +84 −0 lib/snipe/julian.rb
  3. +13 −0 spec/julian_spec.rb
View
@@ -43,6 +43,71 @@ Other:
- scrape @done(2009-02-23 23:13:51)
- publish @done(2009-02-23 23:13:52)
+
+
+Tables:
+ Dates:
+ - key 'date/#{jd}'
+ - type 'Date'
+ - jd #{jd}
+ - ord "yyyy-ddd"
+ - iso "yyyy-mm-dd"
+
+ Tweets:
+ - key = 'tweet/#{status_id}' where id is item minux xml
+ - type 'Tweet'
+ - status_id
+ - author (username)
+ - text ( full text )
+ - url
+ - destinationurl
+ - source
+ - at
+ - author_snapshot ( 'author/#{username}/#{jd}' )
+ - post_date ( julan day of at )
+ - post_at ( julian time of at )
+ - split_at ( julian time )
+ - scrape_at ( julian time )
+ - store_at ( julian time )
+ - publish_at ( julian time )
+ - tags (csv)
+ - mentions (csv)
+ - urls (csv)
+
+ Authors:
+ - id = 'author/#{username}'
+ - type 'Author'
+ - username
+ - name
+ - location
+ - website
+ - bio
+ - picture_url
+ - follower_count
+ - following_count
+ - update_count
+ - first_snap_date( julian day)
+ - snap_date ( julian day )
+ - snap_at ( julian time )
+
+ AuthorSnapshots:
+ - id = 'author/#{username}/snapshot/#{jd}
+ - type 'AuthorSnapshot'
+ - snap_date ( jd )
+ - snap_at ( julian time )
+ - everything else from Authors
+
+ Follows:
+ - id = 'author/#{username}/follows/#{username}'
+ -
+ - type 'Follow'
+ - first_snap_date
+ - last_snap_date
+ - is_current
+
+
+
+
Gnip v2.1
- switch to that notification stream
- start publishing
View
@@ -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
View
@@ -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
+

0 comments on commit 19a6219

Please sign in to comment.