beta-0.89.2
[luatex.git] / source / libs / poppler / poppler-src / poppler / DateInfo.cc
blob563204a1743750a5a4fd960249e9584c1300f16f
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 //
8 // To see a description of the changes please see the Changelog file that
9 // came with your tarball or type make ChangeLog if you are building from git
11 //========================================================================
13 //========================================================================
15 // Based on code from pdfinfo.cc
17 // Copyright 1998-2003 Glyph & Cog, LLC
19 //========================================================================
21 #include <config.h>
23 #include "DateInfo.h"
25 #include <stdio.h>
26 #include <string.h>
28 /* See PDF Reference 1.3, Section 3.8.2 for PDF Date representation */
29 GBool parseDateString(const char *dateString, int *year, int *month, int *day, int *hour, int *minute, int *second, char *tz, int *tzHour, int *tzMinute)
31 if ( dateString == NULL ) return gFalse;
32 if ( strlen(dateString) < 2 ) return gFalse;
34 if ( dateString[0] == 'D' && dateString[1] == ':' )
35 dateString += 2;
37 *month = 1;
38 *day = 1;
39 *hour = 0;
40 *minute = 0;
41 *second = 0;
42 *tz = 0x00;
43 *tzHour = 0;
44 *tzMinute = 0;
46 if ( sscanf( dateString,
47 "%4d%2d%2d%2d%2d%2d%c%2d%*c%2d",
48 year, month, day, hour, minute, second,
49 tz, tzHour, tzMinute ) > 0 ) {
50 /* Workaround for y2k bug in Distiller 3 stolen from gpdf, hoping that it won't
51 * be used after y2.2k */
52 if ( *year < 1930 && strlen (dateString) > 14)
54 int century, years_since_1900;
55 if ( sscanf( dateString,
56 "%2d%3d%2d%2d%2d%2d%2d",
57 &century, &years_since_1900, month, day, hour, minute, second) == 7 )
59 *year = century * 100 + years_since_1900;
61 else
63 return gFalse;
67 if (*year <= 0) return gFalse;
69 return gTrue;
72 return gFalse;
76 GooString *timeToDateString(time_t *timet) {
77 GooString *dateString;
78 char s[5];
79 struct tm *gt;
80 size_t len;
81 time_t timep = timet ? *timet : time(NULL);
83 #ifdef HAVE_GMTIME_R
84 struct tm t;
85 gt = gmtime_r (&timep, &t);
86 #else
87 gt = gmtime (&timep);
88 #endif
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;