diff options
| author | Greg Roach <fisharebest@gmail.com> | 2014-09-13 13:26:26 +0100 |
|---|---|---|
| committer | Greg Roach <fisharebest@gmail.com> | 2014-09-13 13:26:26 +0100 |
| commit | 7630cce493bb9b4657907c5ab9da28069c587f5a (patch) | |
| tree | a4ab8b8863f8cef72446d0e849fc9ba4a85432a3 /library/fisharebest/ext-calendar | |
| parent | ed0b0fe23f53f3ab89c14f7029f1683abc969d25 (diff) | |
| download | webtrees-7630cce493bb9b4657907c5ab9da28069c587f5a.tar.gz webtrees-7630cce493bb9b4657907c5ab9da28069c587f5a.tar.bz2 webtrees-7630cce493bb9b4657907c5ab9da28069c587f5a.zip | |
Remove dependency on PHP's ext/calendar extension, and use our own
Diffstat (limited to 'library/fisharebest/ext-calendar')
| -rw-r--r-- | library/fisharebest/ext-calendar/CHANGELOG.md | 20 | ||||
| -rw-r--r-- | library/fisharebest/ext-calendar/LICENSE.md | 677 | ||||
| -rw-r--r-- | library/fisharebest/ext-calendar/README.md | 138 | ||||
| -rw-r--r-- | library/fisharebest/ext-calendar/src/ArabicCalendar.php | 116 | ||||
| -rw-r--r-- | library/fisharebest/ext-calendar/src/Calendar.php | 225 | ||||
| -rw-r--r-- | library/fisharebest/ext-calendar/src/CalendarInterface.php | 29 | ||||
| -rw-r--r-- | library/fisharebest/ext-calendar/src/FrenchCalendar.php | 120 | ||||
| -rw-r--r-- | library/fisharebest/ext-calendar/src/GregorianCalendar.php | 137 | ||||
| -rw-r--r-- | library/fisharebest/ext-calendar/src/JewishCalendar.php | 491 | ||||
| -rw-r--r-- | library/fisharebest/ext-calendar/src/JulianCalendar.php | 179 | ||||
| -rw-r--r-- | library/fisharebest/ext-calendar/src/PersianCalendar.php | 146 | ||||
| -rw-r--r-- | library/fisharebest/ext-calendar/src/Shim.php | 567 | ||||
| -rw-r--r-- | library/fisharebest/ext-calendar/src/shims.php | 120 |
13 files changed, 2965 insertions, 0 deletions
diff --git a/library/fisharebest/ext-calendar/CHANGELOG.md b/library/fisharebest/ext-calendar/CHANGELOG.md new file mode 100644 index 0000000000..ae300d070b --- /dev/null +++ b/library/fisharebest/ext-calendar/CHANGELOG.md @@ -0,0 +1,20 @@ +CHANGE LOG +========== + +## 1.1.0 (2014-09-13) + - Add support for Arabic (Hijri) and Persian (Jalali) calendars. + - Convert logic to lookup tables for better performance. + +## 1.0.3 (2014-09-12) + - Simplify the way shims are created. + +## 1.0.2 (2014-09-11) + - Wrong encoding of Hebrew dates. + - Emulate PHP bug 54254 in Hebrew dates. + +## 1.0.1 (2014-09-11) + - Improve coverage of unit tests. + - Convert logic to lookup tables for better performance. + +## 1.0.0 (2014-09-10) + - Initial release, with support for all the functions and constants from `ext/calendar`. diff --git a/library/fisharebest/ext-calendar/LICENSE.md b/library/fisharebest/ext-calendar/LICENSE.md new file mode 100644 index 0000000000..9662fa46e1 --- /dev/null +++ b/library/fisharebest/ext-calendar/LICENSE.md @@ -0,0 +1,677 @@ +GNU GENERAL PUBLIC LICENSE +========================== + +Version 3, 29 June 2007 + + Copyright © 2007 Free Software Foundation, Inc. <<http://fsf.org/>> + + Everyone is permitted to copy and distribute verbatim copies + of this license document, but changing it is not allowed. + +## Preamble + + The GNU General Public License is a free, copyleft license for +software and other kinds of works. + + The licenses for most software and other practical works are designed +to take away your freedom to share and change the works. By contrast, +the GNU General Public License is intended to guarantee your freedom to +share and change all versions of a program--to make sure it remains free +software for all its users. We, the Free Software Foundation, use the +GNU General Public License for most of our software; it applies also to +any other work released this way by its authors. You can apply it to +your programs, too. + + When we speak of free software, we are referring to freedom, not +price. Our General Public Licenses are designed to make sure that you +have the freedom to distribute copies of free software (and charge for +them if you wish), 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 prevent others from denying you +these rights or asking you to surrender the rights. Therefore, you have +certain responsibilities if you distribute copies of the software, or if +you modify it: responsibilities to respect the freedom of others. + + For example, if you distribute copies of such a program, whether +gratis or for a fee, you must pass on to the recipients the same +freedoms that you received. You must make sure that they, too, receive +or can get the source code. And you must show them these terms so they +know their rights. + + Developers that use the GNU GPL protect your rights with two steps: +(1) assert copyright on the software, and (2) offer you this License +giving you legal permission to copy, distribute and/or modify it. + + For the developers’ and authors’ protection, the GPL clearly explains +that there is no warranty for this free software. For both users’ and +authors’ sake, the GPL requires that modified versions be marked as +changed, so that their problems will not be attributed erroneously to +authors of previous versions. + + Some devices are designed to deny users access to install or run +modified versions of the software inside them, although the manufacturer +can do so. This is fundamentally incompatible with the aim of +protecting users’ freedom to change the software. The systematic +pattern of such abuse occurs in the area of products for individuals to +use, which is precisely where it is most unacceptable. Therefore, we +have designed this version of the GPL to prohibit the practice for those +products. If such problems arise substantially in other domains, we +stand ready to extend this provision to those domains in future versions +of the GPL, as needed to protect the freedom of users. + + Finally, every program is threatened constantly by software patents. +States should not allow patents to restrict development and use of +software on general-purpose computers, but in those that do, we wish to +avoid the special danger that patents applied to a free program could +make it effectively proprietary. To prevent this, the GPL assures that +patents cannot be used to render the program non-free. + + The precise terms and conditions for copying, distribution and +modification follow. + +## TERMS AND CONDITIONS + +### 0. Definitions. + + “This License” refers to version 3 of the GNU General Public License. + + “Copyright” also means copyright-like laws that apply to other kinds of +works, such as semiconductor masks. + + “The Program” refers to any copyrightable work licensed under this +License. Each licensee is addressed as “you”. “Licensees” and +“recipients” may be individuals or organizations. + + To “modify” a work means to copy from or adapt all or part of the work +in a fashion requiring copyright permission, other than the making of an +exact copy. The resulting work is called a “modified version” of the +earlier work or a work “based on” the earlier work. + + A “covered work” means either the unmodified Program or a work based +on the Program. + + To “propagate” a work means to do anything with it that, without +permission, would make you directly or secondarily liable for +infringement under applicable copyright law, except executing it on a +computer or modifying a private copy. Propagation includes copying, +distribution (with or without modification), making available to the +public, and in some countries other activities as well. + + To “convey” a work means any kind of propagation that enables other +parties to make or receive copies. Mere interaction with a user through +a computer network, with no transfer of a copy, is not conveying. + + An interactive user interface displays “Appropriate Legal Notices” +to the extent that it includes a convenient and prominently visible +feature that (1) displays an appropriate copyright notice, and (2) +tells the user that there is no warranty for the work (except to the +extent that warranties are provided), that licensees may convey the +work under this License, and how to view a copy of this License. If +the interface presents a list of user commands or options, such as a +menu, a prominent item in the list meets this criterion. + +### 1. Source Code. + + The “source code” for a work means the preferred form of the work +for making modifications to it. “Object code” means any non-source +form of a work. + + A “Standard Interface” means an interface that either is an official +standard defined by a recognized standards body, or, in the case of +interfaces specified for a particular programming language, one that +is widely used among developers working in that language. + + The “System Libraries” of an executable work include anything, other +than the work as a whole, that (a) is included in the normal form of +packaging a Major Component, but which is not part of that Major +Component, and (b) serves only to enable use of the work with that +Major Component, or to implement a Standard Interface for which an +implementation is available to the public in source code form. A +“Major Component”, in this context, means a major essential component +(kernel, window system, and so on) of the specific operating system +(if any) on which the executable work runs, or a compiler used to +produce the work, or an object code interpreter used to run it. + + The “Corresponding Source” for a work in object code form means all +the source code needed to generate, install, and (for an executable +work) run the object code and to modify the work, including scripts to +control those activities. However, it does not include the work’s +System Libraries, or general-purpose tools or generally available free +programs which are used unmodified in performing those activities but +which are not part of the work. For example, Corresponding Source +includes interface definition files associated with source files for +the work, and the source code for shared libraries and dynamically +linked subprograms that the work is specifically designed to require, +such as by intimate data communication or control flow between those +subprograms and other parts of the work. + + The Corresponding Source need not include anything that users +can regenerate automatically from other parts of the Corresponding +Source. + + The Corresponding Source for a work in source code form is that +same work. + +### 2. Basic Permissions. + + All rights granted under this License are granted for the term of +copyright on the Program, and are irrevocable provided the stated +conditions are met. This License explicitly affirms your unlimited +permission to run the unmodified Program. The output from running a +covered work is covered by this License only if the output, given its +content, constitutes a covered work. This License acknowledges your +rights of fair use or other equivalent, as provided by copyright law. + + You may make, run and propagate covered works that you do not +convey, without conditions so long as your license otherwise remains +in force. You may convey covered works to others for the sole purpose +of having them make modifications exclusively for you, or provide you +with facilities for running those works, provided that you comply with +the terms of this License in conveying all material for which you do +not control copyright. Those thus making or running the covered works +for you must do so exclusively on your behalf, under your direction +and control, on terms that prohibit them from making any copies of +your copyrighted material outside their relationship with you. + + Conveying under any other circumstances is permitted solely under +the conditions stated below. Sublicensing is not allowed; section 10 +makes it unnecessary. + +### 3. Protecting Users’ Legal Rights From Anti-Circumvention Law. + + No covered work shall be deemed part of an effective technological +measure under any applicable law fulfilling obligations under article +11 of the WIPO copyright treaty adopted on 20 December 1996, or +similar laws prohibiting or restricting circumvention of such +measures. + + When you convey a covered work, you waive any legal power to forbid +circumvention of technological measures to the extent such circumvention +is effected by exercising rights under this License with respect to +the covered work, and you disclaim any intention to limit operation or +modification of the work as a means of enforcing, against the work’s +users, your or third parties’ legal rights to forbid circumvention of +technological measures. + +### 4. Conveying Verbatim Copies. + +You may convey 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; +keep intact all notices stating that this License and any +non-permissive terms added in accord with section 7 apply to the code; +keep intact all notices of the absence of any warranty; and give all +recipients a copy of this License along with the Program. + + You may charge any price or no price for each copy that you convey, +and you may offer support or warranty protection for a fee. + +### 5. Conveying Modified Source Versions. + + You may convey a work based on the Program, or the modifications to +produce it from the Program, in the form of source code under the +terms of section 4, provided that you also meet all of these conditions: + +* **a)** The work must carry prominent notices stating that you modified + it, and giving a relevant date. + +* **b)** The work must carry prominent notices stating that it is + released under this License and any conditions added under section 7. + This requirement modifies the requirement in section 4 to + “keep intact all notices”. + +* **c)** You must license the entire work, as a whole, under this + License to anyone who comes into possession of a copy. This + License will therefore apply, along with any applicable section 7 + additional terms, to the whole of the work, and all its parts, + regardless of how they are packaged. This License gives no + permission to license the work in any other way, but it does not + invalidate such permission if you have separately received it. + +* **d)** If the work has interactive user interfaces, each must display + Appropriate Legal Notices; however, if the Program has interactive + interfaces that do not display Appropriate Legal Notices, your + work need not make them do so. + + A compilation of a covered work with other separate and independent +works, which are not by their nature extensions of the covered work, +and which are not combined with it such as to form a larger program, +in or on a volume of a storage or distribution medium, is called an +“aggregate” if the compilation and its resulting copyright are not +used to limit the access or legal rights of the compilation’s users +beyond what the individual works permit. Inclusion of a covered work +in an aggregate does not cause this License to apply to the other +parts of the aggregate. + +### 6. Conveying Non-Source Forms. + + You may convey a covered work in object code form under the terms +of sections 4 and 5, provided that you also convey the +machine-readable Corresponding Source under the terms of this License, +in one of these ways: + +* **a)** Convey the object code in, or embodied in, a physical product + (including a physical distribution medium), accompanied by the + Corresponding Source fixed on a durable physical medium + customarily used for software interchange. + +* **b)** Convey the object code in, or embodied in, a physical product + (including a physical distribution medium), accompanied by a + written offer, valid for at least three years and valid for as + long as you offer spare parts or customer support for that product + model, to give anyone who possesses the object code either (1) a + copy of the Corresponding Source for all the software in the + product that is covered by this License, on a durable physical + medium customarily used for software interchange, for a price no + more than your reasonable cost of physically performing this + conveying of source, or (2) access to copy the + Corresponding Source from a network server at no charge. + +* **c)** Convey individual copies of the object code with a copy of the + written offer to provide the Corresponding Source. This + alternative is allowed only occasionally and noncommercially, and + only if you received the object code with such an offer, in accord + with subsection 6b. + +* **d)** Convey the object code by offering access from a designated + place (gratis or for a charge), and offer equivalent access to the + Corresponding Source in the same way through the same place at no + further charge. You need not require recipients to copy the + Corresponding Source along with the object code. If the place to + copy the object code is a network server, the Corresponding Source + may be on a different server (operated by you or a third party) + that supports equivalent copying facilities, provided you maintain + clear directions next to the object code saying where to find the + Corresponding Source. Regardless of what server hosts the + Corresponding Source, you remain obligated to ensure that it is + available for as long as needed to satisfy these requirements. + +* **e)** Convey the object code using peer-to-peer transmission, provided + you inform other peers where the object code and Corresponding + Source of the work are being offered to the general public at no + charge under subsection 6d. + + A separable portion of the object code, whose source code is excluded +from the Corresponding Source as a System Library, need not be +included in conveying the object code work. + + A “User Product” is either (1) a “consumer product”, which means any +tangible personal property which is normally used for personal, family, +or household purposes, or (2) anything designed or sold for incorporation +into a dwelling. In determining whether a product is a consumer product, +doubtful cases shall be resolved in favor of coverage. For a particular +product received by a particular user, “normally used” refers to a +typical or common use of that class of product, regardless of the status +of the particular user or of the way in which the particular user +actually uses, or expects or is expected to use, the product. A product +is a consumer product regardless of whether the product has substantial +commercial, industrial or non-consumer uses, unless such uses represent +the only significant mode of use of the product. + + “Installation Information” for a User Product means any methods, +procedures, authorization keys, or other information required to install +and execute modified versions of a covered work in that User Product from +a modified version of its Corresponding Source. The information must +suffice to ensure that the continued functioning of the modified object +code is in no case prevented or interfered with solely because +modification has been made. + + If you convey an object code work under this section in, or with, or +specifically for use in, a User Product, and the conveying occurs as +part of a transaction in which the right of possession and use of the +User Product is transferred to the recipient in perpetuity or for a +fixed term (regardless of how the transaction is characterized), the +Corresponding Source conveyed under this section must be accompanied +by the Installation Information. But this requirement does not apply +if neither you nor any third party retains the ability to install +modified object code on the User Product (for example, the work has +been installed in ROM). + + The requirement to provide Installation Information does not include a +requirement to continue to provide support service, warranty, or updates +for a work that has been modified or installed by the recipient, or for +the User Product in which it has been modified or installed. Access to a +network may be denied when the modification itself materially and +adversely affects the operation of the network or violates the rules and +protocols for communication across the network. + + Corresponding Source conveyed, and Installation Information provided, +in accord with this section must be in a format that is publicly +documented (and with an implementation available to the public in +source code form), and must require no special password or key for +unpacking, reading or copying. + +### 7. Additional Terms. + +“Additional permissions” are terms that supplement the terms of this +License by making exceptions from one or more of its conditions. +Additional permissions that are applicable to the entire Program shall +be treated as though they were included in this License, to the extent +that they are valid under applicable law. If additional permissions +apply only to part of the Program, that part may be used separately +under those permissions, but the entire Program remains governed by +this License without regard to the additional permissions. + + When you convey a copy of a covered work, you may at your option +remove any additional permissions from that copy, or from any part of +it. (Additional permissions may be written to require their own +removal in certain cases when you modify the work.) You may place +additional permissions on material, added by you to a covered work, +for which you have or can give appropriate copyright permission. + + Notwithstanding any other provision of this License, for material you +add to a covered work, you may (if authorized by the copyright holders of +that material) supplement the terms of this License with terms: + +* **a)** Disclaiming warranty or limiting liability differently from the + terms of sections 15 and 16 of this License; or + +* **b)** Requiring preservation of specified reasonable legal notices or + author attributions in that material or in the Appropriate Legal + Notices displayed by works containing it; or + +* **c)** Prohibiting misrepresentation of the origin of that material, or + requiring that modified versions of such material be marked in + reasonable ways as different from the original version; or + +* **d)** Limiting the use for publicity purposes of names of licensors or + authors of the material; or + +* **e)** Declining to grant rights under trademark law for use of some + trade names, trademarks, or service marks; or + +* **f)** Requiring indemnification of licensors and authors of that + material by anyone who conveys the material (or modified versions of + it) with contractual assumptions of liability to the recipient, for + any liability that these contractual assumptions directly impose on + those licensors and authors. + + All other non-permissive additional terms are considered “further +restrictions” within the meaning of section 10. If the Program as you +received it, or any part of it, contains a notice stating that it is +governed by this License along with a term that is a further +restriction, you may remove that term. If a license document contains +a further restriction but permits relicensing or conveying under this +License, you may add to a covered work material governed by the terms +of that license document, provided that the further restriction does +not survive such relicensing or conveying. + + If you add terms to a covered work in accord with this section, you +must place, in the relevant source files, a statement of the +additional terms that apply to those files, or a notice indicating +where to find the applicable terms. + + Additional terms, permissive or non-permissive, may be stated in the +form of a separately written license, or stated as exceptions; +the above requirements apply either way. + +### 8. Termination. + + You may not propagate or modify a covered work except as expressly +provided under this License. Any attempt otherwise to propagate or +modify it is void, and will automatically terminate your rights under +this License (including any patent licenses granted under the third +paragraph of section 11). + + However, if you cease all violation of this License, then your +license from a particular copyright holder is reinstated (a) +provisionally, unless and until the copyright holder explicitly and +finally terminates your license, and (b) permanently, if the copyright +holder fails to notify you of the violation by some reasonable means +prior to 60 days after the cessation. + + Moreover, your license from a particular copyright holder is +reinstated permanently if the copyright holder notifies you of the +violation by some reasonable means, this is the first time you have +received notice of violation of this License (for any work) from that +copyright holder, and you cure the violation prior to 30 days after +your receipt of the notice. + + Termination of your rights under this section does not terminate the +licenses of parties who have received copies or rights from you under +this License. If your rights have been terminated and not permanently +reinstated, you do not qualify to receive new licenses for the same +material under section 10. + +### 9. Acceptance Not Required for Having Copies. + + You are not required to accept this License in order to receive or +run a copy of the Program. Ancillary propagation of a covered work +occurring solely as a consequence of using peer-to-peer transmission +to receive a copy likewise does not require acceptance. However, +nothing other than this License grants you permission to propagate or +modify any covered work. These actions infringe copyright if you do +not accept this License. Therefore, by modifying or propagating a +covered work, you indicate your acceptance of this License to do so. + +### 10. Automatic Licensing of Downstream Recipients. + + Each time you convey a covered work, the recipient automatically +receives a license from the original licensors, to run, modify and +propagate that work, subject to this License. You are not responsible +for enforcing compliance by third parties with this License. + + An “entity transaction” is a transaction transferring control of an +organization, or substantially all assets of one, or subdividing an +organization, or merging organizations. If propagation of a covered +work results from an entity transaction, each party to that +transaction who receives a copy of the work also receives whatever +licenses to the work the party's predecessor in interest had or could +give under the previous paragraph, plus a right to possession of the +Corresponding Source of the work from the predecessor in interest, if +the predecessor has it or can get it with reasonable efforts. + + You may not impose any further restrictions on the exercise of the +rights granted or affirmed under this License. For example, you may +not impose a license fee, royalty, or other charge for exercise of +rights granted under this License, and you may not initiate litigation +(including a cross-claim or counterclaim in a lawsuit) alleging that +any patent claim is infringed by making, using, selling, offering for +sale, or importing the Program or any portion of it. + +### 11. Patents. + + A “contributor” is a copyright holder who authorizes use under this +License of the Program or a work on which the Program is based. The +work thus licensed is called the contributor’s “contributor version”. + + A contributor’s “essential patent claims” are all patent claims +owned or controlled by the contributor, whether already acquired or +hereafter acquired, that would be infringed by some manner, permitted +by this License, of making, using, or selling its contributor version, +but do not include claims that would be infringed only as a +consequence of further modification of the contributor version. For +purposes of this definition, “control” includes the right to grant +patent sublicenses in a manner consistent with the requirements of +this License. + + Each contributor grants you a non-exclusive, worldwide, royalty-free +patent license under the contributor’s essential patent claims, to +make, use, sell, offer for sale, import and otherwise run, modify and +propagate the contents of its contributor version. + + In the following three paragraphs, a “patent license” is any express +agreement or commitment, however denominated, not to enforce a patent +(such as an express permission to practice a patent or covenant not to +sue for patent infringement). To “grant” such a patent license to a +party means to make such an agreement or commitment not to enforce a +patent against the party. + + If you convey a covered work, knowingly relying on a patent license, +and the Corresponding Source of the work is not available for anyone +to copy, free of charge and under the terms of this License, through a +publicly available network server or other readily accessible means, +then you must either (1) cause the Corresponding Source to be so +available, or (2) arrange to deprive yourself of the benefit of the +patent license for this particular work, or (3) arrange, in a manner +consistent with the requirements of this License, to extend the patent +license to downstream recipients. “Knowingly relying” means you have +actual knowledge that, but for the patent license, your conveying the +covered work in a country, or your recipient’s use of the covered work +in a country, would infringe one or more identifiable patents in that +country that you have reason to believe are valid. + + If, pursuant to or in connection with a single transaction or +arrangement, you convey, or propagate by procuring conveyance of, a +covered work, and grant a patent license to some of the parties +receiving the covered work authorizing them to use, propagate, modify +or convey a specific copy of the covered work, then the patent license +you grant is automatically extended to all recipients of the covered +work and works based on it. + +A patent license is “discriminatory” if it does not include within +the scope of its coverage, prohibits the exercise of, or is +conditioned on the non-exercise of one or more of the rights that are +specifically granted under this License. You may not convey a covered +work if you are a party to an arrangement with a third party that is +in the business of distributing software, under which you make payment +to the third party based on the extent of your activity of conveying +the work, and under which the third party grants, to any of the +parties who would receive the covered work from you, a discriminatory +patent license (a) in connection with copies of the covered work +conveyed by you (or copies made from those copies), or (b) primarily +for and in connection with specific products or compilations that +contain the covered work, unless you entered into that arrangement, +or that patent license was granted, prior to 28 March 2007. + + Nothing in this License shall be construed as excluding or limiting +any implied license or other defenses to infringement that may +otherwise be available to you under applicable patent law. + +### 12. No Surrender of Others’ Freedom. + + If conditions are imposed on you (whether by court order, agreement or +otherwise) that contradict the conditions of this License, they do not +excuse you from the conditions of this License. If you cannot convey a +covered work so as to satisfy simultaneously your obligations under this +License and any other pertinent obligations, then as a consequence you may +not convey it at all. For example, if you agree to terms that obligate you +to collect a royalty for further conveying from those to whom you convey +the Program, the only way you could satisfy both those terms and this +License would be to refrain entirely from conveying the Program. + +### 13. Use with the GNU Affero General Public License. + + Notwithstanding any other provision of this License, you have +permission to link or combine any covered work with a work licensed +under version 3 of the GNU Affero General Public License into a single +combined work, and to convey the resulting work. The terms of this +License will continue to apply to the part which is the covered work, +but the special requirements of the GNU Affero General Public License, +section 13, concerning interaction through a network will apply to the +combination as such. + +### 14. Revised Versions of this License. + + The Free Software Foundation may publish revised and/or new versions of +the GNU 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 that a certain numbered version of the GNU General +Public License “or any later version” applies to it, you have the +option of following the terms and conditions either of that numbered +version or of any later version published by the Free Software +Foundation. If the Program does not specify a version number of the +GNU General Public License, you may choose any version ever published +by the Free Software Foundation. + + If the Program specifies that a proxy can decide which future +versions of the GNU General Public License can be used, that proxy’s +public statement of acceptance of a version permanently authorizes you +to choose that version for the Program. + + Later license versions may give you additional or different +permissions. However, no additional obligations are imposed on any +author or copyright holder as a result of your choosing to follow a +later version. + +### 15. Disclaimer of Warranty. + + 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. + +### 16. Limitation of Liability. + + IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING +WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MODIFIES AND/OR CONVEYS +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. + +### 17. Interpretation of Sections 15 and 16. + + If the disclaimer of warranty and limitation of liability provided +above cannot be given local legal effect according to their terms, +reviewing courts shall apply local law that most closely approximates +an absolute waiver of all civil liability in connection with the +Program, unless a warranty or assumption of liability accompanies a +copy of the Program in return for a fee. + +END OF TERMS AND CONDITIONS + +## 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 the public, 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 +state the exclusion of warranty; and each file should have at least +the “copyright” line and a pointer to where the full notice is found. + + <one line to give the program’s name and a brief idea of what it does.> + Copyright (C) <year> <name of author> + + 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 3 of the License, 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, see <http://www.gnu.org/licenses/>. + +Also add information on how to contact you by electronic and paper mail. + + If the program does terminal interaction, make it output a short +notice like this when it starts in an interactive mode: + + <program> Copyright (C) <year> <name of author> + This program 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, your program’s commands +might be different; for a GUI interface, you would use an “about box”. + + You should also get your employer (if you work as a programmer) or school, +if any, to sign a “copyright disclaimer” for the program, if necessary. +For more information on this, and how to apply and follow the GNU GPL, see +<<http://www.gnu.org/licenses/>>. + + The GNU General Public License does not permit incorporating your program +into proprietary programs. If your program is a subroutine library, you +may consider it more useful to permit linking proprietary applications with +the library. If this is what you want to do, use the GNU Lesser General +Public License instead of this License. But first, please read +<<http://www.gnu.org/philosophy/why-not-lgpl.html>>. diff --git a/library/fisharebest/ext-calendar/README.md b/library/fisharebest/ext-calendar/README.md new file mode 100644 index 0000000000..1f8225fb21 --- /dev/null +++ b/library/fisharebest/ext-calendar/README.md @@ -0,0 +1,138 @@ +[](https://travis-ci.org/fisharebest/ext-calendar) +[](https://coveralls.io/r/fisharebest/ext-calendar) +[](https://insight.sensiolabs.com/projects/952d6e11-6941-447b-9757-fc8dbc3d2a1f) +[](https://scrutinizer-ci.com/g/fisharebest/ext-calendar/?branch=master) +[](https://codeclimate.com/github/fisharebest/ext-calendar) + +PHP calendar functions +====================== + +This package provides an implementation of the +[Arabic (Hijri)](https://en.wikipedia.org/wiki/Islamic_calendar), +[French Republican](https://en.wikipedia.org/wiki/French_Republican_Calendar), +[Gregorian](https://en.wikipedia.org/wiki/Gregorian_calendar), +[Julian](https://en.wikipedia.org/wiki/Julian_calendar), +[Jewish](https://en.wikipedia.org/wiki/Hebrew_calendar) and +[Persian(Jalali)](https://en.wikipedia.org/wiki/Iranian_calendars) calendars, plus +[shims](https://en.wikipedia.org/wiki/Shim_%28computing%29) for the +[functions](https://php.net/ref.calendar) and [constants](https://php.net/calendar.constants) +in PHP‘s [ext/calendar](https://php.net/calendar) extension. +It allows you to use these functions on servers that do not have the extension installed. + +* [cal_days_in_month()](https://php.net/cal_days_in_month) +* [cal_from_jd()](https://php.net/cal_from_jd) +* [cal_info()](https://php.net/cal_info) +* [cal_to_jd()](https://php.net/cal_to_jd) +* [easter_date()](https://php.net/easter_date) +* [easter_days()](https://php.net/easter_days) +* [FrenchToJD()](https://php.net/FrenchToJD) +* [GregorianToJD()](https://php.net/GregorianToJD) +* [JDDayOfWeek()](https://php.net/JDDayOfWeek) +* [JDMonthName()](https://php.net/JDMonthName) +* [JDToFrench()](https://php.net/JDToFrench) +* [JDToGregorian()](https://php.net/JDToGregorian) +* [jdtojewish()](https://php.net/jdtojewish) +* [JDToJulian()](https://php.net/JDToJulian) +* [jdtounix()](https://php.net/jdtounix) +* [JewishToJD()](https://php.net/JewishToJD) +* [JulianToJD()](https://php.net/JulianToJD) +* [unixtojd()](https://php.net/unixtojd) + +How to use it +============= + +Add the package as a dependency in your `composer.json` file: + +``` javascript +require { + "fisharebest/ext-calendar": "1.*" +} +``` + +If you want to create the “shim” functions, you must tell the package to create them. + +```php +// Put this in your bootstrap.php or similar. +\Fisharebest\ExtCalendar\Shim::create(); +``` + +Now you can use the PHP functions, whether the ext/calendar is installed or not: + +``` php +print_r(cal_info(CAL_GREGORIAN)); +``` + +Alternatively, just use the calendar classes directly. + +``` php +use Fisharebest\ExtCalendar; + +// Create a calendar +$calendar = new ArabicCalendar; +$calendar = new FrenchCalendar; +$calendar = new GregorianCalendar; +$calendar = new JewishCalendar; +$calendar = new JulianCalendar; +$calendar = new PersianCalendar; + +// Date conversions +$julian_day = $calendar->ymdToJd($year, $month, $day); +list($year, $month, $day) = $calendar->jdToYmd($julian_day); + +// Information functions (see the source for more) +$is_leap_year = $calendar->leapYear($year); +$day_of_week = $calendar->dayOfWeek($julian_day); +$month_length = $calendar->daysInMonth($year, $month); +``` + +Known restrictions and limitations +================================== + +When faced with invalid inputs, the shim functions trigger `E_USER_WARNING` instead of `E_WARNING`. The text of the error messages is the same. + +The functions `easterdate()` and `jdtounixtime()` use PHP‘s timezone, instead of the operating system‘s timezone. These may be different. + +Compatibility with different versions of PHP +============================================ + +The following PHP bugs are emulated, according to the version of PHP being used. +Thus the package always provides the same behaviour as the native `ext/calendar` extension. + +* [#54254](https://bugs.php.net/bug.php?id=54254) Jewish month "Adar" - fixed in PHP 5.5 + +* [#67960](https://bugs.php.net/bug.php?id=67960) Constants `CAL_DOW_SHORT` and `CAL_DOW_LONG` - found/fixed by this project! + +* [#67976](https://bugs.php.net/bug.php?id=67976) Wrong value in `cal_days_in_month()` for French calendar - found by this project! + +HHVM +==== + +The package should work on [HHVM](http://hhvm.com/). However, we can’t run the unit +tests on it, as HHVM does not (yet?) include the `ext/calendar` extension. + +Development and contributions +============================= + +Note that `require-dev` will require `ext-calendar`, as we use it as a reference +implementation for testing. + +Due to the known restrictions above, you may need to run unit tests using `TZ=UTC phpunit`. + +Pull requests are welcome. Please ensure you include unit-tests where +applicable, and follow the existing coding conventions. These are to follow +[PSR](http://www.php-fig.org/) standards, except for: + +* tabs are used for indentation +* opening braces always go on the end of the previous line + +History +======= + +These functions were originally written for the [webtrees](http://www.webtrees.net) +project. As part of a refactoring process, they were extracted to a standalone +library, given version numbers, unit tests, etc. + +Future plans +============ + +It is hoped to extend the French calendar to allow it to be used for modern times. diff --git a/library/fisharebest/ext-calendar/src/ArabicCalendar.php b/library/fisharebest/ext-calendar/src/ArabicCalendar.php new file mode 100644 index 0000000000..55c045d36a --- /dev/null +++ b/library/fisharebest/ext-calendar/src/ArabicCalendar.php @@ -0,0 +1,116 @@ +<?php +namespace Fisharebest\ExtCalendar; + +/** + * class ArabicCalendar - calculations for the Arabic (Hijri) calendar. + * + * @author Greg Roach <fisharebest@gmail.com> + * @copyright (c) 2014 Greg Roach + * @license 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 3 of the License, 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, see <http://www.gnu.org/licenses/>. + */ +class ArabicCalendar extends Calendar implements CalendarInterface { + /** Same as PHP’s ext/calendar extension */ + const PHP_CALENDAR_NAME = 'Arabic'; + + /** Same as PHP’s ext/calendar extension */ + const PHP_CALENDAR_NUMBER = 4; // PHP uses 0-3 + + /** Same as PHP’s ext/calendar extension */ + const PHP_CALENDAR_SYMBOL = 'CAL_ARABIC'; + + /** See the GEDCOM specification */ + const GEDCOM_CALENDAR_ESCAPE = '@#DHIJRI@'; + + /** The earliest Julian Day number that can be converted into this calendar. */ + const JD_START = 1948440; // 1 Muharram 1 AH = 16 JUL 0622 (Julian) + + /** The maximum number of days in any month */ + const MAX_DAYS_IN_MONTH = 30; + + /** + * Month lengths for regular years and leap-years. + * + * @var int[][] + */ + protected static $DAYS_IN_MONTH = array( + 0 => array(1 => 30, 28, 30, 29, 30, 29, 30, 29, 30, 29, 30, 29), + 1 => array(1 => 30, 28, 30, 29, 30, 29, 30, 29, 30, 29, 30, 30), + ); + + /** + * Determine whether a year is a leap year. + * + * @param int $year + * @return bool + */ + public function leapYear($year) { + return ((11 * $year + 14) % 30) < 11; + } + + /** + * Convert a Julian day number into a year/month/day. + * + * @param $jd + * + * @return int[]; + */ + public function jdToYmd($jd) { + $year = (int)((30 * ($jd - 1948439) + 10646) / 10631); + $month = (int)((11 * ($jd - $year * 354 - (int)((3 + 11 * $year) / 30) - 1948085) + 330) / 325); + $day = $jd - 29 * ($month - 1) - (int)((6 * $month - 1) / 11) - $year * 354 - (int)((3 + 11 * $year) / 30) - 1948084; + + return array($year, $month, $day); + } + + /** + * Convert a year/month/day into a Julian day number + * + * @param int $year + * @param int $month + * @param int $day + * + * @return int + */ + public function ymdToJd($year, $month, $day) { + return $day + 29 * ($month - 1) + (int)((6 * $month - 1) / 11) + $year * 354 + (int)((3 + 11 * $year) / 30) + 1948084; + } + + /** + * Month names for the calendar. + * + * @return string[] + */ + public function monthNames() { + return array( + 1 => 'Muharram', 'Safar', 'Rabi‘ I', 'Rabi‘ II', 'Jumada I', 'Jumada II', + 'Rajab', 'Sha‘aban', 'Ramadan', 'Shawwal', 'Dhu al-Qi‘dah', 'Dhu al-Hijjah', + ); + } + + /** + * Calculate the number of days in a month. + * + * @param int $year + * @param int $month + * + * @return int + */ + public function daysInMonth($year, $month) { + if ($year == 0 || $month < 1 || $month > self::MAX_MONTHS_IN_YEAR) { + return trigger_error('invalid date.', E_USER_WARNING); + } else { + return static::$DAYS_IN_MONTH[$this->leapYear($year)][$month]; + } + } +} diff --git a/library/fisharebest/ext-calendar/src/Calendar.php b/library/fisharebest/ext-calendar/src/Calendar.php new file mode 100644 index 0000000000..2f48e8b581 --- /dev/null +++ b/library/fisharebest/ext-calendar/src/Calendar.php @@ -0,0 +1,225 @@ +<?php +namespace Fisharebest\ExtCalendar; + +/** + * class Calendar - generic base class for specific calendars. + * + * @author Greg Roach <fisharebest@gmail.com> + * @copyright (c) 2014 Greg Roach + * @license 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 3 of the License, 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, see <http://www.gnu.org/licenses/>. + */ +abstract class Calendar { + /** Same as PHP’s ext/calendar extension */ + const PHP_CALENDAR_NAME = 'Undefined'; + + /** Same as PHP’s ext/calendar extension */ + const PHP_CALENDAR_NUMBER = -1; + + /** Same as PHP’s ext/calendar extension */ + const PHP_CALENDAR_SYMBOL = 'CAL_UNDEFINED'; + + /** See the GEDCOM specification */ + const GEDCOM_CALENDAR_ESCAPE = '@#DUNKNOWN@'; + + /** The earliest Julian Day number that can be converted into this calendar. */ + const JD_START = 1; + + /** The latest Julian Day number that can be converted into this calendar. */ + const JD_END = 2147483647; + + /** The maximum number of months in any year */ + const MAX_MONTHS_IN_YEAR = 12; + + /** The maximum number of days in any month */ + const MAX_DAYS_IN_MONTH = 31; + + /** + * The concrete class needs to provide the implementation. + * + * @return string[] + */ + protected abstract function monthNames(); + + /** + * English names for the days of the week. + * + * @return string[] + */ + protected function dayNames() { + return array( + 'Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', + ); + } + + /** + * Abbreviated English names for the days of the week. + * + * @return string[] + */ + protected function dayNamesAbbreviated() { + return array( + 'Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', + ); + } + + /** + * The concrete class needs to provide the implementation. + * + * @param $jd + */ + public abstract function jdToYmd($jd); + + /** + * The concrete class needs to provide the implementation. + * + * @param int $year + * @param int $month + * @param int $day + * + * @return int + */ + public abstract function ymdToJd($year, $month, $day); + + /** + * Convert a Julian Day number into a calendar date. + * + * @param $jd + * @return int[] Array of month, day and year + */ + public function calFromJd($jd) { + $dow = $this->dayOfWeek($jd); + + if ($jd >= static::JD_START && $jd <= static::JD_END) { + list($year, $month, $day) = $this->jdToYmd($jd); + + return array( + 'date' => $month . '/' . $day . '/' . $year, + 'month' => $month, + 'day' => $day, + 'year' => $year, + 'dow' => $dow, + 'abbrevdayname' => $this->dayNameAbbreviated($dow), + 'dayname' => $this->dayName($dow), + 'abbrevmonth' => $this->jdMonthNameAbbreviated($jd), + 'monthname' => $this->jdMonthName($jd), + ); + } else { + return array( + 'date' => '0/0/0', + 'month' => 0, + 'day' => 0, + 'year' => 0, + 'dow' => $dow, + 'abbrevdayname' => $this->dayNameAbbreviated($dow), + 'dayname' => $this->dayName($dow), + 'abbrevmonth' => '', + 'monthname' => '', + ); + } + } + + /** + * Provide information about this calendar. + * + * @return array + */ + public function phpCalInfo() { + return array( + 'months' => $this->monthNames(), + 'abbrevmonths' => $this->monthNamesAbbreviated(), + 'maxdaysinmonth' => static::MAX_DAYS_IN_MONTH, + 'calname' => static::PHP_CALENDAR_NAME, + 'calsymbol' => static::PHP_CALENDAR_SYMBOL, + ); + } + + /** + * Calculate the day of the week for a given Julian Day number. + * + * @param int $jd + * + * @return int 0=Sunday ... 6=Saturday + */ + public function dayOfWeek($jd) { + $dow = ($jd + 1) % 7; + if ($dow < 0) { + return $dow + 7; + } else { + return $dow; + } + } + + /** + * English name for a day of the week. + * + * @param int $dow Day of the week + * + * @return string + */ + public function dayName($dow) { + $days = $this->dayNames(); + + return $days[$dow]; + } + + /** + * Abbreviated English name for a day of the week. + * + * @param int $dow Day of the week + * + * @return string + */ + public function dayNameAbbreviated($dow) { + $days = $this->dayNamesAbbreviated(); + + return $days[$dow]; + } + + /** + * Calculate the name of a month, for a specified Julian Day number. + * + * @param $jd + * + * @return string + */ + public function jdMonthName($jd) { + list(, $month) = $this->jdToYmd($jd); + $months = $this->monthNames(); + + return $months[$month]; + } + + /** + * Calculate the name of a month, for a specified Julian Day number. + * + * @param int $jd + * + * @return string + */ + public function jdMonthNameAbbreviated($jd) { + list(, $month) = $this->jdToYmd($jd); + $months = $this->monthNamesAbbreviated(); + + return $months[$month]; + } + + /** + * Unless otherwise defined, abbreviated month names are the same as full names. + * + * @return string[] + */ + public function monthNamesAbbreviated() { + return $this->monthNames(); + } +} diff --git a/library/fisharebest/ext-calendar/src/CalendarInterface.php b/library/fisharebest/ext-calendar/src/CalendarInterface.php new file mode 100644 index 0000000000..3373b03fac --- /dev/null +++ b/library/fisharebest/ext-calendar/src/CalendarInterface.php @@ -0,0 +1,29 @@ +<?php +namespace Fisharebest\ExtCalendar; + +/** + * interface CalendarInterface - each calendar implementation needs to provide + * these methods. + * + * @author Greg Roach <fisharebest@gmail.com> + * @copyright (c) 2014 Greg Roach + * @license 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 3 of the License, 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, see <http://www.gnu.org/licenses/>. + */ +interface CalendarInterface { + public function daysInMonth($year, $month); + public function dayOfWeek($jd); + public function jdToYmd($jd); + public function leapYear($year); + public function ymdToJd($year, $month, $day); +} diff --git a/library/fisharebest/ext-calendar/src/FrenchCalendar.php b/library/fisharebest/ext-calendar/src/FrenchCalendar.php new file mode 100644 index 0000000000..2f30a192ea --- /dev/null +++ b/library/fisharebest/ext-calendar/src/FrenchCalendar.php @@ -0,0 +1,120 @@ +<?php +namespace Fisharebest\ExtCalendar; + +/** + * class FrenchCalendar - calculations for the French Republican calendar. + * + * @author Greg Roach <fisharebest@gmail.com> + * @copyright (c) 2014 Greg Roach + * @license 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 3 of the License, 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, see <http://www.gnu.org/licenses/>. + */ +class FrenchCalendar extends Calendar implements CalendarInterface { + /** Same as PHP’s ext/calendar extension */ + const PHP_CALENDAR_NAME = 'French'; + + /** Same as PHP’s ext/calendar extension */ + const PHP_CALENDAR_NUMBER = 3; + + /** Same as PHP’s ext/calendar extension */ + const PHP_CALENDAR_SYMBOL = 'CAL_FRENCH'; + + /** See the GEDCOM specification */ + const GEDCOM_CALENDAR_ESCAPE = '@#DFRENCH R@'; + + /** The earliest Julian Day number that can be converted into this calendar. */ + const JD_START = 2375840; + + /** The latest Julian Day number that can be converted into this calendar. */ + const JD_END = 2380952; // For compatibility with PHP, this is 0014-13-05 + + /** The maximum number of months in any year */ + const MAX_MONTHS_IN_YEAR = 13; + + /** The maximum number of days in any month */ + const MAX_DAYS_IN_MONTH = 30; + + /** + * Determine whether a year is a leap year. + * + * @param int $year + * @return bool + */ + public function leapYear($year) { + return $year % 4 == 3; + } + + /** + * Convert a Julian day number into a year/month/day. + * + * @param $jd + * + * @return int[]; + */ + public function jdToYmd($jd) { + $year = (int)(($jd - 2375109) * 4 / 1461) - 1; + $month = (int)(($jd - 2375475 - $year * 365 - (int)($year / 4)) / 30) + 1; + $day = $jd - 2375444 - $month * 30 - $year * 365 - (int)($year / 4); + + return array($year, $month, $day); + } + + /** + * Convert a year/month/day into a Julian day number + * + * @param int $year + * @param int $month + * @param int $day + * + * @return int + */ + public function ymdToJd($year, $month, $day) { + return 2375444 + $day + $month * 30 + $year * 365 + (int)($year / 4); + } + + /** + * Month names for the calendar. + * + * @return string[] + */ + public function monthNames() { + return array( + 1 => 'Vendemiaire', 'Brumaire', 'Frimaire', 'Nivose', 'Pluviose', 'Ventose', + 'Germinal', 'Floreal', 'Prairial', 'Messidor', 'Thermidor', 'Fructidor', 'Extra', + ); + } + + /** + * Calculate the number of days in a month. + * + * @param int $year + * @param int $month + * + * @return int + */ + public function daysInMonth($year, $month) { + if ($year < 1 || $year > 14) { + return trigger_error('invalid date.', E_USER_WARNING); + } elseif ($month >=1 && $month <= 12) { + return 30; + } elseif ($month == 13) { + if ($this->leapYear($year)) { + return 6; + } else { + return $year == 14 ? -2380948 : 5; // Emulate a bug in PHP + } + } else { + return trigger_error('invalid date.', E_USER_WARNING); + } + } +} diff --git a/library/fisharebest/ext-calendar/src/GregorianCalendar.php b/library/fisharebest/ext-calendar/src/GregorianCalendar.php new file mode 100644 index 0000000000..2856a4210a --- /dev/null +++ b/library/fisharebest/ext-calendar/src/GregorianCalendar.php @@ -0,0 +1,137 @@ +<?php +namespace Fisharebest\ExtCalendar; + +/** + * class GregorianCalendar - calculations for the (proleptic) Gregorian calendar. + * + * @author Greg Roach <fisharebest@gmail.com> + * @copyright (c) 2014 Greg Roach + * @license 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 3 of the License, 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, see <http://www.gnu.org/licenses/>. + */ +class GregorianCalendar extends JulianCalendar implements CalendarInterface { + /** Same as PHP’s ext/calendar extension */ + const PHP_CALENDAR_NAME = 'Gregorian'; + + /** Same as PHP’s ext/calendar extension */ + const PHP_CALENDAR_NUMBER = 0; + + /** Same as PHP’s ext/calendar extension */ + const PHP_CALENDAR_SYMBOL = 'CAL_GREGORIAN'; + + /** See the GEDCOM specification */ + const GEDCOM_CALENDAR_ESCAPE = '@#DGREGORIAN@'; + + /** + * Determine whether a year is a leap year. + * + * @param int $year + * @return bool + */ + public function leapYear($year) { + if ($year < 0) { + $year++; + } + + return $year % 4 == 0 && $year % 100 != 0 || $year % 400 == 0; + } + + /** + * Convert a Julian day number into a year/month/day. + * + * @param $jd + * + * @return int[]; + */ + public function jdToYmd($jd) { + $a = $jd + 32044; + $b = (int)((4 * $a + 3) / 146097); + $c = $a - (int)($b * 146097 / 4); + $d = (int)((4 * $c + 3) / 1461); + $e = $c - (int)((1461 * $d) / 4); + $m = (int)((5 * $e + 2) / 153); + $day = $e - (int)((153 * $m + 2) / 5) + 1; + $month = $m + 3 - 12 * (int)($m / 10); + $year = $b * 100 + $d - 4800 + (int)($m / 10); + if ($year < 1) { // 0=1BC, -1=2BC, etc. + $year--; + } + + return array($year, $month, $day); + } + + /** + * Convert a year/month/day into a Julian day number + * + * @param int $year + * @param int $month + * @param int $day + * + * @return int + */ + public function ymdToJd($year, $month, $day) { + if ($year < 0) { + // 1 B.C.E. => 0, 2 B.C.E> => 1, etc. + ++$year; + } + $a = (int)((14 - $month) / 12); + $year = $year + 4800 - $a; + $month = $month + 12 * $a - 3; + + return $day + (int)((153 * $month + 2) / 5) + 365 * $year + (int)($year / 4) - (int)($year / 100) + (int)($year / 400) - 32045; + } + + /** + * Get the number of days after March 21 that easter falls, for a given year. + * + * Uses the algorithm found in PHP’s ext/calendar/easter.c + * + * @param int $year + * + * @return int + */ + public function easterDays($year) { + // The “golden” number + $golden = $year % 19 + 1; + + // The “dominical” number (finding a Sunday) + $dom = ($year + (int)($year / 4) - (int)($year / 100) + (int)($year / 400)) % 7; + if ($dom < 0) { + $dom += 7; + } + + // The solar correction + $solar = (int)(($year - 1600) / 100) - (int)(($year - 1600) / 400); + + // The lunar correction + $lunar = (int)((int)(($year-1400) / 100) * 8) / 25; + + // The uncorrected “Paschal full moon” date + $pfm = (3 - 11 * $golden + $solar - $lunar) % 30; + if ($pfm < 0) { + $pfm += 30; + } + + // The corrected “Paschal full moon” date + if ($pfm == 29 || $pfm == 28 && $golden > 11) { + $pfm--; + } + + $tmp = (4 - $pfm - $dom) % 7; + if ($tmp < 0) { + $tmp += 7; + } + + return $pfm + $tmp + 1; + } +} diff --git a/library/fisharebest/ext-calendar/src/JewishCalendar.php b/library/fisharebest/ext-calendar/src/JewishCalendar.php new file mode 100644 index 0000000000..038188fcd0 --- /dev/null +++ b/library/fisharebest/ext-calendar/src/JewishCalendar.php @@ -0,0 +1,491 @@ +<?php +namespace Fisharebest\ExtCalendar; + +/** + * class JewishCalendar - calculations for the Jewish calendar. + * + * Hebrew characters in this file have UTF-8 encoding. + * + * @author Greg Roach <fisharebest@gmail.com> + * @copyright (c) 2014 Greg Roach + * @license 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 3 of the License, 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, see <http://www.gnu.org/licenses/>. + */ +class JewishCalendar extends Calendar implements CalendarInterface{ + /** Same as PHP’s ext/calendar extension */ + const PHP_CALENDAR_NAME = 'Jewish'; + + /** Same as PHP’s ext/calendar extension */ + const PHP_CALENDAR_NUMBER = 2; + + /** Same as PHP’s ext/calendar extension */ + const PHP_CALENDAR_SYMBOL = 'CAL_JEWISH'; + + /** See the GEDCOM specification */ + const GEDCOM_CALENDAR_ESCAPE = '@#DHEBREW@'; + + /** The earliest Julian Day number that can be converted into this calendar. */ + const JD_START = 347998; // 1 Tishri 0001 AM + + /** The latest Julian Day number that can be converted into this calendar. */ + const JD_END = 324542846; + + /** The maximum number of days in any month. */ + const MAX_DAYS_IN_MONTH = 30; + + /** The maximum number of months in any year. */ + const MAX_MONTHS_IN_YEAR = 13; + + /** Place this symbol before the final letter of a sequence of numerals. */ + const GERSHAYIM = '״'; + + /** Place this symbol after a single numeral. */ + const GERESH = '׳'; + + /** Word for thousand. */ + const ALAFIM = 'אלפים'; + + /** A year that is one day shorter than normal. */ + const DEFECTIVE_YEAR = -1; + + /** A year that has the normal number of days. */ + const REGULAR_YEAR = 0; + + /** A year that is one day longer than normal. */ + const COMPLETE_YEAR = 1; + + /** + * Hebrew numbers are represented by letters, similar to roman numerals. + * + * @var string[] + */ + private static $HEBREW_NUMERALS = array( + 9000 => 'ט', + 8000 => 'ח', + 7000 => 'ז', + 6000 => 'ו', + 5000 => 'ה', + 4000 => 'ד', + 3000 => 'ג', + 2000 => 'ב', + 1000 => 'א', + 400 => 'ת', + 300 => 'ש', + 200 => 'ר', + 100 => 'ק', + 90 => 'צ', + 80 => 'פ', + 70 => 'ע', + 60 => 'ס', + 50 => 'נ', + 40 => 'מ', + 30 => 'ל', + 20 => 'כ', + 19 => 'יט', + 18 => 'יח', + 17 => 'יז', + 16 => 'טז', + 15 => 'טו', + 10 => 'י', + 9 => 'ט', + 8 => 'ח', + 7 => 'ז', + 6 => 'ו', + 5 => 'ה', + 4 => 'ד', + 3 => 'ג', + 2 => 'ב', + 1 => 'א', + ); + + /** + * Some numerals take a different form when they appear at the end of a number. + * + * @var string[] + */ + private static $HEBREW_NUMERALS_FINAL = array( + 90 => 'צ', + 80 => 'פ', + 70 => 'ע', + 60 => 'ס', + 50 => 'נ', + 40 => 'מ', + 30 => 'ל', + 20 => 'כ', + 10 => 'י', + ); + + /** + * These months have fixed lengths. Others are variable. + * + * @var int[] + */ + private static $FIXED_MONTH_LENGTHS = array( + 1 => 30, 4 => 29, 5 => 30, 7 => 29, 8 => 30, 9 => 29, 10 => 30, 11 => 29, 12 => 30, 13 => 29 + ); + + /** + * Cumulative number of days for each month in each type of year. + * First index is false/true (non-leap year, leap year) + * Second index is year type (-1, 0, 1) + * Third index is month number (1 ... 13) + * + * @var int[][][] + */ + private static $CUMULATIVE_DAYS = array( + 0 => array( // Non-leap years + -1 => array( // Deficient years + 1 => 0, 30, 59, 88, 117, 147, 147, 176, 206, 235, 265, 294, 324 + ), + 0 => array( // Regular years + 1 => 0, 30, 59, 89, 118, 148, 148, 177, 207, 236, 266, 295, 325 + ), + 1 => array( // Complete years + 1 => 0, 30, 60, 90, 119, 149, 149, 178, 208, 237, 267, 296, 326 + ), + ), + 1 => array( // Leap years + -1 => array( // Deficient years + 1 => 0, 30, 59, 88, 117, 147, 177, 206, 236, 265, 295, 324, 354 + ), + 0 => array( // Regular years + 1 => 0, 30, 59, 89, 118, 148, 178, 207, 237, 266, 296, 325, 355 + ), + 1 => array( // Complete years + 1 => 0, 30, 60, 90, 119, 149, 179, 208, 238, 267, 297, 326, 356 + ), + ), + ); + + + /** + * Rosh Hashanah cannot fall on a Sunday, Wednesday or Friday. Move the year start accordingly. + * + * @var int[] + */ + private static $ROSH_HASHANAH = array(347998, 347997, 347997, 347998, 347997, 347998, 347997); + + /** + * Determine whether a year is a leap year. + * + * @param int $year + * @return bool + */ + public function leapYear($year) { + return (7 * $year + 1) % 19 < 7; + } + + /** + * Convert a Julian day number into a year. + * + * @param int $jd + * + * @return int; + */ + protected function jdToY($jd) { + // Generate an approximate year - may be out by one either way. Add one to it. + $year = (int)(($jd - 347998) / 365) + 1; + + // Adjust by subtracting years; + while ($this->yToJd($year) > $jd) { + $year--; + } + + return $year; + } + + /** + * Convert a Julian day number into a year/month/day. + * + * @param int $jd + * + * @return int[]; + */ + public function jdToYmd($jd) { + // Find the year + $year = $this->jdToY($jd); + + // Add one month at a time, to use up the remaining days. + $month = 1; + $day = $jd - $this->yToJd($year) + 1; + + while ($day > $this->daysInMonth($year, $month)) { + $day -= $this->daysInMonth($year, $month); + $month += 1; + } + + // PHP 5.4 and earlier converted non leap-year Adar into month 6, instead of month 7. + $month -= (Shim::emulateBug54254() && $month == 7 && !$this->leapYear($year)) ? 1 : 0; + + return array($year, $month, $day); + } + + /** + * Calculate the Julian Day number of the first day in a year. + * + * @param int $year + * + * @return int + */ + protected function yToJd($year) { + $div19 = (int)(($year - 1) / 19); + $mod19 = ($year - 1) % 19; + $months = 235 * $div19 + 12 * $mod19 + (int)((7 * $mod19 + 1) / 19); + $parts = 204 + 793 * ($months % 1080); + $hours = 5 + 12 * $months + 793 * (int)($months / 1080) + (int)($parts / 1080); + $conjunction = 1080 * ($hours % 24) + ($parts % 1080); + $jd = 1 + 29 * $months + (int)($hours / 24); + + if ( + $conjunction >= 19440 || + $jd % 7 === 2 && $conjunction >= 9924 && !$this->leapYear($year) || + $jd % 7 === 1 && $conjunction >= 16789 && $this->leapYear($year - 1) + ) { + $jd++; + } + + // The actual year start depends on the day of the week + return $jd + self::$ROSH_HASHANAH[$jd % 7]; + } + + /** + * Convert a year/month/day into a Julian day number. + * + * @param int $year + * @param int $month + * @param int $day + * + * @return int + */ + public function ymdToJd($year, $month, $day) { + return + $this->yToJd($year) + + self::$CUMULATIVE_DAYS[$this->leapYear($year)][$this->yearType($year)][$month] + + $day - 1; + } + + /** + * Determine whether a year is normal, defective or complete. + * + * @param $year + * + * @return int defective (-1), normal (0) or complete (1) + */ + private function yearType($year) { + $year_length = $this->yToJd($year + 1) - $this->yToJd($year); + + if ($year_length === 353 || $year_length === 383) { + return self::DEFECTIVE_YEAR; + } elseif ($year_length === 355 || $year_length === 385) { + return self::COMPLETE_YEAR; + } else { + return self::REGULAR_YEAR; + } + } + + /** + * Calculate the number of days in Heshvan. + * + * @param int $year + * + * @return int + */ + private function daysInMonthHeshvan($year) { + if ($this->yearType($year) === self::COMPLETE_YEAR) { + return 30; + } else { + return 29; + } + } + + /** + * Calculate the number of days in Kislev. + * + * @param int $year + * + * @return int + */ + private function daysInMonthKislev($year) { + if ($this->yearType($year) === self::DEFECTIVE_YEAR) { + return 29; + } else { + return 30; + } + } + + /** + * Calculate the number of days in Adar I. + * + * @param int $year + * + * @return int + */ + private function daysInMonthAdarI($year) { + if ($this->leapYear($year)) { + return 30; + } else { + return 0; + } + } + + /** + * Calculate the number of days in a given month. + * + * @param int $year + * @param int $month + * + * @return int + */ + public function daysInMonth($year, $month) { + if ($year === 0 || $month < 1 || $month > 13) { + return trigger_error('invalid date.', E_USER_WARNING); + } elseif ($month === 2) { + return $this->daysInMonthHeshvan($year); + } elseif ($month === 3) { + return $this->daysInMonthKislev($year); + } elseif ($month === 6) { + return $this->daysInMonthAdarI($year); + } else { + return self::$FIXED_MONTH_LENGTHS[$month]; + } + } + + /** + * Month names. + * + * @link https://bugs.php.net/bug.php?id=54254 + * + * @return string[] + */ + public function monthNames() { + return array( + 1 => 'Tishri', 'Heshvan', 'Kislev', 'Tevet', 'Shevat', + Shim::emulateBug54254() ? 'AdarI' : 'Adar I', + Shim::emulateBug54254() ? 'AdarII' : 'Adar II', + 'Nisan', 'Iyyar', 'Sivan', 'Tammuz', 'Av', 'Elul', + ); + } + + /** + * Calculate the name of a month, for a specified Julian Day number. + * + * @param $jd + * + * @return string + */ + public function jdMonthName($jd) { + list($year, $month) = $this->jdToYmd($jd); + $months = $this->monthNames(); + + if (!$this->leapYear($year) && ($month === 6 || $month === 7)) { + return Shim::emulateBug54254() ? 'AdarI' : 'Adar'; + } else { + return $months[$month]; + } + } + + /** + * Calculate the name of a month, for a specified Julian Day number. + * + * @param int $jd + * + * @return string + */ + public function jdMonthNameAbbreviated($jd) { + return $this->jdMonthName($jd); + } + + + /** + * Hebrew month names. + * + * @link https://bugs.php.net/bug.php?id=54254 + * + * @param int $year + * + * @return string[] + */ + protected function hebrewMonthNames($year) { + $leap_year = $this->leapYear($year); + + return array( + 1 => 'תשרי', 'חשון', 'כסלו', 'טבת', 'שבט', + $leap_year ? (Shim::emulateBug54254() ? 'אדר' : 'אדר א׳') : 'אדר', + $leap_year ? (Shim::emulateBug54254() ? '\'אדר ב' : 'אדר ב׳') : 'אדר', + 'ניסן', 'אייר', 'סיון', 'תמוז', 'אב', 'אלול', + ); + } + + /** + * The Hebrew name of a given month. + * + * @param int $year + * @param int $month + * + * @return string + */ + protected function hebrewMonthName($year, $month) { + $months = $this->hebrewMonthNames($year); + + return $months[$month]; + } + + /** + * Convert a number into Hebrew numerals. + * + * @param int $number + * @param bool $alafim_garesh + * @param bool $alafim + * @param bool $gereshayim + * + * @return string + */ + protected function numToHebrew($number, $alafim_garesh, $alafim, $gereshayim) { + $hebrew = ''; + while ($number > 0) { + foreach (self::$HEBREW_NUMERALS_FINAL as $n => $h) { + if ($number == $n) { + $hebrew .= $h; + break 2; + } + } + foreach (self::$HEBREW_NUMERALS as $n => $h) { + if ($number >= $n) { + $hebrew .= $h; + $number -= $n; + break; + } + } + } + return $hebrew; + } + + /** + * Convert a Julian Day number into a Hebrew date. + * + * @param int $jd + * @param bool $alafim_garesh + * @param bool $alafim + * @param bool $gereshayim + * + * @return string $string + */ + public function jdToHebrew($jd, $alafim_garesh, $alafim, $gereshayim) { + list($year, $month, $day) = $this->jdToYmd($jd); + + return + $this->numToHebrew($day, $alafim_garesh, $alafim, $gereshayim) . ' ' . + $this->hebrewMonthName($year, $month) . ' ' . + $this->numToHebrew($year, $alafim_garesh, $alafim, $gereshayim); + } +} diff --git a/library/fisharebest/ext-calendar/src/JulianCalendar.php b/library/fisharebest/ext-calendar/src/JulianCalendar.php new file mode 100644 index 0000000000..c74e82cda2 --- /dev/null +++ b/library/fisharebest/ext-calendar/src/JulianCalendar.php @@ -0,0 +1,179 @@ +<?php +namespace Fisharebest\ExtCalendar; + +/** + * class JulianCalendar - calculations for the Julian calendar. + * + * @author Greg Roach <fisharebest@gmail.com> + * @copyright (c) 2014 Greg Roach + * @license 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 3 of the License, 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, see <http://www.gnu.org/licenses/>. + */ +class JulianCalendar extends Calendar implements CalendarInterface { + /** Same as PHP’s ext/calendar extension */ + const PHP_CALENDAR_NAME = 'Julian'; + + /** Same as PHP’s ext/calendar extension */ + const PHP_CALENDAR_NUMBER = 1; + + /** Same as PHP’s ext/calendar extension */ + const PHP_CALENDAR_SYMBOL = 'CAL_JULIAN'; + + /** See the GEDCOM specification */ + const GEDCOM_CALENDAR_ESCAPE = '@#DJULIAN@'; + + /** + * Month lengths for regular years and leap-years. + * + * @var int[][] + */ + protected static $DAYS_IN_MONTH = array( + 0 => array(1 => 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31), + 1 => array(1 => 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31), + ); + + /** + * English month names. + * + * @return string[] + */ + public function monthNames() { + return array( + 1 => 'January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December', + ); + } + + /** + * Abbreviated English month names. + * + * @return string[] + */ + public function monthNamesAbbreviated() { + return array( + 1 => 'Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec', + ); + } + + + /** + * Determine whether a year is a leap year. + * + * @param int $year + * @return bool + */ + public function leapYear($year) { + if ($year < 0) { + $year++; + } + + return $year % 4 == 0; + } + + /** + * Convert a Julian day number into a year/month/day. + * + * @param $jd + * + * @return int[]; + */ + public function jdToYmd($jd) { + $c = $jd + 32082; + $d = (int)((4 * $c + 3) / 1461); + $e = $c - (int)(1461 * $d / 4); + $m = (int)((5 * $e + 2) / 153); + $day = $e - (int)((153 * $m + 2) / 5) + 1; + $month = $m + 3 - 12 * (int)($m / 10); + $year = $d - 4800 + (int)($m / 10); + if ($year < 1) { + // 0=1BC, -1=2BC, etc. + $year--; + } + + return array($year, $month, $day); + } + + /** + * Convert a year/month/day into a Julian day number + * + * @param int $year + * @param int $month + * @param int $day + * + * @return int + */ + public function ymdToJd($year, $month, $day) { + if ($year < 0) { + // 1 B.C.E. => 0, 2 B.C.E> => 1, etc. + ++$year; + } + $a = (int)((14 - $month) / 12); + $year = $year + 4800 - $a; + $month = $month + 12 * $a - 3; + + return $day + (int)((153 * $month + 2) / 5) + 365 * $year + (int)($year / 4) - 32083; + } + + /** + * Calculate the number of days in a month. + * + * @param int $year + * @param int $month + * + * @return int + */ + public function daysInMonth($year, $month) { + if ($year == 0 || $month < 1 || $month > 12) { + return trigger_error('invalid date.', E_USER_WARNING); + } else { + return static::$DAYS_IN_MONTH[$this->leapYear($year)][$month]; + } + } + + /** + * Get the number of days after March 21 that easter falls, for a given year. + * + * Uses the algorithm found in PHP’s ext/calendar/easter.c + * + * @param int $year + * + * @return int + */ + public function easterDays($year) { + // The “golden” number + $golden = 1 + $year % 19; + + // The “dominical” number (finding a Sunday) + $dom = ($year + (int)($year / 4) + 5) % 7; + if ($dom < 0) { + $dom += 7; + } + + // The uncorrected “Paschal full moon” date + $pfm = (3 - 11 * $golden - 7) % 30; + if ($pfm < 0) { + $pfm += 30; + } + + // The corrected “Paschal full moon” date + if ($pfm == 29 || $pfm == 28 && $golden > 11) { + $pfm--; + } + + $tmp = (4 - $pfm - $dom) % 7; + if ($tmp < 0) { + $tmp += 7; + } + + return $pfm + $tmp + 1; + } +} diff --git a/library/fisharebest/ext-calendar/src/PersianCalendar.php b/library/fisharebest/ext-calendar/src/PersianCalendar.php new file mode 100644 index 0000000000..7d22e7bc33 --- /dev/null +++ b/library/fisharebest/ext-calendar/src/PersianCalendar.php @@ -0,0 +1,146 @@ +<?php +namespace Fisharebest\ExtCalendar; + +/** + * class PersianCalendar - calculations for the Persian (Jalali) calendar. + * + * @author Greg Roach <fisharebest@gmail.com> + * @copyright (c) 2014 Greg Roach + * @license 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 3 of the License, 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, see <http://www.gnu.org/licenses/>. + */ +class PersianCalendar extends Calendar implements CalendarInterface { + /** Same as PHP’s ext/calendar extension */ + const PHP_CALENDAR_NAME = 'Persian'; + + /** Same as PHP’s ext/calendar extension */ + const PHP_CALENDAR_NUMBER = 5; // PHP uses 0-3 + + /** Same as PHP’s ext/calendar extension */ + const PHP_CALENDAR_SYMBOL = 'CAL_PERSIAN'; + + /** See the GEDCOM specification */ + const GEDCOM_CALENDAR_ESCAPE = '@#DJALALI@'; + + /** The earliest Julian Day number that can be converted into this calendar. */ + const JD_START = 1948321; // 1 Farvardīn 0001 AP = ?? ??? ???? (Julian) + + /** + * Month lengths for regular years and leap-years. + * + * @var int[][] + */ + protected static $DAYS_IN_MONTH = array( + 0 => array(1 => 31, 31, 31, 31, 31, 31, 30, 30, 30, 30, 30, 29), + 1 => array(1 => 31, 31, 31, 31, 31, 31, 30, 30, 30, 30, 30, 30), + ); + + /** + * In each 128 year cycle, the following years are leap years. + * + * @var int[] + */ + private static $LEAP_YEAR_CYCLE = array( + 0, 5, 9, 13, 17, 21, 25, 29, 34, 38, 42, 46, 50, 54, 58, 62, 67, 71, 75, 79, 83, 87, 91, 95, 100, 104, 108, 112, 116, 120, 124 + ); + + /** + * Determine whether a year is a leap year. + * + * @param int $year + * @return bool + */ + public function leapYear($year) { + return in_array((($year + 2346) % 2820) % 128, self::$LEAP_YEAR_CYCLE); + } + + /** + * Convert a Julian day number into a year/month/day. + * + * @param $jd + * + * @return int[]; + */ + public function jdToYmd($jd) { + $depoch = $jd - self::YMDtoJD(475, 1, 1); + $cycle = (int)($depoch / 1029983); + $cyear = $depoch % 1029983; + if ($cyear == 1029982) { + $ycycle = 2820; + } else { + $aux1 = (int)($cyear / 366); + $aux2 = $cyear % 366; + $ycycle = (int)(((2134 * $aux1) + (2816 * $aux2) + 2815) / 1028522) + + $aux1 + 1; + } + $year = $ycycle + (2820 * $cycle) + 474; + if ($year <= 0) { + $year--; + } + $yday = ($jd - self::YMDtoJD($year, 1, 1)) + 1; + $month = ($yday <= 186) ? ceil($yday / 31) : ceil(($yday - 6) / 30); + $day = ($jd - self::YMDtoJD($year, $month, 1)) + 1; + + return array($year, (int)$month, (int)$day); + } + + /** + * Convert a year/month/day into a Julian day number + * + * @param int $year + * @param int $month + * @param int $day + * + * @return int + */ + public function ymdToJd($year, $month, $day) { + $epbase = $year - (($year >= 0) ? 474 : 473); + $epyear = 474 + $epbase % 2820; + + return + $day + + (($month <= 7) ? (($month - 1) * 31) : ((($month - 1) * 30) + 6)) + + (int)((($epyear * 682) - 110) / 2816) + + ($epyear - 1) * 365 + + (int)($epbase / 2820) * 1029983 + + (self::JD_START - 1); + } + + /** + * Month names for the calendar. + * + * @return string[] + */ + public function monthNames() { + return array( + 1 => 'Farvardin', 'Ordibehesht', 'Khordad', 'Tir', 'Mordad', 'Shahrivar', + 'Mehr', 'Aban', 'Azar', 'Dey', 'Bahman', 'Esfand', + ); + } + + /** + * Calculate the number of days in a month. + * + * @param int $year + * @param int $month + * + * @return int + */ + public function daysInMonth($year, $month) { + if ($year == 0 || $month < 1 || $month > self::MAX_MONTHS_IN_YEAR) { + return trigger_error('invalid date.', E_USER_WARNING); + } else { + return static::$DAYS_IN_MONTH[$this->leapYear($year)][$month]; + } + } +} diff --git a/library/fisharebest/ext-calendar/src/Shim.php b/library/fisharebest/ext-calendar/src/Shim.php new file mode 100644 index 0000000000..ee7ce55bbb --- /dev/null +++ b/library/fisharebest/ext-calendar/src/Shim.php @@ -0,0 +1,567 @@ +<?php +namespace Fisharebest\ExtCalendar; + +/** + * class Shim - PHP implementations of functions from the PHP calendar extension. + * + * @link http://php.net/manual/en/book.calendar.php + * + * @author Greg Roach <fisharebest@gmail.com> + * @copyright (c) 2014 Greg Roach + * @license 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 3 of the License, 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, see <http://www.gnu.org/licenses/>. + */ +class Shim { + /** + * Create the necessary shims to emulate the ext/calendar packate. + * + * @return void + */ + public static function create() { + if (!function_exists('cal_info')) { + require __DIR__ . '/shims.php'; + } + } + + /** + * Do we need to emulate PHP bug #54254? + * + * This bug relates to the names used for months 6 and 7 in the Jewish calendar. + * + * It was fixed in PHP 5.5.0 + * + * @link https://bugs.php.net/bug.php?id=54254 + * + * @return bool + */ + public static function emulateBug54254() { + return version_compare(PHP_VERSION, '5.5.0', '<'); + } + + /** + * Do we need to emulate PHP bug #67960? + * + * This bug relates to the constants CAL_DOW_SHORT and CAL_DOW_LONG. + * + * It will hopefully be fixed in PHP 5.6.1 + * + * @link https://bugs.php.net/bug.php?id=67960 + * @link https://github.com/php/php-src/pull/806 + * + * @return bool + */ + public static function emulateBug67960() { + return true; + } + + /** + * Return the number of days in a month for a given year and calendar. + * + * Shim implementation of \cal_days_in_month() + * + * @link https://php.net/cal_days_in_month + * + * @param $calendar + * @param $month + * @param $year + * + * @return int The number of days in the specified month + */ + public static function calDaysInMonth($calendar, $month, $year) { + switch ($calendar) { + case CAL_GREGORIAN: + $gregorian = new GregorianCalendar; + return $gregorian->daysInMonth($year, $month); + + case CAL_JULIAN: + $julian= new JulianCalendar; + return $julian->daysInMonth($year, $month); + + case CAL_FRENCH: + $french = new FrenchCalendar(); + return $french->daysInMonth($year, $month); + + case CAL_JEWISH: + $jewish = new JewishCalendar(); + return $jewish->daysInMonth($year, $month); + + default: + return trigger_error('invalid calendar ID ' . $calendar . '.', E_USER_WARNING); + } + } + + /** + * Converts from Julian Day Count to a supported calendar. + * + * Shim implementation of \cal_from_jd() + * + * @link https://php.net/cal_from_jd + * + * @param int $jd Julian Day number + * @param int $calendar Calendar constant + * + * @return array + */ + public static function calFromJd($jd, $calendar) { + switch ($calendar) { + case CAL_FRENCH: + $french = new FrenchCalendar; + return $french->calFromJd($jd); + + case CAL_GREGORIAN: + $gregorian = new GregorianCalendar; + return $gregorian->calFromJd($jd); + + case CAL_JEWISH: + $jewish = new JewishCalendar; + return $jewish->calFromJd($jd); + + case CAL_JULIAN: + $julian = new JulianCalendar; + return $julian->calFromJd($jd); + + default: + return trigger_error('invalid calendar ID ' . $calendar, E_USER_WARNING); + } + } + + /** + * Returns information about a particular calendar. + * + * Shim implementation of \cal_info() + * + * @link https://php.net/cal_info + * + * @param int $calendar + * + * @return array + */ + public static function calInfo($calendar) { + switch ($calendar) { + case -1: + return array( + CAL_GREGORIAN => static::calInfo(CAL_GREGORIAN), + CAL_JULIAN => static::calInfo(CAL_JULIAN), + CAL_JEWISH => static::calInfo(CAL_JEWISH), + CAL_FRENCH => static::calInfo(CAL_FRENCH), + ); + case CAL_FRENCH: + $french = new FrenchCalendar; + return $french->phpCalInfo(); + + case CAL_GREGORIAN: + $gregorian = new GregorianCalendar; + return $gregorian->phpCalInfo(); + + case CAL_JEWISH: + $jewish = new JewishCalendar; + return $jewish->phpCalInfo(); + + case CAL_JULIAN: + $julian = new JulianCalendar; + return $julian->phpCalInfo(); + + default: + return trigger_error('invalid calendar ID ' . $calendar . '.', E_USER_WARNING); + } + } + + /** + * Converts from a supported calendar to Julian Day Count + * + * Shim implementation of \cal_to_jd() + * + * @link https://php.net/cal_to_jd + * + * @param int $calendar + * @param int $month + * @param int $day + * @param int $year + * + * @return int + */ + public static function calToJd($calendar, $month, $day, $year) { + switch ($calendar) { + case CAL_FRENCH: + return Shim::frenchToJd($month, $day, $year); + + case CAL_GREGORIAN: + return Shim::gregorianToJd($month, $day, $year); + + case CAL_JEWISH: + return Shim::jewishToJd($month, $day, $year); + + case CAL_JULIAN: + return Shim::julianToJd($month, $day, $year); + + default: + return trigger_error('invalid calendar ID ' . $calendar . '.', E_USER_WARNING); + } + } + + /** + * Get Unix timestamp for midnight on Easter of a given year. + * + * Shim implementation of \easter_date() + * + * @link https://php.net/easter_date + * + * @param int $year + * + * @return int + */ + public static function easterDate($year=null) { + if ($year === null) { + $year = date('Y'); + } elseif ($year < 1970 || $year > 2037) { + return trigger_error('This function is only valid for years between 1970 and 2037 inclusive', E_USER_WARNING); + } + $gregorian = new GregorianCalendar; + $days = $gregorian->easterDays($year); + + // Calculate time-zone offset + $date_time = new \DateTime('now', new \DateTimeZone(date_default_timezone_get())); + $offset_seconds = $date_time->format('Z'); + + if ($days < 11) { + return jdtounix($gregorian->ymdToJd($year, 3, $days + 21)) - $offset_seconds; + } else { + return jdtounix($gregorian->ymdToJd($year, 4, $days - 10)) - $offset_seconds; + } + } + + /** + * Get number of days after March 21 on which Easter falls for a given year. + * + * Shim implementation of \easter_days() + * + * @link https://php.net/easter_days + * + * @param int $year + * @param int $method Use the Julian or Gregorian calendar + * + * @return int + */ + public static function easterDays($year, $method) { + $julian = new JulianCalendar; + $gregorian = new GregorianCalendar; + + switch ($method) { + case CAL_EASTER_ROMAN: + if ($year <= 1582) { + return $julian->easterDays($year); + } else { + return $gregorian->easterDays($year); + } + case CAL_EASTER_ALWAYS_GREGORIAN: + return $gregorian->easterDays($year); + + case CAL_EASTER_ALWAYS_JULIAN: + return $julian->easterDays($year); + + default: // CAL_EASTER_DEFAULT or any other value + if ($year <= 1752) { + return $julian->easterDays($year); + } else { + return $gregorian->easterDays($year); + } + } + } + + /** + * Converts a date from the French Republican Calendar to a Julian Day Count. + * + * Shim implementation of \FrenchToJD() + * + * @link https://php.net/FrenchToJD + * + * @param int $month + * @param int $day + * @param int $year + * + * @return int + */ + public static function frenchToJd($month, $day, $year) { + if ($year == 0) { + return 0; + } else { + $french = new FrenchCalendar; + + return $french->ymdToJd($year, $month, $day); + } + } + + /** + * Converts a Gregorian date to Julian Day Count. + * + * Shim implementation of \GregorianToJD() + * + * @link https://php.net/GregorianToJD + * + * @param int $month + * @param int $day + * @param int $year + * + * @return int + */ + public static function gregorianToJd($month, $day, $year) { + if ($year == 0) { + return 0; + } else { + $gregorian = new GregorianCalendar; + + return $gregorian->ymdToJd($year, $month, $day); + } + } + + /** + * Returns the day of the week. + * + * Shim implementation of \JDDayOfWeek() + * + * @link https://php.net/JDDayOfWeek + * @link https://bugs.php.net/bug.php?id=67960 + * + * @param int $julianday + * @param int $mode + * + * @return mixed + */ + public static function jdDayOfWeek($julianday, $mode) { + $julian = new JulianCalendar; + $dow = $julian->dayOfWeek($julianday); + + switch ($mode) { + case 1: // 1, not CAL_DOW_LONG - see bug 67960 + return $julian->dayName($dow); + case 2: // 2, not CAL_DOW_SHORT - see bug 67960 + return $julian->dayNameAbbreviated($dow); + default: // CAL_DOW_DAYNO or anything else + return $dow; + } + } + + /** + * Returns a month name. + * + * Shim implementation of \JDMonthName() + * + * @link https://php.net/JDMonthName + * + * @param int $julianday + * @param int $mode + * + * @return mixed + */ + public static function jdMonthName($julianday, $mode) { + switch ($mode) { + case CAL_MONTH_GREGORIAN_LONG: + $gregorian = new GregorianCalendar; + return $gregorian->jdMonthName($julianday); + + case CAL_MONTH_JULIAN_LONG: + $julian = new JulianCalendar; + return $julian->jdMonthName($julianday); + + case CAL_MONTH_JULIAN_SHORT: + $julian = new JulianCalendar; + return $julian->jdMonthNameAbbreviated($julianday); + + case CAL_MONTH_JEWISH: + $jewish = new JewishCalendar; + return $jewish->jdMonthName($julianday); + + case CAL_MONTH_FRENCH: + $french = new FrenchCalendar; + return $french->jdMonthName($julianday); + + default: // CAL_MONTH_GREGORIAN_SHORT and anything else + $gregorian = new GregorianCalendar; + return $gregorian->jdMonthNameAbbreviated($julianday); + } + } + + /** + * Converts a Julian Day Count to the French Republican Calendar. + * + * Shim implementation of \JDToFrench() + * + * @link https://php.net/JDToFrench + * + * @param int $juliandaycount A Julian Day number + * + * @return string A string of the form "month/day/year" + */ + public static function jdToFrench($juliandaycount) { + if ($juliandaycount >= FrenchCalendar::JD_START && $juliandaycount <= FrenchCalendar::JD_END) { + $french = new FrenchCalendar; + $cal = $french->calFromJd($juliandaycount); + + return $cal['date']; + } else { + return '0/0/0'; + } + } + + /** + * Converts Julian Day Count to Gregorian date. + * + * Shim implementation of \JDToGregorian() + * + * @link https://php.net/JDToGregorian + * + * @param int $julianday A Julian Day number + * + * @return string A string of the form "month/day/year" + */ + public static function jdToGregorian($julianday) { + $gregorian = new GregorianCalendar; + $cal = $gregorian->calFromJd($julianday); + + return $cal['date']; + } + + /** + * Converts a Julian day count to a Jewish calendar date. + * + * Shim implementation of \JdtoJjewish() + * + * @link https://php.net/JdtoJewish + * + * @param int $juliandaycount A Julian Day number + * @param bool $hebrew If set, the date is returned in Hebrew text + * @param int $fl If set, then add alafim and gereshayim to the text + * + * @return string A string of the form "month/day/year" + */ + public static function jdToJewish($juliandaycount, $hebrew = false, $fl = 0) { + $jewish = new JewishCalendar; + + if ($hebrew) { + $hebrew = $jewish->jdToHebrew($juliandaycount, $fl & CAL_JEWISH_ADD_ALAFIM_GERESH, $fl & CAL_JEWISH_ADD_ALAFIM, $fl & CAL_JEWISH_ADD_GERESHAYIM); + // Our code generates Hebrew punctuation. PHP uses ASCII punctuation. + $hebrew = strtr($hebrew, array(JewishCalendar::GERESH => "'", JewishCalendar::GERSHAYIM => '"')); + // Our code generates UTF-8. PHP generates ISO-8859=8 + $hebrew = mb_convert_encoding($hebrew, 'ISO-8859-8', 'UTF-8'); + + return $hebrew; + } else { + list($year, $month, $day) = $jewish->jdToYmd($juliandaycount); + + return $month . '/' . $day . '/' . $year; + } + } + + /** + * Converts a Julian Day Count to a Julian Calendar Date. + * + * Shim implementation of \JDToJulian() + * + * @link https://php.net/JDToJulian + * + * @param int $julianday A Julian Day number + * + * @return string A string of the form "month/day/year" + */ + public static function jdToJulian($julianday) { + $julian = new JulianCalendar; + $cal = $julian->calFromJd($julianday); + + return $cal['date']; + } + + /** + * Convert Julian Day to Unix timestamp. + * + * Shim implementation of \jdtounix() + * + * @link https://php.net/jdtounix + * + * @param int $jday + * + * @return int + */ + public static function jdToUnix($jday) { + if ($jday >= 2440588 && $jday <= 2465343) { + return (int)($jday - 2440588) * 86400; + } else { + return false; + } + } + + /** + * Converts a date in the Jewish Calendar to Julian Day Count. + * + * Shim implementation of \JewishToJD() + * + * @link https://php.net/JewishToJD + * + * @param int $month + * @param int $day + * @param int $year + * + * @return int + */ + public static function jewishToJD($month, $day, $year) { + if ($year == 0) { + return 0; + } else { + $jewish = new JewishCalendar; + + return $jewish->ymdToJd($year, $month, $day); + } + } + + /** + * Converts a Julian Calendar date to Julian Day Count. + * + * Shim implementation of \JdToJulian() + * + * @link https://php.net/JdToJulian + * + * @param int $month + * @param int $day + * @param int $year + * + * @return int + */ + public static function julianToJd($month, $day, $year) { + if ($year == 0) { + return 0; + } else { + $julian = new JulianCalendar; + + return $julian->ymdToJd($year, $month, $day); + } + } + + /** + * Convert Unix timestamp to Julian Day. + * + * Shim implementation of \unixtojd() + * + * @link https://php.net/unixtojd + * + * @param int $timestamp + * + * @return int + */ + public static function unixToJd($timestamp) { + if ($timestamp < 0) { + return false; + } else { + // Convert timestamp based on local timezone + return static::GregorianToJd(gmdate('n', $timestamp), gmdate('j', $timestamp), gmdate('Y', $timestamp)); + } + } +} diff --git a/library/fisharebest/ext-calendar/src/shims.php b/library/fisharebest/ext-calendar/src/shims.php new file mode 100644 index 0000000000..e3cf1df6dd --- /dev/null +++ b/library/fisharebest/ext-calendar/src/shims.php @@ -0,0 +1,120 @@ +<?php +/** + * Link the global functions and constants from ext/calendar interface to our + * own shim functions. + * + * @author Greg Roach <fisharebest@gmail.com> + * @copyright (c) 2014 Greg Roach + * @license 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 3 of the License, 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, see <http://www.gnu.org/licenses/>. + */ + +use Fisharebest\ExtCalendar\FrenchCalendar; +use Fisharebest\ExtCalendar\GregorianCalendar; +use Fisharebest\ExtCalendar\JewishCalendar; +use Fisharebest\ExtCalendar\JulianCalendar; +use Fisharebest\ExtCalendar\Shim; + +define(FrenchCalendar::PHP_CALENDAR_NAME, FrenchCalendar::PHP_CALENDAR_NUMBER); +define(GregorianCalendar::PHP_CALENDAR_NAME, GregorianCalendar::PHP_CALENDAR_NUMBER); +define(JewishCalendar::PHP_CALENDAR_NAME, JewishCalendar::PHP_CALENDAR_NUMBER); +define(JulianCalendar::PHP_CALENDAR_NAME, JulianCalendar::PHP_CALENDAR_NUMBER); +define('CAL_NUM_CALS', 4); +define('CAL_DOW_DAYNO', 0); +define('CAL_DOW_SHORT', Shim::emulateBug67960() ? 1 : 2); +define('CAL_DOW_LONG', Shim::emulateBug67960() ? 2 : 1); +define('CAL_MONTH_GREGORIAN_SHORT', 0); +define('CAL_MONTH_GREGORIAN_LONG', 1); +define('CAL_MONTH_JULIAN_SHORT', 2); +define('CAL_MONTH_JULIAN_LONG', 3); +define('CAL_MONTH_JEWISH', 4); +define('CAL_MONTH_FRENCH', 5); +define('CAL_EASTER_DEFAULT', 0); +define('CAL_EASTER_ROMAN', 1); +define('CAL_EASTER_ALWAYS_GREGORIAN', 2); +define('CAL_EASTER_ALWAYS_JULIAN', 3); +define('CAL_JEWISH_ADD_ALAFIM_GERESH', 2); +define('CAL_JEWISH_ADD_ALAFIM', 4); +define('CAL_JEWISH_ADD_GERESHAYIM ', 8); + +function cal_days_in_month($calendar, $month, $year) { + return Shim::calDaysInMonth($calendar, $month, $year); +} + +function cal_from_jd($jd, $calendar) { + return Shim::calFromJd($jd, $calendar); +} + +function cal_info($calendar = -1) { + return Shim::calInfo($calendar); +} + +function cal_to_jd($calendar, $month, $day, $year) { + return Shim::calToJd($calendar, $month, $day, $year); +} + +function easter_date($year = null) { + return Shim::easterDate($year); +} + +function easter_days($year=null, $method=CAL_EASTER_DEFAULT) { + return Shim::easterDays($year ? $year : date('Y'), $method); +} + +function FrenchToJD($month, $day, $year) { + return Shim::FrenchToJD($month, $day, $year); +} + +function GregorianToJD($month, $day, $year) { + return Shim::GregorianToJD($month, $day, $year); +} + +function JDDayOfWeek($julianday, $mode = CAL_DOW_DAYNO) { + return Shim::JDDayOfWeek($julianday, $mode); +} + +function JDMonthName($julianday, $mode) { + return Shim::JDMonthName($julianday, $mode); +} + +function JDToFrench($juliandaycount) { + return Shim::JDToFrench($juliandaycount); +} + +function JDToGregorian($julianday) { + return Shim::JDToGregorian($julianday); +} + +function jdtojewish($juliandaycount, $hebrew = false, $fl = 0) { + return Shim::jdtojewish($juliandaycount, $hebrew, $fl); +} + +function JDToJulian($julianday) { + return Shim::JDToJulian($julianday); +} + +function jdtounix($jday) { + return Shim::jdtounix($jday); +} + +function JewishToJD($month, $day, $year) { + return Shim::JewishToJD($month, $day, $year); +} + +function JulianToJD($month, $day, $year) { + return Shim::JulianToJD($month, $day, $year); +} + +function unixtojd($timestamp = null) { + return Shim::unixtojd($timestamp ? $timestamp : time()); +} |
