1/* PO/POT file timestamps.
2   Copyright (C) 1995-1998, 2000-2003, 2006 Free Software Foundation, Inc.
3   Written by Ulrich Drepper <drepper@gnu.ai.mit.edu>, April 1995.
4
5   This program is free software: you can redistribute it and/or modify
6   it under the terms of the GNU General Public License as published by
7   the Free Software Foundation; either version 3 of the License, or
8   (at your option) any later version.
9
10   This program is distributed in the hope that it will be useful,
11   but WITHOUT ANY WARRANTY; without even the implied warranty of
12   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13   GNU General Public License for more details.
14
15   You should have received a copy of the GNU General Public License
16   along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
17
18#ifdef HAVE_CONFIG_H
19# include <config.h>
20#endif
21
22/* Specification.  */
23#include "po-time.h"
24
25#include "xvasprintf.h"
26
27
28#define TM_YEAR_ORIGIN 1900
29
30/* Yield A - B, measured in seconds.  */
31static long
32difftm (const struct tm *a, const struct tm *b)
33{
34  int ay = a->tm_year + TM_YEAR_ORIGIN - 1;
35  int by = b->tm_year + TM_YEAR_ORIGIN - 1;
36  /* Some compilers cannot handle this as a single return statement.  */
37  long days = (
38	       /* difference in day of year  */
39	       a->tm_yday - b->tm_yday
40	       /* + intervening leap days  */
41	       + ((ay >> 2) - (by >> 2))
42	       - (ay / 100 - by / 100)
43	       + ((ay / 100 >> 2) - (by / 100 >> 2))
44	       /* + difference in years * 365  */
45	       + (long) (ay - by) * 365l);
46
47  return 60l * (60l * (24l * days + (a->tm_hour - b->tm_hour))
48	        + (a->tm_min - b->tm_min))
49	 + (a->tm_sec - b->tm_sec);
50}
51
52
53char *
54po_strftime (const time_t *tp)
55{
56  struct tm local_time;
57  char tz_sign;
58  long tz_min;
59
60  local_time = *localtime (tp);
61  tz_sign = '+';
62  tz_min = difftm (&local_time, gmtime (tp)) / 60;
63  if (tz_min < 0)
64    {
65      tz_min = -tz_min;
66      tz_sign = '-';
67    }
68  return xasprintf ("%d-%02d-%02d %02d:%02d%c%02ld%02ld",
69		    local_time.tm_year + TM_YEAR_ORIGIN,
70		    local_time.tm_mon + 1,
71		    local_time.tm_mday,
72		    local_time.tm_hour,
73		    local_time.tm_min,
74		    tz_sign, tz_min / 60, tz_min % 60);
75}
76