summaryrefslogtreecommitdiff
path: root/src/mt/dates.cc
blob: 8c244e512c29ad3358ebbc1f69ff36b312ce127b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
/*************************************************************************
 *
 * HTCd - Copyright (C) 1998-2006 Henrik Rydberg
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 */

#include <mt/dates.h>
#include <mt/split.h>
#include <ctype.h>

const mstring SWEEK[MAXLANG][7]={
  "Sun","Mon","Tue","Wed","Thu","Fri","Sat",
  "Sön","Mån","Tis","Ons","Tor","Fre","Lör"
};
const mstring SMONTH[MAXLANG][12]={
  "Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec",
  "Jan","Feb","Mar","Apr","Maj","Jun","Jul","Aug","Sep","Okt","Nov","Dec"
};
const mstring WEEK[MAXLANG][7]={
  "Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday",
  "Söndag","Måndag","Tisdag","Onsdag","Torsdag","Fredag","Lördag"
};
const mstring MONTH[MAXLANG][12]={
  "January","February","March","April","May","June",
  "July","August","September","October","November","December",
  "Januari","Februari","Mars","April","Maj","Juni",
  "Juli","Augusti","September","Oktober","November","December"
};

time_t Today(time_t now)
{
  tm s; localtime_r(&now,&s);
  s.tm_hour=0;
  s.tm_min=0;
  s.tm_sec=0;
  return mktime(&s);
}

time_t Sunday(time_t now)
{
  tm s; localtime_r(&now,&s);
  s.tm_mday-=s.tm_wday;
  return mktime(&s);
}

const mstring& Week(int lang,time_t t)
{
  tm s; return WEEK[lang][localtime_r(&t,&s)->tm_wday];
}

const mstring& Month(int lang,time_t t)
{
  tm s; return MONTH[lang][localtime_r(&t,&s)->tm_mon];
}

const mstring& SWeek(int lang,time_t t)
{
  tm s; return SWEEK[lang][localtime_r(&t,&s)->tm_wday];
}

const mstring& SMonth(int lang,time_t t)
{
  tm s; return SMONTH[lang][localtime_r(&t,&s)->tm_mon];
}

mstring httpTime(time_t t)
{
  tm tt; gmtime_r(&t,&tt);
  return mstring().convert("%s, %02d %s %4d %02d:%02d:%02d GMT",
                           SWEEK[0][tt.tm_wday].c_str(),
                           tt.tm_mday,
                           SMONTH[0][tt.tm_mon].c_str(),
                           tt.tm_year+1900,
                           tt.tm_hour,
                           tt.tm_min,
                           tt.tm_sec);
}

mstring htcTime(int lang,time_t t)
{
  tm tt; localtime_r(&t,&tt);
  mstring s(32); asctime_r(&tt,s.data()); s.resize(24);
  if(lang) {
    for(int i=0;i<7;i++) if(s(0,3)==SWEEK[0][i]) {
      s.replace(0,3,SWEEK[lang][i]);
      break;
    }
    for(int i=0;i<12;i++) if(s(4,3)==SMONTH[0][i]) {
      s.replace(4,3,SMONTH[lang][i]);
      break;
    }
  }
  return s;
}

static int Month(int& mon,const sref& st)
{
  mstring s=st; s.tolower(); s[0]=toupper(s[0]);
  for(int lang=0;lang<MAXLANG;lang++) {
    for(int m=0;m<12;m++) if(s==SMONTH[lang][m]) { mon=m; return 1; }
  }
  return 0;
}

static int Week(int& week,const sref& st)
{
  mstring s=st; s.tolower(); s[0]=toupper(s[0]);
  for(int lang=0;lang<MAXLANG;lang++) {
    for(int w=0;w<7;w++) if(s==SWEEK[lang][w]) { week=w; return 1; }
  }
  return 0;
}

static int Time(int& th,int& tmi,int& ts,const sref& st)
{
  static const charset sep_tag(":");
  sref hour,min,sec,when=st;
  int h,m,s;
  if(when.find(':')<0) return 0;
  Split(hour,when,sep_tag);
  Split(min,when,sep_tag);
  Split(sec,when,sep_tag);
  h=atoi(hour);
  m=atoi(min);
  s=atoi(sec);
  if(h>=0&&h<=24&&m>=0&&m<60&&s>=0&&s<60) { th=h; tmi=m; ts=s; return 1; }
  return 0;
}

static int Day(int& day,const sref& s)
{
  if(s.find_not_of(cset_digit)>=0) return 0;
  int d=atoi(s);
  if(d>0&&d<32) { day=d; return 1; }
  return 0;
}

static int Year(int& year,const sref& s)
{
  if(s.find_not_of(cset_digit)>=0) return 0;
  int y=atoi(s);
  if(y>=32&&y<9999) {  year=y; return 1; }
  return 0;
}

time_t htcTime(int lang,const sref& date)
{
  sref week,month,day,when,year,rest=date;
  Split(week,rest);
  Split(month,rest);
  Split(day,rest);
  Split(when,rest);
  Split(year,rest);
  struct tm t;
  Time(t.tm_hour,t.tm_min,t.tm_sec,when);
  t.tm_mday=atoi(day);
  Month(t.tm_mon,month);
  t.tm_year=atoi(year)-1900;
  Week(t.tm_wday,week);
  t.tm_isdst=-1;
  return mktime(&t);
}

time_t cvtTime(const sref& code)
{
  time_t now=time(0);
  tm t; localtime_r(&now,&t);
  t.tm_hour=0;t.tm_min=0;t.tm_sec=0;
  //Start with 00:00:00 today
  sref first,rest=code;
  int w=0,m=0,d=0,h=0,y=0;
  while(Split(first,rest)) {
    if(!w) w=Week(t.tm_wday,first);
    if(!m) m=Month(t.tm_mon,first);
    if(!h) h=Time(t.tm_hour,t.tm_min,t.tm_sec,first);
    if(!d) d=Day(t.tm_mday,first);
    if(!y) if(y=Year(t.tm_year,first)) t.tm_year-=1900;
  }
  t.tm_isdst=-1;
  return mktime(&t);
}  

//////////////////////////////

int Language(const sref& st)
{
  static const charset seps=cset_ws|charset(",;");
  mstring s=st; s.tolower();
  sref first,rest=s;
  while(Split(first,rest,seps)) {
    for(int i=0;i<MAXLANG;i++) if(first==LANG[i]) return i;
  }
  return -1;
}