source: trunk/gsdl/lib/gsdltimes.cpp@ 1310

Last change on this file since 1310 was 1310, checked in by sjboddie, 24 years ago

Removed CVS logging information from source files

  • Property svn:executable set to *
  • Property svn:keywords set to Author Date Id Revision
File size: 3.5 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
29// returns "" if an error occurs
30text_t time2text (time_t time) {
31 tm *tm_ptr = gmtime(&time);
32 if (tm_ptr == NULL) return "";
33
34 // year
35 text_t timestr(tm_ptr->tm_year+1900);
36 timestr.push_back('/');
37
38 // month (note the +1)
39 if ((tm_ptr->tm_mon+1) < 10) timestr.push_back('0');
40 timestr += (tm_ptr->tm_mon+1);
41 timestr.push_back('/');
42
43 // day
44 if (tm_ptr->tm_mday < 10) timestr.push_back('0');
45 timestr += tm_ptr->tm_mday;
46 timestr.push_back(' ');
47
48 // hour
49 if (tm_ptr->tm_hour < 10) timestr.push_back('0');
50 timestr += tm_ptr->tm_hour;
51 timestr.push_back(':');
52
53 // minutes
54 if (tm_ptr->tm_min < 10) timestr.push_back('0');
55 timestr += tm_ptr->tm_min;
56 timestr.push_back(':');
57
58 // seconds
59 if (tm_ptr->tm_sec < 10) timestr.push_back('0');
60 timestr += tm_ptr->tm_sec;
61
62 return timestr;
63}
64
65// returns -1 if an error occurs
66time_t text2time (const text_t &timestr) {
67 tm timetm;
68 text_t tempstr;
69
70 text_t::const_iterator time_here = timestr.begin();
71 text_t::const_iterator time_end = timestr.end();
72
73 // year
74 if (time_here == time_end) return (time_t) -1;
75 time_here = getdelimitstr (time_here, time_end, '/', tempstr);
76 timetm.tm_year = tempstr.getint() - 1900;
77 if (timetm.tm_year < 0) return (time_t) -1;
78
79 // month (note the -1)
80 if (time_here == time_end) return (time_t) -1;
81 time_here = getdelimitstr (time_here, time_end, '/', tempstr);
82 timetm.tm_mon = tempstr.getint() - 1;
83 if (timetm.tm_mon < 0 || timetm.tm_mon > 11)
84 return (time_t) -1;
85
86 // day
87 if (time_here == time_end) return (time_t) -1;
88 time_here = getdelimitstr (time_here, time_end, ' ', tempstr);
89 timetm.tm_mday = tempstr.getint();
90 if (timetm.tm_mday < 1 || timetm.tm_mday > 31)
91 return (time_t) -1;
92
93 // hour
94 if (time_here == time_end) return (time_t) -1;
95 time_here = getdelimitstr (time_here, time_end, ':', tempstr);
96 timetm.tm_hour = tempstr.getint();
97 if (timetm.tm_hour < 0 || timetm.tm_hour > 23)
98 return (time_t) -1;
99
100 // min
101 if (time_here == time_end) return (time_t) -1;
102 time_here = getdelimitstr (time_here, time_end, ':', tempstr);
103 timetm.tm_min = tempstr.getint();
104 if (timetm.tm_min < 0 || timetm.tm_min > 59)
105 return (time_t) -1;
106
107 // sec
108 if (time_here == time_end) return (time_t) -1;
109 time_here = getdelimitstr (time_here, time_end, ':', tempstr);
110 timetm.tm_sec = tempstr.getint();
111 if (timetm.tm_sec < 0 || timetm.tm_sec > 59)
112 return (time_t) -1;
113
114 return mktime (&timetm);
115}
Note: See TracBrowser for help on using the repository browser.