From 5df79c53745fde5d6c3340a2979b1429cd5892c1 Mon Sep 17 00:00:00 2001 From: Henrik Rydberg Date: Sat, 8 Oct 2011 20:30:28 +0200 Subject: Initial import of htcd system 1.0 Signed-off-by: Henrik Rydberg --- src/mt/dates.cc | 207 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 207 insertions(+) create mode 100644 src/mt/dates.cc (limited to 'src/mt/dates.cc') 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 @@ +/************************************************************************* + * + * 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 +#include +#include + +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=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