Compare commits

...

71 Commits

Author SHA1 Message Date
Laurence Withers 82aa0f2f20 Bump version 2015-06-12 11:01:21 +01:00
Laurence Withers 2e610c4db3 Add 2015-06-30 leap second to built-in table 2015-06-12 10:55:53 +01:00
Laurence Withers dd3fe182af Update build system (Doxyfile) 2015-06-12 10:53:31 +01:00
Laurence Withers 0d5c2078b3 Bump version 2013-04-05 09:35:19 +00:00
Laurence Withers 6b25293f35 Fix build system ordering
Make sure that the library is built before either of the isodate or setisodate
apps, otherwise we end up with problems compiling from scratch.
2013-04-05 09:32:22 +00:00
Laurence Withers dcb15f2081 Bump version 2012-01-07 17:42:57 +00:00
Laurence Withers 4c470ec1e9 Bump version 2012-01-07 17:42:57 +00:00
Laurence Withers ba81c458e1 Update for 2012-06-30 leap second 2012-01-07 17:42:44 +00:00
Laurence Withers 49536349f7 Bump version 2011-11-18 12:19:09 +00:00
Laurence Withers c7b38eab5a Bump version 2011-11-18 12:19:09 +00:00
Laurence Withers c198eaf34b Remove accidental variable definition 2011-11-16 12:04:15 +00:00
Laurence Withers b622223b64 Remove unneeded nonnull attribute 2011-11-16 09:58:21 +00:00
Laurence Withers 81df4e8a05 Add C++ extern C include guards 2011-11-15 16:49:46 +00:00
Laurence Withers 7a5f30bfcd Update build system 2011-11-15 16:49:46 +00:00
Laurence Withers d910e88bc3 Bump version 2010-08-02 12:01:43 +00:00
Laurence Withers 2aa3d5aaac Bump version 2010-08-02 12:01:43 +00:00
Laurence Withers d013bff263 Remove incorrect nonnull attribute
iso8601_leap_table_load() had an incorrect ‘nonnull’ attribute, which could
lead to crashes as the compiled code could omit the test for null which then
substitutes the system default. Fix it by removing the attribute.
2010-08-02 12:00:20 +00:00
Laurence Withers 8cc71fb069 Bump version 2010-06-18 12:49:36 +00:00
Laurence Withers 2d9621e0a5 Bump version 2010-06-18 12:49:33 +00:00
Laurence Withers 944331702f Cope with negative C library timestamps
While perhaps not strictly valid, negative timestamps can potentially be
returned by the C library and passed in to iso8601_from_ts() etc. This commit
adds a check for such timestamps and copes with the gracefully, whereas
previously invalid ISO8601 timestamps would have been generated.
2010-06-18 12:47:38 +00:00
Laurence Withers 0d56edd890 Bump version 2010-01-28 16:22:46 +00:00
Laurence Withers 65fe613272 Bump version 2010-01-28 16:22:39 +00:00
Laurence Withers 64e06d17c4 Add explicit note on params which may be null 2010-01-28 16:22:03 +00:00
Laurence Withers cd9a2d252a iso8601_difference(): incorrect nonnull attribute
iso8601_difference was incorrectly marked as having all of its attributes
required to be non-null, but this is not the case according to the docs or
the implementation. This was causing a crash as the compiled library was
taking advantage of the nonnull notation to remove some tests against null
which, according to the docs, are actually required.

Fix by tightening the nonnull specification to those parameters which actually
require it.
2010-01-28 16:17:53 +00:00
Laurence Withers ed05b5c872 Bump version 2010-01-08 19:26:21 +00:00
Laurence Withers bd4ddd7621 Write brief description for library
Add a brief description of the library on the doxygen front page.
2010-01-08 19:25:58 +00:00
Laurence Withers 420df40a5b Bring C code up to coding standards
Reformatting and comment improvement only. No code changes.
2010-01-08 19:22:36 +00:00
Laurence Withers 9f3fbd4116 Minor build system update
A fix for when there are multiple libraries in a single package. Doesn't apply
here but just in case.
2010-01-08 18:49:50 +00:00
Laurence Withers 6285d214aa Documentation and annotation updates
This commit updates the Doxygen documentation (particularly taking into account
a request from Kelly Dunlop for more verbose notice about which members are
changed by iso8601_from_*()) and file/function annotation/formatting to current
standards.
2010-01-08 18:46:34 +00:00
Laurence Withers e504a722f5 Bump version 2009-05-29 14:52:52 +00:00
Laurence Withers 1a0553e083 Bump version 2009-05-29 14:52:52 +00:00
Laurence Withers 7ba82a2988 Add default path for iso8601_leap_table_load() 2009-05-29 14:51:26 +00:00
Laurence Withers b7d039a32c Add iso8601_elapsed_divide() 2009-05-29 14:51:14 +00:00
Laurence Withers dcfc699af0 Bump version 2009-04-06 14:58:26 +00:00
Laurence Withers 47ccc537b8 Bump version 2009-04-06 14:58:26 +00:00
Laurence Withers 1d9447dcd2 Add iso8601_cmp(), comparison function for qsort() 2009-04-06 14:58:05 +00:00
Laurence Withers d09e55e8ae Update build system 2009-04-06 14:56:00 +00:00
Laurence Withers 3309803b9e Bump version 2009-02-12 02:32:14 +00:00
Laurence Withers 076d215860 Bump version 2009-02-12 02:32:14 +00:00
Laurence Withers 6937f2f12b Bugfix in rounding of iso8601_add_elapsed()
When adding a number of nanoseconds so that the resultant date lies exactly on
a second boundary, rounding was not performed when it should have been. Fix
the rounding so that the seconds field rolls over.
2009-02-12 02:31:04 +00:00
Laurence Withers c4679ded38 Bump version 2009-01-03 19:40:43 +00:00
Laurence Withers 557c7e1efb Bump version 2009-01-03 19:40:43 +00:00
Laurence Withers 711872660b Add documentation for leap second table functions 2009-01-03 19:40:16 +00:00
Laurence Withers ddaa9a8f8d Link to enumerations correctly in documentation 2009-01-03 19:34:17 +00:00
Laurence Withers 394dc40b97 Add mechanism to free old leap table
To avoid memory leaks, we add a simple internal mechanism to free the table
of leap seconds if it was previously loaded from disk. The user is still
responsible for freeing their own tables, of course.
2009-01-03 19:33:24 +00:00
Laurence Withers 73df6f836d Remove stale pkgconfig file 2009-01-03 19:22:21 +00:00
Laurence Withers a0f747067d Add functions to load/set leap second table 2009-01-03 19:21:46 +00:00
Laurence Withers e908febba2 Move -std=gnu99 to correct place, use _GNU_SOURCE 2009-01-03 19:21:25 +00:00
Laurence Withers 2c016208f2 Use dynamic table for leap second days 2009-01-03 19:08:21 +00:00
Laurence Withers 849744bb07 Use std=gnu99
This allows us to use C99 features such as lldiv along with GNU extensions
and POSIX, etc.
2009-01-03 19:04:54 +00:00
Laurence Withers 2d51d09c83 Upgrade build system 2008-12-20 22:36:28 +00:00
Laurence Withers cad2283f6a Add iso8601_add_multiple()
Add a function to efficiently add (or subtract) a multiple of a period
to a timestamp.
2008-12-11 15:37:12 +00:00
Laurence Withers d431f68054 Add leap second for 2008-12-31
Add an entry to record the leap second at 2008-12-31, and tidy up the table
formatting.
2008-12-10 18:24:32 +00:00
Laurence Withers 0d504baaee Bump version 2008-01-07 13:17:17 +00:00
Laurence Withers 93efb6294d Bump version 2008-01-07 13:16:22 +00:00
Laurence Withers 6246f8ebcd Fix missing null termination when tz_sec = 0, add test for it 2008-01-07 13:16:18 +00:00
Laurence Withers 048196532e Bump version 2008-01-04 22:47:16 +00:00
Laurence Withers 1f43445813 Fix bug where iso8601_print() could overrun...
If a date with a timezone offset of 0 had a length exactly matching the
buffer size, we would write past the end of the buffer.
2008-01-04 22:47:10 +00:00
Laurence Withers 69628be83e Bump version 2007-12-05 17:45:34 +00:00
Laurence Withers a4e99696a1 Update build system 2007-12-05 17:45:23 +00:00
Laurence Withers 21adf7acf0 Add setisodate utility.
This utility is capable of setting the system clock, taking its argument
in ISO8601 format.
2007-09-13 11:16:23 +00:00
Laurence Withers 64c5ce0d65 Add the isodate utility.
This utility provides a way to display dates in ISO8601 format. The
exact output format can be controlled. It can either display dates
passed on the commandline or it can display the current date/time.
2007-09-13 11:09:32 +00:00
Laurence Withers 07a31fd8f7 Add doxygen markup for date/time precision enumerations 2007-09-13 11:08:51 +00:00
Laurence Withers c47e0e241f Add functions for converting to/from a struct timespec, and for setting
the system clock
2007-09-13 11:05:51 +00:00
Laurence Withers 2a51412ade Upgrade build system, bump version 2007-09-13 10:25:31 +00:00
Laurence Withers 7d35697775 Bump version 2007-08-21 09:54:57 +00:00
Laurence Withers f49b80de01 Add iso8601_difference() 2007-08-21 09:54:51 +00:00
Laurence Withers 4deda83c58 Bump version 2007-07-29 11:58:18 +00:00
Laurence Withers 6ae62fb796 Bump version 2007-07-29 11:58:18 +00:00
Laurence Withers ec6c9a41f7 Fix up elapsed manipulation 2007-07-29 11:57:13 +00:00
Laurence Withers e59586b265 Add an RPN date manipulator 2007-07-29 11:53:55 +00:00
54 changed files with 4775 additions and 731 deletions

1
.gitignore vendored
View File

@ -1,2 +1,3 @@
obj
html
.*.swp

846
COPYING
View File

@ -1,278 +1,674 @@
GNU GENERAL PUBLIC LICENSE
Version 2, June 1991
Version 3, 29 June 2007
Copyright (C) 1989, 1991 Free Software Foundation, Inc.
59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
Copyright (C) 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 licenses for most software are designed to take away your
freedom to share and change it. By contrast, the GNU General Public
License is intended to guarantee your freedom to share and change free
software--to make sure the software is free for all its users. This
General Public License applies to most of the Free Software
Foundation's software and to any other program whose authors commit to
using it. (Some other Free Software Foundation software is covered by
the GNU Library General Public License instead.) You can apply it to
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
this service 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.
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 make restrictions that forbid
anyone to deny you these rights or to ask you to surrender the rights.
These restrictions translate to certain responsibilities for you if you
distribute copies of the software, or if you modify it.
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 give the recipients all the rights that
you have. You must make sure that they, too, receive or can get the
source code. And you must show them these terms so they know their
rights.
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.
We protect your rights with two steps: (1) copyright the software, and
(2) offer you this license which gives you legal permission to copy,
distribute and/or modify the software.
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.
Also, for each author's protection and ours, we want to make certain
that everyone understands that there is no warranty for this free
software. If the software is modified by someone else and passed on, we
want its recipients to know that what they have is not the original, so
that any problems introduced by others will not reflect on the original
authors' reputations.
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.
Finally, any free program is threatened constantly by software
patents. We wish to avoid the danger that redistributors of a free
program will individually obtain patent licenses, in effect making the
program proprietary. To prevent this, we have made it clear that any
patent must be licensed for everyone's free use or not licensed at all.
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.
GNU GENERAL PUBLIC LICENSE
TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
0. This License applies to any program or other work which contains
a notice placed by the copyright holder saying it may be distributed
under the terms of this General Public License. The "Program", below,
refers to any such program or work, and a "work based on the Program"
means either the Program or any derivative work under copyright law:
that is to say, a work containing the Program or a portion of it,
either verbatim or with modifications and/or translated into another
language. (Hereinafter, translation is included without limitation in
the term "modification".) Each licensee is addressed as "you".
TERMS AND CONDITIONS
Activities other than copying, distribution and modification are not
covered by this License; they are outside its scope. The act of
running the Program is not restricted, and the output from the Program
is covered only if its contents constitute a work based on the
Program (independent of having been made by running the Program).
Whether that is true depends on what the Program does.
0. Definitions.
1. You may copy and distribute verbatim copies of the Program's
source code as you receive it, in any medium, provided that you
conspicuously and appropriately publish on each copy an appropriate
copyright notice and disclaimer of warranty; keep intact all the
notices that refer to this License and to the absence of any warranty;
and give any other recipients of the Program a copy of this License
along with the Program.
"This License" refers to version 3 of the GNU General Public License.
You may charge a fee for the physical act of transferring a copy, and
you may at your option offer warranty protection in exchange for a fee.
"Copyright" also means copyright-like laws that apply to other kinds of
works, such as semiconductor masks.
2. You may modify your copy or copies of the Program or any portion
of it, thus forming a work based on the Program, and copy and
distribute such modifications or work under the terms of Section 1
above, provided that you also meet all of these conditions:
"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.
a) You must cause the modified files to carry prominent notices
stating that you changed the files and the date of any change.
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.
b) You must cause any work that you distribute or publish, that in
whole or in part contains or is derived from the Program or any
part thereof, to be licensed as a whole at no charge to all third
parties under the terms of this License.
A "covered work" means either the unmodified Program or a work based
on the Program.
c) If the modified program normally reads commands interactively
when run, you must cause it, when started running for such
interactive use in the most ordinary way, to print or display an
announcement including an appropriate copyright notice and a
notice that there is no warranty (or else, saying that you provide
a warranty) and that users may redistribute the program under
these conditions, and telling the user how to view a copy of this
License. (Exception: if the Program itself is interactive but
does not normally print such an announcement, your work based on
the Program is not required to print an announcement.)
These requirements apply to the modified work as a whole. If
identifiable sections of that work are not derived from the Program,
and can be reasonably considered independent and separate works in
themselves, then this License, and its terms, do not apply to those
sections when you distribute them as separate works. But when you
distribute the same sections as part of a whole which is a work based
on the Program, the distribution of the whole must be on the terms of
this License, whose permissions for other licensees extend to the
entire whole, and thus to each and every part regardless of who wrote it.
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.
Thus, it is not the intent of this section to claim rights or contest
your rights to work written entirely by you; rather, the intent is to
exercise the right to control the distribution of derivative or
collective works based on the Program.
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.
In addition, mere aggregation of another work not based on the Program
with the Program (or with a work based on the Program) on a volume of
a storage or distribution medium does not bring the other work under
the scope of this License.
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.
3. You may copy and distribute the Program (or a work based on it,
under Section 2) in object code or executable form under the terms of
Sections 1 and 2 above provided that you also do one of the following:
1. Source Code.
a) Accompany it with the complete corresponding machine-readable
source code, which must be distributed under the terms of Sections
1 and 2 above on a medium customarily used for software interchange; or,
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.
b) Accompany it with a written offer, valid for at least three
years, to give any third party, for a charge no more than your
cost of physically performing source distribution, a complete
machine-readable copy of the corresponding source code, to be
distributed under the terms of Sections 1 and 2 above on a medium
customarily used for software interchange; or,
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.
c) Accompany it with the information you received as to the offer
to distribute corresponding source code. (This alternative is
allowed only for noncommercial distribution and only if you
received the program in object code or executable form with such
an offer, in accord with Subsection b above.)
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 source code for a work means the preferred form of the work for
making modifications to it. For an executable work, complete source
code means all the source code for all modules it contains, plus any
associated interface definition files, plus the scripts used to
control compilation and installation of the executable. However, as a
special exception, the source code distributed need not include
anything that is normally distributed (in either source or binary
form) with the major components (compiler, kernel, and so on) of the
operating system on which the executable runs, unless that component
itself accompanies the executable.
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.
If distribution of executable or object code is made by offering
access to copy from a designated place, then offering equivalent
access to copy the source code from the same place counts as
distribution of the source code, even though third parties are not
compelled to copy the source along with the object code.
4. You may not copy, modify, sublicense, or distribute the Program
except as expressly provided under this License. Any attempt
otherwise to copy, modify, sublicense or distribute the Program is
void, and will automatically terminate your rights under this License.
However, parties who have received copies, or rights, from you under
this License will not have their licenses terminated so long as such
parties remain in full compliance.
The Corresponding Source need not include anything that users
can regenerate automatically from other parts of the Corresponding
Source.
5. You are not required to accept this License, since you have not
signed it. However, nothing else grants you permission to modify or
distribute the Program or its derivative works. These actions are
prohibited by law if you do not accept this License. Therefore, by
modifying or distributing the Program (or any work based on the
Program), you indicate your acceptance of this License to do so, and
all its terms and conditions for copying, distributing or modifying
the Program or works based on it.
The Corresponding Source for a work in source code form is that
same work.
6. Each time you redistribute the Program (or any work based on the
Program), the recipient automatically receives a license from the
original licensor to copy, distribute or modify the Program subject to
these terms and conditions. You may not impose any further
restrictions on the recipients' exercise of the rights granted herein.
You are not responsible for enforcing compliance by third parties to
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.
7. If, as a consequence of a court judgment or allegation of patent
infringement or for any other reason (not limited to patent issues),
conditions are imposed on you (whether by court order, agreement or
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
distribute so as to satisfy simultaneously your obligations under this
License and any other pertinent obligations, then as a consequence you
may not distribute the Program at all. For example, if a patent
license would not permit royalty-free redistribution of the Program by
all those who receive copies directly or indirectly through you, then
the only way you could satisfy both it and this License would be to
refrain entirely from distribution of the Program.
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.
If any portion of this section is held invalid or unenforceable under
any particular circumstance, the balance of the section is intended to
apply and the section as a whole is intended to apply in other
circumstances.
13. Use with the GNU Affero General Public License.
It is not the purpose of this section to induce you to infringe any
patents or other property right claims or to contest validity of any
such claims; this section has the sole purpose of protecting the
integrity of the free software distribution system, which is
implemented by public license practices. Many people have made
generous contributions to the wide range of software distributed
through that system in reliance on consistent application of that
system; it is up to the author/donor to decide if he or she is willing
to distribute software through any other system and a licensee cannot
impose that choice.
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.
This section is intended to make thoroughly clear what is believed to
be a consequence of the rest of this License.
8. If the distribution and/or use of the Program is restricted in
certain countries either by patents or by copyrighted interfaces, the
original copyright holder who places the Program under this License
may add an explicit geographical distribution limitation excluding
those countries, so that distribution is permitted only in or among
countries not thus excluded. In such case, this License incorporates
the limitation as if written in the body of this License.
14. Revised Versions of this License.
9. The Free Software Foundation may publish revised and/or new versions
of the General Public License from time to time. Such new versions will
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 a version number of this License which applies to it and "any
later version", you have the option of following the terms and conditions
either of that version or of any later version published by the Free
Software Foundation. If the Program does not specify a version number of
this License, you may choose any version ever published by the Free Software
Foundation.
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.
10. If you wish to incorporate parts of the Program into other free
programs whose distribution conditions are different, write to the author
to ask for permission. For software which is copyrighted by the Free
Software Foundation, write to the Free Software Foundation; we sometimes
make exceptions for this. Our decision will be guided by the two goals
of preserving the free status of all derivatives of our free software and
of promoting the sharing and reuse of software generally.
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.
NO WARRANTY
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.
11. BECAUSE THE PROGRAM IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY
FOR THE PROGRAM, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN
OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES
PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED
OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS
TO THE QUALITY AND PERFORMANCE OF THE PROGRAM IS WITH YOU. SHOULD THE
PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING,
REPAIR OR CORRECTION.
15. Disclaimer of Warranty.
12. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING
WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR
REDISTRIBUTE THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES,
INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING
OUT OF THE USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED
TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY
YOU OR THIRD PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER
PROGRAMS), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE
POSSIBILITY OF SUCH DAMAGES.
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>.

8
README
View File

@ -1,7 +1,7 @@
libiso8601
========================================================================
(c)2007, Laurence Withers, <l@lwithers.me.uk>.
Released under the GNU GPLv2. See file COPYING or
Released under the GNU GPLv3. See file COPYING or
http://www.gnu.org/copyleft/gpl.html for details.
Really Quick Instructions
@ -14,4 +14,10 @@ You might want to edit 'config' first. You might also want to set
'INSTALL_PREFIX', which is prepended onto the destination of any
installed file.
Thanks
------
Robert Dunlop, <rjd@xyzzy.org.uk>
* found null terminating bug with iso8601_print()
@TODO@

36
config
View File

@ -1,20 +1,38 @@
# libiso8601/config
# kate: replace-trailing-space-save true; space-indent true; tab-width 4;
# vim: expandtab:ts=4:sw=4
# vim: syntax=sh:expandtab:ts=4:sw=4
#
# (c)2007, Laurence Withers, <l@lwithers.me.uk>.
# Released under the GNU GPLv2. See file COPYING or
# Released under the GNU GPLv3. See file COPYING or
# http://www.gnu.org/copyleft/gpl.html for details.
#
# This file contains options used to build libiso8601.
#
# PREFIX is the most important option. Many other paths are derived from it, as follows:
#
# PREFIX | / | /usr | /usr/local | /opt/*
# ------------+-------------------+-------------------+-------------------+-----------------
# BINDIR | /bin | /usr/bin | /usr/local/bin | /opt/*/bin
# SBINDIR | /sbin | /usr/sbin | /usr/local/sbin | /opt/*/sbin
# LIBDIR | /lib | /usr/lib | /usr/local/lib | /opt/*/lib
# INCLUDEDIR | /usr/include | /usr/include | /usr/local/include| /opt/*/include
# CONFIGDIR | /etc | /etc | /usr/local/etc | /etc/opt/*
# VARDIR | /var | /var | /var | /var/opt/*
# SHAREDIR | /usr/share | /usr/share | /usr/local/share | /opt/*/share
# DOCSDIR | /usr/share/doc | /usr/share/doc | /usr/local/share/doc, /opt/*/doc
# WEBDIR | /srv/http | /srv/http | /srv/http | /opt/*/http
#
# Specific notes:
# When installing, all paths are prepended with INSTALL_PREFIX.
# Any parameter can be overridden by setting an environment variable.
# CGIDIR is set to ${WEBDIR}/cgi-bin .
#
[ -z "${PREFIX}" ] && PREFIX="/usr/local"
source "scripts/paths"
# Project-specific variables below.
[ -z "${DEFAULT_LEAP_TABLE}" ] && DEFAULT_LEAP_TABLE="${SHAREDIR}/libiso8601/leap-table"
[ -z "${CC}" ] && CC="gcc"
[ -z "${CFLAGS}" ] && CFLAGS="-g -O2 -W -Wall"
[ -z "${BINDIR}" ] && BINDIR="${PREFIX}/bin"
[ -z "${LIBDIR}" ] && LIBDIR="${PREFIX}/lib"
[ -z "${INCLUDEDIR}" ] && INCLUDEDIR="${PREFIX}/include"
[ -z "${FINALLIBDIR}" ] && FINALLIBDIR="${LIBDIR}"
[ -z "${FINALINCLUDEDIR}" ] && FINALINCLUDEDIR="${INCLUDEDIR}"
[ -z "${DOCSDIR}" ] && DOCSDIR="${PREFIX}/doc/libiso8601"

13
make.sh
View File

@ -1,8 +1,8 @@
#!/bin/sh
#!/bin/bash
# libiso8601/make.sh
#
# (c)2006-2007, Laurence Withers, <l@lwithers.me.uk>.
# Released under the GNU GPLv2. See file COPYING or
# (c)2009, Laurence Withers, <l@lwithers.me.uk>.
# Released under the GNU GPLv3. See file COPYING or
# http://www.gnu.org/copyleft/gpl.html for details.
#
@ -15,19 +15,19 @@ then
echo "Configuration file not found???"
exit 1
fi
source "config" # don't fail on error, since last command in config might return false
source "./config" # don't fail on error, since last command in config might return false
# Get version information
source version || exit 1
source "./version" || exit 1
VERSION="${VERMAJOR}.${VERMINOR}.${VERMICRO}"
# Get standard functions
[ -z "${VERBOSE}" ] && VERBOSE="0"
source scripts/functions.sh || exit 1
source "./scripts/functions.sh" || exit 1
# List of directories which will be emptied by clean.
@ -292,5 +292,4 @@ done
exit 0
# kate: replace-trailing-space-save true; space-indent true; tab-width 4;
# vim: expandtab:ts=4:sw=4

View File

@ -1,9 +1,9 @@
#!/bin/sh
#!/bin/bash
# libiso8601/test.sh
#
# (c)2006, Laurence Withers, <l@lwithers.me.uk>.
# Released under the GNU GPLv2. See file COPYING or
# http://www.gnu.org/copyleft/gpl.html for details.
# Copyright: ©20092011, Güralp Systems Ltd.
# Author: Laurence Withers <lwithers@guralp.com>
# License: GPLv3
#
# Running this script on its own will display a summary of all the
@ -21,7 +21,7 @@ run_test() {
return 1
fi
LD_LIBRARY_PATH="obj" ${EXE} "$@" || return 1
LD_LIBRARY_PATH="obj:${LD_LIBRARY_PATH}" "${EXE}" "$@" || return 1
return 0
}
@ -33,14 +33,15 @@ print_tests() {
echo "---------------------------------------------------------------------"
for EXE in obj/tests/*
do
[ -x ${EXE} ] || continue
NAME=$(echo ${EXE} | sed 's,obj/tests/,,')
[ -x "${EXE}" ] || continue
NAME="$(echo "${EXE}" | sed 's,obj/tests/,,')"
echo -ne "${NAME}\t"
LD_LIBRARY_PATH="obj" ${EXE} --print-summary
LD_LIBRARY_PATH="obj:${LD_LIBRARY_PATH}" "${EXE}" --print-summary
done
}
# Main script
if [ $# -eq 0 ]
then
@ -48,7 +49,6 @@ then
exit 0
fi
run_test $*
run_test "$@"
# kate: replace-trailing-space-save true; space-indent true; tab-width 4;
# vim: expandtab:ts=4:sw=4

2
scripts/.gitignore vendored
View File

@ -7,8 +7,10 @@ build.docs.none
build.files.none
build.firmware.gpasm
build.firmware.sdcc
build.header.c
build.lib.c
build.lib.c++
build.make.none
build.module.c
build.tests.c
build.tests.c++

View File

@ -1,9 +1,8 @@
#!/bin/sh
# libiso8601/scripts/functions.sh
#
# (c)2006, Laurence Withers, <l@lwithers.me.uk>.
# Released under the GNU GPLv2. See file COPYING or
# http://www.gnu.org/copyleft/gpl.html for details.
# Copyright: ©20072011, Güralp Systems Ltd.
# Author: Laurence Withers, <lwithers@guralp.com>
# License: GPLv3
#
# Common functions
@ -64,5 +63,4 @@ do_cmd_redir() {
fi
}
# kate: replace-trailing-space-save true; space-indent true; tab-width 4;
# vim: expandtab:ts=4:sw=4

63
scripts/paths Normal file
View File

@ -0,0 +1,63 @@
# libiso8601/scripts/paths
#
# Copyright: ©2011, Güralp Systems Ltd.
# Author: Laurence Withers, <lwithers@guralp.com>
# License: GPLv3
#
# Default path setup. Not meant for editing; use environment variables
# to override values if needed.
#
MY_PREFIX="${PREFIX}"
[ "${MY_PREFIX}" == "/" ] && MY_PREFIX=""
[ -z "${BINDIR}" ] && BINDIR="${PREFIX}/bin"
[ -z "${SBINDIR}" ] && SBINDIR="${PREFIX}/sbin"
[ -z "${LIBDIR}" ] && LIBDIR="${PREFIX}/lib"
if [ -z "${INCLUDEDIR}" ]
then
case "${PREFIX}" in
/) INCLUDEDIR="/usr/include" ;;
*) INCLUDEDIR="${PREFIX}/include" ;;
esac
fi
if [ -z "${CONFIGDIR}" ]
then
case "${PREFIX}" in
/ | /usr) CONFIGDIR="/etc" ;;
/opt*) CONFIGDIR="/etc${PREFIX}" ;;
*) CONFIGDIR="${PREFIX}/etc" ;;
esac
fi
if [ -z "${VARDIR}" ]
then
case "${PREFIX}" in
/ | /usr | /usr/local) VARDIR="/var" ;;
/opt*) VARDIR="/var${PREFIX}" ;;
*) VARDIR="${PREFIX}/var" ;;
esac
fi
if [ -z "${SHAREDIR}" ]
then
case "${PREFIX}" in
/) SHAREDIR="/usr/share" ;;
*) SHAREDIR="${PREFIX}/share" ;;
esac
fi
[ -z "${DOCSDIR}" ] && DOCSDIR="${SHAREDIR}/doc"
if [ -z "${SRVDIR}" ]
then
case "${PREFIX}" in
/ | /usr | /usr/local) SRVDIR="/srv" ;;
*) SRVDIR="${PREFIX}/srv" ;;
esac
fi
[ -z "${WEBDIR}" ] && WEBDIR="${SRVDIR}/http"
[ -z "${CGIDIR}" ] && CGIDIR="${WEBDIR}/cgi-bin"
# vim: syntax=sh:expandtab:ts=4:sw=4

File diff suppressed because it is too large Load Diff

View File

@ -1,15 +1,20 @@
/* libiso8601/src/docs/MainPage.dox
*
* (c)2007, Laurence Withers, <l@lwithers.me.uk>.
* Released under the GNU GPLv2. See file COPYING or
* (c)2007-2010, Laurence Withers, <l@lwithers.me.uk>.
* Released under the GNU GPLv3. See file COPYING or
* http://www.gnu.org/copyleft/gpl.html for details.
*/
/*! \mainpage
/*! \mainpage Library for manipulating ISO8601-format dates and times
\c libiso8601 is a library containing conversion routines for ISO8601-format
dates and times and a set of associated date/time manipulation routines. It has
support for leap seconds and nanosecond accuracy and is intended for use in
e.g. physical science applications.
*/
/* options for text editors
kate: replace-trailing-space-save true; space-indent true; tab-width 4;
vim: expandtab:ts=4:sw=4
vim: expandtab:ts=4:sw=4:syntax=doxygen
*/

View File

@ -39,5 +39,4 @@ then
docs_BUILT=1
fi
# kate: replace-trailing-space-save true; space-indent true; tab-width 4;
# vim: expandtab:ts=4:sw=4
# vim: syntax=sh:expandtab:ts=4:sw=4

View File

@ -17,5 +17,4 @@ do
done
print_success "Documentation installed"
# kate: replace-trailing-space-save true; space-indent true; tab-width 4;
# vim: expandtab:ts=4:sw=4
# vim: syntax=sh:expandtab:ts=4:sw=4

1
src/isodate/.params Normal file
View File

@ -0,0 +1 @@
app c isodate bin

147
src/isodate/000_TopSource.c Normal file
View File

@ -0,0 +1,147 @@
/* libiso8601/src/isodate/000_TopSource.c
*
* (c)2007, Laurence Withers, <l@lwithers.me.uk>.
* Released under the GNU GPLv2. See file COPYING or
* http://www.gnu.org/copyleft/gpl.html for details.
*/
/* Below are all the includes used throughout the application. */
#include <stdio.h>
#include <getopt.h>
#include <stdlib.h>
#include "iso8601.h"
void usage(void)
{
fputs("Usage:\n\n"
" isodate [options] [date ...]\n\n"
"Valid options:\n"
" -h, --help Display this screen.\n"
" -e, --extended Use extended display format.\n"
" -y, --year Only display year.\n"
" -n, --month Display year, month.\n"
" -d, --day Display year, month, day [default].\n"
" -o, --ordinal Display year, ordinal day.\n"
" -w, --week Display ISO year, ISO week.\n"
" -W, --week-day Display ISO year, ISO week, ISO weekday.\n"
" -r, --hour Display hour.\n"
" -R, --hour-fraction Display hour and fraction of hour.\n"
" -m, --minute Display hour, minute.\n"
" -M, --minute-fraction Display hour, minute and fraction of\n"
" minute.\n"
" -s, --second Display hour, minute, second.\n"
" -S, --second-fraction Display hour, minute, second and fraction\n"
" of second [default].\n"
" -U, --utc-offset <n> Display time as if timezone offset was\n"
" <n> seconds.\n"
" -Z, --utc Display time in UTC.\n"
"\n"
"If specified, one or more ISO8601 dates will be parsed and displayed\n"
"in the format given. Otherwise, the system clock will be queried and\n"
"its time displayed.\n"
"\n", stdout);
}
struct option options[] = {
{ "help", no_argument, 0, 'h' },
{ "extended", no_argument, 0, 'e' },
{ "year", no_argument, 0, 'y' },
{ "month", no_argument, 0, 'n' },
{ "day", no_argument, 0, 'd' },
{ "ordinal", no_argument, 0, 'o' },
{ "week", no_argument, 0, 'w' },
{ "week-day", no_argument, 0, 'W' },
{ "hour", no_argument, 0, 'r' },
{ "hour-fraction", no_argument, 0, 'R' },
{ "minute", no_argument, 0, 'm' },
{ "minute-fraction", no_argument, 0, 'M' },
{ "second", no_argument, 0, 's' },
{ "second-fraction", no_argument, 0, 'S' },
{ "utc-offset", required_argument, 0, 'U' },
{ "utc", no_argument, 0, 'Z' },
{ 0, 0, 0, 0 }
};
int main(int argc, char* argv[])
{
struct iso8601_details det = {
.date_prec = iso8601_prec_day,
.time_prec = iso8601_prec_secfrac,
.extended = 0,
.tz_sec = 0
};
int override_tz = 0, ret = 0;
char* endp, datestr[100];
struct iso8601_date date;
struct iso8601_details det2;
while(1) {
switch(getopt_long(argc, argv, "heyndowWrRmMsSU:Z", options, 0)) {
case -1: goto opts_done;
case '?': return 1;
case 'h': usage(); return 0;
case 'e': det.extended = 1; break;
case 'y': det.date_prec = iso8601_prec_year; break;
case 'n': det.date_prec = iso8601_prec_month; break;
case 'd': det.date_prec = iso8601_prec_day; break;
case 'o': det.date_prec = iso8601_prec_ord; break;
case 'w': det.date_prec = iso8601_prec_week; break;
case 'W': det.date_prec = iso8601_prec_wday; break;
case 'r': det.time_prec = iso8601_prec_hour; break;
case 'R': det.time_prec = iso8601_prec_hourfrac; break;
case 'm': det.time_prec = iso8601_prec_min; break;
case 'M': det.time_prec = iso8601_prec_minfrac; break;
case 's': det.time_prec = iso8601_prec_sec; break;
case 'S': det.time_prec = iso8601_prec_secfrac; break;
case 'Z': det.tz_sec = 0; override_tz = 1; break;
case 'U':
endp = 0;
det.tz_sec = strtol(optarg, &endp, 0);
if(!endp || *endp || det.tz_sec <= -86400 || det.tz_sec >= 86400) {
fputs("Invalid UTC offset. Must be an integral number of seconds\n"
"between -86400 and 86400, exclusive.\n", stderr);
return 1;
}
override_tz = 1;
}
}
opts_done:
if(optind == argc) {
iso8601_now(&date, &det2);
if(!override_tz) det.tz_sec = det2.tz_sec;
iso8601_print(datestr, sizeof(datestr), &date, &det);
fputs(datestr, stdout);
putc('\n', stdout);
} else {
for(; optind != argc; ++optind) {
if(iso8601_parse(argv[optind], &date, 0, &det2)) {
fputs("Couldn't parse date.\n", stderr);
ret = 1;
continue;
}
if(!override_tz) det.tz_sec = det2.tz_sec;
iso8601_print(datestr, sizeof(datestr), &date, &det);
fputs(datestr, stdout);
putc('\n', stdout);
}
}
return ret;
}
/* options for text editors
kate: replace-trailing-space-save true; space-indent true; tab-width 4;
vim: expandtab:ts=4:sw=4
*/

43
src/isodate/build.app Normal file
View File

@ -0,0 +1,43 @@
# These are external variables, and shouldn't clash with anything else
# isodate
# isodate_BUILT
#
build_target libiso8601
if [ -z ${isodate_BUILT} ]
then
isodate="obj/isodate"
EXTRAS="-std=gnu99 -D_GNU_SOURCE -DAPP_NAME=\"isodate\" \
${libiso8601} ${libiso8601_DEP_CFLAGS} ${libiso8601_DEP_LIBS}"
echo "Building application ${isodate}..."
do_cmd source src/isodate/build.monolithic || return 1
MODIFIED=0
for test in ${MONOLITHIC_TESTS} ${SRC}
do
if [ ${test} -nt ${isodate} ]
then
MODIFIED=1
break
fi
done
if [ ${MODIFIED} -ne 0 ]
then
echo " Compiling..."
do_cmd ${CC} ${CFLAGS} -I obj -o "${isodate}" ${SRC} ${EXTRAS} || return 1
print_success "Application built"
else
print_success "Application up to date"
fi
isodate_BUILT=1
fi
# vim: syntax=sh:expandtab:ts=4:sw=4

View File

@ -0,0 +1 @@
source src/isodate/build.app

View File

@ -0,0 +1 @@
source src/isodate/build.install-app

View File

@ -0,0 +1,11 @@
build_target isodate
# make paths (this is for Gentoo in particular)
build_dir_tree "${BINDIR}" || return 1
# install binary
echo "Installing binaries into '${BINDIR}'"
install_file "${isodate}" "${BINDIR}" 0755 || return 1
print_success "Done"
# vim: syntax=sh:expandtab:ts=4:sw=4

View File

@ -0,0 +1,17 @@
# These are external variables, and shouldn't clash with anything else
# isodate_MONOLITHIC
#
SRC="obj/isodate.c"
MONOLITHIC_TESTS="src/isodate/build.app src/isodate/build.monolithic"
if [ -z "${isodate_MONOLITHIC}" ]
then
MONOLITHIC_SOURCE="$(find src/isodate/ -name '*.c' | sort)"
make_monolithic ${SRC} C || return 1
isodate_MONOLITHIC=1
MONOLITHIC_DOC="${MONOLITHIC_DOC} ${SRC}"
fi
# vim: syntax=sh:expandtab:ts=4:sw=4

View File

@ -1,16 +1,21 @@
/* libiso8601/src/libiso8601/000_TopHeader.h
*
* (c)2006, Laurence Withers, <l@lwithers.me.uk>.
* Released under the GNU GPLv2. See file COPYING or
* (c)2006-2010, Laurence Withers, <l@lwithers.me.uk>.
* Released under the GNU GPLv3. See file COPYING or
* http://www.gnu.org/copyleft/gpl.html for details.
*/
#ifndef HEADER_libiso8601
#define HEADER_libiso8601
#ifdef __cplusplus
extern "C" {
#endif
/* standard includes, or includes needed for type declarations */
#include <time.h>
#include <stdint.h>
#include <sys/time.h>
/* options for text editors
kate: replace-trailing-space-save true; space-indent true; tab-width 4;

View File

@ -1,7 +1,7 @@
/* libiso8601/src/libiso8601/000_TopSource.c
*
* (c)2006, Laurence Withers, <l@lwithers.me.uk>.
* Released under the GNU GPLv2. See file COPYING or
* (c)2006-2010, Laurence Withers, <l@lwithers.me.uk>.
* Released under the GNU GPLv3. See file COPYING or
* http://www.gnu.org/copyleft/gpl.html for details.
*/
@ -10,9 +10,12 @@
/* Below are all the includes used throughout the library. */
#include <ctype.h>
#include <errno.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/stat.h>
#include <sys/time.h>
/* Useful define to alleviate typos */

View File

@ -1,25 +1,28 @@
/* libiso8601/src/libiso8601/100_calc.c
*
* (c)2006, Laurence Withers, <l@lwithers.me.uk>.
* Released under the GNU GPLv2. See file COPYING or
* (c)2006-2010, Laurence Withers, <l@lwithers.me.uk>.
* Released under the GNU GPLv3. See file COPYING or
* http://www.gnu.org/copyleft/gpl.html for details.
*/
/* CALCULATION ROUTINES
*
* This file contains calculation routines used internally in the library. Our date format is the
* number of days elapsed since 0000-001 (so that date would be 0, -0001-365 would be -1, etc.).
* Time is represented as the number of seconds elapsed since midnight at the start of the day. It
* is a value between 0 and 86399 (or 86400 for leap seconds).
*/
/* CALCULATION ROUTINES ********************************************************
This file contains calculation routines used internally in the library. Our
date format is the number of days elapsed since 0000-001 (so that date would be
0, -0001-365 would be -1, etc.). Time is represented as the number of seconds
elapsed since midnight at the start of the day. It is a value between 0 and
86399 (or 86400 for leap seconds).
*******************************************************************************/
#define DAYS_IN_400_YEARS (146097)
int iso8601_isleap(int year)
int
iso8601_isleap(int year)
{
if(year % 4) return 0;
if(year % 100) return 1;
@ -31,14 +34,15 @@ int iso8601_isleap(int year)
/* struct monthcount, _days_in_month_common[], _days_in_month_leap[]
*
* Tables of the number of days in each month, and the number of days elapsed since the start of
* the year for each month.
* Tables of the number of days in each month, and the number of days elapsed
* since the start of the year for each month.
*/
struct monthcount {
int elapsed, days;
};
static const struct monthcount _days_in_month_common[] = {
static const struct monthcount
_days_in_month_common[] = {
{ 0, 31 },
{ 31, 28 },
{ 59, 31 },
@ -53,7 +57,8 @@ static const struct monthcount _days_in_month_common[] = {
{ 334, 31 }
};
static const struct monthcount _days_in_month_leap[] = {
static const struct monthcount
_days_in_month_leap[] = {
{ 0, 31 },
{ 31, 29 },
{ 60, 31 },
@ -70,7 +75,8 @@ static const struct monthcount _days_in_month_leap[] = {
static int _days_in_month(int year, int month)
static int
_days_in_month(int year, int month)
{
const struct monthcount* mc;
@ -85,42 +91,45 @@ static int _days_in_month(int year, int month)
static void _to_year(int* year, int* days_left, const struct iso8601_date* date)
static void
_to_year(int* year, int* days_left, const struct iso8601_date* date)
{
div_t qr;
int ndays = date->day;
// Each 400 years have 97 leap days, giving 365*400+97 = DAYS_IN_400_YEARS days in 400 years
/* Each 400 years have 97 leap days, giving 365*400+97 = DAYS_IN_400_YEARS
days in 400 years */
qr = div(ndays, DAYS_IN_400_YEARS);
*year = qr.quot * 400;
ndays = qr.rem;
// ensure that we always end up with between 0 and 146096 days remaining
/* ensure that we always end up with between 0 and 146096 days remaining */
if(ndays < 0) {
ndays += DAYS_IN_400_YEARS;
*year -= 400;
}
// we insert `fake' leap days for years 101, 201, 301
/* we insert `fake' leap days for years 101, 201, 301 */
if(ndays >= 36890) ++ndays;
if(ndays >= 73415) ++ndays;
if(ndays >= 109940) ++ndays;
// each remaining 100 year block has 24 leap days, giving 365*100+24 = 36524 days
/* each remaining 100 year block has 24 leap days, giving 365*100+24 = 36524
days */
qr = div(ndays, 36525);
*year += qr.quot * 100;
ndays = qr.rem;
// each 4-year block has 1 leap day, giving 365*4 + 1 = 1461 days
/* each 4-year block has 1 leap day, giving 365*4 + 1 = 1461 days */
qr = div(ndays, 1461);
*year += qr.quot * 4;
ndays = qr.rem;
// the first year of a 4-year block has 1 leap day, 366 days
/* the first year of a 4-year block has 1 leap day, 366 days */
if(ndays >= 366) {
--ndays; // pretend to have dealt with leap day
--ndays; /* pretend to have dealt with leap day */
// 365 days per remaining year
/* 365 days per remaining year */
qr = div(ndays, 365);
*year += qr.quot;
ndays = qr.rem;
@ -131,15 +140,16 @@ static void _to_year(int* year, int* days_left, const struct iso8601_date* date)
void iso8601_to_cal(int* year, int* month, int* day, const struct iso8601_date* date)
void
iso8601_to_cal(int* year, int* month, int* day, const struct iso8601_date* date)
{
const struct monthcount* mc;
int ndays;
// perform year calulation
/* perform year calulation */
_to_year(year, &ndays, date);
// now we simply have number of days elapsed since day 001 in `year'.
/* now we simply have number of days elapsed since day 001 in `year'. */
mc = iso8601_isleap(*year) ? _days_in_month_leap : _days_in_month_common;
*month = 1;
while(ndays >= mc->days) {
@ -152,35 +162,38 @@ void iso8601_to_cal(int* year, int* month, int* day, const struct iso8601_date*
void iso8601_to_ord(int* year, int* oday, const struct iso8601_date* date)
void
iso8601_to_ord(int* year, int* oday, const struct iso8601_date* date)
{
int ndays;
// perform year calcutation
/* perform year calcutation */
_to_year(year, &ndays, date);
// now we simply have number of days elapsed since day 001 in `year'.
/* now we simply have number of days elapsed since day 001 in `year'. */
*oday = ndays + 1;
}
static int _weekday_of_year(int year)
static int
_weekday_of_year(int year)
{
int w = 6;
/* Algorithm notes:
* - 0 = sun, 1 = mon, ..., 6 = sat
* - 0000-001 is a Saturday, day 6
* - every year we pass gives us one additional day (364 is divisible by 7)
* - but of course every leap year we pass gives us a further day
* - so for every 400 years, we add 497 (400 common years, 97 leap years); 497 % 7 = 0
* 0 = sun, 1 = mon, ..., 6 = sat
* 0000-001 is a Saturday, day 6
* every year we pass gives us one additional day (364 is divisible by 7)
* but of course every leap year we pass gives us a further day
* so for every 400 years, we add 497 (400 common years, 97 leap years);
* 497 % 7 = 0
*/
year %= 400;
if(year < 0) year += 400; // end up with between 0-399 years left
w += year; // excluding leap years, we increase by 1 day a year
w += (year + 3) / 4; // there is one leap year for every four years
w -= (year - 1) / 100; // but one less for every century over 0
if(year < 0) year += 400; /* end up with between 0-399 years left */
w += year; /* excluding leap years, we increase by 1 day a year */
w += (year + 3) / 4; /* there is one leap year for every four years */
w -= (year - 1) / 100; /* but one less for every century over 0 */
w %= 7;
return w;
@ -188,42 +201,47 @@ static int _weekday_of_year(int year)
void iso8601_to_week(int* year, int* week, int* wday, const struct iso8601_date* date)
void
iso8601_to_week(int* year, int* week, int* wday,
const struct iso8601_date* date)
{
int ndays, w, has53 = 0;
div_t d;
// perform year and weekday calculation
/* perform year and weekday calculation */
_to_year(year, &ndays, date);
w = _weekday_of_year(*year);
// find out what day jan 1 was; from there, we can find the ISO week and year number
/* find out what day jan 1 was; from there, we can find the ISO week and
year number */
switch(w) {
case 4: // W01 starts XXXY-12-28
case 4: /* W01 starts XXXY-12-28 */
w += 7;
has53 = 1; // years starting Thursday have 53 weeks
has53 = 1; /* years starting Thursday have 53 weeks */
break;
case 5: // W01 starts XXXZ-01-03
case 6: // W01 starts XXXZ-01-02
case 5: /* W01 starts XXXZ-01-03 */
case 6: /* W01 starts XXXZ-01-02 */
break;
case 0: // W01 starts XXXZ-01-01
case 1: // W01 starts XXXY-12-31
case 2: // W01 starts XXXY-12-30
w += 7; // for week calculation
case 0: /* W01 starts XXXZ-01-01 */
case 1: /* W01 starts XXXY-12-31 */
case 2: /* W01 starts XXXY-12-30 */
w += 7; /* for week calculation */
break;
case 3: // W01 starts XXXY-12-29
case 3: /* W01 starts XXXY-12-29 */
w += 7;
if(iso8601_isleap(*year)) has53 = 1; // leap years starting Wednesday have 53 weeks
if(iso8601_isleap(*year)) has53 = 1; /* leap years starting Wednesday
have 53 weeks */
}
// now we simply add the number of days elapsed since the start of the year, and % 7
/* now we simply add the number of days elapsed since the start of the
year, and % 7 */
w += ndays;
d = div(w, 7); // w can never be 0
d = div(w, 7); /* w can never be 0 */
// do Sunday correction
/* do Sunday correction */
if(!d.rem) {
d.rem = 7;
--d.quot;
@ -256,54 +274,57 @@ void iso8601_to_week(int* year, int* week, int* wday, const struct iso8601_date*
int _from_year(struct iso8601_date* date, int year)
static int
_from_year(struct iso8601_date* date, int year)
{
div_t qr;
// check for range errors
/* check for range errors */
if(year < -5879609 || year > 5879609) {
errno = ERANGE;
return -1;
}
// Each 400 years have 97 leap days, giving 365*400+97 = DAYS_IN_400_YEARS days in 400 years
/* Each 400 years have 97 leap days, giving 365*400+97 = DAYS_IN_400_YEARS
days in 400 years */
qr = div(year, 400);
date->day = qr.quot * DAYS_IN_400_YEARS;
year = qr.rem;
// ensure we have between 0 and 399 years
/* ensure we have between 0 and 399 years */
if(year < 0) {
date->day -= DAYS_IN_400_YEARS;
year += 400;
}
// excluding leap days, there are 365 days per year
/* excluding leap days, there are 365 days per year */
date->day += 365 * year;
date->day += (year + 3) / 4; // there is one leap year for every four years
date->day -= (year - 1) / 100; // but one less for every century over 0
date->day += (year + 3) / 4; /* one leap year for every four years */
date->day -= (year - 1) / 100; /* but one less for every century over 0 */
return 0;
}
int iso8601_from_cal(struct iso8601_date* date, int year, int month, int day)
int
iso8601_from_cal(struct iso8601_date* date, int year, int month, int day)
{
const struct monthcount* mc;
// check for domain errors
/* check for domain errors */
mc = iso8601_isleap(year) ? _days_in_month_leap : _days_in_month_common;
if(month < 1 || month > 12 || day < 1 || day > mc[month - 1].days) {
errno = EDOM;
return -1;
}
// perform year calculation
/* perform year calculation */
if(_from_year(date, year)) return -1;
// now get number of days elapsed up to start of month
/* now get number of days elapsed up to start of month */
date->day += mc[month - 1].elapsed;
// and add number of days elapsed sinced start of month
/* and add number of days elapsed sinced start of month */
date->day += day - 1;
return 0;
@ -311,18 +332,19 @@ int iso8601_from_cal(struct iso8601_date* date, int year, int month, int day)
int iso8601_from_ord(struct iso8601_date* date, int year, int oday)
int
iso8601_from_ord(struct iso8601_date* date, int year, int oday)
{
// check for domain errors
/* check for domain errors */
if(oday < 1 || oday > (iso8601_isleap(year) ? 366 : 365)) {
errno = EDOM;
return -1;
}
// perform year calculation
/* perform year calculation */
if(_from_year(date, year)) return -1;
// now simply add number of days elapsed this year
/* now simply add number of days elapsed this year */
date->day += oday - 1;
return 0;
@ -330,18 +352,19 @@ int iso8601_from_ord(struct iso8601_date* date, int year, int oday)
int iso8601_from_week(struct iso8601_date* date, int isoyear, int week, int wday)
int
iso8601_from_week(struct iso8601_date* date, int isoyear, int week, int wday)
{
int day1off, maxwk;
// compute year part
/* compute year part */
_from_year(date, isoyear);
// 400-year cycle; ensure we're between 0-400 years
/* 400-year cycle; ensure we're between 0-400 years */
isoyear %= 400;
if(isoyear < 0) isoyear += 400;
// domain check
/* domain check */
maxwk = (((isoyear % 7) == 52) || ((isoyear % 28) == 24)) ? 53 : 52;
if(wday < 1 || wday > 7 || week < 1 || week > maxwk) {
errno = EDOM;
@ -349,18 +372,20 @@ int iso8601_from_week(struct iso8601_date* date, int isoyear, int week, int wday
}
/* Algorithm notes:
* We now compute the offset between the start day of the ISO year and the start day of the
* real year. Year 0000 starts on a Saturday, meaning the ISO year 0000 starts on 0000-003
* (offset of +2 days). Each year reduces the offset by one day, and each leap year reduces it
* by a further day; the offset wraps from -3 to +3.
* We now compute the offset between the start day of the ISO year and
* the start day of the real year. Year 0000 starts on a Saturday, meaning
* the ISO year 0000 starts on 0000-003 (offset of +2 days). Each year
* reduces the offset by one day, and each leap year reduces it by a
* further day; the offset wraps from -3 to +3.
*/
day1off = 2 - isoyear; // reduce offset by 1 for each year
day1off += (isoyear - 1) / 100; // cancel out 1 leap year for each century past the first
day1off -= (isoyear + 3) / 4; // 1 leap year every 4 years
day1off = 2 - isoyear; /* reduce offset by 1 for each year */
day1off += (isoyear - 1) / 100; /* cancel out 1 leap year for each century
past the first */
day1off -= (isoyear + 3) / 4; /* 1 leap year every 4 years */
day1off %= 7;
if(day1off < -3) day1off += 7;
// now simply add in the day offset and days/weeks elapsed
/* now simply add in the day offset and days/weeks elapsed */
date->day += day1off + (week - 1) * 7 + wday - 1;
return 0;
@ -368,11 +393,13 @@ int iso8601_from_week(struct iso8601_date* date, int isoyear, int week, int wday
void iso8601_to_clocktime(int* hour, int* min, int* sec, const struct iso8601_date* date)
void
iso8601_to_clocktime(int* hour, int* min, int* sec,
const struct iso8601_date* date)
{
div_t qr;
// special case: leap second
/* special case: leap second */
if(date->sec == 86400) {
*hour = 23;
*min = 59;
@ -380,7 +407,7 @@ void iso8601_to_clocktime(int* hour, int* min, int* sec, const struct iso8601_da
return;
}
// normal case
/* normal case */
qr = div(date->sec, 3600);
*hour = qr.quot;
qr = div(qr.rem, 60);
@ -390,21 +417,22 @@ void iso8601_to_clocktime(int* hour, int* min, int* sec, const struct iso8601_da
int iso8601_from_clocktime(struct iso8601_date* date, int hour, int min, int sec)
int
iso8601_from_clocktime(struct iso8601_date* date, int hour, int min, int sec)
{
// special case: leap second
/* special case: leap second */
if(hour == 23 && min == 59 && sec == 60) {
date->sec = 86400;
return 0;
}
// domain check
/* domain check */
if(hour < 0 || hour > 23 || min < 0 || min > 59 || sec < 0 || sec > 59) {
errno = EDOM;
return -1;
}
// normal calculation
/* normal calculation */
date->sec = hour * 3600 + min * 60 + sec;
return 0;
}

View File

@ -1,59 +1,92 @@
/* libiso8601/src/libiso8601/100_leap.c
*
* (c)2006, Laurence Withers, <l@lwithers.me.uk>.
* Released under the GNU GPLv2. See file COPYING or
* (c)2006-2010, Laurence Withers, <l@lwithers.me.uk>.
* Released under the GNU GPLv3. See file COPYING or
* http://www.gnu.org/copyleft/gpl.html for details.
*/
static int leap_second_days[] = {
720439, // 19720630
720623, // 19721231
720988, // 19731231
721353, // 19741231
721718, // 19751231
722084, // 19761231
722449, // 19771231
722814, // 19781231
723179, // 19791231
723726, // 19810630
724091, // 19820630
724456, // 19830630
725187, // 19850630
726101, // 19871231
726832, // 19891231
727197, // 19901231
727744, // 19920630
728109, // 19930630
728474, // 19940630
729023, // 19951231
729570, // 19970630
730119, // 19981231
732676 // 20051231
/* leap_second_days_table[]
* This is a sorted array of days (in libiso8601 format) on which a positive
* leap second occurred. This table is built in to the library, but may be
* overridden at runtime by changing what leap_second_days points to. There
* should be no other references to this table, therefore.
*/
static int
leap_second_days_table[] = {
720439, /* 1972-06-30 */
720623, /* 1972-12-31 */
720988, /* 1973-12-31 */
721353, /* 1974-12-31 */
721718, /* 1975-12-31 */
722084, /* 1976-12-31 */
722449, /* 1977-12-31 */
722814, /* 1978-12-31 */
723179, /* 1979-12-31 */
723726, /* 1981-06-30 */
724091, /* 1982-06-30 */
724456, /* 1983-06-30 */
725187, /* 1985-06-30 */
726101, /* 1987-12-31 */
726832, /* 1989-12-31 */
727197, /* 1990-12-31 */
727744, /* 1992-06-30 */
728109, /* 1993-06-30 */
728474, /* 1994-06-30 */
729023, /* 1995-12-31 */
729570, /* 1997-06-30 */
730119, /* 1998-12-31 */
732676, /* 2005-12-31 */
733772, /* 2008-12-31 */
735049, /* 2012-06-30 */
736144, /* 2015-06-30 */
};
int iso8601_seconds_leap(const struct iso8601_date* date)
{
int num_ly = 0, i = 0;
/* leap_second_days[], leap_second_days_num
* Pointer to an array (and number of elements in the array) representing the
* table of positive leap seconds. The array is sorted and contains libiso8601
* day numbers of days with positive leap seconds.
*/
static int*
leap_second_days = leap_second_days_table;
static int
leap_second_days_num = sizeof(leap_second_days_table) / sizeof(int);
num_ly = sizeof(leap_second_days) / sizeof(int);
for(i = 0; i < num_ly; ++i) if(leap_second_days[i] == date->day) return 86401;
/* iso8601_seconds_leap()
* Returns the number of seconds that elapse on a given date, which requires us
* to search the table of leap seconds to see if we need to return a special
* case.
*/
int
iso8601_seconds_leap(const struct iso8601_date* date)
{
int i;
for(i = 0; i < leap_second_days_num; ++i) {
if(leap_second_days[i] == date->day) return 86401;
}
return 86400;
}
static int _leap_elapsed_day(int sday, int eday)
/* _leap_elapsed_day()
* Returns the number of leap seconds that have elapsed between sday (start
* day) and eday (end day), both in libiso8601 format.
*/
static int
_leap_elapsed_day(int sday, int eday)
{
int spos, epos;
for(spos = 0; (unsigned)spos < sizeof(leap_second_days) / sizeof(int); ++spos) {
for(spos = 0; spos < leap_second_days_num; ++spos) {
if(sday <= leap_second_days[spos]) break;
}
for(epos = 0; (unsigned)epos < sizeof(leap_second_days) / sizeof(int); ++epos) {
for(epos = 0; epos < leap_second_days_num; ++epos) {
if(eday <= leap_second_days[epos]) break;
}
@ -62,13 +95,121 @@ static int _leap_elapsed_day(int sday, int eday)
int iso8601_leap_elapsed(const struct iso8601_date* start, const struct iso8601_date* end)
/* iso8601_leap_elapsed()
* Wrapper around _leap_elapsed_day().
*/
int
iso8601_leap_elapsed(const struct iso8601_date* start,
const struct iso8601_date* end)
{
return _leap_elapsed_day(start->day, end->day);
}
/* leap_table_free_old
* If set, then when we update the leap table, we should free the old array.
* Initially clear so that we don't free our static built-in table.
*/
static int leap_table_free_old = 0;
/* iso8601_leap_table_set()
* Switch to using a new table of leap seconds, possibly freeing the old one.
*/
void
iso8601_leap_table_set(int* new_table, int new_size)
{
if(leap_table_free_old) free(leap_second_days);
leap_table_free_old = 0;
leap_second_days = new_table;
leap_second_days_num = new_size;
}
/* leap_table_signature
* The first 8 bytes of a leap second table must match this string, otherwise
* the file is taken not to be of the correct format.
*/
static const char*
leap_table_signature = "/O9PdPZI";
/* iso8601_leap_table_load()
* Loads a new table of leap seconds from disk. Ensures the signature of the
* given file matches leap_table_signature, and does some basic sanity
* checking (file in sorted order etc.).
*/
int
iso8601_leap_table_load(const char* fname)
{
struct stat st;
int fd, saved_errno, i, new_size, * days = 0;
char buf[12];
if(!fname) fname = DEFAULT_LEAP_TABLE;
if(stat(fname, &st)) return -1;
if(st.st_size < 12) {
errno = EINVAL;
return -1;
}
fd = open(fname, O_RDONLY);
if(fd == -1) return -1;
if(TEMP_FAILURE_RETRY( read(fd, buf, 12) ) != 12) goto outerr;
if(memcmp(buf, leap_table_signature, 8)) {
errno = EINVAL;
return -1;
}
#define GET_UINT32(_from) ( \
( ((uint8_t*)_from)[0] << 24 ) | \
( ((uint8_t*)_from)[1] << 16 ) | \
( ((uint8_t*)_from)[2] << 8 ) | \
( ((uint8_t*)_from)[3] ) \
)
new_size = GET_UINT32(buf + 8);
if((12 + new_size * 4) != st.st_size) {
errno = EINVAL;
goto outerr;
}
days = malloc(sizeof(int) * new_size);
if(!days) goto outerr;
for(i = 0; i < new_size; ++i) {
if(TEMP_FAILURE_RETRY( read(fd, buf, 4) ) != 4) goto outerr;
days[i] = GET_UINT32(buf);
if(i && days[i] <= days[i - 1]) {
errno = EINVAL;
goto outerr;
}
}
TEMP_FAILURE_RETRY( close(fd) );
iso8601_leap_table_set(days, new_size);
leap_table_free_old = 1;
return 0;
#undef GET_UINT32
outerr:
saved_errno = errno;
free(days);
TEMP_FAILURE_RETRY( close(fd) );
errno = saved_errno;
return -1;
}
/* options for text editors
kate: replace-trailing-space-save true; space-indent true; tab-width 4;
vim: expandtab:ts=4:sw=4

View File

@ -1,7 +1,7 @@
/* libiso8601/src/libiso8601/100_types.h
*
* (c)2006, Laurence Withers, <l@lwithers.me.uk>.
* Released under the GNU GPLv2. See file COPYING or
* (c)2006-2010, Laurence Withers, <l@lwithers.me.uk>.
* Released under the GNU GPLv3. See file COPYING or
* http://www.gnu.org/copyleft/gpl.html for details.
*/
@ -9,19 +9,20 @@
/*! \brief Date/time point.
This structure contains the details to represent a specific instant on the UTC timescale. It uses
Jan 1, year 0000 as the origin (when \a day will be 0). \a sec is the number of seconds elapsed
since start of day, and \a nsec is the number of nanoseconds elapsed since the start of the current
second.
This structure contains the details to represent a specific instant on the UTC
timescale. It uses Jan 1, year 0000 as the origin (when \a day will be 0). \a
sec is the number of seconds elapsed since start of day, and \a nsec is the
number of nanoseconds elapsed since the start of the current second.
We correctly deal with leap seconds by encoding 23:59:60 as having a \a sec field of 86400.
We correctly deal with leap seconds by encoding 23:59:60 as having a \a sec
field of 86400.
*/
struct iso8601_date {
/*! \brief Number of nanoseconds elapsed since start of second. */
int32_t nsec;
/*! \brief Number of days elapsed since Jan 1, year 0000. May be negative. */
/*! \brief Number of days elapsed since Jan 1, year 0000. May be negative.*/
int32_t day;
/*! \brief Number of seconds elapsed since start of day. */
@ -31,10 +32,12 @@ struct iso8601_date {
/*! \brief Date (day portion) precision.
\ingroup parser
This enumeration will record how precisely the date was specified, as well as the format in use. It
allows the library to determine the earliest and latest dates that could possibly be represented
with the given input and also allows the output format to match the input format.
This enumeration will record how precisely the date was specified, as well as
the format in use. It allows the library to determine the earliest and latest
dates that could possibly be represented with the given input and also allows
the output format to match the input format.
*/
enum iso8601_date_prec {
@ -55,43 +58,52 @@ enum iso8601_date_prec {
/*! \brief Year, week and weekday specified (week format). */
iso8601_prec_wday
}date_prec;
};
/*! \brief Time precision.
\ingroup parser
This enumeration records how precisely the time was specified as well as its format. The fractional
format will record whether it was the hour, minute or second that was specified with a fractional
part, allowing a processed date/time to be presented to the user in the format it was originally
This enumeration records how precisely the time was specified as well as its
format. The fractional format will record whether it was the hour, minute or
second that was specified with a fractional part, allowing a processed
date/time to be presented to the user in the format it was originally
encountered.
*/
enum iso8601_time_prec {
/*! \brief Don't display date. */
iso8601_prec_none,
/*! \brief Display integer part of hour. */
iso8601_prec_hour,
/*! \brief Display hour and integer part of minute. */
iso8601_prec_min,
/*! \brief Display hour, minute and integer part of second. */
iso8601_prec_sec,
/*! \brief Display hour and fractional part of hour. */
iso8601_prec_hourfrac,
/*! \brief Display hour, minute and fractional part of minute. */
iso8601_prec_minfrac,
/*! \brief Display hour, minute, second and nanoseconds. */
iso8601_prec_secfrac
}time_prec;
};
/*! \brief Date/time formatting details.
This structure simply records details related to the formatting (and precision) of a date/time
structure. The structure can be filled out by the parser so that a program's output can match
the format of its input. Alternatively it can be controlled by the program to provide a consistent
output format.
This structure simply records details related to the formatting (and precision)
of a date/time structure. The structure can be filled out by the parser so that
a program's output can match the format of its input. Alternatively it can be
controlled by the program to provide a consistent output format.
*/
struct iso8601_details {
/*! \brief Date precision (enum iso8601_date_prec). */
/*! \brief Date precision (\ref iso8601_date_prec). */
uint8_t date_prec;
/*! \brief Time precision (enum iso8601_time_prec). */
/*! \brief Time precision (\ref iso8601_time_prec). */
uint8_t time_prec;
/*! \brief Flag: non-zero if extended format should be used. */
@ -105,9 +117,10 @@ struct iso8601_details {
/*! \brief Short period elapsed time.
This structure contains the details of an elapsed time period, split into seconds and nanoseconds.
None of its components can ever be negative. This is only capable of representing short (compared to
struct iso8601_date's range) periods of up to approximately 136 years.
This structure contains the details of an elapsed time period, split into
seconds and nanoseconds. None of its components can ever be negative. This is
only capable of representing short (compared to struct iso8601_date's range)
periods of up to approximately 136 years.
*/
struct iso8601_elapsed {
@ -121,5 +134,5 @@ struct iso8601_elapsed {
/* options for text editors
kate: replace-trailing-space-save true; space-indent true; tab-width 4;
vim: expandtab:ts=4:sw=4
vim: expandtab:ts=4:sw=4:syntax=ch.doxygen
*/

View File

@ -1,24 +1,25 @@
/* libiso8601/src/libiso8601/200_parser.c
*
* (c)2006, Laurence Withers, <l@lwithers.me.uk>.
* Released under the GNU GPLv2. See file COPYING or
* (c)2006-2010, Laurence Withers, <l@lwithers.me.uk>.
* Released under the GNU GPLv3. See file COPYING or
* http://www.gnu.org/copyleft/gpl.html for details.
*/
int iso8601_parse(const char* str, struct iso8601_date* earliest, struct iso8601_date* latest,
struct iso8601_details* details)
int
iso8601_parse(const char* str, struct iso8601_date* earliest,
struct iso8601_date* latest, struct iso8601_details* details)
{
enum {
state_none,
state_year, // e.g. `2006' or `2006123'
state_date2, // 2nd component of date, e.g. `2006-' or `2006-1'
state_day, // e.g. `2006-01-' or `2006-01-01'
state_week_basic, // e.g. `2006W' or `2006W123'
state_week_extended, // e.g. `2006-W' or `2006-W31'
state_week_day, // e.g. `2006-W31-'
state_week_done, // `2006-W31-1'
state_year, /* e.g. `2006' or `2006123' */
state_date2, /* 2nd component of date, e.g. `2006-' or `2006-1' */
state_day, /* e.g. `2006-01-' or `2006-01-01' */
state_week_basic, /* e.g. `2006W' or `2006W123' */
state_week_extended, /* e.g. `2006-W' or `2006-W31' */
state_week_day, /* e.g. `2006-W31-' */
state_week_done, /* `2006-W31-1' */
state_time_basic,
state_time_hour,
state_time_min,
@ -34,8 +35,9 @@ int iso8601_parse(const char* str, struct iso8601_date* earliest, struct iso8601
div_t qr;
char ch;
int num = 0, neg = 0, dig = 0, tz_neg = 0, nsec = -1, nsec_dig = -1, leap_sec_req = 0;
int y = 0, m = -1, d = -1, w = -1, wd = -1, hour = -1, min = -1, sec = -1, tz_sec = 0;
int num = 0, neg = 0, dig = 0, tz_neg = 0, nsec = -1, nsec_dig = -1,
leap_sec_req = 0, y = 0, m = -1, d = -1, w = -1, wd = -1, hour = -1,
min = -1, sec = -1, tz_sec = 0;
double frac;
struct iso8601_elapsed elapsed;
@ -436,7 +438,8 @@ int iso8601_parse(const char* str, struct iso8601_date* earliest, struct iso8601
case '-':
tz_neg = -1;
case '+':
state = (state == state_time_nsec_basic) ? state_tz_basic : state_tz_hour;
state = (state == state_time_nsec_basic) ?
state_tz_basic : state_tz_hour;
break;
}
break;
@ -544,7 +547,8 @@ done:
if(m != -1) {
if(d == -1) {
ERROR_IF(earliest && iso8601_from_cal(earliest, y, m, 1));
ERROR_IF(latest && iso8601_from_cal(latest, y, m, _days_in_month(y, m)));
ERROR_IF(latest && iso8601_from_cal(latest, y, m,
_days_in_month(y, m)));
if(details) details->date_prec = iso8601_prec_month;
} else {
@ -608,7 +612,8 @@ done:
} else if(min != -1) {
if(nsec_dig == -1) {
ERROR_IF(earliest && iso8601_from_clocktime(earliest, hour, min, 0));
ERROR_IF(earliest && iso8601_from_clocktime(earliest,
hour, min, 0));
ERROR_IF(latest && iso8601_from_clocktime(latest, hour, min, 59));
if(latest) latest->nsec = 999999999;
if(details) details->time_prec = iso8601_prec_min;
@ -617,7 +622,8 @@ done:
frac = nsec * 60.0 / 1e9;
sec = (int)frac;
nsec = (frac - sec) * 1e9;
ERROR_IF(earliest && iso8601_from_clocktime(earliest, hour, min, sec));
ERROR_IF(earliest && iso8601_from_clocktime(earliest,
hour, min, sec));
if(earliest) earliest->nsec = nsec;
ERROR_IF(latest && iso8601_from_clocktime(latest, hour, min, sec));
if(latest) latest->nsec = nsec;
@ -639,7 +645,8 @@ done:
frac *= 60;
sec = (int)frac;
nsec = (frac - sec) * 1e9;
ERROR_IF(earliest && iso8601_from_clocktime(earliest, hour, min, sec));
ERROR_IF(earliest && iso8601_from_clocktime(earliest,
hour, min, sec));
if(earliest) earliest->nsec = nsec;
ERROR_IF(latest && iso8601_from_clocktime(latest, hour, min, sec));
if(latest) latest->nsec = nsec;
@ -676,7 +683,8 @@ done:
int iso8601_invalid(const struct iso8601_date* date)
int
iso8601_invalid(const struct iso8601_date* date)
{
return date->nsec < 0
|| date->nsec >= BILLION

View File

@ -1,7 +1,7 @@
/* libiso8601/src/libiso8601/200_parser.h
*
* (c)2006, Laurence Withers, <l@lwithers.me.uk>.
* Released under the GNU GPLv2. See file COPYING or
* (c)2006-2010, Laurence Withers, <l@lwithers.me.uk>.
* Released under the GNU GPLv3. See file COPYING or
* http://www.gnu.org/copyleft/gpl.html for details.
*/
@ -9,31 +9,41 @@
/*! \defgroup parser Parsing, printing and validation routines.
These routines are used for parsing an ISO8601 date/time string into the internal structure used
to represent them, and for validating such dates/times.
These routines are used for parsing an ISO8601 date/time string into the
internal structure used to represent them, and for validating such dates/times.
*/
/*!@{*/
/*! \brief Parse ISO8601 date/time.
\param str The input string. Whitespace will be stripped.
\param[out] earliest The earliest possible time the string could represent. May be 0.
\param[out] latest The latest possible time the string could represent. May be 0.
\param[out] details Stores details such as the precision to which the time/date were specified. May
\param[out] earliest The earliest possible time the string could represent. May
be 0.
\param[out] latest The latest possible time the string could represent. May be
0.
\param[out] details Stores details such as the precision to which the time/date
were specified. May be 0.
\retval -1 on error.
\retval 0 on success.
Parses a string containing the ISO8601 date/time. Deals with any format of date, optionally storing
the details in \a details. The time may be partial, in which case this function returns the earliest
and latest times that could possibly be represented by the string.
Parses a string containing the ISO8601 date/time. Deals with any format of
date, optionally storing the details in \a details. The time may be partial, in
which case this function returns the earliest and latest times that could
possibly be represented by the string.
Note that this function will accept leap seconds (23:59:60) on days on which they occurred.
Note that this function will accept leap seconds (23:59:60) on days on which
they occurred.
*/
int iso8601_parse(const char* str, struct iso8601_date* earliest, struct iso8601_date* latest,
struct iso8601_details* details);
int iso8601_parse(const char* str, struct iso8601_date* earliest,
struct iso8601_date* latest, struct iso8601_details* details)
#ifndef DOXYGEN
__attribute__((nonnull(1),warn_unused_result))
#endif
;
@ -45,12 +55,17 @@ int iso8601_parse(const char* str, struct iso8601_date* earliest, struct iso8601
\param details Formatting details (may be 0).
\returns Pointer to buffer (\a str).
Formats and prints an ISO8601 date, optionally using the details in \a details. Will always return
a null-terminated result, even if that means truncating the output to fit the buffer.
Formats and prints an ISO8601 date, optionally using the details in \a details.
Will always return a null-terminated result, even if that means truncating the
output to fit the buffer.
*/
char* iso8601_print(char* str, int amt, const struct iso8601_date* date,
const struct iso8601_details* details);
const struct iso8601_details* details)
#ifndef DOXYGEN
__attribute__((nonnull(1,3)))
#endif
;
@ -60,18 +75,21 @@ char* iso8601_print(char* str, int amt, const struct iso8601_date* date,
\retval -1 if not valid.
\retval 0 if valid.
Checks the details of \a date to ensure that they are sensible. This involves checking that \a sec
is in the range 0 to 86399 (or 86400 if there is a leap second), and that \a nsec is in the range 0
to 999999999.
Checks the details of \a date to ensure that they are sensible. This involves
checking that \a sec is in the range 0 to 86399 (or 86400 if there is a leap
second), and that \a nsec is in the range 0 to 999999999.
*/
int iso8601_invalid(const struct iso8601_date* date);
int iso8601_invalid(const struct iso8601_date* date)
#ifndef DOXYGEN
__attribute__((nonnull))
#endif
;
/*!@}*/
/* options for text editors
kate: replace-trailing-space-save true; space-indent true; tab-width 4;
vim: expandtab:ts=4:sw=4
vim: expandtab:ts=4:sw=4:syntax=ch.doxygen
*/

View File

@ -1,13 +1,18 @@
/* libiso8601/src/libiso8601/print.c
/* libiso8601/src/libiso8601/200_print.c
*
* (c)2006, Laurence Withers, <l@lwithers.me.uk>.
* Released under the GNU GPLv2. See file COPYING or
* (c)2006-2010, Laurence Withers, <l@lwithers.me.uk>.
* Released under the GNU GPLv3. See file COPYING or
* http://www.gnu.org/copyleft/gpl.html for details.
*/
static const struct iso8601_details _default_details = {
/* _default_details
* If the user doesn't pass precision details to iso8601_print(), these are
* used instead.
*/
static const struct iso8601_details
_default_details = {
iso8601_prec_day,
iso8601_prec_sec,
1,
@ -16,7 +21,8 @@ static const struct iso8601_details _default_details = {
char* iso8601_print(char* str, int amt, const struct iso8601_date* date,
char*
iso8601_print(char* str, int amt, const struct iso8601_date* date,
const struct iso8601_details* details)
{
int y, m, d, ret = 0, extended;
@ -27,10 +33,10 @@ char* iso8601_print(char* str, int amt, const struct iso8601_date* date,
str_orig = str;
// use default details if none provided
/* use default details if none provided */
if(!details) details = &_default_details;
// adjust output for timezone
/* adjust output for timezone */
dttz = *date;
if(details->tz_sec) {
elapsed.sec = details ? details->tz_sec : 0;
@ -38,7 +44,7 @@ char* iso8601_print(char* str, int amt, const struct iso8601_date* date,
iso8601_add_elapsed(&dttz, &elapsed);
}
// determine whether or not to force extended output
/* determine whether or not to force extended output */
iso8601_to_cal(&y, &m, &d, &dttz);
extended = details->extended || y < 0 || y > 9999;
@ -55,27 +61,32 @@ char* iso8601_print(char* str, int amt, const struct iso8601_date* date,
case iso8601_prec_day:
if(y < 0) ret = snprintf(str, amt, "%05d-%02d-%02d", y, m, d);
else ret = snprintf(str, amt, extended ? "%04d-%02d-%02d" : "%04d%02d%02d", y, m, d);
else ret = snprintf(str, amt, extended ?
"%04d-%02d-%02d" : "%04d%02d%02d", y, m, d);
break;
case iso8601_prec_ord:
iso8601_to_ord(&y, &d, &dttz);
if(y < 0) ret = snprintf(str, amt, "%05d-%03d", y, d);
else ret = snprintf(str, amt, extended ? "%04d-%03d" : "%04d%03d", y, d);
else ret = snprintf(str, amt, extended ?
"%04d-%03d" : "%04d%03d", y, d);
break;
case iso8601_prec_week:
iso8601_to_week(&y, &m, &d, &dttz);
extended = y < 0 || y > 9999 || details->extended; // ISO year is different
/* note: ISO year can differ, so recompute extended flag */
extended = y < 0 || y > 9999 || details->extended;
if(y < 0) snprintf(str, amt, "%05d-W%02d", y, m);
else snprintf(str, amt, extended ? "%04d-W%02d" : "%04dW%02d", y, m);
return str_orig;
case iso8601_prec_wday:
iso8601_to_week(&y, &m, &d, &dttz);
extended = y < 0 || y > 9999 || details->extended; // ISO year is different
/* note: ISO year can differ, so recompute extended flag */
extended = y < 0 || y > 9999 || details->extended;
if(y < 0) ret = snprintf(str, amt, "%05d-W%02d-%d", y, m, d);
else ret = snprintf(str, amt, extended ? "%04d-W%02d-%d" : "%04dW%02d%d", y, m, d);
else ret = snprintf(str, amt, extended ?
"%04d-W%02d-%d" : "%04dW%02d%d", y, m, d);
break;
}
@ -111,7 +122,8 @@ char* iso8601_print(char* str, int amt, const struct iso8601_date* date,
break;
case iso8601_prec_sec:
ret = snprintf(str, amt, extended ? "T%02d:%02d:%02d" : "T%02d%02d%02d", y, m, d);
ret = snprintf(str, amt, extended ?
"T%02d:%02d:%02d" : "T%02d%02d%02d", y, m, d);
break;
case iso8601_prec_hourfrac:
@ -121,24 +133,22 @@ char* iso8601_print(char* str, int amt, const struct iso8601_date* date,
case iso8601_prec_minfrac:
frac = m + (d + date->nsec / 1e9) / 60.0;
ret = snprintf(str, amt, extended ? "T%02d:%#012.9f" : "T%02d%#012.9f", y, frac);
ret = snprintf(str, amt, extended ?
"T%02d:%#012.9f" : "T%02d%#012.9f", y, frac);
break;
case iso8601_prec_secfrac:
frac = d + date->nsec / 1e9;
ret = snprintf(str, amt, extended ? "T%02d:%02d:%#012.9f" : "T%02d%02d%#012.9f", y, m, frac);
ret = snprintf(str, amt, extended ?
"T%02d:%02d:%#012.9f" : "T%02d%02d%#012.9f", y, m, frac);
break;
}
if(ret < 1 || ret >= amt) return str_orig;
if(ret < 1 || (ret + 1) >= amt) return str_orig;
str += ret;
amt -= ret;
if(details->tz_sec) {
if(!--amt) {
*str = 0;
return str_orig;
}
if(details->tz_sec < 0) {
*str++ = '-';
ret = -details->tz_sec;
@ -146,6 +156,7 @@ char* iso8601_print(char* str, int amt, const struct iso8601_date* date,
*str++ = '+';
ret = details->tz_sec;
}
--amt;
y = ret / 3600;
ret -= y * 3600;
@ -153,12 +164,14 @@ char* iso8601_print(char* str, int amt, const struct iso8601_date* date,
ret -= m * 60;
d = ret;
if(d) snprintf(str, amt, extended ? "%02d:%02d:%02d" : "%02d%02d%02d", y, m, d);
else if(m) snprintf(str, amt, extended ? "%02d:%02d" : "%02d%02d", y, m);
if(d) snprintf(str, amt, extended ?
"%02d:%02d:%02d" : "%02d%02d%02d", y, m, d);
else if(m) snprintf(str, amt, extended ?
"%02d:%02d" : "%02d%02d", y, m);
else snprintf(str, amt, "%02d", y);
} else {
*str++ = 'Z';
if(amt > 1) *str = 0;
*str++ = 0;
}
return str_orig;

View File

@ -1,16 +1,17 @@
/* libiso8601/src/libiso8601/400_c_library.c
*
* (c)2006, Laurence Withers, <l@lwithers.me.uk>.
* Released under the GNU GPLv2. See file COPYING or
* (c)2006-2010, Laurence Withers, <l@lwithers.me.uk>.
* Released under the GNU GPLv3. See file COPYING or
* http://www.gnu.org/copyleft/gpl.html for details.
*/
// days between 0000-001 and 1970-001 (the unix epoch)
/* days between 0000-001 and 1970-001 (the unix epoch) */
#define EPOCH_GDAY (719528)
void iso8601_now(struct iso8601_date* date, struct iso8601_details* details)
void
iso8601_now(struct iso8601_date* date, struct iso8601_details* details)
{
static int use_gettimeofday = 0;
struct timespec ts;
@ -21,7 +22,8 @@ void iso8601_now(struct iso8601_date* date, struct iso8601_details* details)
if(use_gettimeofday || clock_gettime(CLOCK_REALTIME, &ts)) {
use_gettimeofday = 1;
gettimeofday(&tv, 0);
ts.tv_sec = tv.tv_sec;
if(tv.tv_sec < 0) ts.tv_sec = 0;
else ts.tv_sec = tv.tv_sec;
ts.tv_nsec = tv.tv_usec * 1000L;
}
@ -44,11 +46,44 @@ void iso8601_now(struct iso8601_date* date, struct iso8601_details* details)
void iso8601_from_ts(struct iso8601_date* date, const struct timespec* ts)
int
iso8601_set_sysclock(const struct iso8601_date* date)
{
static int use_settimeofday = 0;
struct timeval tv;
struct timespec ts;
/* not all systems have clock_settime implemented */
if(use_settimeofday) goto try_settimeofday;
/* try setting clock */
iso8601_to_ts(&ts, date);
if(clock_settime(CLOCK_REALTIME, &ts)) {
if(errno == EPERM) return -1;
use_settimeofday = 1;
goto try_settimeofday;
}
return 0;
/* fallback using settimeofday() */
try_settimeofday:
iso8601_to_tv(&tv, date);
return settimeofday(&tv, 0);
}
void
iso8601_from_ts(struct iso8601_date* date, const struct timespec* ts)
{
ldiv_t qr;
qr = ldiv(ts->tv_sec, 86400);
if(ts->tv_sec < 0) {
--qr.quot;
qr.rem += 86400;
}
date->day = EPOCH_GDAY + qr.quot;
date->sec = qr.rem;
date->nsec = ts->tv_nsec;
@ -56,7 +91,8 @@ void iso8601_from_ts(struct iso8601_date* date, const struct timespec* ts)
void iso8601_to_ts(struct timespec* ts, const struct iso8601_date* date)
void
iso8601_to_ts(struct timespec* ts, const struct iso8601_date* date)
{
ts->tv_sec = 86400L * (date->day - EPOCH_GDAY) + date->sec;
ts->tv_nsec = date->nsec;
@ -64,11 +100,42 @@ void iso8601_to_ts(struct timespec* ts, const struct iso8601_date* date)
void iso8601_from_time_t(struct iso8601_date* date, const time_t* t)
void
iso8601_from_tv(struct iso8601_date* date, const struct timeval* tv)
{
ldiv_t qr;
qr = ldiv(tv->tv_sec, 86400);
if(tv->tv_sec < 0) {
--qr.quot;
qr.rem += 86400;
}
date->day = EPOCH_GDAY + qr.quot;
date->sec = qr.rem;
date->nsec = tv->tv_usec * 1000;
}
void
iso8601_to_tv(struct timeval* tv, const struct iso8601_date* date)
{
tv->tv_sec = 86400L * (date->day - EPOCH_GDAY) + date->sec;
tv->tv_usec = date->nsec / 1000;
}
void
iso8601_from_time_t(struct iso8601_date* date, const time_t* t)
{
ldiv_t qr;
qr = ldiv(*t, 86400);
if(*t < 0) {
--qr.quot;
qr.rem += 86400;
}
date->day = EPOCH_GDAY + qr.quot;
date->sec = qr.rem;
date->nsec = 0;
@ -76,7 +143,8 @@ void iso8601_from_time_t(struct iso8601_date* date, const time_t* t)
void iso8601_to_time_t(time_t* t, const struct iso8601_date* date)
void
iso8601_to_time_t(time_t* t, const struct iso8601_date* date)
{
*t = 86400L * (date->day - EPOCH_GDAY) + date->sec;
}

View File

@ -1,7 +1,7 @@
/* libiso8601/src/libiso8601/400_c_library.h
*
* (c)2006, Laurence Withers, <l@lwithers.me.uk>.
* Released under the GNU GPLv2. See file COPYING or
* (c)2006-2010, Laurence Withers, <l@lwithers.me.uk>.
* Released under the GNU GPLv3. See file COPYING or
* http://www.gnu.org/copyleft/gpl.html for details.
*/
@ -9,7 +9,8 @@
/*! \defgroup c_library C library integration.
These functions enable integration with the C library (system call wrappers and conversion).
These functions enable integration with the C library (system call wrappers and
conversion).
*/
/*!@{*/
@ -18,30 +19,82 @@ These functions enable integration with the C library (system call wrappers and
/*! \brief Retrieve the current time.
\param[out] date Current date/time (may be 0), in UTC.
\param[out] details Details (may be 0), including timezone.
\param[out] date Current date/time (may be 0), in UTC. May be 0.
\param[out] details Details (may be 0), including timezone. May be 0.
Retrieves the current time from the system clock, storing it into \a date and \a details (both
parameters optional).
Retrieves the current time from the system clock, storing it into \a date and
\a details (both parameters optional).
*/
void iso8601_now(struct iso8601_date* date, struct iso8601_details* details);
/*! \brief Set the system clock.
\param date Date to set.
\retval 0 on success.
\retval -1 on error (and see \a errno).
Attempts to set the system clock using \c clock_settime(), falling back to \c
settimeofday(). The user will need the \c CAP_SYS_TIME capability to set the
clock, otherwise \a errno will be set to \c EPERM.
*/
int iso8601_set_sysclock(const struct iso8601_date* date)
#ifndef DOXYGEN
__attribute__((nonnull))
#endif
;
/*! \brief Convert from a struct timespec. */
void iso8601_from_ts(struct iso8601_date* date, const struct timespec* ts);
void iso8601_from_ts(struct iso8601_date* date, const struct timespec* ts)
#ifndef DOXYGEN
__attribute__((nonnull))
#endif
;
/*! \brief Convert to a struct timespec. */
void iso8601_to_ts(struct timespec* ts, const struct iso8601_date* date);
void iso8601_to_ts(struct timespec* ts, const struct iso8601_date* date)
#ifndef DOXYGEN
__attribute__((nonnull))
#endif
;
/*! \brief Convert from a struct timeval. */
void iso8601_from_tv(struct iso8601_date* date, const struct timeval* tv)
#ifndef DOXYGEN
__attribute__((nonnull))
#endif
;
/*! \brief Convert to a struct timeval. */
void iso8601_to_tv(struct timeval* tv, const struct iso8601_date* date)
#ifndef DOXYGEN
__attribute__((nonnull))
#endif
;
/*! \brief Convert from a time_t. */
void iso8601_from_time_t(struct iso8601_date* date, const time_t* t);
void iso8601_from_time_t(struct iso8601_date* date, const time_t* t)
#ifndef DOXYGEN
__attribute__((nonnull))
#endif
;
/*! \brief Convert to a time_t. */
void iso8601_to_time_t(time_t* t, const struct iso8601_date* date);
void iso8601_to_time_t(time_t* t, const struct iso8601_date* date)
#ifndef DOXYGEN
__attribute__((nonnull))
#endif
;
/*!@}*/
/* options for text editors
kate: replace-trailing-space-save true; space-indent true; tab-width 4;
vim: expandtab:ts=4:sw=4
vim: expandtab:ts=4:sw=4:syntax=ch.doxygen
*/

View File

@ -1,7 +1,7 @@
/* libiso8601/src/libiso8601/400_calc.h
*
* (c)2006, Laurence Withers, <l@lwithers.me.uk>.
* Released under the GNU GPLv2. See file COPYING or
* (c)2006-2010, Laurence Withers, <l@lwithers.me.uk>.
* Released under the GNU GPLv3. See file COPYING or
* http://www.gnu.org/copyleft/gpl.html for details.
*/
@ -9,8 +9,8 @@
/*! \defgroup calc Conversion and calculation routines.
This set of functions is useful for converting dates and times into formats understood by humans,
and vice versa.
This set of functions is useful for converting dates and times into formats
understood by humans, and vice versa.
*/
/*!@{*/
@ -38,7 +38,12 @@ int iso8601_isleap(int year);
\param date Date to convert.
*/
void iso8601_to_cal(int* year, int* month, int* day, const struct iso8601_date* date);
void iso8601_to_cal(int* year, int* month, int* day,
const struct iso8601_date* date)
#ifndef DOXYGEN
__attribute__((nonnull))
#endif
;
@ -51,11 +56,18 @@ void iso8601_to_cal(int* year, int* month, int* day, const struct iso8601_date*
\retval 0 on success.
\retval -1 on error (and see \a errno).
Converts the date specified in \a year, \a month and \a day into \a date. Does not touch the \a sec
or \a nsec time members of \a date.
Converts the date specified in \a year, \a month and \a day into \a date.
\note Does not touch the \a sec or \a nsec time members of \a date. This means
they will be unchanged after a call to this function. If this will lead to
unexpected results, initialise the structure to 0 first.
*/
int iso8601_from_cal(struct iso8601_date* date, int year, int month, int day);
int iso8601_from_cal(struct iso8601_date* date, int year, int month, int day)
#ifndef DOXYGEN
__attribute__((nonnull))
#endif
;
@ -66,7 +78,11 @@ int iso8601_from_cal(struct iso8601_date* date, int year, int month, int day);
\param date Date to convert.
*/
void iso8601_to_ord(int* year, int* oday, const struct iso8601_date* date);
void iso8601_to_ord(int* year, int* oday, const struct iso8601_date* date)
#ifndef DOXYGEN
__attribute__((nonnull))
#endif
;
@ -78,11 +94,18 @@ void iso8601_to_ord(int* year, int* oday, const struct iso8601_date* date);
\retval 0 on success.
\retval -1 on error (and see \a errno).
Converts the date specified into \a year and \a oday into \a date. Does not touch the \a sec or
\a nsec time members of \a date.
Converts the date specified into \a year and \a oday into \a date.
\note Does not touch the \a sec or \a nsec time members of \a date. This means
they will be unchanged after a call to this functoin. If this will lead to
unexpected results, initialise the structure to 0 first.
*/
int iso8601_from_ord(struct iso8601_date* date, int year, int oday);
int iso8601_from_ord(struct iso8601_date* date, int year, int oday)
#ifndef DOXYGEN
__attribute__((nonnull))
#endif
;
@ -94,7 +117,12 @@ int iso8601_from_ord(struct iso8601_date* date, int year, int oday);
\param date Date to convert.
*/
void iso8601_to_week(int* year, int* week, int* wday, const struct iso8601_date* date);
void iso8601_to_week(int* year, int* week, int* wday,
const struct iso8601_date* date)
#ifndef DOXYGEN
__attribute__((nonnull))
#endif
;
@ -107,8 +135,18 @@ void iso8601_to_week(int* year, int* week, int* wday, const struct iso8601_date*
\retval 0 on success.
\retval -1 on error (and see \a errno).
Converts the date specified into \a year, \a week and \a wday into \a date.
\note Does not touch the \a sec or \a nsec time members of \a date. This means
they will be unchanged after a call to this functoin. If this will lead to
unexpected results, initialise the structure to 0 first.
*/
int iso8601_from_week(struct iso8601_date* date, int year, int week, int wday);
int iso8601_from_week(struct iso8601_date* date, int year, int week, int wday)
#ifndef DOXYGEN
__attribute__((nonnull))
#endif
;
@ -119,11 +157,16 @@ int iso8601_from_week(struct iso8601_date* date, int year, int week, int wday);
\param[out] sec Output second.
\param date Date/time to convert.
Converts the time stored in \a date into wall clock format, storing the result in \a hour, \a min
and \a sec.
Converts the time stored in \a date into wall clock format, storing the result
in \a hour, \a min and \a sec.
*/
void iso8601_to_clocktime(int* hour, int* min, int* sec, const struct iso8601_date* date);
void iso8601_to_clocktime(int* hour, int* min, int* sec,
const struct iso8601_date* date)
#ifndef DOXYGEN
__attribute__((nonnull))
#endif
;
@ -136,16 +179,25 @@ void iso8601_to_clocktime(int* hour, int* min, int* sec, const struct iso8601_da
\retval 0 on success.
\retval -1 on error (and see \a errno).
Converts the time as specified by \a hour, \a min and \a sec, storing the result in \a date. Does
not touch the \a day (date) member of \a date.
Converts the time as specified by \a hour, \a min and \a sec, storing the
result in \a date.
\note Does not touch the \a day (date) member of \a date. This means it will be
unchanged after a call to this function. If this will lead to unexpected
results, initialise the structure to 0 first.
*/
int iso8601_from_clocktime(struct iso8601_date* date, int hour, int min, int sec);
int iso8601_from_clocktime(struct iso8601_date* date, int hour, int min,
int sec)
#ifndef DOXYGEN
__attribute__((nonnull))
#endif
;
/*!@}*/
/* options for text editors
kate: replace-trailing-space-save true; space-indent true; tab-width 4;
vim: expandtab:ts=4:sw=4
vim: expandtab:ts=4:sw=4:syntax=ch.doxygen
*/

View File

@ -1,7 +1,7 @@
/* libiso8601/src/libiso8601/400_leap.h
*
* (c)2006, Laurence Withers, <l@lwithers.me.uk>.
* Released under the GNU GPLv2. See file COPYING or
* (c)2006-2010, Laurence Withers, <l@lwithers.me.uk>.
* Released under the GNU GPLv3. See file COPYING or
* http://www.gnu.org/copyleft/gpl.html for details.
*/
@ -9,7 +9,30 @@
/*! \defgroup leap Leap second support.
A set of functions for explicitly dealing with leap seconds.
A set of functions for explicitly dealing with leap seconds. All library
functions are implicitly leap second aware, but there are occasions when leap
seconds must be dealt with explicitly. These functions permit this.
Internally, leap seconds are represented as a table of day numbers; each day
number present in the table means that the corresponding day has 86401 seconds
(a leap second).
\note The library does not (yet) have support for negative leap seconds. This
support will be added should a day with a negative leap second ever come.
This will need to addressed in the API as the current API does not support
loading a table of negative leap seconds.
It is possible to create a disk file containing an updated table and to load
that file using \ref iso8601_leap_table_load (or to explicitly use an existing
in-memory table with \ref iso8601_leap_table_set). The format of the on-disk
file is:
<pre># integers are stored big-endian
[8*char] signature, "/O9PdPZI"
[uint32] number of entries, 'n'
# entries are repeated 'n' times, and stored lowest day number first
[uint32] day number of entry</pre>
*/
/*!@{*/
@ -23,14 +46,19 @@ A set of functions for explicitly dealing with leap seconds.
\retval 86400 day with no leap second.
\retval 86401 day with one leap second.
Returns the duration of a day \a date, in seconds. This function takes leap seconds into account and
may be used to determine if a day contains a leap second or not.
Returns the duration of a day \a date, in seconds. This function takes leap
seconds into account and may be used to determine if a day contains a leap
second or not.
\note There have not yet been any days requiring a negative leap second, so at present 86399 will
never be returned.
\note There have not yet been any days requiring a negative leap second, so at
present 86399 will never be returned.
*/
int iso8601_seconds_leap(const struct iso8601_date* date);
int iso8601_seconds_leap(const struct iso8601_date* date)
#ifndef DOXYGEN
__attribute__((nonnull))
#endif
;
@ -40,18 +68,68 @@ int iso8601_seconds_leap(const struct iso8601_date* date);
\param end The end date.
\returns Number of leap seconds elapsed.
Computes the number of leap seconds that have elapsed between two days. Note that this is the sum of
such leap seconds, so it will be 0 if (for example) there is one positive leap second and one
negative leap second. The ordering of the dates is important; if \a start is after \a end, then the
value returned will be negative (for positive leap seconds).
Computes the number of leap seconds that have elapsed between two days. Note
that this is the sum of such leap seconds, so it will be 0 if (for example)
there is one positive leap second and one negative leap second. The ordering of
the dates is important; if \a start is after \a end, then the value returned
will be negative (for positive leap seconds).
*/
int iso8601_leap_elapsed(const struct iso8601_date* start, const struct iso8601_date* end);
int iso8601_leap_elapsed(const struct iso8601_date* start,
const struct iso8601_date* end)
#ifndef DOXYGEN
__attribute__((nonnull))
#endif
;
/*! \brief Update table of leap seconds.
\param new_table Array of day numbers on which leap seconds occur.
\param new_size Number of entries in array.
This function can be used to update the table of leap seconds at runtime. The
\a new_table argument points to an array of integers, each entry being the day
number containing a leap second. The array must be sorted so that lower day
numbers appear towards the start of the array. \a new_size gives the number of
entries in the array. \a new_table must persist in memory as long as library
functions are in use.
\warning If negative leap seconds are ever used, this function will not support
them. There may need to be an ABI change in future to solve this problem.
*/
void iso8601_leap_table_set(int* new_table, int new_size)
#ifndef DOXYGEN
__attribute__((nonnull))
#endif
;
/*! \brief Load new table of leap seconds from disk.
\param fname Filename. May be 0 for system default.
\retval 0 on success.
\retval -1 on error (and see \a errno).
This function attempts to load a new table of leap seconds from disk, using the
filename \a fname. If \a fname is not specified, the system default (set at
compile time) will be used.
If the table is loaded successfully, it will be set with \ref
iso8601_leap_table_set(). On any error, -1 will be returned, and \a errno will
be set appropriately. If \a errno is \c EINVAL, then the file does not contain
a valid leap second table.
*/
int iso8601_leap_table_load(const char* fname);
/*!@}*/
/* options for text editors
kate: replace-trailing-space-save true; space-indent true; tab-width 4;
vim: expandtab:ts=4:sw=4
vim: expandtab:ts=4:sw=4:syntax=ch.doxygen
*/

View File

@ -1,13 +1,14 @@
/* libiso8601/src/libiso8601/400_manip.c
*
* (c)2006, Laurence Withers, <l@lwithers.me.uk>.
* Released under the GNU GPLv2. See file COPYING or
* (c)2006-2010, Laurence Withers, <l@lwithers.me.uk>.
* Released under the GNU GPLv3. See file COPYING or
* http://www.gnu.org/copyleft/gpl.html for details.
*/
int iso8601_lt(const struct iso8601_date* d1, const struct iso8601_date* d2)
int
iso8601_lt(const struct iso8601_date* d1, const struct iso8601_date* d2)
{
if(d1->day < d2->day) return 1;
if(d1->day > d2->day) return 0;
@ -19,7 +20,8 @@ int iso8601_lt(const struct iso8601_date* d1, const struct iso8601_date* d2)
int iso8601_lte(const struct iso8601_date* d1, const struct iso8601_date* d2)
int
iso8601_lte(const struct iso8601_date* d1, const struct iso8601_date* d2)
{
if(d1->day < d2->day) return 1;
if(d1->day > d2->day) return 0;
@ -31,48 +33,75 @@ int iso8601_lte(const struct iso8601_date* d1, const struct iso8601_date* d2)
int iso8601_eq(const struct iso8601_date* d1, const struct iso8601_date* d2)
int
iso8601_eq(const struct iso8601_date* d1, const struct iso8601_date* d2)
{
return (d1->day == d2->day) && (d1->sec == d2->sec) && (d1->nsec == d2->nsec);
return (d1->day == d2->day) && (d1->sec == d2->sec)
&& (d1->nsec == d2->nsec);
}
void iso8601_add_elapsed(struct iso8601_date* date, const struct iso8601_elapsed* per)
int
iso8601_cmp(const struct iso8601_date* d1, const struct iso8601_date* d2)
{
if(d1->day < d2->day) return -1;
if(d1->day > d2->day) return 1;
if(d1->sec < d2->sec) return -1;
if(d1->sec > d2->sec) return 1;
if(d1->nsec < d2->nsec) return -1;
if(d1->nsec > d2->nsec) return 1;
return 0;
}
void
iso8601_add_elapsed(struct iso8601_date* date,
const struct iso8601_elapsed* per)
{
div_t qr;
int leapcorrect;
/* work out number to advance `day' by */
/* work out number to advance day by */
qr = div(per->sec, 86400);
leapcorrect = _leap_elapsed_day(date->day, date->day + qr.quot);
date->day += qr.quot;
date->sec += qr.rem - leapcorrect;
date->nsec += per->nsec;
if(date->nsec >= BILLION) {
++date->sec;
date->nsec -= BILLION;
}
if(date->sec < 0) {
--date->day;
date->sec += iso8601_seconds_leap(date);
}
date->nsec += per->nsec;
if(date->nsec > BILLION) {
++date->sec;
date->nsec -= BILLION;
if(date->sec >= iso8601_seconds_leap(date)) {
date->sec -= iso8601_seconds_leap(date);
++date->day;
}
}
void iso8601_subtract_elapsed(struct iso8601_date* date, const struct iso8601_elapsed* per)
void
iso8601_subtract_elapsed(struct iso8601_date* date,
const struct iso8601_elapsed* per)
{
div_t qr;
int leapcorrect;
/* work out number to advance `day' by */
/* work out number to advance day by */
qr = div(per->sec, 86400);
leapcorrect = _leap_elapsed_day(date->day - qr.quot, date->day);
date->day -= qr.quot;
date->sec -= qr.rem - leapcorrect;
date->nsec -= per->nsec;
if(date->nsec < 0) {
--date->sec;
date->nsec += BILLION;
@ -82,6 +111,7 @@ void iso8601_subtract_elapsed(struct iso8601_date* date, const struct iso8601_el
--date->day;
date->sec += iso8601_seconds_leap(date);
}
if(date->sec >= iso8601_seconds_leap(date)) {
date->sec -= iso8601_seconds_leap(date);
++date->day;
@ -90,6 +120,84 @@ void iso8601_subtract_elapsed(struct iso8601_date* date, const struct iso8601_el
void
iso8601_add_multiple(struct iso8601_date* date,
const struct iso8601_elapsed* per, int n)
{
long long nsec;
lldiv_t qr;
struct iso8601_elapsed mult;
nsec = per->nsec * llabs(n);
qr = lldiv(nsec, BILLION);
mult.sec = qr.quot + per->sec * llabs(n);
mult.nsec = qr.rem;
if(n < 0) iso8601_subtract_elapsed(date, &mult);
else iso8601_add_elapsed(date, &mult);
}
void
iso8601_difference(const struct iso8601_date* d1, const struct iso8601_date* d2,
struct iso8601_elapsed* per, int* sign)
{
const struct iso8601_date* e1, * e2;
/* compute sign */
if(iso8601_lt(d1, d2)) {
if(sign) *sign = -1;
e1 = d2;
e2 = d1;
} else {
if(sign) *sign = 1;
e1 = d1;
e2 = d2;
}
if(!per) return;
/* compute difference, knowing that e1 >= e2 */
per->sec = (e1->day - e2->day) * 86400
+ iso8601_leap_elapsed(e2, e1 /* start, end */)
+ (e1->sec - e2->sec);
if(e1->nsec < e2->nsec) {
--per->sec;
per->nsec = BILLION + e1->nsec - e2->nsec;
} else {
per->nsec = e1->nsec - e2->nsec;
}
}
int
iso8601_elapsed_div(const struct iso8601_elapsed* num,
const struct iso8601_elapsed* denom, struct iso8601_elapsed* remain)
{
unsigned long long pnum, pdenom;
lldiv_t val, v2;
pnum = num->sec;
pnum *= BILLION;
pnum += num->nsec;
pdenom = denom->sec;
pdenom *= BILLION;
pdenom += denom->nsec;
val = lldiv(pnum, pdenom);
if(remain) {
v2 = lldiv(val.rem, BILLION);
remain->sec = v2.quot;
remain->nsec = v2.rem;
}
return val.quot;
}
/* options for text editors
kate: replace-trailing-space-save true; space-indent true; tab-width 4;
vim: expandtab:ts=4:sw=4

View File

@ -1,7 +1,7 @@
/* libiso8601/src/libiso8601/400_manip.h
*
* (c)2006, Laurence Withers, <l@lwithers.me.uk>.
* Released under the GNU GPLv2. See file COPYING or
* (c)2006-2010, Laurence Withers, <l@lwithers.me.uk>.
* Released under the GNU GPLv3. See file COPYING or
* http://www.gnu.org/copyleft/gpl.html for details.
*/
@ -9,8 +9,8 @@
/*! \defgroup manip Manipulation routines.
This set of functions is useful for converting dates and times into formats understood by humans,
and vice versa.
This set of functions is useful for performing arithmetic etc. on dates. It
uses struct \ref iso8601_elapsed to represent the magnitude of time differences.
*/
/*!@{*/
@ -25,7 +25,11 @@ and vice versa.
\retval 0 otherwise
*/
int iso8601_lt(const struct iso8601_date* d1, const struct iso8601_date* d2);
int iso8601_lt(const struct iso8601_date* d1, const struct iso8601_date* d2)
#ifndef DOXYGEN
__attribute__((nonnull))
#endif
;
@ -37,7 +41,11 @@ int iso8601_lt(const struct iso8601_date* d1, const struct iso8601_date* d2);
\retval 0 otherwise
*/
int iso8601_lte(const struct iso8601_date* d1, const struct iso8601_date* d2);
int iso8601_lte(const struct iso8601_date* d1, const struct iso8601_date* d2)
#ifndef DOXYGEN
__attribute__((nonnull))
#endif
;
@ -49,7 +57,28 @@ int iso8601_lte(const struct iso8601_date* d1, const struct iso8601_date* d2);
\retval 0 otherwise
*/
int iso8601_eq(const struct iso8601_date* d1, const struct iso8601_date* d2);
int iso8601_eq(const struct iso8601_date* d1, const struct iso8601_date* d2)
#ifndef DOXYGEN
__attribute__((nonnull))
#endif
;
/*! \brief Comparison (for qsort et al.).
\param d1 First date to compare.
\param d2 Second date to compare.
\retval -1 if \a d1 &lt; \a d2
\retval 0 if \a d1 == \a d2
\retval 1 if \a d1 &gt; \a d2
*/
int iso8601_cmp(const struct iso8601_date* d1, const struct iso8601_date* d2)
#ifndef DOXYGEN
__attribute__((nonnull))
#endif
;
@ -59,7 +88,12 @@ int iso8601_eq(const struct iso8601_date* d1, const struct iso8601_date* d2);
\param per Period to advance date/time by.
*/
void iso8601_add_elapsed(struct iso8601_date* date, const struct iso8601_elapsed* per);
void iso8601_add_elapsed(struct iso8601_date* date,
const struct iso8601_elapsed* per)
#ifndef DOXYGEN
__attribute__((nonnull))
#endif
;
@ -69,12 +103,80 @@ void iso8601_add_elapsed(struct iso8601_date* date, const struct iso8601_elapsed
\param per Period to regress date/time by.
*/
void iso8601_subtract_elapsed(struct iso8601_date* date, const struct iso8601_elapsed* per);
void iso8601_subtract_elapsed(struct iso8601_date* date,
const struct iso8601_elapsed* per)
#ifndef DOXYGEN
__attribute__((nonnull))
#endif
;
/*! \brief Add a multiple of a period to a date.
\param[in,out] date Date to modify.
\param per Period to advance date/time by.
\param n Multiple of \a per.
Adds \a n multiples of \a per to \a date. \a n may be 0 or negative. The result
is stored in \a date. This is an efficient implementation which avoids loops,
but it does use 64-bit arithmetic.
*/
void iso8601_add_multiple(struct iso8601_date* date,
const struct iso8601_elapsed* per, int n)
#ifndef DOXYGEN
__attribute__((nonnull))
#endif
;
/*! \brief Find difference between dates.
\param d1 First date.
\param d2 Second date.
\param[out] per Magnitude of period elapsed between two dates. Pointer may be 0.
\param[out] sign Set to sign of difference (-1 or +1). Pointer may be 0.
This function will perform the calculation <code>|d1 - d2|</code>, storing the
result in \a per (if it is not a null pointer). The sign of the result is
stored in \a sign (if it is not a null pointer), i.e. -1 if \a d2 &gt; \a d1 or
+1 if \a d2 &lt;= \a d1.
*/
void iso8601_difference(const struct iso8601_date* d1,
const struct iso8601_date* d2, struct iso8601_elapsed* per, int* sign)
#ifndef DOXYGEN
__attribute__((nonnull(1,2)))
#endif
;
/*! \brief Divide one period by another.
\param num Numerator.
\param denom Denominator (divisor).
\param[out] remain Remainder. May be 0.
\returns Number of times \a denom divides into \a num.
This function computes the number of times that \a denom can be divided into \a
num, returning that number. If desired, the remaining period which could not be
divided can be written into \a remain. Uses 64-bit arithmetic internally.
*/
int iso8601_elapsed_div(const struct iso8601_elapsed* num,
const struct iso8601_elapsed* denom, struct iso8601_elapsed* remain)
#ifndef DOXYGEN
__attribute__((nonnull(1,2)));
#endif
;
/*!@}*/
/* options for text editors
kate: replace-trailing-space-save true; space-indent true; tab-width 4;
vim: expandtab:ts=4:sw=4
vim: expandtab:ts=4:sw=4:syntax=ch.doxygen
*/

View File

@ -1,10 +1,14 @@
/* libiso8601/src/libiso8601/999_BottomHeader.h
*
* (c)2006, Laurence Withers, <l@lwithers.me.uk>.
* Released under the GNU GPLv2. See file COPYING or
* (c)2006-2010, Laurence Withers, <l@lwithers.me.uk>.
* Released under the GNU GPLv3. See file COPYING or
* http://www.gnu.org/copyleft/gpl.html for details.
*/
#ifdef __cplusplus
}
#endif
#endif
/* options for text editors

View File

@ -7,14 +7,12 @@ build_dir_tree "${INCLUDEDIR}" || return 1
# install library
echo "Installing libraries into '${LIBDIR}'"
source src/libiso8601/soversion
install_file ${libiso8601} ${LIBDIR} 0755 || return 1
BASE="${libiso8601_BASE}.so"
MAJOR="${BASE}.${SOMAJOR}"
MINOR="${MAJOR}.${SOMINOR}"
MICRO="${MINOR}.${SOMICRO}"
install_symlink "${MINOR}" "${MICRO}" "${LIBDIR}"
install_symlink "${MAJOR}" "${MINOR}" "${LIBDIR}"
install_symlink "${BASE}" "${MAJOR}" "${LIBDIR}"
MICRO="${MAJOR}.${SOMICRO}"
install_symlink "${BASE}" "${MICRO}" "${LIBDIR}"
# install header
echo "Installing header file '${libiso8601_HEADER}' into ${INCLUDEDIR}"
@ -29,12 +27,11 @@ do_cmd_redir "${CONFFILE}" sed \
-e "s,@VERSION@,${VERSION}," \
-e "s,@DEP_CFLAGS@,${libiso8601_DEP_CFLAGS}," \
-e "s,@DEP_LIBS@,${libiso8601_DEP_LIBS}," \
-e "s,@LIB_DIR@,${FINALLIBDIR}," \
-e "s,@INCLUDE_DIR@,${FINALINCLUDEDIR}," \
-e "s,@LIB_DIR@,${LIBDIR}," \
-e "s,@INCLUDE_DIR@,${INCLUDEDIR}," \
src/libiso8601/config-script
do_cmd chmod 0755 "${CONFFILE}"
print_success "Done"
# kate: replace-trailing-space-save true; space-indent true; tab-width 4;
# vim: expandtab:ts=4:sw=4
# vim: syntax=sh:expandtab:ts=4:sw=4

View File

@ -11,10 +11,12 @@ then
libiso8601_BASE=libiso8601
source src/libiso8601/soversion
libiso8601="obj/${libiso8601_BASE}.so.${SOMAJOR}.${SOMINOR}.${SOMICRO}"
libiso8601="obj/${libiso8601_BASE}.so.${SOMAJOR}.${SOMICRO}"
libiso8601_DEP_CFLAGS=""
libiso8601_DEP_LIBS="-lrt"
SO_EXTRA="${libiso8601_DEP_CFLAGS} ${libiso8601_DEP_LIBS} -lc"
SO_EXTRA="${libiso8601_DEP_CFLAGS} ${libiso8601_DEP_LIBS} -lc \
-D_GNU_SOURCE -std=gnu99 \
-DDEFAULT_LEAP_TABLE=\"${DEFAULT_LEAP_TABLE}\""
echo "Building library ${libiso8601}..."
@ -34,13 +36,14 @@ then
then
echo " Compiling"
SONAME="${libiso8601_BASE}.so.${SOMAJOR}.${SOMINOR}"
SONAME="${libiso8601_BASE}.so.${SOMAJOR}"
do_cmd ${CC} ${CFLAGS} -Iobj -shared -fpic -o "${libiso8601}" \
-Wl,-soname,${SONAME} \
${SRC} ${SO_EXTRA} || return 1
# make tests work
do_cmd ln -sf $(basename ${libiso8601}) obj/${SONAME} || return 1
# make tests and linking work
do_cmd ln -sf "$(basename "${libiso8601}")" "obj/${SONAME}" || return 1
do_cmd ln -sf "$(basename "${libiso8601}")" "obj/${libiso8601_BASE}.so" || return 1
print_success "Library built"
else
@ -51,5 +54,4 @@ then
libiso8601_HEADER=${HDR}
fi
# kate: replace-trailing-space-save true; space-indent true; tab-width 4;
# vim: expandtab:ts=4:sw=4
# vim: syntax=sh:expandtab:ts=4:sw=4

View File

@ -8,14 +8,13 @@ MONOLITHIC_TESTS="src/libiso8601/build.lib src/libiso8601/build.monolithic"
if [ -z "${libiso8601_MONOLITHIC}" ]
then
MONOLITHIC_SOURCE="$(echo src/libiso8601/*.h)"
MONOLITHIC_SOURCE="$(find src/libiso8601/ -name '*.h' | sort)"
make_monolithic ${HDR} Ch || return 1
MONOLITHIC_SOURCE="$(echo src/libiso8601/*.c)"
MONOLITHIC_SOURCE="$(find src/libiso8601/ -name '*.c' | sort)"
make_monolithic ${SRC} C || return 1
libiso8601_MONOLITHIC=1
MONOLITHIC_DOC="${MONOLITHIC_DOC} ${HDR}"
fi
# kate: replace-trailing-space-save true; space-indent true; tab-width 4;
# vim: expandtab:ts=4:sw=4
# vim: syntax=sh:expandtab:ts=4:sw=4

View File

@ -94,5 +94,4 @@ true
# vim: expandtab:ts=4:sw=4
# kate: replace-trailing-space-save true; space-indent true; tab-width 4;
# vim: syntax=sh:expandtab:ts=4:sw=4

View File

@ -1,21 +0,0 @@
# libiso8601/src/lib/clib/pkgconf.in
#
# Metadata file for pkg-config
# ( http://www.freedesktop.org/software/pkgconfig/ )
#
# (c)2006, Laurence Withers, <l@lwithers.me.uk>.
# Released under the GNU GPLv2. See file COPYING or
# http://www.gnu.org/copyleft/gpl.html for details.
#
# Name, description
Name: @TODO@
Description: @TODO@
Version: @VERSION@
# Requirements
Requires:
# Compilation information
Libs: -L@LIBDIR@ -liso8601
Cflags: -I@INCLUDEDIR@

View File

@ -1,17 +1,15 @@
# libiso8601/src/libiso8601/soversion
#
# (c)2007, Laurence Withers, <l@lwithers.me.uk>.
# Released under the GNU GPLv2. See file COPYING or
# http://www.gnu.org/copyleft/gpl.html for details.
# Copyright: ©20072011, Güralp Systems Ltd.
# Author: Laurence Withers <lwithers@guralp.com>
# License: GPLv3
#
# SOMAJOR and SOMINOR are included in the library's soname. They need to
# be bumped on a binary-incompatible release. They are both single
# integers.
SOMAJOR=0
SOMINOR=1
# SOMAJOR is included in the library's soname, and needs to be bumped
# after a binary-incompatible release. It is a single integer.
SOMAJOR=1
# SOMICRO is bumped every time there is a binary-compatible release.
SOMICRO=0
SOMICRO=10

1
src/setisodate/.params Normal file
View File

@ -0,0 +1 @@
app c setisodate sbin

View File

@ -0,0 +1,45 @@
/* libiso8601/src/setisodate/000_TopSource.c
*
* (c)2007, Laurence Withers, <l@lwithers.me.uk>.
* Released under the GNU GPLv2. See file COPYING or
* http://www.gnu.org/copyleft/gpl.html for details.
*/
/* Below are all the includes used throughout the application. */
#include <stdio.h>
#include "iso8601.h"
int main(int argc, char* argv[])
{
struct iso8601_date d;
char str[100];
if(argc != 2) {
fputs("Expecting one argument: date/time (in ISO8601 format) to set.\n", stderr);
return 1;
}
if(iso8601_parse(argv[1], &d, 0, 0)) {
fputs("Cannot parse date.\n", stderr);
return 1;
}
printf("Setting date to %s.\n",
iso8601_print(str, sizeof(str), &d, 0));
if(iso8601_set_sysclock(&d)) {
perror("iso8601_set_sysclock");
return 1;
}
return 0;
}
/* options for text editors
kate: replace-trailing-space-save true; space-indent true; tab-width 4;
vim: expandtab:ts=4:sw=4
*/

43
src/setisodate/build.app Normal file
View File

@ -0,0 +1,43 @@
# These are external variables, and shouldn't clash with anything else
# setisodate
# setisodate_BUILT
#
build_target libiso8601
if [ -z ${setisodate_BUILT} ]
then
setisodate="obj/setisodate"
EXTRAS="-std=gnu99 -D_GNU_SOURCE -DAPP_NAME=\"setisodate\" \
${libiso8601} ${libiso8601_DEP_CFLAGS} ${libiso8601_DEP_LIBS}"
echo "Building application ${setisodate}..."
do_cmd source src/setisodate/build.monolithic || return 1
MODIFIED=0
for test in ${MONOLITHIC_TESTS} ${SRC}
do
if [ ${test} -nt ${setisodate} ]
then
MODIFIED=1
break
fi
done
if [ ${MODIFIED} -ne 0 ]
then
echo " Compiling..."
do_cmd ${CC} ${CFLAGS} -I obj -o "${setisodate}" ${SRC} ${EXTRAS} || return 1
print_success "Application built"
else
print_success "Application up to date"
fi
setisodate_BUILT=1
fi
# vim: syntax=sh:expandtab:ts=4:sw=4

View File

@ -0,0 +1 @@
source src/setisodate/build.app

View File

@ -0,0 +1 @@
source src/setisodate/build.install-app

View File

@ -0,0 +1,11 @@
build_target setisodate
# make paths (this is for Gentoo in particular)
build_dir_tree "${SBINDIR}" || return 1
# install binary
echo "Installing binaries into '${SBINDIR}'"
install_file "${setisodate}" "${SBINDIR}" 0755 || return 1
print_success "Done"
# vim: syntax=sh:expandtab:ts=4:sw=4

View File

@ -0,0 +1,17 @@
# These are external variables, and shouldn't clash with anything else
# setisodate_MONOLITHIC
#
SRC="obj/setisodate.c"
MONOLITHIC_TESTS="src/setisodate/build.app src/setisodate/build.monolithic"
if [ -z "${setisodate_MONOLITHIC}" ]
then
MONOLITHIC_SOURCE="$(find src/setisodate/ -name '*.c' | sort)"
make_monolithic ${SRC} C || return 1
setisodate_MONOLITHIC=1
MONOLITHIC_DOC="${MONOLITHIC_DOC} ${SRC}"
fi
# vim: syntax=sh:expandtab:ts=4:sw=4

View File

@ -1,3 +1 @@
source src/tests/build.tests
# kate: replace-trailing-space-save true; space-indent true; tab-width 4;
# vim: expandtab:ts=4:sw=4

View File

@ -6,15 +6,15 @@ build_target libiso8601 || return 1
if [ -z ${tests_BUILT} ]
then
LIBS="${libiso8601} "
EXTRAS="-D_GNU_SOURCE"
LIBS="${libiso8601} ${libiso8601_DEP_CFLAGS} ${libiso8601_DEP_LIBS} "
EXTRAS="-lm"
echo "Building test programs..."
do_cmd mkdir -p obj/tests || return 1
for SRC in src/tests/*.c
do
TEST="obj/tests/$(basename ${SRC} | sed -e 's,.c$,,')"
TEST="obj/tests/$(basename "${SRC}" ".c")"
MODIFIED=0
for file in ${LIBS} ${SRC} src/tests/build.tests
do
@ -39,5 +39,4 @@ then
tests_BUILT=1
fi
# kate: replace-trailing-space-save true; space-indent true; tab-width 4;
# vim: expandtab:ts=4:sw=4
# vim: syntax=sh:expandtab:ts=4:sw=4

369
src/tests/manip.c Normal file
View File

@ -0,0 +1,369 @@
/* libiso8601/src/tests/manip.c
*
* (c)2007, Laurence Withers, <l@lwithers.me.uk>.
* Released under the GNU GPLv2. See file COPYING or
* http://www.gnu.org/copyleft/gpl.html for details.
*/
#include "iso8601.h"
#include <math.h>
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
/* safe_strtod()
* Converts `str' into a double, which it will store in `out'. If an error occurs, returns -1,
* else 0 on success.
*/
int safe_strtod(const char* str, double* out)
{
char* endp = 0;
errno = 0;
*out = strtod(str, &endp);
if(errno || !endp || *endp) return -1;
return 0;
}
/* date_stack
* We use a stack of dates and/or periods to implement effectively an RPN manipulator.
*/
enum date_stack_type {
date_stack_type_date,
date_stack_type_per
};
struct date_stack {
enum date_stack_type type;
union {
struct {
struct iso8601_date d;
struct iso8601_details det;
}date;
struct iso8601_elapsed per;
}u;
struct date_stack* next;
};
struct date_stack* date_stack_top = 0;
int date_stack_size = 0;
/* date_stack_push_*()
* Routines for pushing an object onto the date stack.
*/
void date_stack_push_date(const struct iso8601_date* d, const struct iso8601_details* det)
{
struct date_stack* s;
s = malloc(sizeof(struct date_stack));
s->type = date_stack_type_date;
s->u.date.d = *d;
s->u.date.det = *det;
s->next = date_stack_top;
date_stack_top = s;
++date_stack_size;
}
void date_stack_push_elapsed(const struct iso8601_elapsed* per)
{
struct date_stack* s;
s = malloc(sizeof(struct date_stack));
s->type = date_stack_type_per;
s->u.per = *per;
s->next = date_stack_top;
date_stack_top = s;
++date_stack_size;
}
/* date_stack_pop()
* Pops an element from the date stack.
*/
void date_stack_pop(void)
{
struct date_stack* next;
--date_stack_size;
next = date_stack_top->next;
free(date_stack_top);
date_stack_top = next;
}
/* date_stack_pop_date()
* Pops a date from the stack, returning non-0 on error (underflow or type mismatch).
*/
int date_stack_pop_date(struct iso8601_date* d, struct iso8601_details* det)
{
if(!date_stack_top) {
fputs("Stack underflow.\n", stderr);
return -1;
}
if(date_stack_top->type != date_stack_type_date) {
fputs("Type mismatch.\n", stderr);
return -1;
}
*d = date_stack_top->u.date.d;
*det = date_stack_top->u.date.det;
date_stack_pop();
return 0;
}
/* date_stack_pop_elapsed()
* Pops an elapsed period from the stack, returning non-0 on error (underflow or type mismatch).
*/
int date_stack_pop_elapsed(struct iso8601_elapsed* per)
{
if(!date_stack_top) {
fputs("Stack underflow.\n", stderr);
return -1;
}
if(date_stack_top->type != date_stack_type_per) {
fputs("Type mismatch.\n", stderr);
return -1;
}
*per = date_stack_top->u.per;
date_stack_pop();
return 0;
}
/* date_stack_dump()
* Dumps and clears the contents of the date stack.
*/
void date_stack_dump(void)
{
while(date_stack_top) {
switch(date_stack_top->type) {
case date_stack_type_date:
fprintf(stderr, "Date (day=%d, sec=%d, nsec=%d)\n",
date_stack_top->u.date.d.day, date_stack_top->u.date.d.sec, date_stack_top->u.date.d.nsec);
break;
case date_stack_type_per:
fprintf(stderr, "Period (sec=%d, nsec=%d)\n",
date_stack_top->u.per.sec, date_stack_top->u.per.nsec);
break;
default:
fprintf(stderr, "Unknown type %d.\n", date_stack_top->type);
break;
}
date_stack_pop();
}
}
/* usage()
* Displays help.
*/
void usage(void)
{
fputs("Push a date or elapsed time onto the stack by entering it, e.g.:\n"
" 2007-07-29 12345.67\n"
"Operators:\n"
" + (date, period) Advance date by elapsed time, push date.\n"
" - (date, period) Regress date by elapsed time, push date.\n"
" +* (date, period, num) Advance date by elapsed time multiplied by num, push date.\n"
" -* (date, period, num) Regress date by elapsed time multiplied by num, push date.\n"
" dp (date, date) Compute difference, print elapsed time.\n"
" < (date, date) Prints 1 if first date less than second.\n"
" <= (date, date) Prints 1 if first date less than or equal to second.\n"
" == (date, date) Prints 1 if dates equal.\n"
" p (date) Prints a date.\n"
" ds Dump stack.\n"
"\n"
"Type help for this screen at any point.\n"
"\n", stdout);
}
int parse_command(char* cmd)
{
struct iso8601_date date, date2;
struct iso8601_details details, details2;
struct iso8601_elapsed period, num;
double period_d;
char date_str[40];
int ret = 0, sign;
cmd = strtok(cmd, " \n");
while(cmd) {
if(!iso8601_parse(cmd, &date, 0, &details)) {
date_stack_push_date(&date, &details);
} else if(!safe_strtod(cmd, &period_d) && period_d >= 0) {
period.sec = trunc(period_d);
period.nsec = trunc((period_d - period.sec) * 1e9);
date_stack_push_elapsed(&period);
} else if(!strcmp(cmd, "+")) {
if(date_stack_pop_elapsed(&period) || date_stack_pop_date(&date, &details)) {
ret = 1;
} else {
iso8601_add_elapsed(&date, &period);
date_stack_push_date(&date, &details);
}
} else if(!strcmp(cmd, "-")) {
if(date_stack_pop_elapsed(&period) || date_stack_pop_date(&date, &details)) {
ret = 1;
} else {
iso8601_subtract_elapsed(&date, &period);
date_stack_push_date(&date, &details);
}
} else if(!strcmp(cmd, "+*") || !strcmp(cmd, "-*")) {
if(date_stack_pop_elapsed(&num)) {
ret = 1;
} else if(num.nsec) {
ret = 1;
fputs("Number cannot be fractional.\n", stderr);
} else {
if(date_stack_pop_elapsed(&period) || date_stack_pop_date(&date, &details)) {
ret = 1;
} else {
iso8601_add_multiple(&date, &period, ((cmd[0] == '-') ? -1 : 1) * num.sec);
date_stack_push_date(&date, &details);
}
}
} else if(!strcmp(cmd, "dp")) {
if(date_stack_pop_date(&date, &details) || date_stack_pop_date(&date2, &details2)) {
ret = 1;
} else {
iso8601_difference(&date2, &date, &period, &sign);
printf("Difference: %c %lu.%09lu\n",
(sign < 0) ? '-' : '+',
(unsigned long)period.sec,
(unsigned long)period.nsec);
}
} else if(!strcmp(cmd, "<")) {
if(date_stack_pop_date(&date, &details) || date_stack_pop_date(&date2, &details2)) {
ret = 1;
} else {
printf("%d\n", iso8601_lt(&date2, &date));
}
} else if(!strcmp(cmd, "<=")) {
if(date_stack_pop_date(&date, &details) || date_stack_pop_date(&date2, &details2)) {
ret = 1;
} else {
printf("%d\n", iso8601_lte(&date2, &date));
}
} else if(!strcmp(cmd, "==")) {
if(date_stack_pop_date(&date, &details) || date_stack_pop_date(&date2, &details2)) {
ret = 1;
} else {
printf("%d\n", iso8601_eq(&date2, &date));
}
} else if(!strcmp(cmd, "p")) {
if(date_stack_pop_date(&date, &details)) {
ret = 1;
} else {
iso8601_print(date_str, sizeof(date_str), &date, &details);
fputs(date_str, stdout);
putc('\n', stdout);
}
} else if(!strcmp(cmd, "ds")) {
date_stack_dump();
} else {
fputs("Unrecognised command or argument.\n", stderr);
ret = 1;
}
cmd = strtok(0, " \n");
}
return ret;
}
int main(int argc, char* argv[])
{
int ret = 0;
char* buf = 0;
size_t buf_len = 0;
if(argc == 2 && !strcmp(argv[1], "--print-summary")) {
printf("Interactive date manipulator. Also evaluates commandline.\n");
return 0;
}
/* commandline -- evaluate arguments as though interactive */
if(argc > 1) {
for(ret = 1; ret < argc; ++ret) {
if(parse_command(argv[ret])) return 1;
}
return 0;
}
usage();
while(!feof(stdin)) {
printf("%d >> ", date_stack_size);
ret = getline(&buf, &buf_len, stdin);
if(ret == -1) break;
parse_command(buf);
}
return 0;
}
/* options for text editors
kate: replace-trailing-space-save true; space-indent true; tab-width 4;
vim: expandtab:ts=4:sw=4
*/

View File

@ -204,7 +204,7 @@ int main(int argc, char* argv[])
int ret = 0;
struct iso8601_date dt;
struct iso8601_details details, details2;
char buf[100], buf2[100];
char buf[40], buf2[40];
const struct test* test;
if(argc == 2 && !strcmp(argv[1], "--print-summary")) {
@ -215,7 +215,11 @@ int main(int argc, char* argv[])
iso8601_now(&dt, &details);
printf("%-30s %-30s %s\n", "Name", "Basic format", "Extended format");
for(test = tests; test->desc; ++test) {
memset(buf, 'X', sizeof(buf));
memset(buf2, 'X', sizeof(buf2)); /* test null-termination */
test->do_details(&details2, &details);
iso8601_print(buf, sizeof(buf), &dt, &details2);
++details2.extended;
iso8601_print(buf2, sizeof(buf2), &dt, &details2);

View File

@ -1,9 +1,9 @@
/* libiso8601/src/tests/???.c
/* libiso8601/src/tests/xxx.c
*
* (c)2007, Laurence Withers, <l@lwithers.me.uk>.
* Released under the GNU GPLv2. See file COPYING or
* http://www.gnu.org/copyleft/gpl.html for details.
*/
* Copyright: ©20072011, Güralp Systems Ltd.
* Author: Laurence Withers <lwithers@guralp.com>
* License: GPLv3
*/
#include "iso8601.h"
@ -12,12 +12,13 @@
int main(int argc, char* argv[])
int
main(int argc, char* argv[])
{
int ret = 0;
if(argc == 2 && !strcmp(argv[1], "--print-summary")) {
printf("One line summary.\n");
fputs("One line summary.\n", stdout);
return 0;
}
@ -30,7 +31,8 @@ int main(int argc, char* argv[])
return ret;
}
/* options for text editors
kate: replace-trailing-space-save true; space-indent true; tab-width 4;
vim: expandtab:ts=4:sw=4
*/

13
version
View File

@ -1,8 +1,8 @@
# libiso8601/version
#
# (c)2007, Laurence Withers, <l@lwithers.me.uk>.
# Released under the GNU GPLv2. See file COPYING or
# http://www.gnu.org/copyleft/gpl.html for details.
# Copyright: ©20072011, Güralp Systems Ltd.
# Author: Laurence Withers <lwithers@guralp.com>
# License: GPLv3
#
@ -10,8 +10,7 @@
# VERSION contains the full version number of the library, which is
# expected to be in 'major.minor.micro' format.
VERMAJOR=0
VERMINOR=2
VERMICRO=1
VERMINOR=3
VERMICRO=15
# kate: replace-trailing-space-save true; space-indent true; tab-width 4;
# vim: expandtab:ts=4:sw=4
# vim: expandtab:ts=4:sw=4:syntax=sh