source: main/tags/2.13/gsdl/lib/gsdltimes.cpp@ 24552

Last change on this file since 24552 was 534, checked in by sjboddie, 25 years ago

added gpl notice

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