Package: timeswitch
Timeswitch allows dates to be tested against a time range to see if they
are contained within the time range. Time ranges are expressed using a
simple grammar that supports absolute dates and times, repeating
periods, greater, less than, between and exact match comparisons, and
combinations of brackets and infix notation logical operators.A time is made up of the following ordered components, with sensible
defaults for components when omitted:year:range expression with values 1-9999month:range expression with integer values 1-12 or case
insensitive jan, feb, mar, apr, may, jun, jul, aug, sep, oct, nov,
decday:range expression with either integer values 1-31 or case
insensitive mon, tue, wed, thu, fri, sat, sun. When used with a month
a weekday has an optional ordinal 1st, 2nd, 3rd, 4th or 5th prefix to
indicate nth weekday in the monthhour-minute:range expression with two integer values separated by
a colon 0-23:0-59compound:range expression with all or partial non ambiguous
sequences of year month day hour-minute valuesThe expected month, weekday and ordinal strings can be overriden if
necessary using the API.A time range expression is similar to Python array syntax:-is any valuexis a value = xx-yis a value x ≤ value ≤ y. Note that:The expressions on either side of the-must specify the same
rightmost elements, e.g2017 jan - febnot2017 jan - feb 12The repeat period of the right hand side must be the same as or
more frequent thant the first e.gjan - 2017 febis not validFor weekday ranges, Monday is considered the start of the week-xis a value less than or equal to xx-is a value greater than or equal to xTime range expressions can be combined usingand,or,notand( )to form nested expressions.The empty string is always off. A dash is always on:-Always on from 1:30am on the 3rd of April 2017:2017 apr 3 1:30 -Always on every Friday on or after the 3rd of April 2017 (year and month
can be omitted because ‘fri’ is unambiguously a weekday):fri and 2017 apr 3 -Always on every 1st Friday of the month on or after the 3rd of April
2017:1st fri and 2017 apr 3 -Always on every 1st or 3rd Friday of the month on or after the 3rd of
April 2017:(1st fri or 3rd fri) and 2017 apr 3 -First Friday of the month between 6:30pm and 9:50pm:1st fri18:30-21:50Thanksgiving, Christmas, Boxing Day and New Year’s Day:nov 4th thu or dec 25 or dec 26 or jan 1Normal operator precedence isnot,and,or. Parenthesis can
be used to change this:not((junor aug) and 2017)UsageA command line interface for timeswitch is available:$timeswitchusage:timeswitch[-h][-d][-w][-y][-a][-p]expression[date]timeswitch:error:toofewarguments$timeswitch"2017 jan 20 -"on$echo$?0$timeswitch"1972 dec 5 - 1975 nov 11"off$echo$?1$The--year,--week, and--dayoptions provide helpful
visualisations when developing and debugging timeswitch expressions.To use the library directly in Python:fromtimeswitchimporttimeswitchfromdatetimeimportdatetimets=timeswitch('2017 may 4 20:20 - 20:30')printts(datetime(2017,5,4,20,21))# Trueprintts(datetime(2017,5,4,20,31))# Falsetimeswitch()accepts optional namedmonths,weekdaysandordinalarguments. Each is an array of alternative literal strings
used when parsing the corresponding date part, for example if you want
to write ‘first wed’ instead of ‘1st wed’:ts=timeswitch('first wed',ordinals='first second third fourth fifth'.split(' '))ContributingYou may contribute to the project by logging issues through the project
issue tracker or by submitting a pull request.DevelopmentA development environment may be setup like this:gitclonehttps://bitbucket.org/rocketboots/timeswitch.gitcdtimeswitchpipinstall-e.[test]pythonsetup.pynosetestsContributions should include passing unit tests.PyPI RegistrationTo support reStructuredText as PyPI expects, this document is
automatically converted provided thePandocpackage is installed. On OS X,brewinstallpandocpipinstallpypandoctwinepythonsetup.pysdisttwineregisterdist/timeswitch-*.tar.gz