summaryrefslogtreecommitdiff
path: root/src/mt/dates.cc
diff options
context:
space:
mode:
authorHenrik Rydberg <rydberg@euromail.se>2011-10-08 20:30:28 +0200
committerHenrik Rydberg <rydberg@euromail.se>2011-10-08 20:30:28 +0200
commit5df79c53745fde5d6c3340a2979b1429cd5892c1 (patch)
tree1a81af141708b826e9c61e8a04019994fcca8298 /src/mt/dates.cc
Initial import of htcd system 1.0
Signed-off-by: Henrik Rydberg <rydberg@euromail.se>
Diffstat (limited to 'src/mt/dates.cc')
-rw-r--r--src/mt/dates.cc207
1 files changed, 207 insertions, 0 deletions
diff --git a/src/mt/dates.cc b/src/mt/dates.cc
new file mode 100644
index 0000000..8c244e5
--- /dev/null
+++ b/src/mt/dates.cc
@@ -0,0 +1,207 @@
1/*************************************************************************
2 *
3 * HTCd - Copyright (C) 1998-2006 Henrik Rydberg
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18 */
19
20#include <mt/dates.h>
21#include <mt/split.h>
22#include <ctype.h>
23
24const mstring SWEEK[MAXLANG][7]={
25 "Sun","Mon","Tue","Wed","Thu","Fri","Sat",
26 "Sön","Mån","Tis","Ons","Tor","Fre","Lör"
27};
28const mstring SMONTH[MAXLANG][12]={
29 "Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec",
30 "Jan","Feb","Mar","Apr","Maj","Jun","Jul","Aug","Sep","Okt","Nov","Dec"
31};
32const mstring WEEK[MAXLANG][7]={
33 "Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday",
34 "Söndag","Måndag","Tisdag","Onsdag","Torsdag","Fredag","Lördag"
35};
36const mstring MONTH[MAXLANG][12]={
37 "January","February","March","April","May","June",
38 "July","August","September","October","November","December",
39 "Januari","Februari","Mars","April","Maj","Juni",
40 "Juli","Augusti","September","Oktober","November","December"
41};
42
43time_t Today(time_t now)
44{
45 tm s; localtime_r(&now,&s);
46 s.tm_hour=0;
47 s.tm_min=0;
48 s.tm_sec=0;
49 return mktime(&s);
50}
51
52time_t Sunday(time_t now)
53{
54 tm s; localtime_r(&now,&s);
55 s.tm_mday-=s.tm_wday;
56 return mktime(&s);
57}
58
59const mstring& Week(int lang,time_t t)
60{
61 tm s; return WEEK[lang][localtime_r(&t,&s)->tm_wday];
62}
63
64const mstring& Month(int lang,time_t t)
65{
66 tm s; return MONTH[lang][localtime_r(&t,&s)->tm_mon];
67}
68
69const mstring& SWeek(int lang,time_t t)
70{
71 tm s; return SWEEK[lang][localtime_r(&t,&s)->tm_wday];
72}
73
74const mstring& SMonth(int lang,time_t t)
75{
76 tm s; return SMONTH[lang][localtime_r(&t,&s)->tm_mon];
77}
78
79mstring httpTime(time_t t)
80{
81 tm tt; gmtime_r(&t,&tt);
82 return mstring().convert("%s, %02d %s %4d %02d:%02d:%02d GMT",
83 SWEEK[0][tt.tm_wday].c_str(),
84 tt.tm_mday,
85 SMONTH[0][tt.tm_mon].c_str(),
86 tt.tm_year+1900,
87 tt.tm_hour,
88 tt.tm_min,
89 tt.tm_sec);
90}
91
92mstring htcTime(int lang,time_t t)
93{
94 tm tt; localtime_r(&t,&tt);
95 mstring s(32); asctime_r(&tt,s.data()); s.resize(24);
96 if(lang) {
97 for(int i=0;i<7;i++) if(s(0,3)==SWEEK[0][i]) {
98 s.replace(0,3,SWEEK[lang][i]);
99 break;
100 }
101 for(int i=0;i<12;i++) if(s(4,3)==SMONTH[0][i]) {
102 s.replace(4,3,SMONTH[lang][i]);
103 break;
104 }
105 }
106 return s;
107}
108
109static int Month(int& mon,const sref& st)
110{
111 mstring s=st; s.tolower(); s[0]=toupper(s[0]);
112 for(int lang=0;lang<MAXLANG;lang++) {
113 for(int m=0;m<12;m++) if(s==SMONTH[lang][m]) { mon=m; return 1; }
114 }
115 return 0;
116}
117
118static int Week(int& week,const sref& st)
119{
120 mstring s=st; s.tolower(); s[0]=toupper(s[0]);
121 for(int lang=0;lang<MAXLANG;lang++) {
122 for(int w=0;w<7;w++) if(s==SWEEK[lang][w]) { week=w; return 1; }
123 }
124 return 0;
125}
126
127static int Time(int& th,int& tmi,int& ts,const sref& st)
128{
129 static const charset sep_tag(":");
130 sref hour,min,sec,when=st;
131 int h,m,s;
132 if(when.find(':')<0) return 0;
133 Split(hour,when,sep_tag);
134 Split(min,when,sep_tag);
135 Split(sec,when,sep_tag);
136 h=atoi(hour);
137 m=atoi(min);
138 s=atoi(sec);
139 if(h>=0&&h<=24&&m>=0&&m<60&&s>=0&&s<60) { th=h; tmi=m; ts=s; return 1; }
140 return 0;
141}
142
143static int Day(int& day,const sref& s)
144{
145 if(s.find_not_of(cset_digit)>=0) return 0;
146 int d=atoi(s);
147 if(d>0&&d<32) { day=d; return 1; }
148 return 0;
149}
150
151static int Year(int& year,const sref& s)
152{
153 if(s.find_not_of(cset_digit)>=0) return 0;
154 int y=atoi(s);
155 if(y>=32&&y<9999) { year=y; return 1; }
156 return 0;
157}
158
159time_t htcTime(int lang,const sref& date)
160{
161 sref week,month,day,when,year,rest=date;
162 Split(week,rest);
163 Split(month,rest);
164 Split(day,rest);
165 Split(when,rest);
166 Split(year,rest);
167 struct tm t;
168 Time(t.tm_hour,t.tm_min,t.tm_sec,when);
169 t.tm_mday=atoi(day);
170 Month(t.tm_mon,month);
171 t.tm_year=atoi(year)-1900;
172 Week(t.tm_wday,week);
173 t.tm_isdst=-1;
174 return mktime(&t);
175}
176
177time_t cvtTime(const sref& code)
178{
179 time_t now=time(0);
180 tm t; localtime_r(&now,&t);
181 t.tm_hour=0;t.tm_min=0;t.tm_sec=0;
182 //Start with 00:00:00 today
183 sref first,rest=code;
184 int w=0,m=0,d=0,h=0,y=0;
185 while(Split(first,rest)) {
186 if(!w) w=Week(t.tm_wday,first);
187 if(!m) m=Month(t.tm_mon,first);
188 if(!h) h=Time(t.tm_hour,t.tm_min,t.tm_sec,first);
189 if(!d) d=Day(t.tm_mday,first);
190 if(!y) if(y=Year(t.tm_year,first)) t.tm_year-=1900;
191 }
192 t.tm_isdst=-1;
193 return mktime(&t);
194}
195
196//////////////////////////////
197
198int Language(const sref& st)
199{
200 static const charset seps=cset_ws|charset(",;");
201 mstring s=st; s.tolower();
202 sref first,rest=s;
203 while(Split(first,rest,seps)) {
204 for(int i=0;i<MAXLANG;i++) if(first==LANG[i]) return i;
205 }
206 return -1;
207}