dect
/
asterisk
Archived
13
0
Fork 0

add API function for parsing strings to time_t (issue #6320, with mods)

git-svn-id: http://svn.digium.com/svn/asterisk/trunk@10105 f38db490-d61c-443f-a65b-d21fe96a405b
This commit is contained in:
kpfleming 2006-02-15 00:24:24 +00:00
parent 69f7e69869
commit 15db46a445
8 changed files with 66 additions and 72 deletions

View File

@ -76,45 +76,26 @@ static int sayunixtime_exec(struct ast_channel *chan, void *data)
struct localuser *u;
char *s,*zone=NULL,*timec,*format;
time_t unixtime;
struct timeval tv;
s = ast_strdupa(data);
if (!s)
return data ? -1 : 0;
LOCAL_USER_ADD(u);
tv = ast_tvnow();
unixtime = (time_t)tv.tv_sec;
format = "c"; /* default datetime */
if( !strcasecmp(chan->language, "da" ) ) {
format = "A dBY HMS";
} else if ( !strcasecmp(chan->language, "de" ) ) {
format = "A dBY HMS";
} else {
format = "ABdY 'digits/at' IMp";
}
if (data) {
s = data;
if ((s = ast_strdupa(s))) {
timec = strsep(&s,"|");
if ((timec) && (*timec != '\0')) {
long timein;
if (sscanf(timec,"%ld",&timein) == 1) {
unixtime = (time_t)timein;
}
}
if (s) {
zone = strsep(&s,"|");
if (zone && (*zone == '\0'))
zone = NULL;
if (s) {
format = s;
}
}
}
timec = strsep(&s,"|");
ast_get_time_t(timec, &unixtime, time(NULL));
if (s) {
zone = strsep(&s,"|");
if (ast_strlen_zero(zone))
zone = NULL;
}
if (s) /* override format */
format = s;
if (chan->_state != AST_STATE_UP) {
if (chan->_state != AST_STATE_UP)
res = ast_answer(chan);
}
if (!res)
res = ast_say_date_with_format(chan, unixtime, AST_DIGIT_ANY, chan->language, format, zone);

View File

@ -3608,25 +3608,21 @@ static int play_message_datetime(struct ast_channel *chan, struct ast_vm_user *v
int res = 0;
struct vm_zone *the_zone = NULL;
time_t t;
long tin;
if (sscanf(origtime,"%ld",&tin) < 1) {
if (ast_get_time_t(origtime, &t, 0)) {
ast_log(LOG_WARNING, "Couldn't find origtime in %s\n", filename);
return 0;
}
t = tin;
/* Does this user have a timezone specified? */
if (!ast_strlen_zero(vmu->zonetag)) {
/* Find the zone in the list */
struct vm_zone *z;
z = zones;
while (z) {
for (z = zones; z; z = z->next) {
if (!strcmp(z->name, vmu->zonetag)) {
the_zone = z;
break;
}
z = z->next;
}
}

View File

@ -2562,7 +2562,7 @@ static struct iax2_peer *realtime_peer(const char *peername, struct sockaddr_in
struct ast_variable *var;
struct ast_variable *tmp;
struct iax2_peer *peer=NULL;
time_t regseconds, nowtime;
time_t regseconds = 0, nowtime;
int dynamic=0;
if (peername)
@ -2603,8 +2603,7 @@ static struct iax2_peer *realtime_peer(const char *peername, struct sockaddr_in
break;
}
} else if (!strcasecmp(tmp->name, "regseconds")) {
if (sscanf(tmp->value, "%ld", (time_t *)&regseconds) != 1)
regseconds = 0;
ast_get_time_t(tmp->value, &regseconds, 0);
} else if (!strcasecmp(tmp->name, "ipaddr")) {
inet_aton(tmp->value, &(peer->addr.sin_addr));
} else if (!strcasecmp(tmp->name, "port")) {

View File

@ -12152,15 +12152,12 @@ static struct sip_peer *build_peer(const char *name, struct ast_variable *v, int
ast_variables_destroy(peer->chanvars);
peer->chanvars = NULL;
}
while(v) {
if (handle_common_options(&peerflags, &mask, v)) {
v = v->next;
for (; v; v = v->next) {
if (handle_common_options(&peerflags, &mask, v))
continue;
}
if (realtime && !strcasecmp(v->name, "regseconds")) {
if (sscanf(v->value, "%ld", (time_t *)&regseconds) != 1)
regseconds = 0;
ast_get_time_t(v->value, &regseconds, 0);
} else if (realtime && !strcasecmp(v->name, "ipaddr") && !ast_strlen_zero(v->value) ) {
inet_aton(v->value, &(peer->addr.sin_addr));
} else if (realtime && !strcasecmp(v->name, "name"))
@ -12309,10 +12306,6 @@ static struct sip_peer *build_peer(const char *name, struct ast_variable *v, int
peer->maxms = 0;
}
}
/* else if (strcasecmp(v->name,"type"))
* ast_log(LOG_WARNING, "Ignoring %s\n", v->name);
*/
v=v->next;
}
if (!ast_test_flag((&global_flags_page2), SIP_PAGE2_IGNOREREGEXPIRE) && ast_test_flag((&peer->flags_page2), SIP_PAGE2_DYNAMIC) && realtime) {
time_t nowtime;

View File

@ -232,8 +232,8 @@ static int acf_strftime(struct ast_channel *chan, char *cmd, char *parse,
AST_APP_ARG(timezone);
AST_APP_ARG(format);
);
long epochi;
struct tm time;
time_t epochi;
struct tm tm;
buf[0] = '\0';
@ -245,14 +245,13 @@ static int acf_strftime(struct ast_channel *chan, char *cmd, char *parse,
AST_STANDARD_APP_ARGS(args, parse);
if (ast_strlen_zero(args.epoch) || !sscanf(args.epoch, "%ld", &epochi)) {
struct timeval tv = ast_tvnow();
epochi = tv.tv_sec;
}
ast_get_time_t(args.epoch, &epochi, time(NULL));
ast_localtime(&epochi, &tm, args.timezone);
ast_localtime(&epochi, &time, args.timezone);
if (!args.format)
args.format = "%c";
if (!strftime(buf, len, args.format ? args.format : "%c", &time))
if (!strftime(buf, len, args.format, &tm))
ast_log(LOG_WARNING, "C function strftime() output nothing?!!\n");
buf[len - 1] = '\0';

View File

@ -1,7 +1,7 @@
/*
* Asterisk -- An open source telephony toolkit.
*
* Copyright (C) 1999 - 2005, Digium, Inc.
* Copyright (C) 1999 - 2006, Digium, Inc.
*
* Mark Spencer <markster@digium.com>
*
@ -223,6 +223,15 @@ int ast_false(const char *val);
*/
void ast_join(char *s, size_t len, char * const w[]);
/*
\brief Parse a time (integer) string.
\param src String to parse
\param dst Destination
\param _default Value to use if the string does not contain a valid time
\return zero on success, non-zero on failure
*/
int ast_get_time_t(const char *src, time_t *dst, time_t _default);
/* The realloca lets us ast_restrdupa(), but you can't mix any other ast_strdup calls! */
struct ast_realloca {

View File

@ -742,16 +742,13 @@ static int handle_saytime(struct ast_channel *chan, AGI *agi, int argc, char *ar
if (res == 1)
return RESULT_SUCCESS;
fdprintf(agi->fd, "200 result=%d\n", res);
if (res >= 0)
return RESULT_SUCCESS;
else
return RESULT_FAILURE;
return (res >= 0) ? RESULT_SUCCESS : RESULT_FAILURE;
}
static int handle_saydatetime(struct ast_channel *chan, AGI *agi, int argc, char *argv[])
{
int res=0;
long unixtime;
time_t unixtime;
char *format, *zone=NULL;
if (argc < 4)
@ -770,19 +767,15 @@ static int handle_saydatetime(struct ast_channel *chan, AGI *agi, int argc, char
if (argc > 5 && !ast_strlen_zero(argv[5]))
zone = argv[5];
if (sscanf(argv[2], "%ld", &unixtime) != 1)
if (ast_get_time_t(argv[2], &unixtime, 0))
return RESULT_SHOWUSAGE;
res = ast_say_date_with_format(chan, (time_t) unixtime, argv[3], chan->language, format, zone);
res = ast_say_date_with_format(chan, unixtime, argv[3], chan->language, format, zone);
if (res == 1)
return RESULT_SUCCESS;
fdprintf(agi->fd, "200 result=%d\n", res);
if (res >= 0)
return RESULT_SUCCESS;
else
return RESULT_FAILURE;
return (res >= 0) ? RESULT_SUCCESS : RESULT_FAILURE;
}
static int handle_sayphonetic(struct ast_channel *chan, AGI *agi, int argc, char *argv[])

24
utils.c
View File

@ -1046,3 +1046,27 @@ void __ast_string_field_index_build(struct ast_string_field_mgr *mgr,
va_end(ap2);
}
/*
* get values from config variables.
*/
int ast_get_time_t(const char *src, time_t *dst, time_t _default)
{
long t;
if (dst == NULL)
return -1;
*dst = _default;
if (ast_strlen_zero(src))
return -1;
/* only integer at the moment, but one day we could accept more formats */
if (sscanf(src, "%ld", &t) == 1) {
*dst = t;
return 0;
} else
return -1;
}