1 | /**********************************************************************
|
---|
2 | *
|
---|
3 | * gsdltimes.cpp -- functions to deal with time
|
---|
4 | * Copyright (C) 1999 DigiLib Systems Limited, New Zealand
|
---|
5 | *
|
---|
6 | * A component of the Greenstone digital library software
|
---|
7 | * from the New Zealand Digital Library Project at the
|
---|
8 | * University of Waikato, New Zealand.
|
---|
9 | *
|
---|
10 | * This program is free software; you can redistribute it and/or modify
|
---|
11 | * it under the terms of the GNU General Public License as published by
|
---|
12 | * the Free Software Foundation; either version 2 of the License, or
|
---|
13 | * (at your option) any later version.
|
---|
14 | *
|
---|
15 | * This program is distributed in the hope that it will be useful,
|
---|
16 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
17 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
18 | * GNU General Public License for more details.
|
---|
19 | *
|
---|
20 | * You should have received a copy of the GNU General Public License
|
---|
21 | * along with this program; if not, write to the Free Software
|
---|
22 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
|
---|
23 | *
|
---|
24 | * $Id: gsdltimes.cpp 1076 2000-04-06 19:58:04Z cs025 $
|
---|
25 | *
|
---|
26 | *********************************************************************/
|
---|
27 |
|
---|
28 | /*
|
---|
29 | $Log$
|
---|
30 | Revision 1.4 2000/04/06 19:58:00 cs025
|
---|
31 | Correcting a correction - reinstated all lib files due to silly
|
---|
32 | CVS confusion.
|
---|
33 |
|
---|
34 | Revision 1.2 1999/09/07 04:57:42 sjboddie
|
---|
35 | added gpl notice
|
---|
36 |
|
---|
37 | Revision 1.1 1999/07/11 10:52:56 rjmcnab
|
---|
38 | Initial revision.
|
---|
39 |
|
---|
40 | */
|
---|
41 |
|
---|
42 |
|
---|
43 | #include "gsdltimes.h"
|
---|
44 |
|
---|
45 |
|
---|
46 | // returns "" if an error occurs
|
---|
47 | text_t time2text (time_t time) {
|
---|
48 | tm *tm_ptr = gmtime(&time);
|
---|
49 | if (tm_ptr == NULL) return "";
|
---|
50 |
|
---|
51 | // year
|
---|
52 | text_t timestr(tm_ptr->tm_year+1900);
|
---|
53 | timestr.push_back('/');
|
---|
54 |
|
---|
55 | // month (note the +1)
|
---|
56 | if ((tm_ptr->tm_mon+1) < 10) timestr.push_back('0');
|
---|
57 | timestr += (tm_ptr->tm_mon+1);
|
---|
58 | timestr.push_back('/');
|
---|
59 |
|
---|
60 | // day
|
---|
61 | if (tm_ptr->tm_mday < 10) timestr.push_back('0');
|
---|
62 | timestr += tm_ptr->tm_mday;
|
---|
63 | timestr.push_back(' ');
|
---|
64 |
|
---|
65 | // hour
|
---|
66 | if (tm_ptr->tm_hour < 10) timestr.push_back('0');
|
---|
67 | timestr += tm_ptr->tm_hour;
|
---|
68 | timestr.push_back(':');
|
---|
69 |
|
---|
70 | // minutes
|
---|
71 | if (tm_ptr->tm_min < 10) timestr.push_back('0');
|
---|
72 | timestr += tm_ptr->tm_min;
|
---|
73 | timestr.push_back(':');
|
---|
74 |
|
---|
75 | // seconds
|
---|
76 | if (tm_ptr->tm_sec < 10) timestr.push_back('0');
|
---|
77 | timestr += tm_ptr->tm_sec;
|
---|
78 |
|
---|
79 | return timestr;
|
---|
80 | }
|
---|
81 |
|
---|
82 | // returns -1 if an error occurs
|
---|
83 | time_t text2time (const text_t ×tr) {
|
---|
84 | tm timetm;
|
---|
85 | text_t tempstr;
|
---|
86 |
|
---|
87 | text_t::const_iterator time_here = timestr.begin();
|
---|
88 | text_t::const_iterator time_end = timestr.end();
|
---|
89 |
|
---|
90 | // year
|
---|
91 | if (time_here == time_end) return (time_t) -1;
|
---|
92 | time_here = getdelimitstr (time_here, time_end, '/', tempstr);
|
---|
93 | timetm.tm_year = tempstr.getint() - 1900;
|
---|
94 | if (timetm.tm_year < 0) return (time_t) -1;
|
---|
95 |
|
---|
96 | // month (note the -1)
|
---|
97 | if (time_here == time_end) return (time_t) -1;
|
---|
98 | time_here = getdelimitstr (time_here, time_end, '/', tempstr);
|
---|
99 | timetm.tm_mon = tempstr.getint() - 1;
|
---|
100 | if (timetm.tm_mon < 0 || timetm.tm_mon > 11)
|
---|
101 | return (time_t) -1;
|
---|
102 |
|
---|
103 | // day
|
---|
104 | if (time_here == time_end) return (time_t) -1;
|
---|
105 | time_here = getdelimitstr (time_here, time_end, ' ', tempstr);
|
---|
106 | timetm.tm_mday = tempstr.getint();
|
---|
107 | if (timetm.tm_mday < 1 || timetm.tm_mday > 31)
|
---|
108 | return (time_t) -1;
|
---|
109 |
|
---|
110 | // hour
|
---|
111 | if (time_here == time_end) return (time_t) -1;
|
---|
112 | time_here = getdelimitstr (time_here, time_end, ':', tempstr);
|
---|
113 | timetm.tm_hour = tempstr.getint();
|
---|
114 | if (timetm.tm_hour < 0 || timetm.tm_hour > 23)
|
---|
115 | return (time_t) -1;
|
---|
116 |
|
---|
117 | // min
|
---|
118 | if (time_here == time_end) return (time_t) -1;
|
---|
119 | time_here = getdelimitstr (time_here, time_end, ':', tempstr);
|
---|
120 | timetm.tm_min = tempstr.getint();
|
---|
121 | if (timetm.tm_min < 0 || timetm.tm_min > 59)
|
---|
122 | return (time_t) -1;
|
---|
123 |
|
---|
124 | // sec
|
---|
125 | if (time_here == time_end) return (time_t) -1;
|
---|
126 | time_here = getdelimitstr (time_here, time_end, ':', tempstr);
|
---|
127 | timetm.tm_sec = tempstr.getint();
|
---|
128 | if (timetm.tm_sec < 0 || timetm.tm_sec > 59)
|
---|
129 | return (time_t) -1;
|
---|
130 |
|
---|
131 | return mktime (&timetm);
|
---|
132 | }
|
---|