libexplain  1.4.D001
libexplain/buffer/time_t.c
Go to the documentation of this file.
00001 /*
00002  * libexplain - Explain errno values returned by libc functions
00003  * Copyright (C) 2009, 2011, 2013 Peter Miller
00004  *
00005  * This program is free software; you can redistribute it and/or modify
00006  * it under the terms of the GNU Lesser General Public License as
00007  * published by the Free Software Foundation; either version 3 of the
00008  * License, or (at your option) any later version.
00009  *
00010  * This program is distributed in the hope that it will be useful,
00011  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00012  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00013  * Lesser General Public License for more details.
00014  *
00015  * You should have received a copy of the GNU Lesser General Public License
00016  * along with this program. If not, see <http://www.gnu.org/licenses/>.
00017  */
00018 
00019 #include <libexplain/buffer/time_t.h>
00020 #include <libexplain/buffer/pointer.h>
00021 #include <libexplain/option.h>
00022 #include <libexplain/is_efault.h>
00023 
00024 
00025 void
00026 explain_buffer_time_t(explain_string_buffer_t *sb, time_t value)
00027 {
00028     explain_string_buffer_printf(sb, "%ld", (long)value);
00029 
00030     /* Some times less if better. */
00031     if (!explain_option_dialect_specific())
00032         return;
00033 
00034     /* If it happend in the last 12 hours, use a shorter format. */
00035     {
00036         time_t now;
00037         long diff;
00038 
00039         time(&now);
00040         diff = now - value;
00041         if (diff >= 0 && diff < 60L*60L*12L)
00042         {
00043             struct tm       *tmp;
00044 
00045             tmp = localtime(&value);
00046             if (tmp)
00047             {
00048                 char            buffer[200];
00049 
00050                 strftime
00051                 (
00052                     buffer,
00053                     sizeof(buffer),
00054                     " \"%H:%M:%S\"",
00055                     tmp
00056                 );
00057                 explain_string_buffer_puts(sb, buffer);
00058                 return;
00059             }
00060         }
00061 
00062     }
00063 
00064     {
00065         struct tm       *tmp;
00066 
00067         tmp = localtime(&value);
00068         if (tmp)
00069         {
00070             char            buffer[200];
00071 
00072             strftime
00073             (
00074                 buffer,
00075                 sizeof(buffer),
00076                 " \"%a, %Y-%b-%d %H:%M:%S %z\"",
00077                 tmp
00078             );
00079             explain_string_buffer_puts(sb, buffer);
00080         }
00081     }
00082 }
00083 
00084 
00085 void
00086 explain_buffer_time_t_star(explain_string_buffer_t *sb, const time_t *data)
00087 {
00088     if (explain_is_efault_pointer(data, sizeof(*data)))
00089     {
00090         explain_buffer_pointer(sb, data);
00091         return;
00092     }
00093 
00094     explain_string_buffer_printf(sb, "{ ");
00095     explain_buffer_time_t(sb, *data);
00096     explain_string_buffer_puts(sb, " }");
00097 }
00098 
00099 
00100 /* vim: set ts=8 sw=4 et : */