Home | History | Annotate | Line # | Download | only in src
      1 /* PO/POT file timestamps.
      2    Copyright (C) 1995-1998, 2000-2003, 2006 Free Software Foundation, Inc.
      3    Written by Ulrich Drepper <drepper (at) 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 2, or (at your option)
      8    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, write to the Free Software Foundation,
     17    Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.  */
     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 }
     77