📄 cpl_time.c
字号:
/* * $Id: cpl_time.c,v 1.8.2.1 2005/06/03 00:56:22 andrei Exp $ * * Copyright (C) 2001-2003 FhG Fokus * * This file is part of ser, a free SIP server. * * ser 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 * * For a license to use the ser software under conditions * other than those described here, or to purchase support for this * software, please contact iptel.org by e-mail at the following addresses: * info@iptel.org * * ser 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA * * * History: * ------- * 2003-06-24: file imported from tmrec (bogdan) * 2003-xx-xx: file Created (daniel) */#include <stdio.h>#include <string.h>#include <time.h>#include "../../mem/mem.h"#include "cpl_time.h"/************************ imported from "utils.h" ***************************/static inline int strz2int(char *_bp){ int _v; char *_p; if(!_bp) return 0; _v = 0; _p = _bp; while(*_p && *_p>='0' && *_p<='9') { _v += *_p - '0'; _p++; } return _v;}static inline char* trim(char* _s){ int len; char* end; /* Null pointer, there is nothing to do */ if (!_s) return _s; /* Remove spaces and tabs from the beginning of string */ while ((*_s == ' ') || (*_s == '\t')) _s++; len = strlen(_s); end = _s + len - 1; /* Remove trailing spaces and tabs */ while ((*end == ' ') || (*end == '\t')) end--; if (end != (_s + len - 1)) { *(end+1) = '\0'; } return _s;}/************************ imported from "ac_tm.c" ***************************//* #define USE_YWEEK_U // Sunday system * #define USE_YWEEK_V // ISO 8601 */#ifndef USE_YWEEK_U#ifndef USE_YWEEK_V#ifndef USE_YWEEK_W#define USE_YWEEK_W // Monday system#endif#endif#endif#ifdef USE_YWEEK_U#define SUN_WEEK(t) (int)(((t)->tm_yday + 7 - \ ((t)->tm_wday)) / 7)#else#define MON_WEEK(t) (int)(((t)->tm_yday + 7 - \ ((t)->tm_wday ? (t)->tm_wday - 1 : 6)) / 7)#endif#define ac_get_wday_yr(t) (int)((t)->tm_yday/7)#define ac_get_wday_mr(t) (int)(((t)->tm_mday-1)/7)ac_tm_p ac_tm_new(){ ac_tm_p _atp = NULL; _atp = (ac_tm_p)pkg_malloc(sizeof(ac_tm_t)); if(!_atp) return NULL; memset(_atp, 0, sizeof(ac_tm_t)); return _atp;}int ac_tm_fill(ac_tm_p _atp, struct tm* _tm){ if(!_atp || !_tm) return -1; _atp->t.tm_sec = _tm->tm_sec; /* seconds */ _atp->t.tm_min = _tm->tm_min; /* minutes */ _atp->t.tm_hour = _tm->tm_hour; /* hours */ _atp->t.tm_mday = _tm->tm_mday; /* day of the month */ _atp->t.tm_mon = _tm->tm_mon; /* month */ _atp->t.tm_year = _tm->tm_year; /* year */ _atp->t.tm_wday = _tm->tm_wday; /* day of the week */ _atp->t.tm_yday = _tm->tm_yday; /* day in the year */ _atp->t.tm_isdst = _tm->tm_isdst; /* daylight saving time */ _atp->mweek = ac_get_mweek(_tm); _atp->yweek = ac_get_yweek(_tm); _atp->ywday = ac_get_wday_yr(_tm); _atp->mwday = ac_get_wday_mr(_tm); DBG("---> fill = %s\n",asctime(&(_atp->t)) ); return 0;}int ac_tm_set(ac_tm_p _atp, struct tm* _tm){ if(!_atp || !_tm) return -1; _atp->time = mktime(_tm); return ac_tm_fill(_atp, _tm);}int ac_tm_set_time(ac_tm_p _atp, time_t _t){ if(!_atp) return -1; _atp->time = _t; return ac_tm_fill(_atp, localtime(&_t));}int ac_get_mweek(struct tm* _tm){ if(!_tm) return -1;#ifdef USE_YWEEK_U return ((_tm->tm_mday-1)/7 + (7-_tm->tm_wday+(_tm->tm_mday-1)%7)/7);#else return ((_tm->tm_mday-1)/7 + (7-(6+_tm->tm_wday)%7+(_tm->tm_mday-1)%7)/7);#endif}int ac_get_yweek(struct tm* _tm){ int week = -1;#ifdef USE_YWEEK_V int days;#endif if(!_tm) return -1; #ifdef USE_YWEEK_U week = SUN_WEEK(_tm);#else week = MON_WEEK(_tm);#endif#ifdef USE_YWEEK_V days = ((_tm->tm_yday + 7 - (_tm->tm_wday ? _tm->tm_wday-1 : 6)) % 7); if(days >= 4) week++; else if(week == 0) week = 53;#endif return week;}int ac_get_wkst(){#ifdef USE_YWEEK_U return 0;#else return 1;#endif}int ac_tm_reset(ac_tm_p _atp){ if(!_atp) return -1; memset(_atp, 0, sizeof(ac_tm_t)); return 0;}int ac_tm_free(ac_tm_p _atp){ if(!_atp) return -1; if(_atp->mv) pkg_free(_atp->mv); /*pkg_free(_atp);*/ return 0;}ac_maxval_p ac_get_maxval(ac_tm_p _atp){ struct tm _tm; int _v; ac_maxval_p _amp = NULL; if(!_atp) return NULL; _amp = (ac_maxval_p)pkg_malloc(sizeof(ac_maxval_t)); if(!_amp) return NULL; // the number of the days in the year _amp->yday = 365 + is_leap_year(_atp->t.tm_year+1900); // the number of the days in the month switch(_atp->t.tm_mon) { case 1: if(_amp->yday == 366) _amp->mday = 29; else _amp->mday = 28; break; case 3: case 5: case 8: case 10: _amp->mday = 30; break; default: _amp->mday = 31; } // maximum occurrences of a week day in the year memset(&_tm, 0, sizeof(struct tm)); _tm.tm_year = _atp->t.tm_year; _tm.tm_mon = 11; _tm.tm_mday = 31; mktime(&_tm); _v = 0; if(_atp->t.tm_wday > _tm.tm_wday) _v = _atp->t.tm_wday - _tm.tm_wday + 1; else _v = _tm.tm_wday - _atp->t.tm_wday; _amp->ywday = (int)((_tm.tm_yday-_v)/7) + 1; // maximum number of weeks in the year _amp->yweek = ac_get_yweek(&_tm) + 1; // maximum number of the week day in the month _amp->mwday=(int)((_amp->mday-1-(_amp->mday-_atp->t.tm_mday)%7)/7)+1; // maximum number of weeks in the month _v = (_atp->t.tm_wday + (_amp->mday - _atp->t.tm_mday)%7)%7;#ifdef USE_YWEEK_U _amp->mweek = (int)((_amp->mday-1)/7+(7-_v+(_amp->mday-1)%7)/7)+1;#else _amp->mweek = (int)((_amp->mday-1)/7+(7-(6+_v)%7+(_amp->mday-1)%7)/7)+1;#endif _atp->mv = _amp; return _amp;}int ac_print(ac_tm_p _atp){ static char *_wdays[] = {"SU", "MO", "TU", "WE", "TH", "FR", "SA"}; if(!_atp) { printf("\n(null)\n"); return -1; } printf("\nSys time: %d\nTime: %02d:%02d:%02d\n", (int)_atp->time, _atp->t.tm_hour, _atp->t.tm_min, _atp->t.tm_sec); printf("Date: %s, %04d-%02d-%02d\n", _wdays[_atp->t.tm_wday], _atp->t.tm_year+1900, _atp->t.tm_mon+1, _atp->t.tm_mday); printf("Year day: %d\nYear week-day: %d\nYear week: %d\n", _atp->t.tm_yday, _atp->ywday, _atp->yweek); printf("Month week: %d\nMonth week-day: %d\n", _atp->mweek, _atp->mwday); if(_atp->mv) { printf("Max ydays: %d\nMax yweeks: %d\nMax yweekday: %d\n", _atp->mv->yday, _atp->mv->yweek, _atp->mv->ywday);; printf("Max mdays: %d\nMax mweeks: %d\nMax mweekday: %d\n", _atp->mv->mday, _atp->mv->mweek, _atp->mv->mwday);; } return 0;}/************************ imported from "tmrec.c" ***************************/#define _D(c) ((c) -'0')tr_byxxx_p tr_byxxx_new(){ tr_byxxx_p _bxp = NULL; _bxp = (tr_byxxx_p)pkg_malloc(sizeof(tr_byxxx_t)); if(!_bxp) return NULL; memset(_bxp, 0, sizeof(tr_byxxx_t)); return _bxp;}int tr_byxxx_init(tr_byxxx_p _bxp, int _nr){ if(!_bxp) return -1; _bxp->nr = _nr; _bxp->xxx = (int*)pkg_malloc(_nr*sizeof(int)); if(!_bxp->xxx) return -1; _bxp->req = (int*)pkg_malloc(_nr*sizeof(int)); if(!_bxp->req) { pkg_free(_bxp->xxx); return -1; } memset(_bxp->xxx, 0, _nr*sizeof(int)); memset(_bxp->req, 0, _nr*sizeof(int)); return 0;}int tr_byxxx_free(tr_byxxx_p _bxp){ if(!_bxp) return -1; if(_bxp->xxx) pkg_free(_bxp->xxx); if(_bxp->req) pkg_free(_bxp->req); pkg_free(_bxp); return 0;}tmrec_p tmrec_new(){ tmrec_p _trp = NULL; _trp = (tmrec_p)pkg_malloc(sizeof(tmrec_t)); if(!_trp) return NULL; memset(_trp, 0, sizeof(tmrec_t)); localtime_r(&_trp->dtstart,&(_trp->ts)); return _trp;}int tmrec_free(tmrec_p _trp){ if(!_trp) return -1; tr_byxxx_free(_trp->byday); tr_byxxx_free(_trp->bymday); tr_byxxx_free(_trp->byyday); tr_byxxx_free(_trp->bymonth); tr_byxxx_free(_trp->byweekno); /*pkg_free(_trp);*/ return 0;}int tr_parse_dtstart(tmrec_p _trp, char *_in){ if(!_trp || !_in) return -1; _trp->dtstart = ic_parse_datetime(_in, &(_trp->ts)); DBG("----->dtstart = %ld | %s\n", (long)_trp->dtstart, ctime(&(_trp->dtstart))); return (_trp->dtstart==0)?-1:0;}int tr_parse_dtend(tmrec_p _trp, char *_in){ struct tm _tm; if(!_trp || !_in) return -1; _trp->dtend = ic_parse_datetime(_in,&_tm); DBG("----->dtend = %ld | %s\n", (long)_trp->dtend, ctime(&(_trp->dtend))); return (_trp->dtend==0)?-1:0;}int tr_parse_duration(tmrec_p _trp, char *_in){ if(!_trp || !_in) return -1; _trp->duration = ic_parse_duration(_in); return (_trp->duration==0)?-1:0;}int tr_parse_until(tmrec_p _trp, char *_in){ struct tm _tm; if(!_trp || !_in) return -1; _trp->until = ic_parse_datetime(_in, &_tm); return (_trp->until==0)?-1:0;}int tr_parse_freq(tmrec_p _trp, char *_in){ if(!_trp || !_in) return -1; if(!strcasecmp(_in, "daily")) { _trp->freq = FREQ_DAILY; return 0; } if(!strcasecmp(_in, "weekly")) { _trp->freq = FREQ_WEEKLY; return 0; } if(!strcasecmp(_in, "monthly")) { _trp->freq = FREQ_MONTHLY; return 0; } if(!strcasecmp(_in, "yearly")) { _trp->freq = FREQ_YEARLY; return 0; } _trp->freq = FREQ_NOFREQ; return 0;}int tr_parse_interval(tmrec_p _trp, char *_in){ if(!_trp || !_in) return -1; _trp->interval = strz2int(_in); return 0;}int tr_parse_byday(tmrec_p _trp, char *_in){ if(!_trp || !_in) return -1; _trp->byday = ic_parse_byday(_in); return 0;}int tr_parse_bymday(tmrec_p _trp, char *_in){ if(!_trp || !_in) return -1; _trp->bymday = ic_parse_byxxx(_in); return 0;}int tr_parse_byyday(tmrec_p _trp, char *_in){ if(!_trp || !_in) return -1; _trp->byyday = ic_parse_byxxx(_in); return 0;}int tr_parse_bymonth(tmrec_p _trp, char *_in){ if(!_trp || !_in) return -1; _trp->bymonth = ic_parse_byxxx(_in); return 0;}int tr_parse_byweekno(tmrec_p _trp, char *_in){ if(!_trp || !_in) return -1; _trp->byweekno = ic_parse_byxxx(_in); return 0;}int tr_parse_wkst(tmrec_p _trp, char *_in){ if(!_trp || !_in) return -1; _trp->wkst = ic_parse_wkst(_in); return 0;}int tr_print(tmrec_p _trp){ static char *_wdays[] = {"SU", "MO", "TU", "WE", "TH", "FR", "SA"}; int i; if(!_trp) { printf("\n(null)\n"); return -1; } printf("Recurrence definition\n-- start time ---\n"); printf("Sys time: %d\n", (int)_trp->dtstart); printf("Time: %02d:%02d:%02d\n", _trp->ts.tm_hour, _trp->ts.tm_min, _trp->ts.tm_sec); printf("Date: %s, %04d-%02d-%02d\n", _wdays[_trp->ts.tm_wday], _trp->ts.tm_year+1900, _trp->ts.tm_mon+1, _trp->ts.tm_mday); printf("---\n"); printf("End time: %d\n", (int)_trp->dtend); printf("Duration: %d\n", (int)_trp->duration); printf("Until: %d\n", (int)_trp->until); printf("Freq: %d\n", (int)_trp->freq); printf("Interval: %d\n", (int)_trp->interval); if(_trp->byday) { printf("Byday: "); for(i=0; i<_trp->byday->nr; i++) printf(" %d%s", _trp->byday->req[i], _wdays[_trp->byday->xxx[i]]); printf("\n"); } if(_trp->bymday) { printf("Bymday: %d:", _trp->bymday->nr); for(i=0; i<_trp->bymday->nr; i++) printf(" %d", _trp->bymday->xxx[i]*_trp->bymday->req[i]); printf("\n"); } if(_trp->byyday) { printf("Byyday:"); for(i=0; i<_trp->byyday->nr; i++) printf(" %d", _trp->byyday->xxx[i]*_trp->byyday->req[i]); printf("\n"); } if(_trp->bymonth) { printf("Bymonth: %d:", _trp->bymonth->nr); for(i=0; i< _trp->bymonth->nr; i++) printf(" %d", _trp->bymonth->xxx[i]*_trp->bymonth->req[i]); printf("\n"); } if(_trp->byweekno) { printf("Byweekno: "); for(i=0; i<_trp->byweekno->nr; i++) printf(" %d", _trp->byweekno->xxx[i]*_trp->byweekno->req[i]); printf("\n"); } printf("Weekstart: %d\n", _trp->wkst); return 0;}time_t ic_parse_datetime(char *_in, struct tm *_tm){ if(!_in || !_tm) return 0; memset(_tm, 0, sizeof(struct tm)); _tm->tm_year = _D(_in[0])*1000 + _D(_in[1])*100 + _D(_in[2])*10 + _D(_in[3]) - 1900; _tm->tm_mon = _D(_in[4])*10 + _D(_in[5]) - 1; _tm->tm_mday = _D(_in[6])*10 + _D(_in[7]); _tm->tm_hour = _D(_in[9])*10 + _D(_in[10]); _tm->tm_min = _D(_in[11])*10 + _D(_in[12]); _tm->tm_sec = _D(_in[13])*10 + _D(_in[14]); _tm->tm_isdst = -1 /*daylight*/; return mktime(_tm);}time_t ic_parse_duration(char *_in){ time_t _t, _ft; char *_p; int _fl; if(!_in || (*_in!='+' && *_in!='-' && *_in!='P' && *_in!='p')) return 0; if(*_in == 'P' || *_in=='p') _p = _in+1; else { if(strlen(_in)<2 || (_in[1]!='P' && _in[1]!='p')) return 0; _p = _in+2; } _t = _ft = 0; _fl = 1; while(*_p) { switch(*_p) { case '0': case '1': case '2':
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -