Time-Period-1.25000755001750000144 012452140144 13211 5ustar00pboydusers000000000000README100644001750000144 441112452140144 14152 0ustar00pboydusers000000000000Time-Period-1.25README for the Perl Module Time::Period Copyright (c) 1997 Patrick Ryan. All rights reserved. This Perl module uses the conditions given by Perl. This module may only be distributed and or modified under the conditions given by Perl. ----------------- Document Contents ----------------- - Document Contents - Overview - Getting the Distribution - Installation of the Module - Using the Module - Contacting the Author - Removing the Module -------- Overview -------- Period.pm is a Perl module that contains code to deal with time periods. Currently, there is only a function in this module. That function is called inPeriod(). inPeriod() determines if a given time is within a given time period. It will return 1 if it is, 0 if not, and -1 if either the time or the period passed to it were malformed. The time is specified in non-leap year seconds past January 1, 1970, as per the time() function. The period is a string which is of the form described in Period's man page. ------------------------ Getting the Distribution ------------------------ This distribution can be retrieved at from CPAN at . -------------------------- Installation of the Module -------------------------- Make sure the user executing this has sufficient permissions to add and modify files in the global perl installation. The user should use this sequence to install this module. perl Makefile.PL make make install make realclean ---------------- Using the Module ---------------- The module is simple to use. It is fully documented with pod documentation. To read it, execute perldoc Time::Period after the module is installed. A HTML version of the pod documentation is included in the distribution. It was created with pod2html, which comes with Perl. --------------------- Contacting the Author --------------------- I welcome any feedback on this module. If you have comments, questions, or ideas, feel free to send them to me, Patrick Ryan, pgryan@geocities.com. ------------------- Removing the Module ------------------- Under a user with sufficient permissions and from the Period distribution directory, execute perl Makefile.PL if there isn't a file called Makefile. Then execute make uninstall LICENSE100644001750000144 4365512452140144 14334 0ustar00pboydusers000000000000Time-Period-1.25This software is copyright (c) 1997 by Patrick Ryan. This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself. Terms of the Perl programming language system itself a) the GNU General Public License as published by the Free Software Foundation; either version 1, or (at your option) any later version, or b) the "Artistic License" --- The GNU General Public License, Version 1, February 1989 --- This software is Copyright (c) 1997 by Patrick Ryan. This is free software, licensed under: The GNU General Public License, Version 1, February 1989 GNU GENERAL PUBLIC LICENSE Version 1, February 1989 Copyright (C) 1989 Free Software Foundation, Inc. 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA Everyone is permitted to copy and distribute verbatim copies of this license document, but changing it is not allowed. Preamble The license agreements of most software companies try to keep users at the mercy of those companies. By contrast, our General Public License is intended to guarantee your freedom to share and change free software--to make sure the software is free for all its users. The General Public License applies to the Free Software Foundation's software and to any other program whose authors commit to using it. You can use it for your programs, too. When we speak of free software, we are referring to freedom, not price. Specifically, the General Public License is designed to make sure that you have the freedom to give away or sell copies of free software, that you receive source code or can get it if you want it, that you can change the software or use pieces of it in new free programs; and that you know you can do these things. To protect your rights, we need to make restrictions that forbid anyone to deny you these rights or to ask you to surrender the rights. These restrictions translate to certain responsibilities for you if you distribute copies of the software, or if you modify it. For example, if you distribute copies of a such a program, whether gratis or for a fee, you must give the recipients all the rights that you have. You must make sure that they, too, receive or can get the source code. And you must tell them their rights. We protect your rights with two steps: (1) copyright the software, and (2) offer you this license which gives you legal permission to copy, distribute and/or modify the software. Also, for each author's protection and ours, we want to make certain that everyone understands that there is no warranty for this free software. If the software is modified by someone else and passed on, we want its recipients to know that what they have is not the original, so that any problems introduced by others will not reflect on the original authors' reputations. The precise terms and conditions for copying, distribution and modification follow. GNU GENERAL PUBLIC LICENSE TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION 0. This License Agreement applies to any program or other work which contains a notice placed by the copyright holder saying it may be distributed under the terms of this General Public License. The "Program", below, refers to any such program or work, and a "work based on the Program" means either the Program or any work containing the Program or a portion of it, either verbatim or with modifications. Each licensee is addressed as "you". 1. You may copy and distribute verbatim copies of the Program's source code as you receive it, in any medium, provided that you conspicuously and appropriately publish on each copy an appropriate copyright notice and disclaimer of warranty; keep intact all the notices that refer to this General Public License and to the absence of any warranty; and give any other recipients of the Program a copy of this General Public License along with the Program. You may charge a fee for the physical act of transferring a copy. 2. You may modify your copy or copies of the Program or any portion of it, and copy and distribute such modifications under the terms of Paragraph 1 above, provided that you also do the following: a) cause the modified files to carry prominent notices stating that you changed the files and the date of any change; and b) cause the whole of any work that you distribute or publish, that in whole or in part contains the Program or any part thereof, either with or without modifications, to be licensed at no charge to all third parties under the terms of this General Public License (except that you may choose to grant warranty protection to some or all third parties, at your option). c) If the modified program normally reads commands interactively when run, you must cause it, when started running for such interactive use in the simplest and most usual way, to print or display an announcement including an appropriate copyright notice and a notice that there is no warranty (or else, saying that you provide a warranty) and that users may redistribute the program under these conditions, and telling the user how to view a copy of this General Public License. d) You may charge a fee for the physical act of transferring a copy, and you may at your option offer warranty protection in exchange for a fee. Mere aggregation of another independent work with the Program (or its derivative) on a volume of a storage or distribution medium does not bring the other work under the scope of these terms. 3. You may copy and distribute the Program (or a portion or derivative of it, under Paragraph 2) in object code or executable form under the terms of Paragraphs 1 and 2 above provided that you also do one of the following: a) accompany it with the complete corresponding machine-readable source code, which must be distributed under the terms of Paragraphs 1 and 2 above; or, b) accompany it with a written offer, valid for at least three years, to give any third party free (except for a nominal charge for the cost of distribution) a complete machine-readable copy of the corresponding source code, to be distributed under the terms of Paragraphs 1 and 2 above; or, c) accompany it with the information you received as to where the corresponding source code may be obtained. (This alternative is allowed only for noncommercial distribution and only if you received the program in object code or executable form alone.) Source code for a work means the preferred form of the work for making modifications to it. For an executable file, complete source code means all the source code for all modules it contains; but, as a special exception, it need not include source code for modules which are standard libraries that accompany the operating system on which the executable file runs, or for standard header files or definitions files that accompany that operating system. 4. You may not copy, modify, sublicense, distribute or transfer the Program except as expressly provided under this General Public License. Any attempt otherwise to copy, modify, sublicense, distribute or transfer the Program is void, and will automatically terminate your rights to use the Program under this License. However, parties who have received copies, or rights to use copies, from you under this General Public License will not have their licenses terminated so long as such parties remain in full compliance. 5. By copying, distributing or modifying the Program (or any work based on the Program) you indicate your acceptance of this license to do so, and all its terms and conditions. 6. Each time you redistribute the Program (or any work based on the Program), the recipient automatically receives a license from the original licensor to copy, distribute or modify the Program subject to these terms and conditions. You may not impose any further restrictions on the recipients' exercise of the rights granted herein. 7. The Free Software Foundation may publish revised and/or new versions of the General Public License from time to time. Such new versions will be similar in spirit to the present version, but may differ in detail to address new problems or concerns. Each version is given a distinguishing version number. If the Program specifies a version number of the license which applies to it and "any later version", you have the option of following the terms and conditions either of that version or of any later version published by the Free Software Foundation. If the Program does not specify a version number of the license, you may choose any version ever published by the Free Software Foundation. 8. If you wish to incorporate parts of the Program into other free programs whose distribution conditions are different, write to the author to ask for permission. For software which is copyrighted by the Free Software Foundation, write to the Free Software Foundation; we sometimes make exceptions for this. Our decision will be guided by the two goals of preserving the free status of all derivatives of our free software and of promoting the sharing and reuse of software generally. NO WARRANTY 9. BECAUSE THE PROGRAM IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING, REPAIR OR CORRECTION. 10. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR REDISTRIBUTE THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. END OF TERMS AND CONDITIONS Appendix: How to Apply These Terms to Your New Programs If you develop a new program, and you want it to be of the greatest possible use to humanity, the best way to achieve this is to make it free software which everyone can redistribute and change under these terms. To do so, attach the following notices to the program. It is safest to attach them to the start of each source file to most effectively convey the exclusion of warranty; and each file should have at least the "copyright" line and a pointer to where the full notice is found. Copyright (C) 19yy This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 1, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301 USA Also add information on how to contact you by electronic and paper mail. If the program is interactive, make it output a short notice like this when it starts in an interactive mode: Gnomovision version 69, Copyright (C) 19xx name of author Gnomovision comes with ABSOLUTELY NO WARRANTY; for details type `show w'. This is free software, and you are welcome to redistribute it under certain conditions; type `show c' for details. The hypothetical commands `show w' and `show c' should show the appropriate parts of the General Public License. Of course, the commands you use may be called something other than `show w' and `show c'; they could even be mouse-clicks or menu items--whatever suits your program. You should also get your employer (if you work as a programmer) or your school, if any, to sign a "copyright disclaimer" for the program, if necessary. Here a sample; alter the names: Yoyodyne, Inc., hereby disclaims all copyright interest in the program `Gnomovision' (a program to direct compilers to make passes at assemblers) written by James Hacker. , 1 April 1989 Ty Coon, President of Vice That's all there is to it! --- The Artistic License 1.0 --- This software is Copyright (c) 1997 by Patrick Ryan. This is free software, licensed under: The Artistic License 1.0 The Artistic License Preamble The intent of this document is to state the conditions under which a Package may be copied, such that the Copyright Holder maintains some semblance of artistic control over the development of the package, while giving the users of the package the right to use and distribute the Package in a more-or-less customary fashion, plus the right to make reasonable modifications. Definitions: - "Package" refers to the collection of files distributed by the Copyright Holder, and derivatives of that collection of files created through textual modification. - "Standard Version" refers to such a Package if it has not been modified, or has been modified in accordance with the wishes of the Copyright Holder. - "Copyright Holder" is whoever is named in the copyright or copyrights for the package. - "You" is you, if you're thinking about copying or distributing this Package. - "Reasonable copying fee" is whatever you can justify on the basis of media cost, duplication charges, time of people involved, and so on. (You will not be required to justify it to the Copyright Holder, but only to the computing community at large as a market that must bear the fee.) - "Freely Available" means that no fee is charged for the item itself, though there may be fees involved in handling the item. It also means that recipients of the item may redistribute it under the same conditions they received it. 1. You may make and give away verbatim copies of the source form of the Standard Version of this Package without restriction, provided that you duplicate all of the original copyright notices and associated disclaimers. 2. You may apply bug fixes, portability fixes and other modifications derived from the Public Domain or from the Copyright Holder. A Package modified in such a way shall still be considered the Standard Version. 3. You may otherwise modify your copy of this Package in any way, provided that you insert a prominent notice in each changed file stating how and when you changed that file, and provided that you do at least ONE of the following: a) place your modifications in the Public Domain or otherwise make them Freely Available, such as by posting said modifications to Usenet or an equivalent medium, or placing the modifications on a major archive site such as ftp.uu.net, or by allowing the Copyright Holder to include your modifications in the Standard Version of the Package. b) use the modified Package only within your corporation or organization. c) rename any non-standard executables so the names do not conflict with standard executables, which must also be provided, and provide a separate manual page for each non-standard executable that clearly documents how it differs from the Standard Version. d) make other distribution arrangements with the Copyright Holder. 4. You may distribute the programs of this Package in object code or executable form, provided that you do at least ONE of the following: a) distribute a Standard Version of the executables and library files, together with instructions (in the manual page or equivalent) on where to get the Standard Version. b) accompany the distribution with the machine-readable source of the Package with your modifications. c) accompany any non-standard executables with their corresponding Standard Version executables, giving the non-standard executables non-standard names, and clearly documenting the differences in manual pages (or equivalent), together with instructions on where to get the Standard Version. d) make other distribution arrangements with the Copyright Holder. 5. You may charge a reasonable copying fee for any distribution of this Package. You may charge any fee you choose for support of this Package. You may not charge a fee for this Package itself. However, you may distribute this Package in aggregate with other (possibly commercial) programs as part of a larger (possibly commercial) software distribution provided that you do not advertise this Package as a product of your own. 6. The scripts and library files supplied as input to or produced as output from the programs of this Package do not automatically fall under the copyright of this Package, but belong to whomever generated them, and may be sold commercially, and may be aggregated with this Package. 7. C or perl subroutines supplied by you and linked into this Package shall not be considered part of this Package. 8. The name of the Copyright Holder may not be used to endorse or promote products derived from this software without specific prior written permission. 9. THIS PACKAGE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE. The End dist.ini100644001750000144 53312452140144 14717 0ustar00pboydusers000000000000Time-Period-1.25name = Time-Period author = Patrick Ryan, Paul Boyd license = Perl_5 copyright_holder = Patrick Ryan copyright_year = 1997 version = 1.25 [GatherDir] [PruneCruft] [ManifestSkip] [MetaYAML] [License] [ExtraTests] [ExecDir] [ShareDir] [MakeMaker] [Manifest] [TestRelease] [ConfirmRelease] [UploadToCPAN] [AutoPrereqs] META.yml100644001750000144 101512452140144 14540 0ustar00pboydusers000000000000Time-Period-1.25--- abstract: 'A Perl module to deal with time periods.' author: - 'Patrick Ryan, Paul Boyd ' build_requires: POSIX: '0' Test::More: '0' strict: '0' warnings: '0' configure_requires: ExtUtils::MakeMaker: '6.30' dynamic_config: 0 generated_by: 'Dist::Zilla version 5.019, CPAN::Meta::Converter version 2.140640' license: perl meta-spec: url: http://module-build.sourceforge.net/META-spec-v1.4.html version: '1.4' name: Time-Period requires: Exporter: '0' perl: '5.001' version: '1.25' MANIFEST100644001750000144 45212452140144 14404 0ustar00pboydusers000000000000Time-Period-1.25# This file was automatically generated by Dist::Zilla::Plugin::Manifest v5.019. LICENSE MANIFEST META.yml Makefile.PL README dist.ini lib/Time/Period.pm t/01_time_period.t t/02_weekday.t t/03_year.t t/04_month.t t/05_week.t t/06_year_day.t t/07_month_day.t t/08_hour.t t/09_minute.t t/10_second.t t000755001750000144 012452140144 13375 5ustar00pboydusers000000000000Time-Period-1.2508_hour.t100644001750000144 662712452140144 15221 0ustar00pboydusers000000000000Time-Period-1.25/tuse strict; use warnings; use Test::More tests => 33; use Time::Period; use POSIX; my $base_date = POSIX::mktime(0, 0, 0, 1, 0, 111); # 01/01/2011 00:00:00 (Saturday) my $hour = 60 * 60; is(inPeriod($base_date, 'hr {0}'), 1, 'should match a single hour'); is(inPeriod($base_date, 'hour {0}'), 1, 'should match a single hour, by the long form'); is(inPeriod($base_date - $hour, 'hr {0}'), 0, 'should be able to not match a single hour'); is(inPeriod($base_date + $hour, 'hr {0-3}'), 1, 'should be able to match a range of hours'); is(inPeriod($base_date - $hour, 'hr {0-3}'), 0, 'should be able to not match a range of hours'); is(inPeriod($base_date, 'hr {23-5}'), 1, 'should be able to match a range of days when the first hour is greater than the second'); is(inPeriod($base_date - $hour, 'hr {23-5}'), 1, 'should be able to match a range of days when the first hour is greater than the second'); is(inPeriod($base_date - $hour * 2, 'hr {23-5}'), 0, 'should be able to not match a range of days when the first hour is greater than the second'); is(inPeriod($base_date + $hour * 5, 'hr {1-2}'), 0, 'should be able to not match a range of days when the first hour is less than the second'); is(inPeriod(0, 'hr {one}'), -1, 'should return -1 for non-numeric hours (single)'); is(inPeriod(0, 'hr {one - 3}'), -1, 'should return -1 for non-numeric hours (left)'); is(inPeriod(0, 'hr {24}'), -1, 'should return -1 for hours greater than 23 (single)'); is(inPeriod(0, 'hr {24-1}'), -1, 'should return -1 for hours greater than 23 (left)'); is(inPeriod($base_date, 'hr {12am}'), 1, '12am should be treated as midnight (single)'); is(inPeriod($base_date, 'hr {12am-1}'), 1, '12am should be treated as midnight (left)'); is(inPeriod($base_date, 'hr {23-12am}'), 1, '12am should be treated as midnight (right)'); is(inPeriod($base_date + $hour, 'hr {1am}'), 1, '"am" times should not be altered (single)'); is(inPeriod($base_date + $hour, 'hr {1am-2}'), 1, '"am" times should not be altered (left)'); is(inPeriod($base_date + $hour, 'hr {0-1am}'), 1, '"am" times should not be altered (left)'); is(inPeriod($base_date + $hour * 12, 'hr {12noon}'), 1, '12noon should be treated as 12pm (single)'); is(inPeriod($base_date + $hour * 12, 'hr {12noon-13}'), 1, '12noon should be treated as 12pm (left)'); is(inPeriod($base_date + $hour * 12, 'hr {11-12noon}'), 1, '12noon should be treated as 12pm (right)'); is(inPeriod(0, 'hr {13noon}'), -1, 'only "12noon" is valid -- not 13noon (single)'); is(inPeriod(0, 'hr {13noon-13}'), -1, 'only "12noon" is valid -- not 13noon (left)'); is(inPeriod(0, 'hr {11-13noon}'), -1, 'only "12noon" is valid -- not 13noon (right)'); is(inPeriod($base_date + $hour * 13, 'hr {1pm}'), 1, '"pm" times should have 12 hours added to them (single)'); is(inPeriod($base_date + $hour * 13, 'hr {1pm-13}'), 1, '"pm" times should have 12 hours added to them (left)'); is(inPeriod($base_date + $hour * 13, 'hr {11-1pm}'), 1, '"pm" times should have 12 hours added to them (right)'); is(inPeriod($base_date + $hour * 12, 'hr {12pm}'), 1, '12pm should be treated as noon (single)'); is(inPeriod($base_date + $hour * 12, 'hr {12pm-13}'), 1, '12pm should be treated as noon (left)'); is(inPeriod($base_date + $hour * 12, 'hr {11-12pm}'), 1, '12pm should be treated as noon (right)'); is(inPeriod(0, 'hr {3 - one}'), -1, 'should return -1 for non-numeric hours (right)'); is(inPeriod(0, 'hr {1-24}'), -1, 'should return -1 for hours greater than 23 (right)'); 03_year.t100644001750000144 373512452140144 15174 0ustar00pboydusers000000000000Time-Period-1.25/tuse strict; use warnings; use Test::More tests => 16; use Time::Period; use POSIX; my $base_date = POSIX::mktime(0, 0, 0, 1, 0, 111); # 01/01/2011 00:00:00 (Saturday) my $year = 365 * 24 * 60 * 60; is(inPeriod($base_date, 'yr {2011}'), 1, 'should match a single year'); is(inPeriod($base_date, 'yr {2010}'), 0, 'should be able to not match a single year'); is(inPeriod($base_date, 'yr {2010-2015}'), 1, 'should match a range of years'); is(inPeriod($base_date, 'yr {2015-2010}'), 1, 'should match a range of years when the first year is greater than the latter'); is(inPeriod($base_date, 'yr {2000-2009}'), 0, 'should be able to not match a range (too low)'); is(inPeriod($base_date, 'yr {2012-2013}'), 0, 'should be able to not match a range (too high)'); is(inPeriod($base_date - 20 * $year, 'yr {90-95}'), 1, 'should do the right thing for a range when the year is less than 100'); is(inPeriod($base_date - 20 * $year, 'yr {91}'), 1, 'should do the right thing for a single year when it\'s less than 100'); is(inPeriod($base_date, 'yr {}'), 0, 'should never match an empty year'); is(inPeriod(0, 'yr {1960}'), -1, 'should return -1 for years before 1970 (single)'); is(inPeriod(0, 'yr {1960-2000}'), -1, 'should return -1 for years before 1970 (left)'); is(inPeriod(0, 'yr {2000-1960}'), -1, 'should return -1 for years before 1970 (right)'); is(inPeriod(0, 'yr {_}'), -1, 'should return -1 for non-alphnumeric years (single)'); is(inPeriod(0, 'yr {_ - 2000}'), -1, 'should return -1 for non-alphnumeric years (left)'); is(inPeriod(0, 'yr {2000 - _}'), -1, 'should return -1 for non-alphnumeric years (right)'); # This is an old bug, the 4-digit year calculation used to assume 365 days were # in a year, consequently the last day of the a leap year appeared to be in # the following year. See https://rt.cpan.org/Public/Bug/Display.html?id=6353 # # (1356930000 = Mon Dec 31 00:00:00 EST 2012) is(inPeriod(1356930000, 'yr {2012}'), 1, 'should be able to match the last day of the year on leap year'); 05_week.t100644001750000144 437512452140144 15172 0ustar00pboydusers000000000000Time-Period-1.25/tuse strict; use warnings; use Test::More tests => 20; use Time::Period; use POSIX; my $base_date = POSIX::mktime(0, 0, 0, 1, 0, 111); # 01/01/2011 00:00:00 (Saturday) my $week = 7 * 24 * 60 * 60; is(inPeriod($base_date, 'wk {1}'), 1, 'should match a single week'); is(inPeriod($base_date, 'week {1}'), 1, 'should match a single week, by the long form'); is(inPeriod($base_date - $week, 'wk {1}'), 0, 'should be able to not match a single week'); is(inPeriod($base_date + $week, 'wk {1-3}'), 1, 'should be able to match a range of weeks'); is(inPeriod($base_date - $week, 'wk {1-3}'), 0, 'should be able to not match a range of weeks'); is(inPeriod($base_date - $week, 'wk {5-1}'), 0, 'should be able to not match a range of weeks when the first week is greater than the second'); is(inPeriod($base_date - $week, 'wk {3-2}'), 1, 'should be able to match a range of weeks when the first week is greater than the second'); is(inPeriod($base_date - $week, 'wk {5-4}'), 1, 'should be able to match a range of weeks when the first week is greater than the second'); is(inPeriod($base_date - $week, 'wk {5-6}'), 0, 'should be able to not match a range of weeks when the first week is less than the second'); is(inPeriod(1296450000, 'wk {6}'), 1, 'should be able to match the 6th week of a month'); is(inPeriod(0, 'wk {one}'), -1, 'should return -1 for non-numeric week numbers (single)'); is(inPeriod(0, 'wk {one - 3}'), -1, 'should return -1 for non-numeric week numbers (left)'); is(inPeriod(0, 'wk {3-one}'), -1, 'should return -1 for non-numeric week numbers (right)'); is(inPeriod(0, 'wk {0}'), -1, 'should return -1 for week numbers less than 1 (single)'); is(inPeriod(0, 'wk {0-3}'), -1, 'should return -1 for week numbers less than 1 (left)'); is(inPeriod(0, 'wk {3-0}'), -1, 'should return -1 for week numbers less than 1 (right)'); is(inPeriod(0, 'wk {7}'), -1, 'should return -1 for week numbers greater than 6 (single)'); is(inPeriod(0, 'wk {7-1}'), -1, 'should return -1 for week numbers greater than 6 (left)'); is(inPeriod(0, 'wk {1-7}'), -1, 'should return -1 for week numbers greater than 6 (right)'); my $sunday = POSIX::mktime(0, 0, 12, 8, 5, 114); # 01/01/2011 00:00:00 (Saturday) is(inPeriod($sunday, 'wk { 2 }'), 1, 'should be able to match the week when the day is Sunday'); Makefile.PL100644001750000144 221212452140144 15241 0ustar00pboydusers000000000000Time-Period-1.25 # This file was automatically generated by Dist::Zilla::Plugin::MakeMaker v5.019. use strict; use warnings; use 5.001; use ExtUtils::MakeMaker 6.30; my %WriteMakefileArgs = ( "ABSTRACT" => "A Perl module to deal with time periods.", "AUTHOR" => "Patrick Ryan, Paul Boyd ", "CONFIGURE_REQUIRES" => { "ExtUtils::MakeMaker" => "6.30" }, "DISTNAME" => "Time-Period", "EXE_FILES" => [], "LICENSE" => "perl", "NAME" => "Time::Period", "PREREQ_PM" => { "Exporter" => 0 }, "TEST_REQUIRES" => { "POSIX" => 0, "Test::More" => 0, "strict" => 0, "warnings" => 0 }, "VERSION" => "1.25", "test" => { "TESTS" => "t/*.t" } ); my %FallbackPrereqs = ( "Exporter" => 0, "POSIX" => 0, "Test::More" => 0, "strict" => 0, "warnings" => 0 ); unless ( eval { ExtUtils::MakeMaker->VERSION(6.63_03) } ) { delete $WriteMakefileArgs{TEST_REQUIRES}; delete $WriteMakefileArgs{BUILD_REQUIRES}; $WriteMakefileArgs{PREREQ_PM} = \%FallbackPrereqs; } delete $WriteMakefileArgs{CONFIGURE_REQUIRES} unless eval { ExtUtils::MakeMaker->VERSION(6.52) }; WriteMakefile(%WriteMakefileArgs); 04_month.t100644001750000144 553712452140144 15364 0ustar00pboydusers000000000000Time-Period-1.25/tuse strict; use warnings; use Test::More tests => 27; use Time::Period; use POSIX; my $base_date = POSIX::mktime(0, 0, 0, 1, 0, 111); # 01/01/2011 00:00:00 (Saturday) my $month = 29 * 24 * 60 * 60; is(inPeriod($base_date, 'mo {jan}'), 1, 'should match a single month'); is(inPeriod($base_date - $month, 'mo {jan}'), 0, 'should be able to not match a single month'); is(inPeriod($base_date, 'mo {dec-feb}'), 1, 'should be able to match a range of months'); is(inPeriod($base_date, 'mo {dec-feb}'), 1, 'should be able to not match a range of months'); is(inPeriod($base_date, 'mo {mar-oct}'), 0, 'should be able to not match a range when the first month comes before the second'); is(inPeriod($base_date - $month * 2, 'mo {oct-dec}'), 1, 'should be able to match a range of months'); is(inPeriod($base_date - $month * 2, 'mo {dec-oct}'), 0, 'should be able to not match a range when the first month comes after the second'); is(inPeriod($base_date - $month * 2, 'mo {dec-nov}'), 1, 'should "wrap around" to match ranges'); is(inPeriod($base_date, 'mo {feb-mar}'), 0, 'should be able to not match a range when the first month comes before the second'); is(inPeriod($base_date, 'mo {January}'), 1, 'should allow long month names (single)'); is(inPeriod($base_date, 'mo {foobar}'), -1, 'should return -1 for invalid month names (single)'); is(inPeriod($base_date, 'mo {1}'), 1, 'should allow numeric months (single)'); is(inPeriod($base_date, 'mo {17}'), -1, 'should return -1 for numeric months greater than 12 (single)'); is(inPeriod($base_date, 'mo {0}'), -1, 'should return -1 for numeric months less than 1 (single)'); is(inPeriod($base_date, 'mo {_}'), -1, 'should return -1 for non-alphanumeric months (single)'); is(inPeriod($base_date, 'mo {january - march}'), 1, 'should allow long month names (left)'); is(inPeriod($base_date, 'mo {foobar - march}'), -1, 'should return -1 for invalid month names (left)'); is(inPeriod($base_date, 'mo {1 - mar}'), 1, 'should allow numeric months (left)'); is(inPeriod($base_date, 'mo {17 - mar}'), -1, 'should return -1 for numeric months greater than 12 (left)'); is(inPeriod($base_date, 'mo {0 - mar}'), -1, 'should return -1 for numeric months less than 1 (left)'); is(inPeriod($base_date, 'mo {_ - mar}'), -1, 'should return -1 for non-alphanumeric months (left)'); is(inPeriod($base_date, 'mo {dec - january}'), 1, 'should allow long month names (right)'); is(inPeriod($base_date, 'mo {dec - foobar}'), -1, 'should return -1 for invalid month names (right)'); is(inPeriod($base_date, 'mo {dec - 1}'), 1, 'should allow numeric months (right)'); is(inPeriod($base_date, 'mo {6 - 17}'), -1, 'should return -1 for numeric months greater than 12 (right)'); is(inPeriod($base_date, 'mo {1 - 0}'), -1, 'should return -1 for numeric months less than 1 (right)'); is(inPeriod($base_date, 'mo {6 - _}'), -1, 'should return -1 for non-alphanumeric months (right)'); 10_second.t100644001750000144 325712452140144 15504 0ustar00pboydusers000000000000Time-Period-1.25/tuse strict; use warnings; use Test::More tests => 15; use Time::Period; use POSIX; my $base_date = POSIX::mktime(0, 0, 0, 1, 0, 111); # 01/01/2011 00:00:00 (Saturday) is(inPeriod($base_date, 'sec {0}'), 1, 'should match a single second'); is(inPeriod($base_date, 'second {0}'), 1, 'should match a single second, by the long form'); is(inPeriod($base_date - 1, 'sec {0}'), 0, 'should be able to not match a single second'); is(inPeriod($base_date + 1, 'sec {0-3}'), 1, 'should be able to match a range of seconds'); is(inPeriod($base_date - 1, 'sec {0-3}'), 0, 'should be able to not match a range of seconds'); is(inPeriod($base_date, 'sec {59-5}'), 1, 'should be able to match a range of days when the first second is greater than the second'); is(inPeriod($base_date - 1, 'sec {59-5}'), 1, 'should be able to match a range of days when the first second is greater than the second'); is(inPeriod($base_date - 20, 'sec {59-5}'), 0, 'should be able to not match a range of days when the first second is greater than the second'); is(inPeriod($base_date + 5, 'sec {1-2}'), 0, 'should be able to not match a range of days when the first second is less than the second'); is(inPeriod(0, 'sec {one}'), -1, 'should return -1 for non-numeric seconds (single)'); is(inPeriod(0, 'sec {one - 3}'), -1, 'should return -1 for non-numeric seconds (left)'); is(inPeriod(0, 'sec {3-one}'), -1, 'should return -1 for non-numeric seconds (right)'); is(inPeriod(0, 'sec {60}'), -1, 'should return -1 for seconds greater than 59 (single)'); is(inPeriod(0, 'sec {60-1}'), -1, 'should return -1 for seconds greater than 59 (left)'); is(inPeriod(0, 'sec {1-60}'), -1, 'should return -1 for seconds greater than 59 (right)'); 09_minute.t100644001750000144 335412452140144 15540 0ustar00pboydusers000000000000Time-Period-1.25/tuse strict; use warnings; use Test::More tests => 15; use Time::Period; use POSIX; my $base_date = POSIX::mktime(0, 0, 0, 1, 0, 111); # 01/01/2011 00:00:00 (Saturday) my $minute = 60; is(inPeriod($base_date, 'min {0}'), 1, 'should match a single minute'); is(inPeriod($base_date, 'minute {0}'), 1, 'should match a single minute, by the long form'); is(inPeriod($base_date - $minute, 'min {0}'), 0, 'should be able to not match a single minute'); is(inPeriod($base_date + $minute, 'min {0-3}'), 1, 'should be able to match a range of minutes'); is(inPeriod($base_date - $minute, 'min {0-3}'), 0, 'should be able to not match a range of minutes'); is(inPeriod($base_date, 'min {59-5}'), 1, 'should be able to match a range of days when the first minute is greater than the second'); is(inPeriod($base_date - $minute, 'min {59-5}'), 1, 'should be able to match a range of days when the first minute is greater than the second'); is(inPeriod($base_date - $minute * 20, 'min {59-5}'), 0, 'should be able to not match a range of days when the first minute is greater than the second'); is(inPeriod($base_date + $minute * 5, 'min {1-2}'), 0, 'should be able to not match a range of days when the first minute is less than the second'); is(inPeriod(0, 'min {one}'), -1, 'should return -1 for non-numeric minutes (single)'); is(inPeriod(0, 'min {one - 3}'), -1, 'should return -1 for non-numeric minutes (left)'); is(inPeriod(0, 'min {3-one}'), -1, 'should return -1 for non-numeric minutes (right)'); is(inPeriod(0, 'min {60}'), -1, 'should return -1 for minutes greater than 59 (single)'); is(inPeriod(0, 'min {60-1}'), -1, 'should return -1 for minutes greater than 59 (left)'); is(inPeriod(0, 'min {1-60}'), -1, 'should return -1 for minutes greater than 59 (right)'); 02_weekday.t100644001750000144 543012452140144 15656 0ustar00pboydusers000000000000Time-Period-1.25/tuse strict; use warnings; use Test::More tests => 27; use Time::Period; use POSIX; my $base_date = POSIX::mktime(0, 0, 0, 1, 0, 111); # 01/01/2011 00:00:00 (Saturday) my $day = 24 * 60 * 60; is(inPeriod($base_date, 'wd {sa}'), 1, 'should match a single day'); is(inPeriod($base_date + $day, 'wd {sa}'), 0, 'should be able to not match a single day'); is(inPeriod($base_date, 'wd {fri-sun}'), 1, 'should be able to match a range of days'); is(inPeriod($base_date, 'wd {fri-sun}'), 1, 'should be able to not match a range of days'); is(inPeriod($base_date - $day, 'wd {sa-mon}'), 0, 'should be able to not match a range when the first weekday comes after the second'); is(inPeriod($base_date - $day, 'wd {sa-fr}'), 1, 'should "wrap around" to match ranges'); is(inPeriod($base_date - $day * 2, 'wd {fri-sat}'), 0, 'should be able to not match a range when the first weekday comes before the second'); is(inPeriod($base_date, 'wd {mon-fri}'), 0, 'should be able to not match a range when the first weekday comes before the second'); is(inPeriod($base_date, 'wd {saturday}'), 1, 'should allow long day names (single)'); is(inPeriod($base_date, 'wd {foobar}'), -1, 'should return -1 for invalid day names (single)'); is(inPeriod($base_date, 'wd {7}'), 1, 'should allow numeric days (single)'); is(inPeriod($base_date, 'wd {17}'), -1, 'should return -1 for numeric days greater than 7 (single)'); is(inPeriod($base_date, 'wd {0}'), -1, 'should return -1 for numeric days less than 1 (single)'); is(inPeriod($base_date, 'wd {_}'), -1, 'should return -1 for non-alphanumeric days (single)'); is(inPeriod($base_date, 'wd {saturday - su}'), 1, 'should allow long day names (left)'); is(inPeriod($base_date, 'wd {foobar - su}'), -1, 'should return -1 for invalid day names (left)'); is(inPeriod($base_date, 'wd {7 - su}'), 1, 'should allow numeric days (left)'); is(inPeriod($base_date, 'wd {17 - su}'), -1, 'should return -1 for numeric days greater than 7 (left)'); is(inPeriod($base_date, 'wd {0 - su}'), -1, 'should return -1 for numeric days less than 1 (left)'); is(inPeriod($base_date, 'wd {_ - su}'), -1, 'should return -1 for non-alphanumeric days (left)'); is(inPeriod($base_date, 'wd {fr - saturday}'), 1, 'should allow long day names (right)'); is(inPeriod($base_date, 'wd {fr - foobar}'), -1, 'should return -1 for invalid day names (right)'); is(inPeriod($base_date, 'wd {6 - 7}'), 1, 'should allow numeric days (right)'); is(inPeriod($base_date, 'wd {6 - 17}'), -1, 'should return -1 for numeric days greater than 7 (right)'); is(inPeriod($base_date, 'wd {1 - 0}'), -1, 'should return -1 for numeric days less than 1 (right)'); is(inPeriod($base_date, 'wd {6 - _}'), -1, 'should return -1 for non-alphanumeric days (right)'); my $sunday = $base_date + $day; is(inPeriod($sunday, 'wd {sunday}'), 1, 'should work for Sundays'); 06_year_day.t100644001750000144 425212452140144 16027 0ustar00pboydusers000000000000Time-Period-1.25/tuse strict; use warnings; use Test::More tests => 19; use Time::Period; use POSIX; my $base_date = POSIX::mktime(0, 0, 0, 1, 0, 111); # 01/01/2011 00:00:00 (Saturday) my $day = 24 * 60 * 60; is(inPeriod($base_date, 'yd {1}'), 1, 'should match a single day'); is(inPeriod($base_date, 'yday {1}'), 1, 'should match a single day, by the long form'); is(inPeriod($base_date - $day, 'yd {1}'), 0, 'should be able to not match a single day'); is(inPeriod($base_date + $day, 'yd {1-3}'), 1, 'should be able to match a range of days'); is(inPeriod($base_date - $day, 'yd {1-3}'), 0, 'should be able to not match a range of days'); is(inPeriod($base_date, 'yd {365-5}'), 1, 'should be able to match a range of days when the first year day is greater than the second'); is(inPeriod($base_date - $day, 'yd {365-5}'), 1, 'should be able to match a range of days when the first year day is greater than the second'); is(inPeriod($base_date - $day * 20, 'yd {365-5}'), 0, 'should be able to not match a range of days when the first year day is greater than the second'); is(inPeriod($base_date + $day * 5, 'yd {1-2}'), 0, 'should be able to not match a range of days when the first year day is less than the second'); is(inPeriod(0, 'yd {one}'), -1, 'should return -1 for non-numeric day numbers (single)'); is(inPeriod(0, 'yd {one - 3}'), -1, 'should return -1 for non-numeric day numbers (left)'); is(inPeriod(0, 'yd {3-one}'), -1, 'should return -1 for non-numeric day numbers (right)'); is(inPeriod(0, 'yd {0}'), -1, 'should return -1 for day numbers less than 1 (single)'); is(inPeriod(0, 'yd {0-3}'), -1, 'should return -1 for day numbers less than 1 (left)'); is(inPeriod(0, 'yd {3-0}'), -1, 'should return -1 for day numbers less than 1 (right)'); is(inPeriod(0, 'yd {367}'), -1, 'should return -1 for day numbers greater than 366 (single)'); is(inPeriod(0, 'yd {367-1}'), -1, 'should return -1 for day numbers greater than 366 (left)'); is(inPeriod(0, 'yd {1-367}'), -1, 'should return -1 for day numbers greater than 366 (right)'); # Dec 31 00:00:00 2012 my $last_day = POSIX::mktime(0, 0, 0, 31, 11, 112); is(inPeriod($last_day, 'yd {366}'), 1, 'should be able to match the last day of the year on leap year'); 07_month_day.t100644001750000144 376012452140144 16220 0ustar00pboydusers000000000000Time-Period-1.25/tuse strict; use warnings; use Test::More tests => 18; use Time::Period; use POSIX; my $base_date = POSIX::mktime(0, 0, 0, 1, 0, 111); # 01/01/2011 00:00:00 (Saturday) my $day = 24 * 60 * 60; is(inPeriod($base_date, 'md {1}'), 1, 'should match a single day'); is(inPeriod($base_date, 'mday {1}'), 1, 'should match a single day, by the long form'); is(inPeriod($base_date - $day, 'md {1}'), 0, 'should be able to not match a single day'); is(inPeriod($base_date + $day, 'md {1-3}'), 1, 'should be able to match a range of days'); is(inPeriod($base_date - $day, 'md {1-3}'), 0, 'should be able to not match a range of days'); is(inPeriod($base_date, 'md {31-5}'), 1, 'should be able to match a range of days when the first month day is greater than the second'); is(inPeriod($base_date - $day, 'md {31-5}'), 1, 'should be able to match a range of days when the first month day is greater than the second'); is(inPeriod($base_date - $day * 20, 'md {31-5}'), 0, 'should be able to not match a range of days when the first month day is greater than the second'); is(inPeriod($base_date + $day * 5, 'md {1-2}'), 0, 'should be able to not match a range of days when the first month day is less than the second'); is(inPeriod(0, 'md {one}'), -1, 'should return -1 for non-numeric day numbers (single)'); is(inPeriod(0, 'md {one - 3}'), -1, 'should return -1 for non-numeric day numbers (left)'); is(inPeriod(0, 'md {3-one}'), -1, 'should return -1 for non-numeric day numbers (right)'); is(inPeriod(0, 'md {0}'), -1, 'should return -1 for day numbers less than 1 (single)'); is(inPeriod(0, 'md {0-3}'), -1, 'should return -1 for day numbers less than 1 (left)'); is(inPeriod(0, 'md {3-0}'), -1, 'should return -1 for day numbers less than 1 (right)'); is(inPeriod(0, 'md {32}'), -1, 'should return -1 for day numbers greater than 31 (single)'); is(inPeriod(0, 'md {32-1}'), -1, 'should return -1 for day numbers greater than 31 (left)'); is(inPeriod(0, 'md {1-32}'), -1, 'should return -1 for day numbers greater than 31 (right)'); Time000755001750000144 012452140144 14576 5ustar00pboydusers000000000000Time-Period-1.25/libPeriod.pm100644001750000144 5464312452140144 16552 0ustar00pboydusers000000000000Time-Period-1.25/lib/Time=head1 NAME Time::Period - A Perl module to deal with time periods. =head1 SYNOPSIS C C<$result = inPeriod($time, $period);> =head1 DESCRIPTION The B function determines if a given time falls within a given period. B returns B<1> if the time does fall within the given period, B<0> if not, and B<-1> if B detects a malformed time or period. The time is specified as per the C function, which is assumed to be the number of non-leap seconds since January 1, 1970. The period is specified as a string which adheres to the format sub-period[, sub-period...] or the string "none" or whitespace. The string "none" is not case sensitive. If the period is blank, then any time period is assumed because the time period has not been restricted. In that case, B returns 1. If the period is "none", then no time period applies and B returns 0. A sub-period is of the form scale {range [range ...]} [scale {range [range ...]}] Scale must be one of nine different scales (or their equivalent codes): Scale | Scale | Valid Range Values | Code | *******|*******|************************************************ year | yr | n where n is an integer 0<=n<=99 or n>=1970 month | mo | 1-12 or jan, feb, mar, apr, may, jun, jul, | | aug, sep, oct, nov, dec week | wk | 1-6 yday | yd | 1-366 mday | md | 1-31 wday | wd | 1-7 or su, mo, tu, we, th, fr, sa hour | hr | 0-23 or 12am 1am-11am 12noon 12pm 1pm-11pm minute | min | 0-59 second | sec | 0-59 The same scale type may be specified multiple times. Additional scales simply extend the range defined by previous scales of the same type. The range for a given scale must be a valid value in the form of v or v-v For the range specification v-v, if the first value is larger than the second value (e.g. "min {20-10}"), the range wraps around unless the scale specification is year. Year does not wrap because the year is never really reset, it just increments. Ignoring that fact has lead to the dreaded year 2000 nightmare. When the year rolls over from 99 to 00, it has really rolled over a century, not gone back a century. B supports the dangerous two digit year notation because it is so rampant. However, B converts the two digit notation to four digits by prepending the first two digits from the current year. In the case of 99-1972, the 99 is translated to whatever current century it is (probably 20th), and then range 99-1972 is treated as 1972-1999. If it were the 21st century, then the range would be 1972-2099. Anyway, if v-v is 9-2 and the scale is month, September, October, November, December, January, and February are the months that the range specifies. If v-v is 2-9, then the valid months are February, March, April, May, Jun, July, August, and September. 9-2 is the same as Sep-Feb. v isn't a point in time. In the context of the hour scale, 9 specifies the time period from 9:00:00 am to 9:59:59 am. This is what most people would call 9-10. In other words, v is discrete in its time scale. 9 changes to 10 when 9:59:59 changes to 10:00:00, but it is 9 from 9:00:00 to 9:59:59. Just before 9:00:00, v was 8. Note that whitespace can be anywhere and case is not important. Note also that scales must be specified either in long form (year, month, week, etc.) or in code form (yr, mo, wk, etc.). Scale forms may be mixed in a period statement. Furthermore, when using letters to specify ranges, only the first two for week days or the first three for months are significant. January is a valid specification for jan, and Sunday is a valid specification for su. Sun is also valid for su. =head2 PERIOD EXAMPLES To specify a time period from Monday through Friday, 9am to 5pm, use a period such as wd {Mon-Fri} hr {9am-4pm} When specifing a range by using -, it is best to think of - as meaning through. It is 9am through 4pm, which is just before 5pm. To specify a time period from Monday through Friday, 9am to 5pm on Monday, Wednesday, and Friday, and 9am to 3pm on Tuesday and Thursday, use a period such as wd {Mon Wed Fri} hr {9am-4pm}, wd{Tue Thu} hr {9am-2pm} To specify a time period that extends Mon-Fri 9am-5pm, but alternates weeks in a month, use a period such as wk {1 3 5} wd {Mon Wed Fri} hr {9am-4pm} Or how about a period that specifies winter? mo {Nov-Feb} This is equivalent to the previous example: mo {Jan-Feb Nov-Dec} As is mo {jan feb nov dec} And this is too: mo {Jan Feb}, mo {Nov Dec} Wait! So is this: mo {Jan Feb} mo {Nov Dec} To specify a period that describes every other half-hour, use something like minute { 0-29 } To specify the morning, use hour { 12am-11am } Remember, 11am is not 11:00:00am, but rather 11:00:00am - 11:59:59am. Hmmmm, 5 second blocks could be a fun period... sec {0-4 10-14 20-24 30-34 40-44 50-54} To specify every first half-hour on alternating week days, and the second half-hour the rest of the week, use the period wd {1 3 5 7} min {0-29}, wd {2 4 6} min {30-59} =head1 VERSION 1.25 =head1 HISTORY Version 1.25 ------------ - Fixed a bug with matching week on Sundays (https://rt.cpan.org/Public/Bug/Display.html?id=100850) Version 1.24 ------------ - Minor doc update. Version 1.23 ------------ - Bug fixes: - Validate min and max for right side of hour ranges (e.g. hr { 20-25 } now correctly returns -1) - Range for yd is now 1 to 366 - Years are no longer considered to be 365 days long for calculating a 4-digit year. Version 1.22 ------------ - Fixed tests Version 1.21 ------------ - Bug fix: Stopped using $' and $`. Version 1.20 ------------ - Added the ability to specify no time period. Version 1.13 ------------ - Cleaned up the error checking code. Version 1.12 ------------ - Updated email and web space information. Version 1.11 ------------ - Minor bug fix in 1.10. Version 1.10 ------------ - Released. =head1 AUTHOR Patrick Ryan wrote it. Paul Boyd fixed a few bugs. =head1 COPYRIGHT Copyright (c) 1997 Patrick Ryan. All rights reserved. This Perl module uses the conditions given by Perl. This module may only be distributed and or modified under the conditions given by Perl. =cut package Time::Period; require 5.001; require Exporter; @ISA = qw(Exporter); @EXPORT = qw(inPeriod); $VERSION = "1.25"; sub inPeriod { my($time, $period) = @_[0,1]; my(%scaleCode, %scaleCodeV, $result, $i, $lb, $rb, @subPeriods, $subPeriod, @scales, %scaleResults, $rangeData, @ranges, $range, $v1, $v2); local($scale, $yr, $mo, $wk, $yd, $md, $wd, $hr, $min, $sec); # $scale, $yr, $mo, $wk, $yd, $md, $wd, $hr, $min, and $sec are declared # with local() because they are referenced symbolically. # Test $period and $time for validity. Return -1 if $time contains # non-numbers or is null. Return 1 if $time is numeric but $period is all # whitespace. No period means all times are within the period because # period is not restricted. Return 0 if $period is "none". Also make # $period all lowercase. $time =~ s/^\s*(.*)/$1/; $time =~ s/\s*$//; return -1 if ( ($time =~ /\D/) || ($time eq "") ); return 1 if (!defined($period)); $period =~ s/^\s*(.*)/$1/; $period =~ s/\s*$//; $period = lc($period); return 1 if ($period eq ""); return 0 if ($period eq "none"); # Thise two associative arrays are used to map and validate scales. %scaleCode = ('year' => 'yr', 'month' => 'mo', 'week' => 'wk', 'mday' => 'md', 'wday' => 'wd', 'yday' => 'yd', 'hour' => 'hr', 'minute' => 'min', 'second' => 'sec'); %scaleCodeV = ('yr' => 1, 'mo' => 1, 'wk' => 1, 'md' => 1, 'wd' => 1, 'yd' => 1, 'hr' => 1, 'min' => 1, 'sec' => 1); # The names of these variables must correlate with the scale codes. ($yr, $mo, $wk, $yd, $md, $wd, $hr, $min, $sec) = getTimeVars($time); # The first step is to break $period up into all its sub periods. @subPeriods = split(/\s*,\s*/, $period); # Evaluate each sub-period to see if $time falls within it. If it does # then return 1, if $time does not fall within any of the sub-periods, # return 0. foreach $subPeriod (@subPeriods) { # Do a validity check for braces. Make sure the number of {s equals the # number of }s. If there aren't any, return -1 as well. $lb = $subPeriod =~ tr/{//; $rb = $subPeriod =~ tr/}//; return -1 if ( ($lb != $rb) || ($lb == 0) ); @scales = split(/}\s*/, $subPeriod); # Make sure that the number of {s are equal to the number of scales # found. If it is not, return -1. return -1 if ($lb != @scales); # Evaluate each scale, one by one, in the sub-period. Once this # completes, there will be a hash called %scaleResults which will contain # boolean values. The key to this hash will be the code version of # each scale in @scales, if it was a valid scale. If an invalid string # is found, -1 will be returned. The boolean value will indicate # whether $time falls within the particular scale in question. foreach $scale (@scales) { return -1 if ($scale !~ /^([a-z]*)\s*{\s*(.*)/); $scale = $1; $rangeData = $2; # Check to see if $scale is a valid scale. If it is, make sure # it is in code form. # Is it possibly the long form? if (length($scale) > 3) { # If it doesn't map to a code... return -1 if (!defined($scaleCode{$scale})); $scale = $scaleCode{$scale}; # Okay, it's not longer than 3 characters, is it 2 or 3 characters long? } elsif (length($scale) > 1) { # Is it an invalid code? return -1 if (!defined($scaleCodeV{$scale})); # It must be zero or one character long, which is an invalid scale. } else { return -1; } # $scale is a valid scale and it is now in code form. # Erase any whitespace between any "v - v"s so that they become "v-v". $rangeData =~ s/(\w+)\s*-\s*(\w+)/$1-$2/g; @ranges = split(/\s+/, $rangeData); $scaleResults{$scale} = 0 if (!defined($scaleResults{$scale})); # Alright, $range is one of the ranges (could be the only one) for # $scale. If $range is valid within the context of $scale and $time, # set $scaleResults{$scale} to 1. foreach $range (@ranges) { if ($range =~ /(.*)-(.*)/) { $v1 = $1; $v2 = $2; return -1 if ($v1 !~ /\w/ || $v2 !~ /\w/); } else { return -1 if ($range !~ /\w/); } # This line calls the function named by $scale and feeds it the # variable $range and the variable named by $scale. $result = &$scale($range, $$scale); return -1 if ($result == -1); $scaleResults{$scale} = 1 if ($result == 1); } } # Now, there is a boolean value associated with each scale. If every # scale is 1, then $time falls within this sub-period, which means $time # falls within the period, so return 1. If that condition isn't met, # then the loop will test other sub-periods, if they exist, and return 0 # if none of them cover $time. $i = 1; foreach $scale (keys %scaleResults) { $i = 0 if ($scaleResults{$scale} == 0); } # This is a sub-period where the time falls into all of the scales # specified. return 1 if ($i == 1); # Reset scale for a new sub-period. %scaleResults = (); } # $time didn't fall into any of the sub-periods. :( return 0; } sub getTimeVars { # This function takes $time (seconds past 0000 Jan 1, 1970) and returns # it in component form. Specifically, this function returns # ($year, $month, $week, $yday, $mday, $wday, $hour, $minute, $second). my($time) = $_[0]; my($sec, $min, $hr, $md, $mo, $yr, $wd, $yd, @pwd, @wd, $wk, $i); # Now, break $time into $yr, $mo, $wk, $md, $wd, $yd, $hr, $min, and $sec. ($sec, $min, $hr, $md, $mo, $yr, $wd, $yd) = localtime($time); # The assumption for the ranges from localtime are # Year ($yr) = 0-99 # Month ($mo) = 0-11 # Year Day ($yd) = 0-365 # Month Day ($md) = 1-31 # Week Day ($wd) = 0-6 # Hour ($hr) = 0-23 # Minute ($min) = 0-59 # Second ($sec) = 0-59 # Calculate the full year (yyyy). $yr += 1900; # Figure out which week $time is in ($wk) so that $wk goes from 0-5. # Set up an array where a week day maps to the previous week day. @pwd = (6, 0, 1, 2, 3, 4, 5); # Define an array @wd from 1 to $md that maps $md to its corresponding # day of the week. $wd[$md] = $wd; for ($i = $md - 1; $i >= 0; $i--) { $wd[$i] = $pwd[$wd[$i+1]]; } # Calculate which week it is. $wk = 0; for ($i = 1; $i <= $md; $i++) { # Itterate $i from 1 to $md. If $i happens to land on a Sunday, # increment $wk unless $i is also 1, which means its still week 0. if ( $wd[$i] == 0 && $i != 1 ) { $wk++; } } return ($yr, $mo, $wk, $yd, $md, $wd, $hr, $min, $sec); } sub yr { # A function to determine if a given range is within a given year. # Returns 1 if it is, 0 if not, and -1 if the supplied range is invalid. my($range, $yr) = @_[0,1]; my($v1, $v2); if ($range =~ /(.*)-(.*)/) { $v1 = $1; $v2 = $2; return -1 if ( ($v1 =~ /\D/) || ($v2 =~ /\D/) ); return -1 if ( ($v1 < 0) || ($v2 < 0) ); return -1 if ( ($v1 > 99) && ($v1 < 1970) ); return -1 if ( ($v2 > 99) && ($v2 < 1970) ); $v1 = (100 * substr($yr, 0, 2) + $v1) if ($v1 <= 99); $v2 = (100 * substr($yr, 0, 2) + $v2) if ($v2 <= 99); if ($v1 > $v2) { $i = $v2; $v2 = $v1; $v1 = $i; } return 1 if ( ($v1 <= $yr) && ($yr <= $v2) ); } else { return -1 if ( ($range =~ /\D/) || ($range < 0) || ( ($range > 99) && ($range < 1970) ) ); $range = (100 * substr($yr, 0, 2) + $range) if ($range <= 99); return 1 if ($range == $yr); } return 0; } sub mo { # A function to determine if a given range is within a given month. # Returns 1 if it is, 0 if not, and -1 if the supplied range is invalid. my($range, $mo) = @_[0,1]; my(%mo, %moV, $v1, $v2); # These associative arrays are used to validate months and to map the # letter designations to their numeric equivalents. %mo = ('jan' => 0, 'feb' => 1, 'mar' => 2, 'apr' => 3, 'may' => 4, 'jun' => 5, 'jul' => 6, 'aug' => 7, 'sep' => 8, 'oct' => 9, 'nov' => 10, 'dec' => 11); %moV = ('jan' => 1, 'feb' => 1, 'mar' => 1, 'apr' => 1, 'may' => 1, 'jun' => 1, 'jul' => 1, 'aug' => 1, 'sep' => 1, 'oct' => 1, 'nov' => 1, 'dec' => 1); if ($range =~ /(.*)-(.*)/) { $v1 = $1; $v2 = $2; if ($v1 =~ /[a-z]/) { $v1 = substr($v1, 0, 3); return -1 if (!defined($moV{$v1})); $v1 = $mo{$v1}; } elsif ($v1 =~ /\D/) { return -1; } else { $v1--; return -1 if ( ($v1 < 0) || ($v1 > 11) ); } if ($v2 =~ /[a-z]/) { $v2 = substr($v2, 0, 3); return -1 if (!defined($moV{$v2})); $v2 = $mo{$v2}; } elsif ($v2 =~ /\D/) { return -1; } else { $v2--; return -1 if ( ($v2 < 0) || ($v2 > 11) ); } if ($v1 > $v2) { return 1 if ( ($v1 <= $mo) || ($v2 >= $mo) ); } else { return 1 if ( ($v1 <= $mo) && ($mo <= $v2) ); } } else { if ($range =~ /[a-z]/) { $range = substr($range, 0, 3); return -1 if (!defined($moV{$range})); $range = $mo{$range}; } elsif ($range =~ /\D/) { return -1; } else { $range--; return -1 if ( ($range < 0) || ($range > 11) ); } return 1 if ($range == $mo); } return 0; } sub wk { # A function to determine if a given range is within a given week. # Returns 1 if it is, 0 if not, and -1 if the supplied range is invalid. my($range, $wk) = @_[0,1]; my($v1, $v2); if ($range =~ /(.*)-(.*)/) { $v1 = $1; $v2 = $2; return -1 if ( ($v1 =~ /\D/) || ($v2 =~ /\D/) ); $v1--; $v2--; return -1 if ( ($v1 < 0) || ($v1 > 5) ); return -1 if ( ($v2 < 0) || ($v2 > 5) ); if ($v1 > $v2) { return 1 if ( ($v1 <= $wk) || ($v2 >= $wk) ); } else { return 1 if ( ($v1 <= $wk) && ($wk <= $v2) ); } } else { return -1 if ($range =~ /\D/); $range--; return -1 if ( ($range < 0) || ($range > 5) ); return 1 if ($range == $wk); } return 0; } sub yd { # A function to determine if a given range is within a given day of the # year. Returns 1 if it is, 0 if not, and -1 if the supplied range is # invalid. my($range, $yd) = @_[0,1]; my($v1, $v2); if ($range =~ /(.*)-(.*)/) { $v1 = $1; $v2 = $2; return -1 if ( ($v1 =~ /\D/) || ($v2 =~ /\D/) ); $v1--; $v2--; return -1 if ( ($v1 < 0) || ($v1 > 365) ); return -1 if ( ($v2 < 0) || ($v2 > 365) ); if ($v1 > $v2) { return 1 if ( ($v1 <= $yd) || ($v2 >= $yd) ); } else { return 1 if ( ($v1 <= $yd) && ($yd <= $v2) ); } } else { $range--; return -1 if (($range =~ /\D/) || ($range < 0) || ($range > 365)); return 1 if ($range == $yd); } return 0; } sub md { # A function to determine if a given range is within a given day of the # month. Returns 1 if it is, 0 if not, and -1 if the supplied range is # invalid. my($range, $md) = @_[0,1]; my($v1, $v2); if ($range =~ /(.*)-(.*)/) { $v1 = $1; $v2 = $2; return -1 if ( ($v1 =~ /\D/) || ($v2 =~ /\D/) ); return -1 if ( ($v1 < 1) || ($v1 > 31) ); return -1 if ( ($v2 < 1) || ($v2 > 31) ); if ($v1 > $v2) { return 1 if ( ($v1 <= $md) || ($v2 >= $md) ); } else { return 1 if ( ($v1 <= $md) && ($md <= $v2) ); } } else { return -1 if (($range =~ /\D/) || ($range < 1) || ($range > 31)); return 1 if ($range == $md); } return 0; } sub wd { # A function to determine if a given range is within a given day of the # week. Returns 1 if it is, 0 if not, and -1 if the supplied range is # invalid. my($range, $wd) = @_[0,1]; my(%wd, %wdV, $v1, $v2); # These associative arrays are used to validate week days and to map the # letter designations to their numeric equivalents. %wd = ('su' => 0, 'mo' => 1, 'tu' => 2, 'we' => 3, 'th' => 4, 'fr' => 5, 'sa' => 6); %wdV = ('su' => 1, 'mo' => 1, 'tu' => 1, 'we' => 1, 'th' => 1, 'fr' => 1, 'sa' => 1); if ($range =~ /(.*)-(.*)/) { $v1 = $1; $v2 = $2; if ($v1 =~ /[a-z]/) { $v1 = substr($v1, 0, 2); return -1 if (!defined($wdV{$v1})); $v1 = $wd{$v1}; } elsif ($v1 =~ /\D/) { return -1; } else { $v1--; return -1 if ( ($v1 < 0) || ($v1 > 6) ); } if ($v2 =~ /[a-z]/) { $v2 = substr($v2, 0, 2); return -1 if (!defined($wdV{$v2})); $v2 = $wd{$v2}; } elsif ($v2 =~ /\D/) { return -1; } else { $v2--; return -1 if ( ($v2 < 0) || ($v2 > 6) ); } if ($v1 > $v2) { return 1 if ( ($v1 <= $wd) || ($v2 >= $wd) ); } else { return 1 if ( ($v1 <= $wd) && ($wd <= $v2) ); } } else { if ($range =~ /[a-z]/) { $range = substr($range, 0, 2); return -1 if (!defined($wdV{$range})); $range = $wd{$range}; } elsif ($range =~ /\D/) { return -1; } else { $range--; return -1 if ( ($range < 0) || ($range > 6) ); } return 1 if ($range == $wd); } return 0; } sub hr { # A function to determine if a given range is within a given hour. # Returns 1 if it is, 0 if not, and -1 if the supplied range is invalid. my($range, $hr) = @_[0,1]; my($v1, $v2); if ($range =~ /(.*)-(.*)/) { $v1 = $1; $v2 = $2; if ($v1 =~ /^(\d+)am$/) { if ($1 == 12) { $v1 = 0; } else { $v1 = $1; } } elsif ($v1 =~ /^(\d+)pm$/) { if ($1 == 12) { $v1 = $1; } else { $v1 = $1+12; } } elsif ($v1 =~ /^(\d+)noon$/) { return -1 if ($1 != 12); $v1 = $1; } if ($v2 =~ /^(\d+)am$/) { if ($1 == 12) { $v2 = 0; } else { $v2 = $1; } } elsif ($v2 =~ /^(\d+)pm$/) { if ($1 == 12) { $v2 = $1; } else { $v2 = $1+12; } } elsif ($v2 =~ /^(\d+)noon$/) { return -1 if ($1 != 12); $v2 = $1; } return -1 if ( ($v1 =~ /\D/) || ($v1 < 0) || ($v1 > 23) ); return -1 if ( ($v2 =~ /\D/) || ($v2 < 0) || ($v2 > 23) ); if ($v1 > $v2) { return 1 if ( ($v1 <= $hr) || ($v2 >= $hr) ); } else { return 1 if ( ($v1 <= $hr) && ($hr <= $v2) ); } } else { if ($range =~ /^(\d+)am$/) { if ($1 == 12) { $range = 0; } else { $range = $1; } } elsif ($range =~ /^(\d+)pm$/) { if ($1 == 12) { $range = $1; } else { $range = $1+12; } } elsif ($range =~ /^(\d+)noon$/) { return -1 if ($1 != 12); $range = $1; } return -1 if (($range =~ /\D/) || ($range < 0) || ($range > 23)); return 1 if ($range == $hr); } return 0; } sub min { # A function to determine if a given range is within a given minute. # Returns 1 if it is, 0 if not, and -1 if the supplied range is invalid. my($range, $min) = @_[0,1]; my($v1, $v2); if ($range =~ /(.*)-(.*)/) { $v1 = $1; $v2 = $2; return -1 if ( ($v1 =~ /\D/) || ($v2 =~ /\D/) ); return -1 if ( ($v1 < 0) || ($v1 > 59) ); return -1 if ( ($v2 < 0) || ($v2 > 59) ); if ($v1 > $v2) { return 1 if ( ($v1 <= $min) || ($v2 >= $min) ); } else { return 1 if ( ($v1 <= $min) && ($min <= $v2) ); } } else { return -1 if (($range =~ /\D/) || ($range < 0) || ($range > 59)); return 1 if ($range == $min); } return 0; } sub sec { # A function to determine if a given range is within a given second. # Returns 1 if it is, 0 if not, and -1 if the supplied range is invalid. my($range, $sec) = @_[0,1]; my($v1, $v2); if ($range =~ /(.*)-(.*)/) { $v1 = $1; $v2 = $2; return -1 if ( ($v1 =~ /\D/) || ($v2 =~ /\D/) ); return -1 if ( ($v1 < 0) || ($v1 > 59) ); return -1 if ( ($v2 < 0) || ($v2 > 59) ); if ($v1 > $v2) { return 1 if ( ($v1 <= $sec) || ($v2 >= $sec) ); } else { return 1 if ( ($v1 <= $sec) && ($sec <= $v2) ); } } else { return -1 if (($range =~ /\D/) || ($range < 0) || ($range > 59)); return 1 if ($range == $sec); } return 0; } 1; 01_time_period.t100644001750000144 374612452140144 16534 0ustar00pboydusers000000000000Time-Period-1.25/tuse strict; use warnings; use Test::More tests => 23; BEGIN { use_ok('Time::Period') }; can_ok(__PACKAGE__, 'inPeriod'); use POSIX; my $base_date = POSIX::mktime(0, 0, 0, 1, 0, 111); # 01/01/2011 00:00:00 (Saturday) my $day = 24 * 60 * 60; is(inPeriod($base_date, 'wd {sa}'), 1, 'returns 1 for a match'); is(inPeriod($base_date, 'wd {fri-sun}'), 1, 'should be able to match ranges'); is(inPeriod($base_date + $day, 'wd {sa}'), 0, 'returns 0 for a non-match'); is(inPeriod('', 'wd {sa}'), -1, 'returns -1 when an empty string is passed for the time'); is(inPeriod('a', 'wd {sa}'), -1, 'returns -1 when time contains something other than a number'); is(inPeriod(0), 1, 'should always match an undefined period'); is(inPeriod(0, ''), 1, 'should always match an empty period'); is(inPeriod(0, 'none'), 0, 'should never match the period "none"'); is(inPeriod(0, 'wd {'), -1, 'should return -1 if there are more left braces than right ones'); is(inPeriod(0, 'wd }'), -1, 'should return -1 if there are more right braces than left ones'); is(inPeriod(0, 'wd'), -1, 'should return -1 if there aren\'t any braces'); is(inPeriod(0, '9{}'), -1, 'should return -1 for a numeric scale name'); is(inPeriod($base_date, 'wday {sa}'), 1, 'should match long-form names'); is(inPeriod($base_date, 'fooey {sa}'), -1, 'should return -1 for invalid long-form names'); is(inPeriod($base_date, 'fo {sa}'), -1, 'should return -1 for invalid short-form names'); is(inPeriod($base_date, 'f {sa}'), -1, 'should return -1 for single character names'); is(inPeriod(0, 'wd {%}'), -1, 'returns -1 if the range contains a non-alphanumeric character'); is(inPeriod(0, 'wd {%-9}'), -1, 'returns -1 if the range contains a non-alphanumeric character'); is(inPeriod(0, 'wd {9-%}'), -1, 'returns -1 if the range contains a non-alphanumeric character'); is(inPeriod($base_date, 'wd {sa} yr {2011}'), 1, 'should be able to match on multiple criteria'); is(inPeriod($base_date, 'wd {sa} yr {2010}'), 0, 'should be able to not match on multiple criteria');