<?xml version="1.0" encoding="UTF-8"?>
<!-- **************************************************************************
.... For copyright and licensing terms, see the file named COPYING.
.... **************************************************************************
.-->
<?xml-stylesheet href="docbook-xml.css" type="text/css"?>

<refentry id="leapsecs">

<refmeta xmlns:xi="http://www.w3.org/2001/XInclude">
<refentrytitle>leapsecs_init</refentrytitle>
<manvolnum>3</manvolnum>
<refmiscinfo class="manual">user commands</refmiscinfo>
<refmiscinfo class="source">djbwares</refmiscinfo>
<xi:include href="version.xml" />
</refmeta>

<refnamediv>
<refname>leapsecs_init</refname>
<refname>leapsecs_sub</refname>
<refname>leapsecs_add</refname>
<refname>leapsecs_read</refname>
<refpurpose>handle UTC leap seconds</refpurpose>
</refnamediv>

<refsynopsisdiv>
<funcsynopsis language="C++">
<funcsynopsisinfo>
#include &lt;leapsecs.h&gt;
</funcsynopsisinfo>
<funcprototype>
	<funcdef>int <function>leapsecs_sub</function></funcdef>
	<paramdef>struct tai * <parameter>t</parameter></paramdef>
</funcprototype>
</funcsynopsis>
<funcsynopsis language="C++">
<funcprototype>
	<funcdef>void <function>leapsecs_add</function></funcdef>
	<paramdef>struct tai * <parameter>t</parameter></paramdef>
	<paramdef>int <parameter>hit</parameter></paramdef>
</funcprototype>
</funcsynopsis>
<funcsynopsis language="C++">
<funcprototype>
	<funcdef>int <function>leapsecs_read</function></funcdef>
</funcprototype>
</funcsynopsis>
<funcsynopsis language="C++">
<funcprototype>
	<funcdef>int <function>leapsecs_init</function></funcdef>
</funcprototype>
</funcsynopsis>
</refsynopsisdiv>

<refsection><title>Description</title>
<para>
<function>leapsecs_sub</function> changes a seconds-since-epoch count into a non-leap-seconds-since-epoch count.
It interprets <replaceable>t</replaceable> as a TAI64 label, subtracts from <replaceable>t</replaceable> the number of leap seconds that have occurred before or at <replaceable>t</replaceable>, and places the result back into <replaceable>t</replaceable>.
</para>

<para>
<function>leapsecs_sub</function> returns 1 if <replaceable>t</replaceable> was a leap second, 0 otherwise.
</para>

<para>
<function>leapsecs_add</function> reverses the effect of <function>leapsecs_sub</function>.
<replaceable>hit</replaceable> must be 1 for a leap second, 0 otherwise.
</para>
</refsection>

<refsection><title>Leap-second table</title>
<para>
The current implementation of <function>leapsecs_sub</function> and <function>leapsecs_add</function> uses a leap-second table read from disk.
</para>

<para>
<function>leapsecs_read</function> reads the leap-second table from <filename>/etc/leapsecs.dat</filename>.
It returns 0 on success, -1 on error.
If <filename>/etc/leapsecs.dat</filename> does not exist, <function>leapsecs_read</function> treats it as an empty file.
</para>

<para>
<function>leapsecs_init</function> is a one-time version of <function>leapsecs_read</function>.
Initially it is the same as <function>leapsecs_read</function>; however, once <function>leapsecs_read</function> returns 0, <function>leapsecs_init</function> will always return 0 without calling <function>leapsecs_read</function> again.
</para>

<para>
<function>leapsecs_add</function> and <function>leapsecs_sub</function> call <function>leapsecs_init</function>.
</para>
<warning>
If <function>leapsecs_init</function> returns failure, <function>leapsecs_add</function> and <function>leapsecs_sub</function> will proceed without a leap-second table.
For reliability, all programs should call <function>leapsecs_init</function> at startup and check for errors.
</warning>

<warning>
New entries are added to the leap-second table on disk every 12 to 18 months.
<function>leapsecs_read</function> may be called repeatedly.
It leaves the old table alone on error.
For reliability, all long-running programs should call <function>leapsecs_read</function> at least once every month.
</warning>

</refsection>

<refsection><title>See also</title>
<variablelist>
<varlistentry><term><citerefentry><refentrytitle>tai</refentrytitle><manvolnum>3</manvolnum></citerefentry></term></varlistentry>
</variablelist>
</refsection>

<refsection>
<title>Author</title>
<para>
Original code and documentation by <personname><firstname>Daniel</firstname> <othername>J.</othername> <surname>Bernstein</surname></personname>.
Converted to DocBook XML by <personname><firstname>Jonathan</firstname> <surname>de Boyne Pollard</surname></personname>.
</para>
</refsection>
</refentry>
