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 | *********************************************************************/
|
---|
25 |
|
---|
26 | #include "gsdltimes.h"
|
---|
27 |
|
---|
28 | // returns a string of the form "YYYY/MM/DD hh:mm:ss" (expressed in
|
---|
29 | // Coordinated Universal Time (UTC))
|
---|
30 | // returns "" if an error occurs
|
---|
31 | text_t time2text (time_t time) {
|
---|
32 | tm *tm_ptr = gmtime(&time);
|
---|
33 | if (tm_ptr == NULL) return "";
|
---|
34 |
|
---|
35 | // year
|
---|
36 | text_t timestr(tm_ptr->tm_year+1900);
|
---|
37 | timestr.push_back('/');
|
---|
38 |
|
---|
39 | // month (note the +1)
|
---|
40 | if ((tm_ptr->tm_mon+1) < 10) timestr.push_back('0');
|
---|
41 | timestr += (tm_ptr->tm_mon+1);
|
---|
42 | timestr.push_back('/');
|
---|
43 |
|
---|
44 | // day
|
---|
45 | if (tm_ptr->tm_mday < 10) timestr.push_back('0');
|
---|
46 | timestr += tm_ptr->tm_mday;
|
---|
47 | timestr.push_back(' ');
|
---|
48 |
|
---|
49 | // hour
|
---|
50 | if (tm_ptr->tm_hour < 10) timestr.push_back('0');
|
---|
51 | timestr += tm_ptr->tm_hour;
|
---|
52 | timestr.push_back(':');
|
---|
53 |
|
---|
54 | // minutes
|
---|
55 | if (tm_ptr->tm_min < 10) timestr.push_back('0');
|
---|
56 | timestr += tm_ptr->tm_min;
|
---|
57 | timestr.push_back(':');
|
---|
58 |
|
---|
59 | // seconds
|
---|
60 | if (tm_ptr->tm_sec < 10) timestr.push_back('0');
|
---|
61 | timestr += tm_ptr->tm_sec;
|
---|
62 |
|
---|
63 | return timestr;
|
---|
64 | }
|
---|
65 |
|
---|
66 | // takes a string like that returned by time2text and returns corresponding time_t
|
---|
67 | // returns -1 if an error occurs
|
---|
68 | time_t text2time (const text_t ×tr) {
|
---|
69 | tm timetm;
|
---|
70 | text_t tempstr;
|
---|
71 |
|
---|
72 | text_t::const_iterator time_here = timestr.begin();
|
---|
73 | text_t::const_iterator time_end = timestr.end();
|
---|
74 |
|
---|
75 | // year
|
---|
76 | if (time_here == time_end) return (time_t) -1;
|
---|
77 | time_here = getdelimitstr (time_here, time_end, '/', tempstr);
|
---|
78 | timetm.tm_year = tempstr.getint() - 1900;
|
---|
79 | if (timetm.tm_year < 0) return (time_t) -1;
|
---|
80 |
|
---|
81 | // month (note the -1)
|
---|
82 | if (time_here == time_end) return (time_t) -1;
|
---|
83 | time_here = getdelimitstr (time_here, time_end, '/', tempstr);
|
---|
84 | timetm.tm_mon = tempstr.getint() - 1;
|
---|
85 | if (timetm.tm_mon < 0 || timetm.tm_mon > 11)
|
---|
86 | return (time_t) -1;
|
---|
87 |
|
---|
88 | // day
|
---|
89 | if (time_here == time_end) return (time_t) -1;
|
---|
90 | time_here = getdelimitstr (time_here, time_end, ' ', tempstr);
|
---|
91 | timetm.tm_mday = tempstr.getint();
|
---|
92 | if (timetm.tm_mday < 1 || timetm.tm_mday > 31)
|
---|
93 | return (time_t) -1;
|
---|
94 |
|
---|
95 | // hour
|
---|
96 | if (time_here == time_end) return (time_t) -1;
|
---|
97 | time_here = getdelimitstr (time_here, time_end, ':', tempstr);
|
---|
98 | timetm.tm_hour = tempstr.getint();
|
---|
99 | if (timetm.tm_hour < 0 || timetm.tm_hour > 23)
|
---|
100 | return (time_t) -1;
|
---|
101 |
|
---|
102 | // min
|
---|
103 | if (time_here == time_end) return (time_t) -1;
|
---|
104 | time_here = getdelimitstr (time_here, time_end, ':', tempstr);
|
---|
105 | timetm.tm_min = tempstr.getint();
|
---|
106 | if (timetm.tm_min < 0 || timetm.tm_min > 59)
|
---|
107 | return (time_t) -1;
|
---|
108 |
|
---|
109 | // sec
|
---|
110 | if (time_here == time_end) return (time_t) -1;
|
---|
111 | time_here = getdelimitstr (time_here, time_end, ':', tempstr);
|
---|
112 | timetm.tm_sec = tempstr.getint();
|
---|
113 | if (timetm.tm_sec < 0 || timetm.tm_sec > 59)
|
---|
114 | return (time_t) -1;
|
---|
115 |
|
---|
116 | return mktime (&timetm);
|
---|
117 | }
|
---|
118 |
|
---|
119 | // returns current date and time formatted like "Thu Dec 07 23:43:38 GMT 2000"
|
---|
120 | // if ltime is true return value will be expressed in local time, otherwise
|
---|
121 | // it'll be Coordinated Universal Time (UTC) - no promises are made that the
|
---|
122 | // resulting string will be formatted exactly as expected, especially on windows
|
---|
123 | // returns "" if an error occurs
|
---|
124 | text_t get_date (bool ltime) {
|
---|
125 |
|
---|
126 | tm *tm_ptr = NULL;
|
---|
127 | time_t t = time(NULL);
|
---|
128 | if (ltime) tm_ptr = localtime (&t);
|
---|
129 | else tm_ptr = gmtime (&t);
|
---|
130 | if (tm_ptr == NULL) return "";
|
---|
131 | char *timestr = new char[128];
|
---|
132 | text_t ret;
|
---|
133 |
|
---|
134 | // we do this in such an ugly way because windows is kind of flakey when
|
---|
135 | // it comes to timezones
|
---|
136 | if (ltime) {
|
---|
137 | strftime (timestr, 128, "%a %b %d %H:%M:%S %z %Y", tm_ptr);
|
---|
138 | ret.setcstr (timestr);
|
---|
139 | } else {
|
---|
140 | strftime (timestr, 128, "%a %b %d %H:%M:%S", tm_ptr);
|
---|
141 | ret.setcstr (timestr);
|
---|
142 | ret += " GMT ";
|
---|
143 | strftime (timestr, 128, "%Y", tm_ptr);
|
---|
144 | ret += timestr;
|
---|
145 | }
|
---|
146 |
|
---|
147 | delete timestr;
|
---|
148 | return ret;
|
---|
149 | }
|
---|