forked from apeiros/chronos
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
19 changed files
with
424 additions
and
2 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
.svn | ||
.DS_Store | ||
/coverage | ||
/conflicts | ||
/unversioned | ||
/trunk/docs | ||
/trunk/pkg | ||
/trunk/web | ||
/silverplatter-irc.xcodeproj | ||
/build/ |
Empty file.
Empty file.
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,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") } } | ||
} |
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,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 | ||
|
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,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 | ||
} |
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,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 `%'. |
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,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 |
Binary file not shown.
Binary file not shown.
Binary file not shown.
Oops, something went wrong.