1 | /**********************************************************************
|
---|
2 | *
|
---|
3 | * gsdltimes.cpp -- functions to deal with time
|
---|
4 | * Copyright (C) 1999 DigiLib Systems Limited, New Zealand
|
---|
5 | *
|
---|
6 | * PUT COPYRIGHT NOTICE HERE
|
---|
7 | *
|
---|
8 | * $Id: gsdltimes.cpp 371 1999-07-11 10:52:56Z rjmcnab $
|
---|
9 | *
|
---|
10 | *********************************************************************/
|
---|
11 |
|
---|
12 | /*
|
---|
13 | $Log$
|
---|
14 | Revision 1.1 1999/07/11 10:52:56 rjmcnab
|
---|
15 | Initial revision.
|
---|
16 |
|
---|
17 | */
|
---|
18 |
|
---|
19 |
|
---|
20 | #include "gsdltimes.h"
|
---|
21 |
|
---|
22 |
|
---|
23 | // returns "" if an error occurs
|
---|
24 | text_t time2text (time_t time) {
|
---|
25 | tm *tm_ptr = localtime(&time);
|
---|
26 | if (tm_ptr == NULL) return "";
|
---|
27 |
|
---|
28 | // year
|
---|
29 | text_t timestr(tm_ptr->tm_year+1900);
|
---|
30 | timestr.push_back('/');
|
---|
31 |
|
---|
32 | // month (note the +1)
|
---|
33 | if ((tm_ptr->tm_mon+1) < 10) timestr.push_back('0');
|
---|
34 | timestr += (tm_ptr->tm_mon+1);
|
---|
35 | timestr.push_back('/');
|
---|
36 |
|
---|
37 | // day
|
---|
38 | if (tm_ptr->tm_mday < 10) timestr.push_back('0');
|
---|
39 | timestr += tm_ptr->tm_mday;
|
---|
40 | timestr.push_back(' ');
|
---|
41 |
|
---|
42 | // hour
|
---|
43 | if (tm_ptr->tm_hour < 10) timestr.push_back('0');
|
---|
44 | timestr += tm_ptr->tm_hour;
|
---|
45 | timestr.push_back(':');
|
---|
46 |
|
---|
47 | // minutes
|
---|
48 | if (tm_ptr->tm_min < 10) timestr.push_back('0');
|
---|
49 | timestr += tm_ptr->tm_min;
|
---|
50 | timestr.push_back(':');
|
---|
51 |
|
---|
52 | // seconds
|
---|
53 | if (tm_ptr->tm_sec < 10) timestr.push_back('0');
|
---|
54 | timestr += tm_ptr->tm_sec;
|
---|
55 |
|
---|
56 | return timestr;
|
---|
57 | }
|
---|
58 |
|
---|
59 | // returns -1 if an error occurs
|
---|
60 | time_t text2time (const text_t ×tr) {
|
---|
61 | tm timetm;
|
---|
62 | text_t tempstr;
|
---|
63 |
|
---|
64 | text_t::const_iterator time_here = timestr.begin();
|
---|
65 | text_t::const_iterator time_end = timestr.end();
|
---|
66 |
|
---|
67 | // year
|
---|
68 | if (time_here == time_end) return (time_t) -1;
|
---|
69 | time_here = getdelimitstr (time_here, time_end, '/', tempstr);
|
---|
70 | timetm.tm_year = tempstr.getint() - 1900;
|
---|
71 | if (timetm.tm_year < 0) return (time_t) -1;
|
---|
72 |
|
---|
73 | // month (note the -1)
|
---|
74 | if (time_here == time_end) return (time_t) -1;
|
---|
75 | time_here = getdelimitstr (time_here, time_end, '/', tempstr);
|
---|
76 | timetm.tm_mon = tempstr.getint() - 1;
|
---|
77 | if (timetm.tm_mon < 0 || timetm.tm_mon > 11)
|
---|
78 | return (time_t) -1;
|
---|
79 |
|
---|
80 | // day
|
---|
81 | if (time_here == time_end) return (time_t) -1;
|
---|
82 | time_here = getdelimitstr (time_here, time_end, ' ', tempstr);
|
---|
83 | timetm.tm_mday = tempstr.getint();
|
---|
84 | if (timetm.tm_mday < 1 || timetm.tm_mday > 31)
|
---|
85 | return (time_t) -1;
|
---|
86 |
|
---|
87 | // hour
|
---|
88 | if (time_here == time_end) return (time_t) -1;
|
---|
89 | time_here = getdelimitstr (time_here, time_end, ':', tempstr);
|
---|
90 | timetm.tm_hour = tempstr.getint();
|
---|
91 | if (timetm.tm_hour < 0 || timetm.tm_hour > 23)
|
---|
92 | return (time_t) -1;
|
---|
93 |
|
---|
94 | // min
|
---|
95 | if (time_here == time_end) return (time_t) -1;
|
---|
96 | time_here = getdelimitstr (time_here, time_end, ':', tempstr);
|
---|
97 | timetm.tm_min = tempstr.getint();
|
---|
98 | if (timetm.tm_min < 0 || timetm.tm_min > 59)
|
---|
99 | return (time_t) -1;
|
---|
100 |
|
---|
101 | // sec
|
---|
102 | if (time_here == time_end) return (time_t) -1;
|
---|
103 | time_here = getdelimitstr (time_here, time_end, ':', tempstr);
|
---|
104 | timetm.tm_sec = tempstr.getint();
|
---|
105 | if (timetm.tm_sec < 0 || timetm.tm_sec > 59)
|
---|
106 | return (time_t) -1;
|
---|
107 |
|
---|
108 | return mktime (&timetm);
|
---|
109 | }
|
---|