[sr-dev] git:master: pv: new PV $timef(format)

Daniel-Constantin Mierla miconda at gmail.com
Tue Oct 12 19:21:32 CEST 2010


Module: sip-router
Branch: master
Commit: a2dfe0b9ed4c53cd35f62532ed451f1d347e3361
URL:    http://git.sip-router.org/cgi-bin/gitweb.cgi/sip-router/?a=commit;h=a2dfe0b9ed4c53cd35f62532ed451f1d347e3361

Author: Daniel-Constantin Mierla <miconda at gmail.com>
Committer: Daniel-Constantin Mierla <miconda at gmail.com>
Date:   Tue Oct 12 19:16:08 2010 +0200

pv: new PV $timef(format)

- print strftime() formatted time
- e.g.,: $timef(%Y%m%d%H%M) => 201010121920
- max internal buf for printed value is 64
- it uses a static buffer, so clone the value if you use the PV many
  times with different formats

---

 modules_k/pv/pv.c      |    2 +
 modules_k/pv/pv_time.c |   55 ++++++++++++++++++++++++++++++++++++++++++++++++
 modules_k/pv/pv_time.h |    3 ++
 3 files changed, 60 insertions(+), 0 deletions(-)

diff --git a/modules_k/pv/pv.c b/modules_k/pv/pv.c
index 2a79578..efeb7ee 100644
--- a/modules_k/pv/pv.c
+++ b/modules_k/pv/pv.c
@@ -374,6 +374,8 @@ static pv_export_t mod_pvs[] = {
 		pv_set_shvar, pv_parse_shvar_name, 0, 0, 0},
 	{ {"time", (sizeof("time")-1)}, PVT_CONTEXT, pv_get_time,
 		0, pv_parse_time_name, 0, 0, 0},
+	{ {"timef", (sizeof("timef")-1)}, PVT_CONTEXT, pv_get_strftime,
+		0, pv_parse_strftime_name, 0, 0, 0},
 	{ {"TV", (sizeof("TV")-1)}, PVT_OTHER, pv_get_timeval,
 		0, pv_parse_timeval_name, 0, 0, 0},
 	{ {"nh", (sizeof("nh")-1)}, PVT_OTHER, pv_get_nh,
diff --git a/modules_k/pv/pv_time.c b/modules_k/pv/pv_time.c
index f904080..0687f53 100644
--- a/modules_k/pv/pv_time.c
+++ b/modules_k/pv/pv_time.c
@@ -100,9 +100,31 @@ error:
 	return -1;
 }
 
+int pv_parse_strftime_name(pv_spec_p sp, str *in)
+{
+	if(sp==NULL || in==NULL || in->len<=0)
+		return -1;
+
+	sp->pvp.pvn.u.isname.name.s.s = as_asciiz(in);
+	if(sp->pvp.pvn.u.isname.name.s.s==NULL)
+	{
+		LM_ERR("no more pkg\n");
+		return -1;
+	}
+	sp->pvp.pvn.u.isname.name.s.len = in->len;
+#if 0
+	/* to-do: free function for pv name structure */
+	sp->pvp.pvn.nfree = pkg_free;
+#endif
+	return 0;
+}
+
 static struct tm _cfgutils_ts;
 static unsigned int _cfgutils_msg_id = 0;
 
+/**
+ * return broken-down time attributes
+ */
 int pv_get_time(struct sip_msg *msg, pv_param_t *param,
 		pv_value_t *res)
 {
@@ -149,6 +171,39 @@ int pv_get_time(struct sip_msg *msg, pv_param_t *param,
 	}
 }
 
+/**
+ * return strftime() formatted time
+ */
+int pv_get_strftime(struct sip_msg *msg, pv_param_t *param,
+		pv_value_t *res)
+{
+	time_t t;
+	str s;
+#define PV_STRFTIME_BUF_SIZE	64
+	static char _pv_strftime_buf[PV_STRFTIME_BUF_SIZE];
+
+	if(msg==NULL || param==NULL)
+		return -1;
+
+	if(_cfgutils_msg_id != msg->id)
+	{
+		pv_update_time(msg, &t);
+		_cfgutils_msg_id = msg->id;
+		if(localtime_r(&t, &_cfgutils_ts) == NULL)
+		{
+			LM_ERR("unable to break time to attributes\n");
+			return -1;
+		}
+	}
+	s.len = strftime(_pv_strftime_buf, PV_STRFTIME_BUF_SIZE,
+			param->pvn.u.isname.name.s.s,  &_cfgutils_ts);
+	if(s.len<=0)
+		return pv_get_null(msg, param, res);
+	s.s = _pv_strftime_buf;
+	return pv_get_strval(msg, param, res, &s);
+}
+
+
 int pv_get_timenows(struct sip_msg *msg, pv_param_t *param,
 		pv_value_t *res)
 {
diff --git a/modules_k/pv/pv_time.h b/modules_k/pv/pv_time.h
index 4a6caf6..d1ab6a9 100644
--- a/modules_k/pv/pv_time.h
+++ b/modules_k/pv/pv_time.h
@@ -29,6 +29,9 @@
 int pv_parse_time_name(pv_spec_p sp, str *in);
 int pv_get_time(struct sip_msg *msg, pv_param_t *param,
 		pv_value_t *res);
+int pv_parse_strftime_name(pv_spec_p sp, str *in);
+int pv_get_strftime(struct sip_msg *msg, pv_param_t *param,
+		pv_value_t *res);
 int pv_get_timenows(struct sip_msg *msg, pv_param_t *param,
 		pv_value_t *res);
 int pv_get_timenowf(struct sip_msg *msg, pv_param_t *param,




More information about the sr-dev mailing list