Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

too lazy

  • Loading branch information...
commit 14fa1de1224a018ad9786cb0ea246e2f64045e01 1 parent 424a95b
Stefan Rusterholz authored apeiros committed
View
BIN  .DS_Store
Binary file not shown
View
10 .gitignore
@@ -0,0 +1,10 @@
+.svn
+.DS_Store
+/coverage
+/conflicts
+/unversioned
+/trunk/docs
+/trunk/pkg
+/trunk/web
+/silverplatter-irc.xcodeproj
+/build/
View
0  CHANGELOG.rdoc 100755 → 100644
File mode changed
View
0  COPYING 100755 → 100644
File mode changed
View
0  README.rdoc 100755 → 100644
File mode changed
View
0  Rakefile 100755 → 100644
File mode changed
View
0  TODO.rdoc 100755 → 100644
File mode changed
View
24 bench/completebench.rb
@@ -0,0 +1,24 @@
+$LOAD_PATH.unshift(File.expand_path(__FILE__+'../../../lib'))
+
+#p $LOAD_PATH
+
+require 'benchmark'
+require 'chronos/datetime'
+require 'date'
+
+include Chronos
+
+N1 = 10_000
+
+Benchmark.bm(30) { |x|
+ x.report('create:Time') { N1.times { Time.mktime(2008,7,1) } }
+ x.report('create:DateTime') { N1.times { DateTime.new(2008,7,1) } }
+ x.report('create:Chronos') { N1.times { Datetime.civil(2008,7,1) } }
+
+ t = Time.mktime(2008,7,1)
+ d = DateTime.new(2008,7,1)
+ c = Datetime.civil(2008,7,1)
+ x.report('strftime:Time') { N1.times { t.strftime("%Y-%m-%d") } }
+ x.report('strftime:DateTime') { N1.times { d.strftime("%Y-%m-%d") } }
+ x.report('strftime:Chronos') { N1.times { c.format("%Y-%m-%d") } }
+}
View
167 dev/info/chronos api.txt
@@ -0,0 +1,167 @@
+Integer
+ #leap_year? # is this year a leapyear?
+ #leap_seconds # number of leap_seconds in this year
+ #picoseconds # => Duration
+ #nanoseconds # => Duration
+ #microseconds # => Duration
+ #milliseconds # => Duration
+ #seconds # => Duration
+ #minutes # => Duration
+ #hours # => Duration
+ #days # => Duration
+ #weeks # => Duration
+ #months # => Duration
+ #years # => Duration
+
+Chronos:
+ Past # Artificial date that always lies in the past
+ Future # Artificial date that always lies in the future
+
+Datetime:
+ ::from_time(Time) # from ruby time classes
+ ::from_date(Date/DateTime) # from ruby date classes
+ ::new(day_number, nanosecond_number)
+
+ #day_number
+ #nanosecond_number
+ #offset
+ #offset_dst
+ #offset_timezone
+ #timezone
+
+ #enumerate(duration, stop=nil) # => Enumerator
+
+ #date?
+ #time?
+ #datetime?
+
+ #<=>(Datetime)
+ #+(Duration/Interval) # => Datetime
+ #+(Datetime) # => only allowed to add a date? to a time?
+ #-(Duration/Interval) # => Datetme
+ #-(Datetime) # => Interval
+
+ #strip_date
+ #strip_time
+ #in(timezone)
+ #to_datetime
+ #to_timestamp
+
+Datetime::Gregorian
+ include Comparable
+
+ ::parse(string, format=nil, language=nil)
+ ::iso_8601(string)
+ ::xml(string)
+ ::_load(string) (Marshal)
+ ::weeks(year) # how many weeks does this year have?
+ ::civil(year, month, day, h,m,s,tz)
+ ::ordinal(year, dayofyear, h,m,s,tz)
+ ::commercial(year, week, dayofweek, h,m,s,tz)
+ ::today
+ ::now
+
+ #year # => Integer
+ #month # => Integer (1-12)
+ #day_of_month (aka #day) # => Integer (1-31)
+ #week # => Integer (0-53)
+ #commercial_week # => Integer (0-52)
+ #day_of_week # => Integer (0-6)
+ #day_of_year # => Integer (1-366)
+ #hour # => Integer (0-23)
+ #minute # => Integer (0-59)
+ #second # => Integer (0-59)
+ #millisecond # => Integer (0-999)
+ #microsecond # => Integer (0-999)
+ #nanosecond # => Integer (0-999)
+ #picosecond # => Integer (0-999)
+
+ #weeks_in_year # how many weeks this year has
+ #weeks_in_month # how many weeks this year has
+ #days_in_year # how many days this year has
+ #day_in_month # how many days this month has
+
+ #monday? # => true/false
+ #tuesday? # => true/false
+ #wednesday? # => true/false
+ #thursday? # => true/false
+ #friday? # => true/false
+ #saturday? # => true/false
+ #sunday? # => true/false
+
+ #month_name(language=nil) # => String
+ #day_name(language=nil) # => String
+
+ #format # => String
+ #strftime # => String
+
+ #end_of_month
+ #end_of
+ #with(values={}) #
+ #at(hour, minute, second) #
+ #on(year, month, day) #
+ #_dump # Marshal
+
+Enumerator:
+ include Enumerable
+ #each
+ #each_until
+ #size
+
+Duration:
+ ::parse(string, format=nil, language=nil)
+ ::iso_8601(string)
+
+ #years
+ #months
+ #weeks
+ #days
+ #hours
+ #minutes
+ #seconds
+
+ #enumerate(startdate=today) #
+
+ +(Datetime) # => Interval
+ +(Duration) # => Duration
+ -(Datetime) # => Interval
+ -(Duration) # => Duration
+
+Interval
+ #+(Interval) # => Interval, ! you can only add another interval if either start+end or end+start are the same !
+ #+(Duration) # => Interval
+
+Calendar:Gregorian
+ #year=
+ #month=
+ #day_of_month=
+ #week=
+ #day_of_week=
+ #day= (day_number, so you can do cal.day += 200)
+ #hour=
+ #minute=
+ #second=
+ #to_datetime
+
+Frequency
+
+format generics:
+ %? - no leading zeros
+ %0? - leading zeros
+ % ? - leading spaces
+ %-? - prefix with - if negative
+ %+? - prefix with +/-
+ +/- and 0/<space> can be combined, e.g.
+ %+0?
+
+ Example:
+ date.format("%0j") # => "014"
+ duration.format("%+Ss") # => "+15s"
+
+Datetime#format
+ Y: year 4 digit
+ y: year 2 digit
+ m: month
+ w: week (ISO)
+ d: day of month
+
View
57 dev/info/chronos internal C.txt
@@ -0,0 +1,57 @@
+datetime struct {
+ daynumber int64 (xffffffffffffffff = no date part)
+ picosecond int64 (xffffffffffffffff = no time part)
+ timezone int16
+ cache1 int32 see table below
+ cache2 int32 see table below
+ language VALUE
+}
+
+timezone:
+ timezone_id 9bit (400 timezones)
+ timezone_offset 7bit (15min units)
+
+
+cache1:
+ has layout 1bit (true/false) (whether already Gregorian or another module uses the cache)
+ year 16bit
+ month 4bit (1-12)
+ day_of_year 9bit (1-366)
+ day_of_week 3bit (0-6)
+ ------------------
+ total 32bit
+
+cache2:
+ dst active 1bit (true/false)
+ week 6bit (0-53)
+ day_of_month 5bit (1-31)
+ hour 5bit (0-23)
+ minute 6bit (0-59)
+ second 6bit (0-60, leap second)
+ ------------------
+ total 30bit
+
+
+Numbers:
+ total size of datetime: 30 bytes (thats less than 1/10 of DateTime)
+ total range: ±2.5e16 years around epoch (that's from before our universe existed up to in 25 quadrillion years)
+ resolution: 1ps is the smallest unit, that's 1e-12 second
+
+
+why two bare integers as cache?
+- better suited for those sub-byte values
+- masking + shifting is very fast, shouldn't impact much
+- enables use by non-gregorian by simply using another masking table
+
+
+
+duration struct {
+ months int32
+ days int16
+ ps int64
+}
+
+interval struct {
+ from datetime
+ until datetime
+}
View
59 dev/info/strftime_formats.txt
@@ -0,0 +1,59 @@
+%A is replaced by national representation of the full weekday name.
+%a is replaced by national representation of the abbreviated weekday name.
+%B is replaced by national representation of the full month name.
+%b is replaced by national representation of the abbreviated month name.
+%C is replaced by (year / 100) as decimal number; single digits are preceded by a zero.
+%c is replaced by national representation of time and date.
+%D is equivalent to ``%m/%d/%y''.
+%d is replaced by the day of the month as a decimal number (01-31).
+%E* %O* POSIX locale extensions. The sequences %Ec %EC %Ex %EX %Ey %EY %Od %Oe %OH %OI %Om %OM %OS %Ou
+ %OU %OV %Ow %OW %Oy are supposed to provide alternate representations.
+ Additionly %OB implemented to represent alternative months names (used standalone, without day
+ mentioned).
+%e is replaced by the day of month as a decimal number (1-31); single digits are preceded by a
+ blank.
+%F is equivalent to ``%Y-%m-%d''.
+%G is replaced by a year as a decimal number with century. This year is the one that contains the
+ greater part of the week (Monday as the first day of the week).
+%g is replaced by the same year as in ``%G'', but as a decimal number without century (00-99).
+%H is replaced by the hour (24-hour clock) as a decimal number (00-23).
+%h the same as %b.
+%I is replaced by the hour (12-hour clock) as a decimal number (01-12).
+%j is replaced by the day of the year as a decimal number (001-366).
+%k is replaced by the hour (24-hour clock) as a decimal number (0-23); single digits are preceded by
+ a blank.
+%l is replaced by the hour (12-hour clock) as a decimal number (1-12); single digits are preceded by
+ a blank.
+%M is replaced by the minute as a decimal number (00-59).
+%m is replaced by the month as a decimal number (01-12).
+%n is replaced by a newline.
+%O* the same as %E*.
+%p is replaced by national representation of either "ante meridiem" or "post meridiem" as appropri-
+ ate.
+%R is equivalent to ``%H:%M''.
+%r is equivalent to ``%I:%M:%S %p''.
+%S is replaced by the second as a decimal number (00-60).
+%s is replaced by the number of seconds since the Epoch, UTC (see mktime(3)).
+%T is equivalent to ``%H:%M:%S''.
+%t is replaced by a tab.
+%U is replaced by the week number of the year (Sunday as the first day of the week) as a decimal
+ number (00-53).
+%u is replaced by the weekday (Monday as the first day of the week) as a decimal number (1-7).
+%V is replaced by the week number of the year (Monday as the first day of the week) as a decimal
+ number (01-53). If the week containing January 1 has four or more days in the new year, then it
+ is week 1; otherwise it is the last week of the previous year, and the next week is week 1.
+%v is equivalent to ``%e-%b-%Y''.
+%W is replaced by the week number of the year (Monday as the first day of the week) as a decimal
+ number (00-53).
+%w is replaced by the weekday (Sunday as the first day of the week) as a decimal number (0-6).
+%X is replaced by national representation of the time.
+%x is replaced by national representation of the date.
+%Y is replaced by the year with century as a decimal number.
+%y is replaced by the year without century as a decimal number (00-99).
+%Z is replaced by the time zone name.
+%z is replaced by the time zone offset from UTC; a leading plus sign stands for east of UTC, a minus
+ sign for west of UTC, hours and minutes follow with two digits each and no delimiter between them
+ (common form for RFC 822 date headers).
+%+ is replaced by national representation of the date and time (the format is similar to that pro-
+ duced by date(1)).
+%% is replaced by `%'.
View
56 dev/info/usecases.txt
@@ -0,0 +1,56 @@
+Use cases:
+ Date & Time:
+ a) Printing a date/time
+ * numbers without padding/leading zero
+ * numbers with padding
+ * numbers with leading zero
+ * different languages
+
+ b) Getting from a given date to a relative other date
+ *with knowing the difference (duration) in between
+ *with knowing parts of the other date, such as:
+ >"following friday"
+ >"monday 2 weeks later"
+ >"same day in a month"
+ >"last day of month"
+ >"the next nth day of the month"
+
+ >"previous friday"
+ >"monday 2 weeks earlier"
+ >"same day a month ago"
+ >"the last nth day of the month
+
+ c) Getting an absolute date with relation to a given date
+ >"n days before end of this month"
+
+ d) Converting a serialized/string form of a date/time into a Datetime
+ >"date for '2001-02-03'"
+ e) Converting a partial serialized/string from of a date/time into a Datetime
+ >"date for '2001'"
+ f) Finding the difference between two dates
+ >"how many days are between 2001-02-13 and 2005-01-17"
+ g) iterating from a start to an end date in a given interval
+ >"every week from 2001-01-01 until 2002-01-01
+ h) iterate from a start while/until a condition is met in a given interval
+ >"every month until week > 10"
+
+ Duration:
+ a) Printing a duration
+ >"3d 02h 15m 02s"
+ b) Parsing a duration
+ c)
+
+ Interval:
+ a) Get how much of a part it is
+ > "How many days (after years and months) are in the interval from 2001-02-13 and 2005-01-17"
+ b) Convert to total amount of a single entity
+ > "How many days are in the interval from 2001-02-13 and 2005-01-17"
+ c) Get all dates that satisfy a given condition
+ > "All saturdays between 2001-02-13 and 2005-01-17"
+
+Solutions:
+ a)
+ date.format("%Y-%0m-%0d %0H:%0M")
+
+also look:
+ Perl: Date::Calc, Date::Manip
View
BIN  ext/.DS_Store
Binary file not shown
View
BIN  ext/cchronos/.DS_Store
Binary file not shown
View
BIN  lib/.DS_Store
Binary file not shown
View
28 lib/chronos/durationtotext.rb
@@ -0,0 +1,28 @@
+require 'enumerator'
+class Duration
+ToTextEn = {
+-(1/0.0) => proc { |s| sprintf "%d weeks ago", -s.div(604800) },
+-1209600 => proc { |s| "one week ago" },
+-604800 => proc { |s| sprintf "%d days ago", -s.div(86500) },
+-172800 => proc { |s| "yesterday" },
+0 => proc { |s| "today" },
+86400 => proc { |s| "tomorrow" },
+172800 => proc { |s| sprintf "in %d days", (s/86400).ceil },
+604800 => proc { |s| "in one week" },
+1209600 => proc { |s| sprintf "in %d weeks", (s/604800).ceil },
+}.to_a.sort
+def initialize(seconds, months=0)
+@seconds = seconds
+@months = months
+end
+
+def to_text
+ToTextEn.each_cons(2) { |(v,t), (v2,t2)|
+return t.call(@seconds) if @seconds >= v && @seconds < v2
+}
+ToTextEn.last.last.call(@seconds)
+end
+alias to_s to_text
+end
+time = Time.today-2*7*24*3600
+30.times { puts "#{time.strftime('%Y-%m-%d')}: #{Duration.new(time-Time.today)}"; time+=86400 }
View
1  lib/chronos/locale/parsers/en_US.rb
@@ -0,0 +1 @@
+add_format "%m/%d/%Y"
View
21 lib/chronos/locale/parsers/generic.rb
@@ -0,0 +1,21 @@
+add_format_as "Y", /(\d{4})/, :year # 4 digits
+add_format_as "m", /(0?[1-9]|1[012])/, :month # 1-12 with optional leading zero
+add_format_as "d", /(0?[1-9]|[12]\d|3[01])/, :day_of_month # 1-31 with optional leading zero
+add_format_as "H", /([01]?\d|2[123])/, :hour # 0-23 with optional leading zero
+add_format_as "M", /([0-5]?\d)/, :minute # 0-59 with optional leading zero
+add_format_as "S", /([0-5]?\d|60)/, :second # 0-60 with optional leading zero (leap second)
+add_format_as "Z", /(#{Chronos::TimezonesByName.keys.sort.join('|')})/, :timezone do |parser, matches| # timezones are known to have no special chars, no escaping required
+ if tz = Chronos::TimezonesByName[matches.first] then
+ tz.offset
+ end
+end
+add_format_as "z", /(+-)/, :timezone do |parser, matches| # timezones are known to have no special chars, no escaping required
+ if tz = Chronos::TimezonesByName[matches.first] then
+ tz.offset
+ end
+end
+
+add_format_as "iso_date", /#{Y}-(#{m})-#{d}/, :year, :month, :day
+add_format_as
+
+add_format(/#{iso_date}/)
View
3  lib/chronos/zone.rb
@@ -16,6 +16,7 @@ module Chronos
)
class Zone
ZonesFile = File.join(File.dirname(__FILE__),"data","zones.tab")
+ ZonesData = File.join(File.dirname(__FILE__),"data","zones.marshal")
@by_name = {}
@by_region = {}
@@ -144,5 +145,3 @@ def self.[](by_name)
end
end
end
-
-load(File.dirname(__FILE__)+"/../../test/chronos/tc_"+File.basename(__FILE__)) if __FILE__ == $0
Please sign in to comment.
Something went wrong with that request. Please try again.