1170754Sdelphij/* PO/POT file timestamps.
2170754Sdelphij   Copyright (C) 1995-1998, 2000-2003 Free Software Foundation, Inc.
3170754Sdelphij   Written by Ulrich Drepper <drepper@gnu.ai.mit.edu>, April 1995.
4170754Sdelphij
5170754Sdelphij   This program is free software; you can redistribute it and/or modify
6170754Sdelphij   it under the terms of the GNU General Public License as published by
7170754Sdelphij   the Free Software Foundation; either version 2, or (at your option)
8170754Sdelphij   any later version.
9170754Sdelphij
10170754Sdelphij   This program is distributed in the hope that it will be useful,
11170754Sdelphij   but WITHOUT ANY WARRANTY; without even the implied warranty of
12170754Sdelphij   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13170754Sdelphij   GNU General Public License for more details.
14170754Sdelphij
15170754Sdelphij   You should have received a copy of the GNU General Public License
16170754Sdelphij   along with this program; if not, write to the Free Software Foundation,
17170754Sdelphij   Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  */
18170754Sdelphij
19170754Sdelphij#ifdef HAVE_CONFIG_H
20170754Sdelphij# include <config.h>
21170754Sdelphij#endif
22170754Sdelphij
23170754Sdelphij/* Specification.  */
24170754Sdelphij#include "po-time.h"
25170754Sdelphij
26170754Sdelphij#include "xerror.h"
27170754Sdelphij
28170754Sdelphij
29170754Sdelphij#define TM_YEAR_ORIGIN 1900
30170754Sdelphij
31170754Sdelphij/* Yield A - B, measured in seconds.  */
32170754Sdelphijstatic long
33170754Sdelphijdifftm (const struct tm *a, const struct tm *b)
34170754Sdelphij{
35170754Sdelphij  int ay = a->tm_year + TM_YEAR_ORIGIN - 1;
36170754Sdelphij  int by = b->tm_year + TM_YEAR_ORIGIN - 1;
37170754Sdelphij  /* Some compilers cannot handle this as a single return statement.  */
38170754Sdelphij  long days = (
39170754Sdelphij	       /* difference in day of year  */
40170754Sdelphij	       a->tm_yday - b->tm_yday
41170754Sdelphij	       /* + intervening leap days  */
42170754Sdelphij	       + ((ay >> 2) - (by >> 2))
43170754Sdelphij	       - (ay / 100 - by / 100)
44170754Sdelphij	       + ((ay / 100 >> 2) - (by / 100 >> 2))
45170754Sdelphij	       /* + difference in years * 365  */
46170754Sdelphij	       + (long) (ay - by) * 365l);
47170754Sdelphij
48170754Sdelphij  return 60l * (60l * (24l * days + (a->tm_hour - b->tm_hour))
49170754Sdelphij	        + (a->tm_min - b->tm_min))
50170754Sdelphij	 + (a->tm_sec - b->tm_sec);
51170754Sdelphij}
52170754Sdelphij
53170754Sdelphij
54170754Sdelphijchar *
55170754Sdelphijpo_strftime (const time_t *tp)
56170754Sdelphij{
57170754Sdelphij  struct tm local_time;
58170754Sdelphij  char tz_sign;
59170754Sdelphij  long tz_min;
60170754Sdelphij
61170754Sdelphij  local_time = *localtime (tp);
62170754Sdelphij  tz_sign = '+';
63170754Sdelphij  tz_min = difftm (&local_time, gmtime (tp)) / 60;
64170754Sdelphij  if (tz_min < 0)
65170754Sdelphij    {
66170754Sdelphij      tz_min = -tz_min;
67170754Sdelphij      tz_sign = '-';
68170754Sdelphij    }
69170754Sdelphij  return xasprintf ("%d-%02d-%02d %02d:%02d%c%02ld%02ld",
70170754Sdelphij		    local_time.tm_year + TM_YEAR_ORIGIN,
71170754Sdelphij		    local_time.tm_mon + 1,
72170754Sdelphij		    local_time.tm_mday,
73170754Sdelphij		    local_time.tm_hour,
74170754Sdelphij		    local_time.tm_min,
75170754Sdelphij		    tz_sign, tz_min / 60, tz_min % 60);
76170754Sdelphij}
77170754Sdelphij