source: main/tags/2.40/gsdl/lib/gsdltimes.cpp@ 25191

Last change on this file since 25191 was 3516, checked in by jrm21, 22 years ago

Apparently darwin doesn't define BSD by default, so we now instead check
for MACH and APPLE to be defined instead.

  • Property svn:executable set to *
  • Property svn:keywords set to Author Date Id Revision
File size: 5.1 KB
Line 
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// !! Actually, we now return a string in localtime, as text2time returns
32// a time in localtime (mktime assumes local timezone). - jrm21 Sep 2002
33text_t time2text (time_t time) {
34 // tm *tm_ptr = gmtime(&time);
35 tm *tm_ptr = localtime(&time);
36
37 if (tm_ptr == NULL) return "";
38
39 // year
40 text_t timestr(tm_ptr->tm_year+1900);
41 timestr.push_back('/');
42
43 // month (note the +1)
44 if ((tm_ptr->tm_mon+1) < 10) timestr.push_back('0');
45 timestr += (tm_ptr->tm_mon+1);
46 timestr.push_back('/');
47
48 // day
49 if (tm_ptr->tm_mday < 10) timestr.push_back('0');
50 timestr += tm_ptr->tm_mday;
51 timestr.push_back(' ');
52
53 // hour
54 if (tm_ptr->tm_hour < 10) timestr.push_back('0');
55 timestr += tm_ptr->tm_hour;
56 timestr.push_back(':');
57
58 // minutes
59 if (tm_ptr->tm_min < 10) timestr.push_back('0');
60 timestr += tm_ptr->tm_min;
61 timestr.push_back(':');
62
63 // seconds
64 if (tm_ptr->tm_sec < 10) timestr.push_back('0');
65 timestr += tm_ptr->tm_sec;
66
67 return timestr;
68}
69
70// takes a string like that returned by time2text and returns corresponding time_t
71// returns -1 if an error occurs
72time_t text2time (const text_t &timestr) {
73 tm timetm;
74 text_t tempstr;
75
76 text_t::const_iterator time_here = timestr.begin();
77 text_t::const_iterator time_end = timestr.end();
78
79 // year
80 if (time_here == time_end) return (time_t) -1;
81 time_here = getdelimitstr (time_here, time_end, '/', tempstr);
82 timetm.tm_year = tempstr.getint() - 1900;
83 if (timetm.tm_year < 0) return (time_t) -1;
84
85 // month (note the -1)
86 if (time_here == time_end) return (time_t) -1;
87 time_here = getdelimitstr (time_here, time_end, '/', tempstr);
88 timetm.tm_mon = tempstr.getint() - 1;
89 if (timetm.tm_mon < 0 || timetm.tm_mon > 11)
90 return (time_t) -1;
91
92 // day
93 if (time_here == time_end) return (time_t) -1;
94 time_here = getdelimitstr (time_here, time_end, ' ', tempstr);
95 timetm.tm_mday = tempstr.getint();
96 if (timetm.tm_mday < 1 || timetm.tm_mday > 31)
97 return (time_t) -1;
98
99 // hour
100 if (time_here == time_end) return (time_t) -1;
101 time_here = getdelimitstr (time_here, time_end, ':', tempstr);
102 timetm.tm_hour = tempstr.getint();
103 if (timetm.tm_hour < 0 || timetm.tm_hour > 23)
104 return (time_t) -1;
105
106 // min
107 if (time_here == time_end) return (time_t) -1;
108 time_here = getdelimitstr (time_here, time_end, ':', tempstr);
109 timetm.tm_min = tempstr.getint();
110 if (timetm.tm_min < 0 || timetm.tm_min > 59)
111 return (time_t) -1;
112
113 // sec
114 if (time_here == time_end) return (time_t) -1;
115 time_here = getdelimitstr (time_here, time_end, ':', tempstr);
116 timetm.tm_sec = tempstr.getint();
117 if (timetm.tm_sec < 0 || timetm.tm_sec > 59)
118 return (time_t) -1;
119
120 timetm.tm_isdst=0;
121
122 /* This is for darwin - mktime seems to use this, so don't leave it
123 uninitialised!! Maybe the same on freebsd/netbsd?
124 */
125#if defined(__MACH__) && defined(__APPLE__)
126 timetm.tm_gmtoff=0;
127#endif
128
129 // mktime assumes timetm is localtime
130 return mktime (&timetm);
131}
132
133// returns current date and time formatted like "Thu Dec 07 23:43:38 GMT 2000"
134// if ltime is true return value will be expressed in local time, otherwise
135// it'll be Coordinated Universal Time (UTC) - no promises are made that the
136// resulting string will be formatted exactly as expected, especially on windows
137// returns "" if an error occurs
138text_t get_date (bool ltime) {
139
140 tm *tm_ptr = NULL;
141 time_t t = time(NULL);
142 if (ltime) tm_ptr = localtime (&t);
143 else tm_ptr = gmtime (&t);
144 if (tm_ptr == NULL) return "";
145 char *timestr = new char[128];
146 text_t ret;
147
148 // we do this in such an ugly way because windows is kind of flakey when
149 // it comes to timezones
150 if (ltime) {
151 strftime (timestr, 128, "%a %b %d %H:%M:%S %z %Y", tm_ptr);
152 ret.setcstr (timestr);
153 } else {
154 strftime (timestr, 128, "%a %b %d %H:%M:%S", tm_ptr);
155 ret.setcstr (timestr);
156 ret += " GMT ";
157 strftime (timestr, 128, "%Y", tm_ptr);
158 ret += timestr;
159 }
160
161 delete timestr;
162 return ret;
163}
Note: See TracBrowser for help on using the repository browser.