fix getsup (HH)
[luatex.git] / source / libs / poppler / poppler-src / poppler / DateInfo.cc
blobfef3f00a658f2914bed19b5ea81b13f8d3f44b1b
1 //========================================================================
2 //
3 // DateInfo.cc
4 //
5 // Copyright (C) 2008 Albert Astals Cid <aacid@kde.org>
6 // Copyright (C) 2009 Carlos Garcia Campos <carlosgc@gnome.org>
7 // Copyright (C) 2015 André Guerreiro <aguerreiro1985@gmail.com>
8 // Copyright (C) 2015 André Esser <bepandre@hotmail.com>
9 // Copyright (C) 2016 Adrian Johnson <ajohnson@redneon.com>
11 // To see a description of the changes please see the Changelog file that
12 // came with your tarball or type make ChangeLog if you are building from git
14 //========================================================================
16 //========================================================================
18 // Based on code from pdfinfo.cc
20 // Copyright 1998-2003 Glyph & Cog, LLC
22 //========================================================================
24 #include <config.h>
26 #include "glibc.h"
27 #include "DateInfo.h"
29 #include <stdio.h>
30 #include <string.h>
32 /* See PDF Reference 1.3, Section 3.8.2 for PDF Date representation */
33 GBool parseDateString(const char *dateString, int *year, int *month, int *day, int *hour, int *minute, int *second, char *tz, int *tzHour, int *tzMinute)
35 if ( dateString == NULL ) return gFalse;
36 if ( strlen(dateString) < 2 ) return gFalse;
38 if ( dateString[0] == 'D' && dateString[1] == ':' )
39 dateString += 2;
41 *month = 1;
42 *day = 1;
43 *hour = 0;
44 *minute = 0;
45 *second = 0;
46 *tz = 0x00;
47 *tzHour = 0;
48 *tzMinute = 0;
50 if ( sscanf( dateString,
51 "%4d%2d%2d%2d%2d%2d%c%2d%*c%2d",
52 year, month, day, hour, minute, second,
53 tz, tzHour, tzMinute ) > 0 ) {
54 /* Workaround for y2k bug in Distiller 3 stolen from gpdf, hoping that it won't
55 * be used after y2.2k */
56 if ( *year < 1930 && strlen (dateString) > 14)
58 int century, years_since_1900;
59 if ( sscanf( dateString,
60 "%2d%3d%2d%2d%2d%2d%2d",
61 &century, &years_since_1900, month, day, hour, minute, second) == 7 )
63 *year = century * 100 + years_since_1900;
65 else
67 return gFalse;
71 if (*year <= 0) return gFalse;
73 return gTrue;
76 return gFalse;
79 // Convert time to PDF date string
80 GooString *timeToDateString(time_t *timet) {
81 GooString *dateString;
82 char s[5];
83 struct tm *gt;
84 size_t len;
85 time_t timep = timet ? *timet : time(NULL);
86 struct tm t;
88 gt = gmtime_r (&timep, &t);
90 dateString = new GooString ("D:");
92 /* Year YYYY */
93 len = strftime (s, sizeof(s), "%Y", gt);
94 dateString->append (s, len);
96 /* Month MM */
97 len = strftime (s, sizeof(s), "%m", gt);
98 dateString->append (s, len);
100 /* Day DD */
101 len = strftime (s, sizeof(s), "%d", gt);
102 dateString->append (s, len);
104 /* Hour HH */
105 len = strftime (s, sizeof(s), "%H", gt);
106 dateString->append (s, len);
108 /* Minute mm */
109 len = strftime (s, sizeof(s), "%M", gt);
110 dateString->append (s, len);
112 /* Second SS */
113 len = strftime (s, sizeof(s), "%S", gt);
114 dateString->append (s, len);
116 return dateString;
119 // Convert PDF date string to time. Returns -1 if conversion fails.
120 time_t dateStringToTime(GooString *dateString) {
121 int year, mon, day, hour, min, sec, tz_hour, tz_minute;
122 char tz;
123 struct tm tm;
124 time_t time;
126 if (!parseDateString (dateString->getCString(), &year, &mon, &day, &hour, &min, &sec, &tz, &tz_hour, &tz_minute))
127 return -1;
129 tm.tm_year = year - 1900;
130 tm.tm_mon = mon - 1;
131 tm.tm_mday = day;
132 tm.tm_hour = hour;
133 tm.tm_min = min;
134 tm.tm_sec = sec;
135 tm.tm_wday = -1;
136 tm.tm_yday = -1;
137 tm.tm_isdst = -1; /* 0 = DST off, 1 = DST on, -1 = don't know */
139 /* compute tm_wday and tm_yday and check date */
140 time = timegm (&tm);
141 if (time == (time_t)-1)
142 return time;
144 time_t offset = (tz_hour*60 + tz_minute)*60;
145 if (tz == '-')
146 offset *= -1;
147 time -= offset;
149 return time;