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