diff --git a/apps/app_alarmreceiver.c b/apps/app_alarmreceiver.c
index 1c759cd423a2e297b913087aaa381ff35ed6c8c5..14f417dcbcced2e26d80dad3cc2b3cd806baaea3 100644
--- a/apps/app_alarmreceiver.c
+++ b/apps/app_alarmreceiver.c
@@ -306,8 +306,8 @@ static int receive_dtmf_digits(struct ast_channel *chan, char *digit_string, int
 static int write_metadata( FILE *logfile, char *signalling_type, struct ast_channel *chan)
 {
 	int res = 0;
-	time_t t;
-	struct tm now;
+	struct timeval t;
+	struct ast_tm now;
 	char *cl,*cn;
 	char workstring[80];
 	char timestamp[80];
@@ -324,12 +324,12 @@ static int write_metadata( FILE *logfile, char *signalling_type, struct ast_chan
 
 	/* Get the current time */
 		
-	time(&t);
+	t = ast_tvnow();
 	ast_localtime(&t, &now, NULL);
 	
 	/* Format the time */
 	
-	strftime(timestamp, sizeof(timestamp), time_stamp_format, &now); 
+	ast_strftime(timestamp, sizeof(timestamp), time_stamp_format, &now); 
 
 	
 	res = fprintf(logfile, "\n\n[metadata]\n\n");
diff --git a/apps/app_minivm.c b/apps/app_minivm.c
index 24ebd0cb9215aeb5626296fe3b86b2ff56384efa..f5dee8b0791cf8351a0222e1877c68b771fed50b 100644
--- a/apps/app_minivm.c
+++ b/apps/app_minivm.c
@@ -425,9 +425,9 @@ struct minivm_stats {
 	int timezones;			/*!< Number of time zones */
 	int templates;			/*!< Number of templates */
 
-	time_t reset;			/*!< Time for last reset */
+	struct timeval reset;			/*!< Time for last reset */
 	int receivedmessages;		/*!< Number of received messages since reset */
-	time_t lastreceived;		/*!< Time for last voicemail sent */
+	struct timeval lastreceived;		/*!< Time for last voicemail sent */
 };
 
 /*! \brief Statistics for voicemail */
@@ -713,12 +713,11 @@ static int base_encode(char *filename, FILE *so)
 
 static int get_date(char *s, int len)
 {
-	struct tm tm;
-	time_t t;
+	struct ast_tm tm;
+	struct timeval tv = ast_tvnow();
 
-	t = time(0);
-	ast_localtime(&t, &tm, NULL);
-	return strftime(s, len, "%a %b %e %r %Z %Y", &tm);
+	ast_localtime(&tv, &tm, NULL);
+	return ast_strftime(s, len, "%a %b %e %r %Z %Y", &tm);
 }
 
 
@@ -898,8 +897,8 @@ static int sendmail(struct minivm_template *template, struct minivm_account *vmu
 	char dur[PATH_MAX];
 	char tmp[80] = "/tmp/astmail-XXXXXX";
 	char tmp2[PATH_MAX];
-	time_t now;
-	struct tm tm;
+	struct timeval now;
+	struct ast_tm tm;
 	struct minivm_zone *the_zone = NULL;
 	int len_passdata;
 	struct ast_channel *ast;
@@ -986,15 +985,15 @@ static int sendmail(struct minivm_template *template, struct minivm_account *vmu
 		AST_LIST_UNLOCK(&minivm_zones);
 	}
 
-	time(&now);
+	now = ast_tvnow();
 	ast_localtime(&now, &tm, the_zone ? the_zone->timezone : NULL);
-	strftime(date, sizeof(date), "%a, %d %b %Y %H:%M:%S %z", &tm);
+	ast_strftime(date, sizeof(date), "%a, %d %b %Y %H:%M:%S %z", &tm);
 
 	/* Start printing the email to the temporary file */
 	fprintf(p, "Date: %s\n", date);
 
 	/* Set date format for voicemail mail */
-	strftime(date, sizeof(date), template->dateformat, &tm);
+	ast_strftime(date, sizeof(date), template->dateformat, &tm);
 
 
 	/* Populate channel with channel variables for substitution */
@@ -1550,14 +1549,13 @@ static int leave_voicemail(struct ast_channel *chan, char *username, struct leav
 	if (!txt) {
 		ast_log(LOG_WARNING, "Error opening text file for output\n");
 	} else {
-		struct tm tm;
-		time_t now;
+		struct ast_tm tm;
+		struct timeval now = ast_tvnow();
 		char timebuf[30];
 		char logbuf[BUFSIZ];
 		get_date(date, sizeof(date));
-		now = time(NULL);
 		ast_localtime(&now, &tm, NULL);
-		strftime(timebuf, sizeof(timebuf), "%H:%M:%S", &tm);
+		ast_strftime(timebuf, sizeof(timebuf), "%H:%M:%S", &tm);
 		
 		snprintf(logbuf, sizeof(logbuf),
 			/* "Mailbox:domain:macrocontext:exten:priority:callerchan:callerid:origdate:origtime:duration:durationstatus:accountcode" */
@@ -1608,7 +1606,7 @@ static int leave_voicemail(struct ast_channel *chan, char *username, struct leav
 		pbx_builtin_setvar_helper(chan, "MVM_FORMAT", fmt);
 
 	}
-	global_stats.lastreceived = time(NULL);
+	global_stats.lastreceived = ast_tvnow();
 	global_stats.receivedmessages++;
 //	/* Go ahead and delete audio files from system, they're not needed any more */
 //	if (ast_fileexists(tmptxtfile, NULL, NULL) <= 0) {
@@ -2384,8 +2382,8 @@ static int load_config(void)
 	ast_set2_flag((&globalflags), FALSE, MVM_OPERATOR);	
 	strcpy(global_charset, "ISO-8859-1");
 	/* Reset statistics */
-	memset(&global_stats, 0, sizeof(struct minivm_stats));
-	global_stats.reset = time(NULL);
+	memset(&global_stats, 0, sizeof(global_stats));
+	global_stats.reset = ast_tvnow();
 
 	/* Make sure we could load configuration file */
 	if (!cfg) {
@@ -2651,7 +2649,7 @@ static int handle_minivm_show_settings(int fd, int argc, char *argv[])
 /*! \brief Show stats */
 static int handle_minivm_show_stats(int fd, int argc, char *argv[])
 {
-	struct tm time;
+	struct ast_tm time;
 	char buf[BUFSIZ];
 
 	ast_cli(fd, "* Mini-Voicemail statistics\n");
@@ -2665,11 +2663,11 @@ static int handle_minivm_show_stats(int fd, int argc, char *argv[])
 	} else {
 		ast_cli(fd, "  Received messages since last reset:  %d\n", global_stats.receivedmessages);
 		ast_localtime(&global_stats.lastreceived, &time, NULL);
-		strftime(buf, sizeof(buf), "%a %b %e %r %Z %Y", &time);
+		ast_strftime(buf, sizeof(buf), "%a %b %e %r %Z %Y", &time);
 		ast_cli(fd, "  Last received voicemail:             %s\n", buf);
 	}
 	ast_localtime(&global_stats.reset, &time, NULL);
-	strftime(buf, sizeof(buf), "%a %b %e %r %Z %Y", &time);
+	ast_strftime(buf, sizeof(buf), "%a %b %e %r %Z %Y", &time);
 	ast_cli(fd, "  Last reset:                          %s\n", buf);
 
 	ast_cli(fd, "\n");
diff --git a/apps/app_playback.c b/apps/app_playback.c
index a3ff14cde696908249b8872b07c7d79863e2908d..542382ea8940d81ca91a12aa859d3b50a81ce8c8 100644
--- a/apps/app_playback.c
+++ b/apps/app_playback.c
@@ -281,12 +281,13 @@ static int say_date_generic(struct ast_channel *chan, time_t t,
 	const char *ints, const char *lang, const char *format, const char *timezone, const char *prefix)
 {
 	char buf[128];
-	struct tm tm;
+	struct ast_tm tm;
+	struct timeval tv = { t, 0 };
         say_args_t a = { chan, ints, lang, -1, -1 };
 	if (format == NULL)
 		format = "";
 
-	ast_localtime(&t, &tm, NULL);
+	ast_localtime(&tv, &tm, NULL);
 	snprintf(buf, sizeof(buf), "%s:%s:%04d%02d%02d%02d%02d.%02d-%d-%3d",
 		prefix,
 		format,
diff --git a/apps/app_rpt.c b/apps/app_rpt.c
index 70badd255f79ae057e2fab1836577249316377e8..c81fbbdb0bd60e6446cfddbab60fb43e638be1d0 100644
--- a/apps/app_rpt.c
+++ b/apps/app_rpt.c
@@ -587,6 +587,7 @@ static void rpt_mutex_spew(void)
 	int i, j;
 	long long diff;
 	char a[100] = "";
+	struct ast_tm tm;
 	struct timeval lasttv;
 
 	ast_mutex_lock(&locklock);
@@ -597,8 +598,8 @@ static void rpt_mutex_spew(void)
 	lasttv.tv_sec = lasttv.tv_usec = 0;
 	for (i = 0; i < 32; i++) {
 		j = (i + lock_ring_index_copy) % 32;
-		strftime(a, sizeof(a) - 1, "%m/%d/%Y %H:%M:%S",
-			localtime(&lock_ring_copy[j].tv.tv_sec));
+		ast_strftime(a, sizeof(a) - 1, "%m/%d/%Y %H:%M:%S",
+			ast_localtime(&lock_ring_copy[j].tv, &tm, NULL));
 		diff = 0;
 		if (lasttv.tv_sec) {
 			diff = (lock_ring_copy[j].tv.tv_sec - lasttv.tv_sec) * 1000000;
@@ -1890,7 +1891,7 @@ static void *rpt_tele_thread(void *this)
 	struct ast_channel *mychannel;
 	const char *p, *ct;
 	time_t t;
-	struct tm localtm;
+	struct ast_tm localtm;
 #ifdef  APP_RPT_LOCK_DEBUG
 	struct lockthread *t;
 #endif
@@ -5619,7 +5620,7 @@ static void queue_id(struct rpt *myrpt)
 static void do_scheduler(struct rpt *myrpt)
 {
 	int res;
-	struct tm tmnow;
+	struct ast_tm tmnow;
 
 	memcpy(&myrpt->lasttv, &myrpt->curtv, sizeof(struct timeval));
 	
diff --git a/apps/app_sms.c b/apps/app_sms.c
index 5575945ebd512d01fff288a5788628bf97f8527b..9c645379bd154245c4ea47db5e9d40fb8660d696 100644
--- a/apps/app_sms.c
+++ b/apps/app_sms.c
@@ -277,9 +277,10 @@ static void numcpy(char *d, char *s)
 /*! \brief static, return a date/time in ISO format */
 static char *isodate(time_t t, char *buf, int len)
 {
-	struct tm tm;
-	ast_localtime(&t, &tm, NULL);
-	strftime(buf, len, "%Y-%m-%dT%H:%M:%S", &tm);
+	struct ast_tm tm;
+	struct timeval tv = { t, 0 };
+	ast_localtime(&tv, &tm, NULL);
+	ast_strftime(buf, len, "%Y-%m-%dT%H:%M:%S", &tm);
 	return buf;
 }
 
@@ -511,10 +512,11 @@ static int packsms(unsigned char dcs, unsigned char *base, unsigned int udhl, un
 /*! \brief pack a date and return */
 static void packdate(unsigned char *o, time_t w)
 {
-	struct tm t;
+	struct ast_tm t;
+	struct timeval tv = { w, 0 };
 	int z;
 
-	ast_localtime(&w, &t, NULL);
+	ast_localtime(&tv, &t, NULL);
 #if defined(__FreeBSD__) || defined(__OpenBSD__) || defined( __NetBSD__ ) || defined(__APPLE__)
 	z = -t.tm_gmtoff / 60 / 15;
 #else
@@ -1113,7 +1115,8 @@ static void putdummydata_proto2(sms_t *h)
 
 static void sms_compose2(sms_t *h, int more)
 {
-	struct tm tm;
+	struct ast_tm tm;
+	struct timeval tv = { h->scts, 0 };
 	char stm[9];
 
 	h->omsg[0] = 0x00;       /* set later... */
@@ -1122,7 +1125,7 @@ static void sms_compose2(sms_t *h, int more)
 	if (h->smsc) {		  /* deliver */
 		h->omsg[0] = 0x11;      /* SMS_DELIVERY */
 		/* Required: 10 11 12 13 14 15 17 (seems they must be ordered!) */
-		ast_localtime(&h->scts, &tm, NULL);
+		ast_localtime(&tv, &tm, NULL);
 		sprintf(stm, "%02d%02d%02d%02d", tm.tm_mon + 1, tm.tm_mday, tm.tm_hour, tm.tm_min);     /* Date mmddHHMM */
 		adddata_proto2(h, 0x14, stm, 8);	       /* Date */
 		if (*h->oa == 0)
@@ -1160,14 +1163,15 @@ static int sms_handleincoming_proto2(sms_t *h)
 {
 	int f, i, sz = 0;
 	int msg, msgsz;
-	struct tm tm;
+	struct ast_tm tm;
+	struct timeval tv = { 0, 0 };
 	char debug_buf[MAX_DEBUG_LEN * 3 + 1];
 
 	sz = h->imsg[1] + 2;
 	/* ast_verbose(VERBOSE_PREFIX_3 "SMS-P2 Frame: %s\n", sms_hexdump(h->imsg, sz, debug_buf)); */
 
 	/* Parse message body (called payload) */
-	h->scts = time(NULL);
+	tv.tv_sec = h->scts = time(NULL);
 	for (f = 4; f < sz; ) {
 		msg = h->imsg[f++];
 		msgsz = h->imsg[f++];
@@ -1183,14 +1187,14 @@ static int sms_handleincoming_proto2(sms_t *h)
 			h->udl = msgsz;
 			break;
 		case 0x14:      /* Date SCTS */
-			h->scts = time(NULL);
-			ast_localtime(&h->scts, &tm, NULL);
+			tv.tv_sec = h->scts = time(NULL);
+			ast_localtime(&tv, &tm, NULL);
 			tm.tm_mon = ( (h->imsg[f] * 10) + h->imsg[f + 1] ) - 1;
 			tm.tm_mday = ( (h->imsg[f + 2] * 10) + h->imsg[f + 3] );
 			tm.tm_hour = ( (h->imsg[f + 4] * 10) + h->imsg[f + 5] );
 			tm.tm_min = ( (h->imsg[f + 6] * 10) + h->imsg[f + 7] );
 			tm.tm_sec = 0;
-			h->scts = mktime(&tm);
+			h->scts = ast_mktime(&tm, NULL);
 			if (option_verbose > 2)
 				ast_verbose(VERBOSE_PREFIX_3 "SMS-P2 Date#%02X=%02d/%02d %02d:%02d\n", msg, tm.tm_mday, tm.tm_mon + 1, tm.tm_hour, tm.tm_min);
 			break;
diff --git a/apps/app_voicemail.c b/apps/app_voicemail.c
index 4f3fd9968459270e629077b6bda02cd66ca0a217..4c051dc8b2d6251ba20ff62c8463dbef62375328 100644
--- a/apps/app_voicemail.c
+++ b/apps/app_voicemail.c
@@ -1964,10 +1964,10 @@ static char *quote(const char *from, char *to, size_t len)
  * fill in *tm for current time according to the proper timezone, if any.
  * Return tm so it can be used as a function argument.
  */
-static const struct tm *vmu_tm(const struct ast_vm_user *vmu, struct tm *tm)
+static const struct ast_tm *vmu_tm(const struct ast_vm_user *vmu, struct ast_tm *tm)
 {
 	const struct vm_zone *z = NULL;
-	time_t t = time(NULL);
+	struct timeval t = ast_tvnow();
 
 	/* Does this user have a timezone specified? */
 	if (!ast_strlen_zero(vmu->zonetag)) {
@@ -2007,7 +2007,7 @@ static void make_email_file(FILE *p, char *srcemail, struct ast_vm_user *vmu, in
 	char fname[256];
 	char dur[256];
 	char tmpcmd[256];
-	struct tm tm;
+	struct ast_tm tm;
 	char *passdata2;
 	size_t len_passdata;
 	char *greeting_attachment;
@@ -2029,11 +2029,11 @@ static void make_email_file(FILE *p, char *srcemail, struct ast_vm_user *vmu, in
 		*greeting_attachment++ = '\0';
 
 	snprintf(dur, sizeof(dur), "%d:%02d", duration / 60, duration % 60);
-	strftime(date, sizeof(date), "%a, %d %b %Y %H:%M:%S %z", vmu_tm(vmu, &tm));
+	ast_strftime(date, sizeof(date), "%a, %d %b %Y %H:%M:%S %z", vmu_tm(vmu, &tm));
 	fprintf(p, "Date: %s" ENDL, date);
 
 	/* Set date format for voicemail mail */
-	strftime(date, sizeof(date), emaildateformat, &tm);
+	ast_strftime(date, sizeof(date), emaildateformat, &tm);
 
 	if (!ast_strlen_zero(fromstring)) {
 		struct ast_channel *ast;
@@ -2214,7 +2214,7 @@ static int sendpage(char *srcemail, char *pager, int msgnum, char *context, char
 	char dur[PATH_MAX];
 	char tmp[80] = "/tmp/astmail-XXXXXX";
 	char tmp2[PATH_MAX];
-	struct tm tm;
+	struct ast_tm tm;
 	FILE *p;
 
 	if ((p = vm_mkftemp(tmp)) == NULL) {
@@ -2227,7 +2227,7 @@ static int sendpage(char *srcemail, char *pager, int msgnum, char *context, char
 	else 
 		snprintf(who, sizeof(who), "%s@%s", srcemail, host);
 	snprintf(dur, sizeof(dur), "%d:%02d", duration / 60, duration % 60);
-	strftime(date, sizeof(date), "%a, %d %b %Y %H:%M:%S %z", vmu_tm(vmu, &tm));
+	ast_strftime(date, sizeof(date), "%a, %d %b %Y %H:%M:%S %z", vmu_tm(vmu, &tm));
 	fprintf(p, "Date: %s\n", date);
 
 	if (*pagerfromstring) {
@@ -2266,7 +2266,7 @@ static int sendpage(char *srcemail, char *pager, int msgnum, char *context, char
 	} else
 		fprintf(p, "Subject: New VM\n\n");
 
-	strftime(date, sizeof(date), "%A, %B %d, %Y at %r", &tm);
+	ast_strftime(date, sizeof(date), "%A, %B %d, %Y at %r", &tm);
 	if (pagerbody) {
 		struct ast_channel *ast;
 		if ((ast = ast_channel_alloc(0, AST_STATE_DOWN, 0, 0, "", "", "", 0, 0))) {
@@ -2293,11 +2293,10 @@ static int sendpage(char *srcemail, char *pager, int msgnum, char *context, char
 
 static int get_date(char *s, int len)
 {
-	struct tm tm;
-	time_t t;
-	t = time(0);
+	struct ast_tm tm;
+	struct timeval t = ast_tvnow();
 	ast_localtime(&t, &tm, NULL);
-	return strftime(s, len, "%a %b %e %r %Z %Y", &tm);
+	return ast_strftime(s, len, "%a %b %e %r %Z %Y", &tm);
 }
 
 static int invent_message(struct ast_channel *chan, char *context, char *ext, int busy, char *ecodes)
@@ -4472,8 +4471,7 @@ static int play_message_datetime(struct ast_channel *chan, struct ast_vm_user *v
 	/* Set the DIFF_* variables */
 	ast_localtime(&t, &time_now, NULL);
 	tv_now = ast_tvnow();
-	tnow = tv_now.tv_sec;
-	ast_localtime(&tnow, &time_then, NULL);
+	ast_localtime(&tv_now, &time_then, NULL);
 
 	/* Day difference */
 	if (time_now.tm_year == time_then.tm_year)
diff --git a/cdr/cdr_csv.c b/cdr/cdr_csv.c
index f40f555a8d58e18b099a75cd9b54a2fb128276d7..bfc509803373a9f3595b932d49859f424abe7e96 100644
--- a/cdr/cdr_csv.c
+++ b/cdr/cdr_csv.c
@@ -188,21 +188,15 @@ static int append_int(char *buf, int s, size_t bufsize)
 static int append_date(char *buf, struct timeval tv, size_t bufsize)
 {
 	char tmp[80] = "";
-	struct tm tm;
-	time_t t;
-	t = tv.tv_sec;
+	struct ast_tm tm;
 	if (strlen(buf) > bufsize - 3)
 		return -1;
 	if (ast_tvzero(tv)) {
 		strncat(buf, ",", bufsize - strlen(buf) - 1);
 		return 0;
 	}
-	if (usegmtime) {
-		gmtime_r(&t,&tm);
-	} else {
-		ast_localtime(&t, &tm, NULL);
-	}
-	strftime(tmp, sizeof(tmp), DATE_FORMAT, &tm);
+	ast_localtime(&tv, &tm, usegmtime ? "GMT" : NULL);
+	ast_strftime(tmp, sizeof(tmp), DATE_FORMAT, &tm);
 	return append_string(buf, tmp, bufsize);
 }
 
diff --git a/cdr/cdr_manager.c b/cdr/cdr_manager.c
index a245d92bf160b80770d0988d9e57049802466657..5dc4da6fe90e70b39716e4826592b0c835ece606 100644
--- a/cdr/cdr_manager.c
+++ b/cdr/cdr_manager.c
@@ -103,8 +103,7 @@ static int load_config(void)
 
 static int manager_log(struct ast_cdr *cdr)
 {
-	time_t t;
-	struct tm timeresult;
+	struct ast_tm timeresult;
 	char strStartTime[80] = "";
 	char strAnswerTime[80] = "";
 	char strEndTime[80] = "";
@@ -114,19 +113,16 @@ static int manager_log(struct ast_cdr *cdr)
 	if (!enablecdr)
 		return 0;
 
-	t = cdr->start.tv_sec;
-	ast_localtime(&t, &timeresult, NULL);
-	strftime(strStartTime, sizeof(strStartTime), DATE_FORMAT, &timeresult);
+	ast_localtime(&cdr->start, &timeresult, NULL);
+	ast_strftime(strStartTime, sizeof(strStartTime), DATE_FORMAT, &timeresult);
 	
 	if (cdr->answer.tv_sec)	{
-    		t = cdr->answer.tv_sec;
-    		ast_localtime(&t, &timeresult, NULL);
-		strftime(strAnswerTime, sizeof(strAnswerTime), DATE_FORMAT, &timeresult);
+		ast_localtime(&cdr->answer, &timeresult, NULL);
+		ast_strftime(strAnswerTime, sizeof(strAnswerTime), DATE_FORMAT, &timeresult);
 	}
 
-	t = cdr->end.tv_sec;
-	ast_localtime(&t, &timeresult, NULL);
-	strftime(strEndTime, sizeof(strEndTime), DATE_FORMAT, &timeresult);
+	ast_localtime(&cdr->end, &timeresult, NULL);
+	ast_strftime(strEndTime, sizeof(strEndTime), DATE_FORMAT, &timeresult);
 
 	/* Custom fields handling */
 	memset(buf, 0 , sizeof(buf));
diff --git a/cdr/cdr_odbc.c b/cdr/cdr_odbc.c
index d1aa1f2d17c561c8b855fef8b6a9406eb1cd72a9..6800027844435b3805fa84ee1b509c53ec37b6b9 100644
--- a/cdr/cdr_odbc.c
+++ b/cdr/cdr_odbc.c
@@ -95,15 +95,12 @@ static int odbc_log(struct ast_cdr *cdr)
 	int ODBC_res;
 	char sqlcmd[2048] = "", timestr[128];
 	int res = 0;
-	struct tm tm;
+	struct ast_tm tm;
 
-	if (usegmtime) 
-		gmtime_r(&cdr->start.tv_sec,&tm);
-	else
-		ast_localtime(&cdr->start.tv_sec, &tm, NULL);
+	ast_localtime(&cdr->start, &tm, usegmtime ? "GMT" : NULL);
 
 	ast_mutex_lock(&odbc_lock);
-	strftime(timestr, sizeof(timestr), DATE_FORMAT, &tm);
+	ast_strftime(timestr, sizeof(timestr), DATE_FORMAT, &tm);
 	memset(sqlcmd,0,2048);
 	if (loguniqueid) {
 		snprintf(sqlcmd,sizeof(sqlcmd),"INSERT INTO %s "
diff --git a/cdr/cdr_pgsql.c b/cdr/cdr_pgsql.c
index 2379d772811ac0402c09e870b1aa841f00cc310d..12d9778c250003652bebbc5aa5fc504cf298f95a 100644
--- a/cdr/cdr_pgsql.c
+++ b/cdr/cdr_pgsql.c
@@ -71,15 +71,15 @@ static PGconn	*conn = NULL;
 
 static int pgsql_log(struct ast_cdr *cdr)
 {
-	struct tm tm;
+	struct ast_tm tm;
 	char sqlcmd[2048] = "", timestr[128];
 	char *pgerror;
 	PGresult *result;
 
 	ast_mutex_lock(&pgsql_lock);
 
-	ast_localtime(&cdr->start.tv_sec, &tm, NULL);
-	strftime(timestr, sizeof(timestr), DATE_FORMAT, &tm);
+	ast_localtime(&cdr->start, &tm, NULL);
+	ast_strftime(timestr, sizeof(timestr), DATE_FORMAT, &tm);
 
 	if ((!connected) && pghostname && pgdbuser && pgpassword && pgdbname) {
 		conn = PQsetdbLogin(pghostname, pgdbport, NULL, NULL, pgdbname, pgdbuser, pgpassword);
diff --git a/cdr/cdr_radius.c b/cdr/cdr_radius.c
index c41d680ddba01408cf22fd4e96079550ad71d91f..b344c0b26272a22c0eb1b1291b7286a534f1284a 100644
--- a/cdr/cdr_radius.c
+++ b/cdr/cdr_radius.c
@@ -98,7 +98,7 @@ static rc_handle *rh = NULL;
 static int build_radius_record(VALUE_PAIR **send, struct ast_cdr *cdr)
 {
 	int recordtype = PW_STATUS_STOP;
-	struct tm tm;
+	struct ast_tm tm;
 	char timestr[128];
 	char *tmp;
 
@@ -143,29 +143,23 @@ static int build_radius_record(VALUE_PAIR **send, struct ast_cdr *cdr)
 
 
 	/* Start Time */
-	if (ast_test_flag(&global_flags, RADIUS_FLAG_USEGMTIME))
-		gmtime_r(&(cdr->start.tv_sec), &tm);
-	else
-		ast_localtime(&(cdr->start.tv_sec), &tm, NULL);
-	strftime(timestr, sizeof(timestr), DATE_FORMAT, &tm);
+	ast_strftime(timestr, sizeof(timestr), DATE_FORMAT, 
+		ast_localtime(&cdr->start, &tm,
+			ast_test_flag(&global_flags, RADIUS_FLAG_USEGMTIME) ? "GMT" : NULL));
 	if (!rc_avpair_add(rh, send, PW_AST_START_TIME, timestr, strlen(timestr), VENDOR_CODE))
 		return -1;
 
 	/* Answer Time */
-	if (ast_test_flag(&global_flags, RADIUS_FLAG_USEGMTIME))
-		gmtime_r(&(cdr->answer.tv_sec), &tm);
-	else
-		ast_localtime(&(cdr->answer.tv_sec), &tm, NULL);
-	strftime(timestr, sizeof(timestr), DATE_FORMAT, &tm);
+	ast_strftime(timestr, sizeof(timestr), DATE_FORMAT, 
+		ast_localtime(&cdr->answer, &tm,
+			ast_test_flag(&global_flags, RADIUS_FLAG_USEGMTIME) ? "GMT" : NULL));
 	if (!rc_avpair_add(rh, send, PW_AST_ANSWER_TIME, timestr, strlen(timestr), VENDOR_CODE))
 		return -1;
 
 	/* End Time */
-	if (ast_test_flag(&global_flags, RADIUS_FLAG_USEGMTIME))
-		gmtime_r(&(cdr->end.tv_sec), &tm);
-	else
-		ast_localtime(&(cdr->end.tv_sec), &tm, NULL);
-	strftime(timestr, sizeof(timestr), DATE_FORMAT, &tm);
+	ast_strftime(timestr, sizeof(timestr), DATE_FORMAT, 
+		ast_localtime(&cdr->end, &tm,
+			ast_test_flag(&global_flags, RADIUS_FLAG_USEGMTIME) ? "GMT" : NULL));
 	if (!rc_avpair_add(rh, send, PW_AST_END_TIME, timestr, strlen(timestr), VENDOR_CODE))
 		return -1;
 
diff --git a/cdr/cdr_sqlite.c b/cdr/cdr_sqlite.c
index 52010ad93beacf55bd73ea0e6f81ecd88b221443..2c526a60c3f0d23c854660490b5a147fc9b923b3 100644
--- a/cdr/cdr_sqlite.c
+++ b/cdr/cdr_sqlite.c
@@ -96,24 +96,20 @@ static int sqlite_log(struct ast_cdr *cdr)
 {
 	int res = 0;
 	char *zErr = 0;
-	struct tm tm;
-	time_t t;
+	struct ast_tm tm;
 	char startstr[80], answerstr[80], endstr[80];
 	int count;
 
 	ast_mutex_lock(&sqlite_lock);
 
-	t = cdr->start.tv_sec;
-	ast_localtime(&t, &tm, NULL);
-	strftime(startstr, sizeof(startstr), DATE_FORMAT, &tm);
+	ast_localtime(&cdr->start, &tm, NULL);
+	ast_strftime(startstr, sizeof(startstr), DATE_FORMAT, &tm);
 
-	t = cdr->answer.tv_sec;
-	ast_localtime(&t, &tm, NULL);
-	strftime(answerstr, sizeof(answerstr), DATE_FORMAT, &tm);
+	ast_localtime(&cdr->answer, &tm, NULL);
+	ast_strftime(answerstr, sizeof(answerstr), DATE_FORMAT, &tm);
 
-	t = cdr->end.tv_sec;
-	ast_localtime(&t, &tm, NULL);
-	strftime(endstr, sizeof(endstr), DATE_FORMAT, &tm);
+	ast_localtime(&cdr->end, &tm, NULL);
+	ast_strftime(endstr, sizeof(endstr), DATE_FORMAT, &tm);
 
 	for(count=0; count<5; count++) {
 		res = sqlite_exec_printf(db,
diff --git a/cdr/cdr_tds.c b/cdr/cdr_tds.c
index 451bf1440229d74d84b72f176d729f689691107f..2707a4b893ddb9146e98baf8993e230d3b68c6eb 100644
--- a/cdr/cdr_tds.c
+++ b/cdr/cdr_tds.c
@@ -278,16 +278,14 @@ static char *anti_injection(const char *str, int len)
 
 static void get_date(char *dateField, struct timeval tv)
 {
-	struct tm tm;
-	time_t t;
+	struct ast_tm tm;
 	char buf[80];
 
 	/* To make sure we have date variable if not insert null to SQL */
 	if (!ast_tvzero(tv))
 	{
-		t = tv.tv_sec;
-		ast_localtime(&t, &tm, NULL);
-		strftime(buf, 80, DATE_FORMAT, &tm);
+		ast_localtime(&tv, &tm, NULL);
+		ast_strftime(buf, 80, DATE_FORMAT, &tm);
 		sprintf(dateField, "'%s'", buf);
 	}
 	else
diff --git a/channels/chan_agent.c b/channels/chan_agent.c
index d1231556e86451d0f58eb59a086a92996cefa0db..5590567ec9443315bf4bb8e215aa901f153b7f02 100644
--- a/channels/chan_agent.c
+++ b/channels/chan_agent.c
@@ -1812,7 +1812,7 @@ static int login_exec(struct ast_channel *chan, void *data)
 				login_state = 1; /* Successful Login */
 
 				/* Ensure we can't be gotten until we're done */
-				gettimeofday(&p->lastdisc, NULL);
+				p->lastdisc = ast_tvnow();
 				p->lastdisc.tv_sec++;
 
 				/* Set Channel Specific Agent Overrides */
diff --git a/channels/chan_iax2.c b/channels/chan_iax2.c
index 2ed8a3692f0217390241c2c1eb5924f61d3b23c5..941695a135dec648b1cb814469c10d380d970bd4 100644
--- a/channels/chan_iax2.c
+++ b/channels/chan_iax2.c
@@ -1274,7 +1274,7 @@ static int make_trunk(unsigned short callno, int locked)
 {
 	int x;
 	int res= 0;
-	struct timeval now;
+	struct timeval now = ast_tvnow();
 	if (iaxs[callno]->oseqno) {
 		ast_log(LOG_WARNING, "Can't make trunk once a call has started!\n");
 		return -1;
@@ -1283,7 +1283,6 @@ static int make_trunk(unsigned short callno, int locked)
 		ast_log(LOG_WARNING, "Call %d is already a trunk\n", callno);
 		return -1;
 	}
-	gettimeofday(&now, NULL);
 	for (x=TRUNK_CALL_START;x<IAX_MAX_CALLS - 1; x++) {
 		ast_mutex_lock(&iaxsl[x]);
 		if (!iaxs[x] && ((now.tv_sec - lastused[x].tv_sec) > MIN_REUSE_TIME)) {
@@ -1369,7 +1368,7 @@ static int find_callno(unsigned short callno, unsigned short dcallno, struct soc
 		 * correct, but it will be changed if needed after authentication. */
 		if (!iax2_getpeername(*sin, host, sizeof(host), lockpeer))
 			snprintf(host, sizeof(host), "%s:%d", ast_inet_ntoa(sin->sin_addr), ntohs(sin->sin_port));
-		gettimeofday(&now, NULL);
+		now = ast_tvnow();
 		for (x=1;x<TRUNK_CALL_START;x++) {
 			/* Find first unused call number that hasn't been used in a while */
 			ast_mutex_lock(&iaxsl[x]);
@@ -1872,7 +1871,7 @@ static void iax2_destroy(int callno)
 
 retry:
 	pvt = iaxs[callno];
-	gettimeofday(&lastused[callno], NULL);
+	lastused[callno] = ast_tvnow();
 	
 	owner = pvt ? pvt->owner : NULL;
 
@@ -2274,9 +2273,7 @@ static int iax2_show_cache(int fd, int argc, char *argv[])
 	struct iax2_dpcache *dp = NULL;
 	char tmp[1024], *pc = NULL;
 	int s, x, y;
-	struct timeval tv;
-
-	gettimeofday(&tv, NULL);
+	struct timeval tv = ast_tvnow();
 
 	AST_LIST_LOCK(&dpcache);
 
@@ -2387,7 +2384,7 @@ static void __get_from_jb(void *p)
 	int ret;
 	long now;
 	long next;
-	struct timeval tv;
+	struct timeval tv = ast_tvnow();
 	
 	/* Make sure we have a valid private structure before going on */
 	ast_mutex_lock(&iaxsl[callno]);
@@ -2400,7 +2397,6 @@ static void __get_from_jb(void *p)
 
 	pvt->jbid = -1;
 	
-	gettimeofday(&tv,NULL);
 	/* round up a millisecond since ast_sched_runq does; */
 	/* prevents us from spinning while waiting for our now */
 	/* to catch up with runq's now */
@@ -2882,14 +2878,10 @@ static int auto_congest(void *data)
 
 static unsigned int iax2_datetime(const char *tz)
 {
-	time_t t;
-	struct tm tm;
+	struct timeval t = ast_tvnow();
+	struct ast_tm tm;
 	unsigned int tmp;
-	time(&t);
-	if (!ast_strlen_zero(tz))
-		ast_localtime(&t, &tm, tz);
-	else
-		ast_localtime(&t, &tm, NULL);
+	ast_localtime(&t, &tm, ast_strlen_zero(tz) ? NULL : tz);
 	tmp  = (tm.tm_sec >> 1) & 0x1f;			/* 5 bits of seconds */
 	tmp |= (tm.tm_min & 0x3f) << 5;			/* 6 bits of minutes */
 	tmp |= (tm.tm_hour & 0x1f) << 11;		/* 5 bits of hours */
@@ -3322,7 +3314,7 @@ static enum ast_bridge_result iax2_bridge(struct ast_channel *c0, struct ast_cha
 		}
 		if ((iaxs[callno0]->transferring == TRANSFER_RELEASED) && (iaxs[callno1]->transferring == TRANSFER_RELEASED)) {
 			/* Call has been transferred.  We're no longer involved */
-			gettimeofday(&tv, NULL);
+			tv = ast_tvnow();
 			if (ast_tvzero(waittimer)) {
 				waittimer = tv;
 			} else if (tv.tv_sec - waittimer.tv_sec > IAX_LINGER_TIMEOUT) {
@@ -3586,7 +3578,7 @@ static unsigned int fix_peerts(struct timeval *tv, int callno, unsigned int ts)
 	long ms;	/* NOT unsigned */
 	if (ast_tvzero(iaxs[callno]->rxcore)) {
 		/* Initialize rxcore time if appropriate */
-		gettimeofday(&iaxs[callno]->rxcore, NULL);
+		iaxs[callno]->rxcore = ast_tvnow();
 		/* Round to nearest 20ms so traces look pretty */
 		iaxs[callno]->rxcore.tv_usec -= iaxs[callno]->rxcore.tv_usec % 20000;
 	}
@@ -3622,7 +3614,7 @@ static unsigned int calc_timestamp(struct chan_iax2_pvt *p, unsigned int ts, str
 		}
 	}
 	if (ast_tvzero(p->offset)) {
-		gettimeofday(&p->offset, NULL);
+		p->offset = ast_tvnow();
 		/* Round to nearest 20ms for nice looking traces */
 		p->offset.tv_usec -= p->offset.tv_usec % 20000;
 	}
@@ -3850,7 +3842,7 @@ static int iax2_trunk_queue(struct chan_iax2_pvt *pvt, struct iax_frame *fr)
 
 		/* if we have enough for a full MTU, ship it now without waiting */
 		if (global_max_trunk_mtu > 0 && tpeer->trunkdatalen + f->datalen + 4 >= global_max_trunk_mtu) {
-			gettimeofday(&now, NULL);
+			now = ast_tvnow();
 			res = send_trunk(tpeer, &now); 
 			trunk_untimed ++; 
 		}
@@ -6243,10 +6235,9 @@ static int timing_read(int *id, int fd, short events, void *cbdata)
 #ifdef ZT_TIMERACK
 	int x = 1;
 #endif
-	struct timeval now;
+	struct timeval now = ast_tvnow();
 	if (iaxtrunkdebug)
 		ast_verbose("Beginning trunk processing. Trunk queue ceiling is %d bytes per host\n", trunkmaxsize);
-	gettimeofday(&now, NULL);
 	if (events & AST_IO_PRI) {
 #ifdef ZT_TIMERACK
 		/* Great, this is a timing interface, just call the ioctl */
@@ -9898,13 +9889,11 @@ static int cache_get_callno_locked(const char *data)
 static struct iax2_dpcache *find_cache(struct ast_channel *chan, const char *data, const char *context, const char *exten, int priority)
 {
 	struct iax2_dpcache *dp = NULL;
-	struct timeval tv;
+	struct timeval tv = ast_tvnow();
 	int x, com[2], timeout, old = 0, outfd, abort, callno;
 	struct ast_channel *c = NULL;
 	struct ast_frame *f = NULL;
 
-	gettimeofday(&tv, NULL);
-
 	AST_LIST_TRAVERSE_SAFE_BEGIN(&dpcache, dp, cache_list) {
 		if (ast_tvcmp(tv, dp->expiry) > 0) {
 			AST_LIST_REMOVE_CURRENT(&dpcache, cache_list);
@@ -9932,7 +9921,7 @@ static struct iax2_dpcache *find_cache(struct ast_channel *chan, const char *dat
 		}
 		ast_copy_string(dp->peercontext, data, sizeof(dp->peercontext));
 		ast_copy_string(dp->exten, exten, sizeof(dp->exten));
-		gettimeofday(&dp->expiry, NULL);
+		dp->expiry = ast_tvnow();
 		dp->orig = dp->expiry;
 		/* Expires in 30 mins by default */
 		dp->expiry.tv_sec += iaxdefaultdpcache;
diff --git a/channels/chan_mgcp.c b/channels/chan_mgcp.c
index c55f389766d19470d473f2d3db0cce965bc8d6eb..76744cc109be75578387ae75faa1583ba6a2fbdb 100644
--- a/channels/chan_mgcp.c
+++ b/channels/chan_mgcp.c
@@ -736,15 +736,11 @@ static int mgcp_postrequest(struct mgcp_endpoint *p, struct mgcp_subchannel *sub
 		gw->msgs = msg;
 	}
 
-	if (gettimeofday(&tv, NULL) < 0) {
-		/* This shouldn't ever happen, but let's be sure */
-		ast_log(LOG_NOTICE, "gettimeofday() failed!\n");
-	} else {
-		msg->expire = tv.tv_sec * 1000 + tv.tv_usec / 1000 + DEFAULT_RETRANS;
+	tv = ast_tvnow();
+	msg->expire = tv.tv_sec * 1000 + tv.tv_usec / 1000 + DEFAULT_RETRANS;
 
-		if (gw->retransid == -1)
-			gw->retransid = ast_sched_add(sched, DEFAULT_RETRANS, retrans_pkt, (void *)gw);
-	}
+	if (gw->retransid == -1)
+		gw->retransid = ast_sched_add(sched, DEFAULT_RETRANS, retrans_pkt, (void *)gw);
 	ast_mutex_unlock(&gw->msgs_lock);
 /* SC
 	if (!gw->messagepending) {
@@ -2232,11 +2228,10 @@ static int transmit_notify_request_with_callerid(struct mgcp_subchannel *sub, ch
 	struct mgcp_request resp;
 	char tone2[256];
 	char *l, *n;
-	time_t t;
-	struct tm tm;
+	struct timeval t = ast_tvnow();
+	struct ast_tm tm;
 	struct mgcp_endpoint *p = sub->parent;
 	
-	time(&t);
 	ast_localtime(&t, &tm, NULL);
 	n = callername;
 	l = callernum;
diff --git a/channels/chan_phone.c b/channels/chan_phone.c
index 4a4629009cfe77c9fc8df2fe8de42cfb6712077f..cf68d1d7fefde848039527e2018437e531733356 100644
--- a/channels/chan_phone.c
+++ b/channels/chan_phone.c
@@ -293,11 +293,10 @@ static int phone_call(struct ast_channel *ast, char *dest, int timeout)
 	struct phone_pvt *p;
 
 	PHONE_CID cid;
-	time_t UtcTime;
-	struct tm tm;
+	struct timeval UtcTime = ast_tvnow();
+	struct ast_tm tm;
 	int start;
 
-	time(&UtcTime);
 	ast_localtime(&UtcTime, &tm, NULL);
 
 	memset(&cid, 0, sizeof(PHONE_CID));
diff --git a/channels/chan_sip.c b/channels/chan_sip.c
index 83c6e7b363eaa98d7dc16da512f63705671ef02e..363b6673a212640f6e1965dfc92ab3e71b1e7f4f 100644
--- a/channels/chan_sip.c
+++ b/channels/chan_sip.c
@@ -1212,7 +1212,7 @@ struct sip_registry {
 	int refresh;			/*!< How often to refresh */
 	struct sip_pvt *call;		/*!< create a sip_pvt structure for each outbound "registration dialog" in progress */
 	enum sipregistrystate regstate;	/*!< Registration state (see above) */
-	time_t regtime;		/*!< Last successful registration time */
+	struct timeval regtime;		/*!< Last successful registration time */
 	int callid_valid;		/*!< 0 means we haven't chosen callid for this registry yet. */
 	unsigned int ocseq;		/*!< Sequence number we got to for REGISTERs for this registry */
 	struct sockaddr_in us;		/*!< Who the server thinks we are */
@@ -11042,7 +11042,7 @@ static int sip_show_registry(int fd, int argc, char *argv[])
 #define FORMAT  "%-30.30s  %-12.12s  %8d %-20.20s %-25.25s\n"
 	char host[80];
 	char tmpdat[256];
-	struct tm tm;
+	struct ast_tm tm;
 	int counter = 0;
 
 	if (argc != 3)
@@ -11051,9 +11051,9 @@ static int sip_show_registry(int fd, int argc, char *argv[])
 	ASTOBJ_CONTAINER_TRAVERSE(&regl, 1, do {
 		ASTOBJ_RDLOCK(iterator);
 		snprintf(host, sizeof(host), "%s:%d", iterator->hostname, iterator->portno ? iterator->portno : STANDARD_SIP_PORT);
-		if (iterator->regtime) {
+		if (iterator->regtime.tv_sec) {
 			ast_localtime(&iterator->regtime, &tm, NULL);
-			strftime(tmpdat, sizeof(tmpdat), "%a, %d %b %Y %T", &tm);
+			ast_strftime(tmpdat, sizeof(tmpdat), "%a, %d %b %Y %T", &tm);
 		} else 
 			tmpdat[0] = '\0';
 		ast_cli(fd, FORMAT, host, iterator->username, iterator->refresh, regstate2str(iterator->regstate), tmpdat);
@@ -12975,7 +12975,7 @@ static int handle_response_register(struct sip_pvt *p, int resp, char *rest, str
 		}
 
 		r->regstate = REG_STATE_REGISTERED;
-		r->regtime = time(NULL);		/* Reset time of last succesful registration */
+		r->regtime = ast_tvnow();		/* Reset time of last succesful registration */
 		manager_event(EVENT_FLAG_SYSTEM, "Registry", "ChannelType: SIP\r\nDomain: %s\r\nStatus: %s\r\n", r->hostname, regstate2str(r->regstate));
 		r->regattempts = 0;
 		ast_debug(1, "Registration successful\n");
@@ -16177,7 +16177,7 @@ static int sip_poke_peer(struct sip_peer *peer)
 #else
 	xmitres = transmit_invite(p, SIP_OPTIONS, 0, 2);
 #endif
-	gettimeofday(&peer->ps, NULL);
+	peer->ps = ast_tvnow();
 	if (xmitres == XMIT_ERROR)
 		sip_poke_noanswer(peer);	/* Immediately unreachable, network problems */
 	else
diff --git a/channels/chan_skinny.c b/channels/chan_skinny.c
index f55002d641a3c98535570225ba8242705106a711..31b5a58446be10b25f15d223393b3fec5f82087f 100644
--- a/channels/chan_skinny.c
+++ b/channels/chan_skinny.c
@@ -3735,21 +3735,21 @@ static int handle_line_state_req_message(struct skinny_req *req, struct skinnyse
 
 static int handle_time_date_req_message(struct skinny_req *req, struct skinnysession *s)
 {
-	time_t timer;
-	struct tm *cmtime;
+	struct timeval tv = ast_tvnow();
+	struct ast_tm cmtime;
 
 	if (!(req = req_alloc(sizeof(struct definetimedate_message), DEFINETIMEDATE_MESSAGE)))
 		return -1;
 
-	timer = time(NULL);
-	cmtime = localtime(&timer);
-	req->data.definetimedate.year = htolel(cmtime->tm_year+1900);
-	req->data.definetimedate.month = htolel(cmtime->tm_mon+1);
-	req->data.definetimedate.dayofweek = htolel(cmtime->tm_wday);
-	req->data.definetimedate.day = htolel(cmtime->tm_mday);
-	req->data.definetimedate.hour = htolel(cmtime->tm_hour);
-	req->data.definetimedate.minute = htolel(cmtime->tm_min);
-	req->data.definetimedate.seconds = htolel(cmtime->tm_sec);
+	ast_localtime(&tv, &cmtime, NULL);
+	req->data.definetimedate.year = htolel(cmtime.tm_year+1900);
+	req->data.definetimedate.month = htolel(cmtime.tm_mon+1);
+	req->data.definetimedate.dayofweek = htolel(cmtime.tm_wday);
+	req->data.definetimedate.day = htolel(cmtime.tm_mday);
+	req->data.definetimedate.hour = htolel(cmtime.tm_hour);
+	req->data.definetimedate.minute = htolel(cmtime.tm_min);
+	req->data.definetimedate.seconds = htolel(cmtime.tm_sec);
+	req->data.definetimedate.milliseconds = htolel(cmtime.tm_usec / 1000);
 	transmit_response(s, req);
 	return 1;
 }
diff --git a/channels/chan_zap.c b/channels/chan_zap.c
index c8b4feb768f1fea5248cb3cdb56e9ce0391c7440..0e52a7b2dc19a973c70c9c447b9e25b5ba2838a1 100644
--- a/channels/chan_zap.c
+++ b/channels/chan_zap.c
@@ -3059,7 +3059,7 @@ static int zt_answer(struct ast_channel *ast)
 		/* Pick up the line */
 		ast_debug(1, "Took %s off hook\n", ast->name);
 		if (p->hanguponpolarityswitch) {
-			gettimeofday(&p->polaritydelaytv, NULL);
+			p->polaritydelaytv = ast_tvnow();
 		}
 		res = zt_set_hook(p->subs[SUB_REAL].zfd, ZT_OFFHOOK);
 		tone_zone_play_tone(p->subs[index].zfd, -1);
@@ -4463,7 +4463,7 @@ static struct ast_frame *zt_handle_event(struct ast_channel *ast)
 				break;
 			}
 			/* Remember last time we got a flash-hook */
-			gettimeofday(&p->flashtime, NULL);
+			p->flashtime = ast_tvnow();
 			switch (mysig) {
 			case SIG_FXOLS:
 			case SIG_FXOGS:
@@ -4733,7 +4733,7 @@ static struct ast_frame *zt_handle_event(struct ast_channel *ast)
 					ast_debug(1, "Answering on polarity switch!\n");
 					ast_setstate(p->owner, AST_STATE_UP);
 					if (p->hanguponpolarityswitch) {
-						gettimeofday(&p->polaritydelaytv, NULL);
+						p->polaritydelaytv = ast_tvnow();
 					}
 				} else
 					ast_debug(1, "Ignore switch to REVERSED Polarity on channel %d, state %d\n", p->channel, ast->_state);
@@ -4836,7 +4836,7 @@ static struct ast_frame *__zt_exception(struct ast_channel *ast)
 			/* Do nothing */
 			break;
 		case ZT_EVENT_WINKFLASH:
-			gettimeofday(&p->flashtime, NULL);
+			p->flashtime = ast_tvnow();
 			if (p->owner) {
 				if (option_verbose > 2) 
 					ast_verbose(VERBOSE_PREFIX_3 "Channel %d flashed to other channel %s\n", p->channel, p->owner->name);
@@ -8539,7 +8539,7 @@ static void *ss7_linkset(void *data)
 	while(1) {
 		ast_mutex_lock(&linkset->lock);
 		if ((next = ss7_schedule_next(ss7))) {
-			gettimeofday(&tv, NULL);
+			tv = ast_tvnow();
 			tv.tv_sec = next->tv_sec - tv.tv_sec;
 			tv.tv_usec = next->tv_usec - tv.tv_usec;
 			if (tv.tv_usec < 0) {
@@ -9302,7 +9302,7 @@ static void *pri_dchannel(void *vpri)
 	int nextidle = -1;
 	struct ast_channel *c;
 	struct timeval tv, lowest, *next;
-	struct timeval lastidle = { 0, 0 };
+	struct timeval lastidle = ast_tvnow();
 	int doidling=0;
 	char *cc;
 	char idlen[80];
@@ -9319,7 +9319,6 @@ static void *pri_dchannel(void *vpri)
 	char plancallingani[256];
 	char calledtonstr[10];
 	
-	gettimeofday(&lastidle, NULL);
 	if (!ast_strlen_zero(pri->idledial) && !ast_strlen_zero(pri->idleext)) {
 		/* Need to do idle dialing, check to be sure though */
 		cc = strchr(pri->idleext, '@');
@@ -9389,7 +9388,7 @@ static void *pri_dchannel(void *vpri)
 						}
 					} else
 						ast_log(LOG_WARNING, "Unable to request channel 'Zap/%s' for idle call\n", idlen);
-					gettimeofday(&lastidle, NULL);
+					lastidle = ast_tvnow();
 				}
 			} else if ((haveidles < pri->minunused) &&
 				   (activeidles > pri->minidle)) {
diff --git a/channels/iax2-parser.c b/channels/iax2-parser.c
index 7695051933deb496be0b55de8463e29586c26d36..1c52cca5908f0dc2602e84ddd524b503c774e9b2 100644
--- a/channels/iax2-parser.c
+++ b/channels/iax2-parser.c
@@ -140,7 +140,7 @@ static void dump_byte(char *output, int maxlen, void *value, int len)
 
 static void dump_datetime(char *output, int maxlen, void *value, int len)
 {
-	struct tm tm;
+	struct ast_tm tm;
 	unsigned long val = (unsigned long) ntohl(get_unaligned_uint32(value));
 	if (len == (int)sizeof(unsigned int)) {
 		tm.tm_sec  = (val & 0x1f) << 1;
@@ -149,7 +149,7 @@ static void dump_datetime(char *output, int maxlen, void *value, int len)
 		tm.tm_mday = (val >> 16) & 0x1f;
 		tm.tm_mon  = ((val >> 21) & 0x0f) - 1;
 		tm.tm_year = ((val >> 25) & 0x7f) + 100;
-		strftime(output, maxlen, "%Y-%m-%d  %T", &tm); 
+		ast_strftime(output, maxlen, "%Y-%m-%d  %T", &tm); 
 	} else
 		ast_copy_string(output, "Invalid DATETIME format!", maxlen);
 }
diff --git a/channels/misdn/ie.c b/channels/misdn/ie.c
index 4093805b5b463aa146f837784257bbebe8f606a4..e7133ca75daa0ad14268e8bd84668266dcc2e0a9 100644
--- a/channels/misdn/ie.c
+++ b/channels/misdn/ie.c
@@ -852,17 +852,11 @@ static void enc_ie_date(unsigned char **ntmode, msg_t *msg, time_t ti, int nt, s
 	unsigned char *p;
 	Q931_info_t *qi = (Q931_info_t *)(msg->data + mISDN_HEADER_LEN);
 	int l;
+	struct timeval tv = { ti, 0 };
+	struct ast_tm tm;
 
-	struct tm *tm;
-
-	tm = localtime(&ti);
-	if (!tm)
-	{
-		printf("%s: ERROR: gettimeofday() returned NULL.\n", __FUNCTION__);
-		return;
-	}
-
-	if (MISDN_IE_DEBG) printf("    year=%d month=%d day=%d hour=%d minute=%d\n", tm->tm_year%100, tm->tm_mon+1, tm->tm_mday, tm->tm_hour, tm->tm_min);
+	ast_localtime(&tv, &tm, NULL);
+	if (MISDN_IE_DEBG) printf("    year=%d month=%d day=%d hour=%d minute=%d\n", tm.tm_year%100, tm.tm_mon+1, tm.tm_mday, tm.tm_hour, tm.tm_min);
 
 	l = 5;
 	p = msg_put(msg, l+2);
@@ -872,11 +866,11 @@ static void enc_ie_date(unsigned char **ntmode, msg_t *msg, time_t ti, int nt, s
 		qi->QI_ELEMENT(date) = p - (unsigned char *)qi - sizeof(Q931_info_t);
 	p[0] = IE_DATE;
 	p[1] = l;
-	p[2] = tm->tm_year % 100;
-	p[3] = tm->tm_mon + 1;
-	p[4] = tm->tm_mday;
-	p[5] = tm->tm_hour;
-	p[6] = tm->tm_min;
+	p[2] = tm.tm_year % 100;
+	p[3] = tm.tm_mon + 1;
+	p[4] = tm.tm_mday;
+	p[5] = tm.tm_hour;
+	p[6] = tm.tm_min;
 }
 
 
diff --git a/funcs/func_strings.c b/funcs/func_strings.c
index 4619206ee11ed3b1e1751e4ab9ecf8b65a99d6dd..c44c49921f83f633d9dfd7fc9e3ffa4c7e9e8099 100644
--- a/funcs/func_strings.c
+++ b/funcs/func_strings.c
@@ -599,20 +599,20 @@ static int acf_strftime(struct ast_channel *chan, const char *cmd, char *parse,
 			     AST_APP_ARG(timezone);
 			     AST_APP_ARG(format);
 	);
-	time_t epochi;
-	struct tm tm;
+	struct timeval tv;
+	struct ast_tm tm;
 
 	buf[0] = '\0';
 
 	AST_STANDARD_APP_ARGS(args, parse);
 
-	ast_get_time_t(args.epoch, &epochi, time(NULL), NULL);
-	ast_localtime(&epochi, &tm, args.timezone);
+	ast_get_timeval(args.epoch, &tv, ast_tvnow(), NULL);
+	ast_localtime(&tv, &tm, args.timezone);
 
 	if (!args.format)
 		args.format = "%c";
 
-	if (!strftime(buf, len, args.format, &tm))
+	if (ast_strftime(buf, len, args.format, &tm) <= 0)
 		ast_log(LOG_WARNING, "C function strftime() output nothing?!!\n");
 
 	buf[len - 1] = '\0';
@@ -624,6 +624,14 @@ static struct ast_custom_function strftime_function = {
 	.name = "STRFTIME",
 	.synopsis = "Returns the current date/time in a specified format.",
 	.syntax = "STRFTIME([<epoch>][|[timezone][|format]])",
+	.desc =
+"STRFTIME sports all of the same formats as the underlying C function\n"
+"strftime(3) - see the man page for details.  It also supports the\n"
+"following format:\n"
+" %[n]q - fractions of a second, with leading zeroes.  For example, %3q will\n"
+"         give milliseconds and %1q will give tenths of a second.  The default\n"
+"         is to output milliseconds (n=3).  The common case is to use it in\n"
+"         combination with %S, as in \"%S.%3q\".\n",
 	.read = acf_strftime,
 };
 
@@ -635,9 +643,10 @@ static int acf_strptime(struct ast_channel *chan, const char *cmd, char *data,
 			     AST_APP_ARG(timezone);
 			     AST_APP_ARG(format);
 	);
-	struct tm time;
-
-	memset(&time, 0, sizeof(struct tm));
+	union {
+		struct ast_tm atm;
+		struct tm time;
+	} t = { { 0, }, };
 
 	buf[0] = '\0';
 
@@ -655,10 +664,10 @@ static int acf_strptime(struct ast_channel *chan, const char *cmd, char *data,
 		return -1;
 	}
 
-	if (!strptime(args.timestring, args.format, &time)) {
+	if (!strptime(args.timestring, args.format, &t.time)) {
 		ast_log(LOG_WARNING, "C function strptime() output nothing?!!\n");
 	} else {
-		snprintf(buf, len, "%d", (int) ast_mktime(&time, args.timezone));
+		snprintf(buf, len, "%d", (int) ast_mktime(&t.atm, args.timezone));
 	}
 
 	return 0;
diff --git a/funcs/func_timeout.c b/funcs/func_timeout.c
index 1da11e54a60066e04314bef9d937fa0b5a52648d..ec730c81985c7c72bca75bc99b42d3d9d6e76f3e 100644
--- a/funcs/func_timeout.c
+++ b/funcs/func_timeout.c
@@ -92,7 +92,7 @@ static int timeout_write(struct ast_channel *chan, const char *cmd, char *data,
 {
 	int x;
 	char timestr[64];
-	struct tm myt;
+	struct ast_tm myt;
 
 	if (!chan)
 		return -1;
@@ -113,8 +113,9 @@ static int timeout_write(struct ast_channel *chan, const char *cmd, char *data,
 		ast_channel_setwhentohangup(chan, x);
 		if (option_verbose > 2) {
 			if (chan->whentohangup) {
-				strftime(timestr, sizeof(timestr), "%Y-%m-%d %H:%M:%S UTC",
-					 gmtime_r(&chan->whentohangup, &myt));
+				struct timeval tv = { chan->whentohangup, 0 };
+				ast_strftime(timestr, sizeof(timestr), "%Y-%m-%d %H:%M:%S.%3q %Z",
+					ast_localtime(&tv, &myt, NULL));
 				ast_verbose(VERBOSE_PREFIX_3 "Channel will hangup at %s.\n",
 					    timestr);
 			} else {
diff --git a/include/asterisk/localtime.h b/include/asterisk/localtime.h
index e03c8b5b46b9f8d2e8ecbaf577a509b4f1c3f7af..cf2256ee65ff9935b8820edc80d814e4b5bc106d 100644
--- a/include/asterisk/localtime.h
+++ b/include/asterisk/localtime.h
@@ -24,11 +24,27 @@
 #ifndef _ASTERISK_LOCALTIME_H
 #define _ASTERISK_LOCALTIME_H
 
+struct ast_tm {
+	int tm_sec;             /* Seconds. [0-60] (1 leap second) */
+	int tm_min;             /* Minutes. [0-59] */
+	int tm_hour;            /* Hours.   [0-23] */
+	int tm_mday;            /* Day.     [1-31] */
+	int tm_mon;             /* Month.   [0-11] */
+	int tm_year;            /* Year - 1900.  */
+	int tm_wday;            /* Day of week. [0-6] */
+	int tm_yday;            /* Days in year.[0-365]	*/
+	int tm_isdst;           /* DST.		[-1/0/1]*/
+	long int tm_gmtoff;     /* Seconds east of UTC.  */
+	char *tm_zone;          /* Timezone abbreviation.  */
+	/* NOTE: do NOT reorder this final item.  The order needs to remain compatible with struct tm */
+	int tm_usec;        /* microseconds */
+};
+
 int ast_tzsetwall(void);
 void ast_tzset(const char *name);
-struct tm *ast_localtime(const time_t *timep, struct tm *p_tm, const char *zone);
-time_t ast_mktime(struct tm * const tmp, const char *zone);
-char *ast_ctime(const time_t * const timep);
-char *ast_ctime_r(const time_t * const timep, char *buf);
+struct ast_tm *ast_localtime(const struct timeval *timep, struct ast_tm *p_tm, const char *zone);
+time_t ast_mktime(struct ast_tm * const tmp, const char *zone);
+char *ast_ctime(const struct timeval * const timep, char *buf);
+int ast_strftime(char *buf, size_t len, const char *format, const struct ast_tm *tm);
 
 #endif /* _ASTERISK_LOCALTIME_H */
diff --git a/include/asterisk/options.h b/include/asterisk/options.h
index a47c81ea81b72ed0d9de6be6cd321e1a761b3369..d6541fe639d2feb466e3fe820c35240541c48b6f 100644
--- a/include/asterisk/options.h
+++ b/include/asterisk/options.h
@@ -116,8 +116,8 @@ extern long option_minmemfree;		/*!< Minimum amount of free system memory - stop
 #endif
 extern char defaultlanguage[];
 
-extern time_t ast_startuptime;
-extern time_t ast_lastreloadtime;
+extern struct timeval ast_startuptime;
+extern struct timeval ast_lastreloadtime;
 extern pid_t ast_mainpid;
 
 extern char record_cache_dir[AST_CACHE_DIR_LEN];
diff --git a/include/asterisk/strings.h b/include/asterisk/strings.h
index 1b8733f50daa935ec72c1892d8ea3283d9f6b250..8b0daab87bddf4a004f5d10e857c578372e709f2 100644
--- a/include/asterisk/strings.h
+++ b/include/asterisk/strings.h
@@ -252,6 +252,16 @@ void ast_join(char *s, size_t len, char * const w[]);
 */
 int ast_get_time_t(const char *src, time_t *dst, time_t _default, int *consumed);
 
+/*
+  \brief Parse a time (float) string.
+  \param src String to parse
+  \param dst Destination
+  \param _default Value to use if the string does not contain a valid time
+  \param consumed The number of characters 'consumed' in the string by the parse (see 'man sscanf' for details)
+  \return zero on success, non-zero on failure
+*/
+int ast_get_timeval(const char *src, struct timeval *tv, struct timeval _default, int *consumed);
+
 /*!
  * Support for dynamic strings.
  *
diff --git a/main/asterisk.c b/main/asterisk.c
index eb5654ab48e347194030897882665b7bc159936a..56c5889857d9cd527c89dea76937027d5d2aab5e 100644
--- a/main/asterisk.c
+++ b/main/asterisk.c
@@ -193,8 +193,8 @@ struct ast_atexit {
 
 static AST_RWLIST_HEAD_STATIC(atexits, ast_atexit);
 
-time_t ast_startuptime;
-time_t ast_lastreloadtime;
+struct timeval ast_startuptime;
+struct timeval ast_lastreloadtime;
 
 static History *el_hist;
 static EditLine *el;
@@ -346,7 +346,7 @@ void ast_unregister_thread(void *id)
 static int handle_show_settings(int fd, int argc, char *argv[])
 {
 	char buf[BUFSIZ];
-	struct tm tm;
+	struct ast_tm tm;
 
 	ast_cli(fd, "\nPBX Core settings\n");
 	ast_cli(fd, "-----------------\n");
@@ -366,11 +366,11 @@ static int handle_show_settings(int fd, int argc, char *argv[])
 	ast_cli(fd, "  Min Free Memory:             %ld MB\n", option_minmemfree);
 #endif
 	if (ast_localtime(&ast_startuptime, &tm, NULL)) {
-		strftime(buf, sizeof(buf), "%H:%M:%S", &tm);
+		ast_strftime(buf, sizeof(buf), "%H:%M:%S", &tm);
 		ast_cli(fd, "  Startup time:                %s\n", buf);
 	}
 	if (ast_localtime(&ast_lastreloadtime, &tm, NULL)) {
-		strftime(buf, sizeof(buf), "%H:%M:%S", &tm);
+		ast_strftime(buf, sizeof(buf), "%H:%M:%S", &tm);
 		ast_cli(fd, "  Last reload time:            %s\n", buf);
 	}
 	ast_cli(fd, "  System:                      %s/%s built by %s on %s %s\n", ast_build_os, ast_build_kernel, ast_build_user, ast_build_machine, ast_build_date);
@@ -1760,8 +1760,8 @@ static char *cli_prompt(EditLine *el)
 			if (*t == '%') {
 				char hostname[MAXHOSTNAMELEN]="";
 				int i;
-				time_t ts;
-				struct tm tm;
+				struct timeval ts = ast_tvnow();
+				struct ast_tm tm = { 0, };
 #ifdef linux
 				FILE *LOADAVG;
 #endif
@@ -1783,10 +1783,8 @@ static char *cli_prompt(EditLine *el)
 					color_used = ((fgcolor == COLOR_WHITE) && (bgcolor == COLOR_BLACK)) ? 0 : 1;
 					break;
 				case 'd': /* date */
-					memset(&tm, 0, sizeof(tm));
-					time(&ts);
 					if (ast_localtime(&ts, &tm, NULL))
-						strftime(p, sizeof(prompt) - strlen(prompt), "%Y-%m-%d", &tm);
+						ast_strftime(p, sizeof(prompt) - strlen(prompt), "%Y-%m-%d", &tm);
 					break;
 				case 'h': /* hostname */
 					if (!gethostname(hostname, sizeof(hostname) - 1))
@@ -1840,10 +1838,8 @@ static char *cli_prompt(EditLine *el)
 					strncat(p, ast_config_AST_SYSTEM_NAME, sizeof(prompt) - strlen(prompt) - 1);
 					break;
 				case 't': /* time */
-					memset(&tm, 0, sizeof(tm));
-					time(&ts);
 					if (ast_localtime(&ts, &tm, NULL))
-						strftime(p, sizeof(prompt) - strlen(prompt), "%H:%M:%S", &tm);
+						ast_strftime(p, sizeof(prompt) - strlen(prompt), "%H:%M:%S", &tm);
 					break;
 				case '#': /* process console or remote? */
 					if (!ast_opt_remote) 
@@ -2943,7 +2939,7 @@ int main(int argc, char *argv[])
 	__ast_mm_init();
 #endif	
 
-	time(&ast_startuptime);
+	ast_startuptime = ast_tvnow();
 	ast_cli_register_multiple(cli_asterisk, sizeof(cli_asterisk) / sizeof(struct ast_cli_entry));
 
 	if (ast_opt_console) {
diff --git a/main/callerid.c b/main/callerid.c
index 6102d40af2c9d533bdf8657549335008267149da..6e3cb95146890c2815f8661478032cf3642caede 100644
--- a/main/callerid.c
+++ b/main/callerid.c
@@ -684,15 +684,14 @@ void callerid_free(struct callerid_state *cid)
 
 static int callerid_genmsg(char *msg, int size, const char *number, const char *name, int flags)
 {
-	time_t t;
-	struct tm tm;
+	struct timeval tv = ast_tvnow();
+	struct ast_tm tm;
 	char *ptr;
 	int res;
 	int i, x;
 
 	/* Get the time */
-	time(&t);
-	ast_localtime(&t, &tm, NULL);
+	ast_localtime(&tv, &tm, NULL);
 	
 	ptr = msg;
 	
diff --git a/main/cdr.c b/main/cdr.c
index e5c96dbec07af7b7e6a31984244a2ef5db43b512..9a61e0a0d4e31ec730ded424e4ca331bbb3674ba 100644
--- a/main/cdr.c
+++ b/main/cdr.c
@@ -206,13 +206,10 @@ static void cdr_get_tv(struct timeval tv, const char *fmt, char *buf, int bufsiz
 	if (fmt == NULL) {	/* raw mode */
 		snprintf(buf, bufsize, "%ld.%06ld", (long)tv.tv_sec, (long)tv.tv_usec);
 	} else {  
-		time_t t = tv.tv_sec;
-		if (t) {
-			struct tm tm;
+		struct ast_tm tm;
 
-			ast_localtime(&t, &tm, NULL);
-			strftime(buf, bufsize, fmt, &tm);
-		}
+		ast_localtime(&tv, &tm, NULL);
+		ast_strftime(buf, bufsize, fmt, &tm);
 	}
 }
 
diff --git a/main/cli.c b/main/cli.c
index 7607793ae047e9ec58db7d352ad8868d68e5afee..235afd2f25301132db18dd5ee1988944f19de4d0 100644
--- a/main/cli.c
+++ b/main/cli.c
@@ -329,7 +329,7 @@ static int modlist_modentry(const char *module, const char *description, int use
 	return 0;
 }
 
-static void print_uptimestr(int fd, time_t timeval, const char *prefix, int printsec)
+static void print_uptimestr(int fd, struct timeval timeval, const char *prefix, int printsec)
 {
 	int x; /* the main part - years, weeks, etc. */
 	struct ast_str *out;
@@ -341,40 +341,40 @@ static void print_uptimestr(int fd, time_t timeval, const char *prefix, int prin
 #define WEEK (DAY*7)
 #define YEAR (DAY*365)
 #define NEEDCOMMA(x) ((x)? ",": "")	/* define if we need a comma */
-	if (timeval < 0)	/* invalid, nothing to show */
+	if (timeval.tv_sec < 0)	/* invalid, nothing to show */
 		return;
 
 	if (printsec)  {	/* plain seconds output */
-		ast_cli(fd, "%s: %lu\n", prefix, (u_long)timeval);
+		ast_cli(fd, "%s: %lu\n", prefix, (u_long)timeval.tv_sec);
 		return;
 	}
 	out = ast_str_alloca(256);
-	if (timeval > YEAR) {
-		x = (timeval / YEAR);
-		timeval -= (x * YEAR);
-		ast_str_append(&out, 0, "%d year%s%s ", x, ESS(x),NEEDCOMMA(timeval));
-	}
-	if (timeval > WEEK) {
-		x = (timeval / WEEK);
-		timeval -= (x * WEEK);
-		ast_str_append(&out, 0, "%d week%s%s ", x, ESS(x),NEEDCOMMA(timeval));
-	}
-	if (timeval > DAY) {
-		x = (timeval / DAY);
-		timeval -= (x * DAY);
-		ast_str_append(&out, 0, "%d day%s%s ", x, ESS(x),NEEDCOMMA(timeval));
-	}
-	if (timeval > HOUR) {
-		x = (timeval / HOUR);
-		timeval -= (x * HOUR);
-		ast_str_append(&out, 0, "%d hour%s%s ", x, ESS(x),NEEDCOMMA(timeval));
-	}
-	if (timeval > MINUTE) {
-		x = (timeval / MINUTE);
-		timeval -= (x * MINUTE);
-		ast_str_append(&out, 0, "%d minute%s%s ", x, ESS(x),NEEDCOMMA(timeval));
-	}
-	x = timeval;
+	if (timeval.tv_sec > YEAR) {
+		x = (timeval.tv_sec / YEAR);
+		timeval.tv_sec -= (x * YEAR);
+		ast_str_append(&out, 0, "%d year%s%s ", x, ESS(x),NEEDCOMMA(timeval.tv_sec));
+	}
+	if (timeval.tv_sec > WEEK) {
+		x = (timeval.tv_sec / WEEK);
+		timeval.tv_sec -= (x * WEEK);
+		ast_str_append(&out, 0, "%d week%s%s ", x, ESS(x),NEEDCOMMA(timeval.tv_sec));
+	}
+	if (timeval.tv_sec > DAY) {
+		x = (timeval.tv_sec / DAY);
+		timeval.tv_sec -= (x * DAY);
+		ast_str_append(&out, 0, "%d day%s%s ", x, ESS(x),NEEDCOMMA(timeval.tv_sec));
+	}
+	if (timeval.tv_sec > HOUR) {
+		x = (timeval.tv_sec / HOUR);
+		timeval.tv_sec -= (x * HOUR);
+		ast_str_append(&out, 0, "%d hour%s%s ", x, ESS(x),NEEDCOMMA(timeval.tv_sec));
+	}
+	if (timeval.tv_sec > MINUTE) {
+		x = (timeval.tv_sec / MINUTE);
+		timeval.tv_sec -= (x * MINUTE);
+		ast_str_append(&out, 0, "%d minute%s%s ", x, ESS(x),NEEDCOMMA(timeval.tv_sec));
+	}
+	x = timeval.tv_sec;
 	if (x > 0 || out->used == 0)	/* if there is nothing, print 0 seconds */
 		ast_str_append(&out, 0, "%d second%s ", x, ESS(x));
 	ast_cli(fd, "%s: %s\n", prefix, out->str);
@@ -382,7 +382,7 @@ static void print_uptimestr(int fd, time_t timeval, const char *prefix, int prin
 
 static char * handle_showuptime(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
 {
-	time_t curtime;
+	struct timeval curtime = ast_tvnow();
 	int printsec;
 
 	switch (cmd) {
@@ -404,11 +404,10 @@ static char * handle_showuptime(struct ast_cli_entry *e, int cmd, struct ast_cli
 		printsec = 0;
 	else
 		return CLI_SHOWUSAGE;
-	curtime = time(NULL);
-	if (ast_startuptime)
-		print_uptimestr(a->fd, curtime - ast_startuptime, "System uptime", printsec);
-	if (ast_lastreloadtime)
-		print_uptimestr(a->fd, curtime - ast_lastreloadtime, "Last reload", printsec);
+	if (ast_startuptime.tv_sec)
+		print_uptimestr(a->fd, ast_tvsub(curtime, ast_startuptime), "System uptime", printsec);
+	if (ast_lastreloadtime.tv_sec)
+		print_uptimestr(a->fd, ast_tvsub(curtime, ast_lastreloadtime), "Last reload", printsec);
 	return CLI_SUCCESS;
 }
 
diff --git a/main/http.c b/main/http.c
index d8471740cfcd8c54f2c8ed53229d43ecd059d039..b61858cdee6c047da94c3f65c27984124ef604d0 100644
--- a/main/http.c
+++ b/main/http.c
@@ -154,8 +154,9 @@ static struct ast_str *static_callback(struct server_instance *ser, const char *
 	struct stat st;
 	int len;
 	int fd;
-	time_t t;
+	struct timeval tv = ast_tvnow();
 	char buf[256];
+	struct ast_tm tm;
 
 	/* Yuck.  I'm not really sold on this, but if you don't deliver static content it makes your configuration 
 	   substantially more challenging, but this seems like a rather irritating feature creep on Asterisk. */
@@ -186,8 +187,7 @@ static struct ast_str *static_callback(struct server_instance *ser, const char *
 	if (fd < 0)
 		goto out403;
 
-	time(&t);
-	strftime(buf, sizeof(buf), "%a, %d %b %Y %H:%M:%S GMT", gmtime(&t));
+	ast_strftime(buf, sizeof(buf), "%a, %d %b %Y %H:%M:%S %Z", ast_localtime(&tv, &tm, "GMT"));
 	fprintf(ser->f, "HTTP/1.1 200 OK\r\n"
 		"Server: Asterisk/%s\r\n"
 		"Date: %s\r\n"
@@ -843,10 +843,11 @@ static void *httpd_helper_thread(void *data)
 		ast_variables_destroy(vars);
 
 	if (out) {
-		time_t t = time(NULL);
+		struct timeval tv = ast_tvnow();
 		char timebuf[256];
+		struct ast_tm tm;
 
-		strftime(timebuf, sizeof(timebuf), "%a, %d %b %Y %H:%M:%S GMT", gmtime(&t));
+		ast_strftime(timebuf, sizeof(timebuf), "%a, %d %b %Y %H:%M:%S %Z", ast_localtime(&tv, &tm, "GMT"));
 		fprintf(ser->f, "HTTP/1.1 %d %s\r\n"
 				"Server: Asterisk/%s\r\n"
 				"Date: %s\r\n"
diff --git a/main/loader.c b/main/loader.c
index 20227b59ebb24e5624d6f1b840261233a3c275c1..1100a460f81208156e36bc118a7589220fd288c0 100644
--- a/main/loader.c
+++ b/main/loader.c
@@ -551,7 +551,7 @@ int ast_module_reload(const char *name)
 		ast_verbose("The previous reload command didn't finish yet\n");
 		return -1;	/* reload already in progress */
 	}
-	ast_lastreloadtime = time(NULL);
+	ast_lastreloadtime = ast_tvnow();
 
 	/* Call "predefined" reload here first */
 	for (i = 0; reload_classes[i].name; i++) {
diff --git a/main/logger.c b/main/logger.c
index 1c7ef56a409752970ee2fd865972dc8fb00af352..6a35ac5fee1fcb6430ab16288a96b4e5c42961bf 100644
--- a/main/logger.c
+++ b/main/logger.c
@@ -866,8 +866,8 @@ void ast_log(int level, const char *file, int line, const char *function, const
 {
 	struct logmsg *logmsg = NULL;
 	struct ast_str *buf = NULL;
-	struct tm tm;
-	time_t t;
+	struct ast_tm tm;
+	struct timeval tv = ast_tvnow();
 	int res = 0;
 	va_list ap;
 
@@ -929,9 +929,8 @@ void ast_log(int level, const char *file, int line, const char *function, const
 	logmsg->type = LOGMSG_NORMAL;
 
 	/* Create our date/time */
-	time(&t);
-	ast_localtime(&t, &tm, NULL);
-	strftime(logmsg->date, sizeof(logmsg->date), dateformat, &tm);
+	ast_localtime(&tv, &tm, NULL);
+	ast_strftime(logmsg->date, sizeof(logmsg->date), dateformat, &tm);
 
 	/* Copy over data */
 	logmsg->level = level;
@@ -993,14 +992,14 @@ void ast_verbose(const char *fmt, ...)
 		return;
 
         if (ast_opt_timestamp) {
-                time_t t;
-                struct tm tm;
+                struct timeval tv;
+                struct ast_tm tm;
                 char date[40];
                 char *datefmt;
 
-                time(&t);
-                ast_localtime(&t, &tm, NULL);
-                strftime(date, sizeof(date), dateformat, &tm);
+				tv = ast_tvnow();
+                ast_localtime(&tv, &tm, NULL);
+                ast_strftime(date, sizeof(date), dateformat, &tm);
                 datefmt = alloca(strlen(date) + 3 + strlen(fmt) + 1);
                 sprintf(datefmt, "[%s] %s", date, fmt);
                 fmt = datefmt;
diff --git a/main/manager.c b/main/manager.c
index 53059479d1b67e2624ff1b15eb4eab29d266b945..43f825ff41ebcb4de653f5c4823eddfe535711a6 100644
--- a/main/manager.c
+++ b/main/manager.c
@@ -2288,15 +2288,15 @@ static int action_corestatus(struct mansession *s, const struct message *m)
 	char idText[150];
 	char startuptime[150];
 	char reloadtime[150];
-	struct tm tm;
+	struct ast_tm tm;
 
 	if (!ast_strlen_zero(actionid))
 		snprintf(idText, sizeof(idText), "ActionID: %s\r\n", actionid);
 
 	ast_localtime(&ast_startuptime, &tm, NULL);
-	strftime(startuptime, sizeof(startuptime), "%H:%M:%S", &tm);
+	ast_strftime(startuptime, sizeof(startuptime), "%H:%M:%S", &tm);
 	ast_localtime(&ast_lastreloadtime, &tm, NULL);
-	strftime(reloadtime, sizeof(reloadtime), "%H:%M:%S", &tm);
+	ast_strftime(reloadtime, sizeof(reloadtime), "%H:%M:%S", &tm);
 
 	astman_append(s, "Response: Success\r\n"
 			"%s"
diff --git a/main/pbx.c b/main/pbx.c
index 550add495f45f46ee773633bca4ddf1c5049e6c4..918fb5f581c029a8038c583032e075ca4009acdb 100644
--- a/main/pbx.c
+++ b/main/pbx.c
@@ -4279,10 +4279,10 @@ int ast_build_timing(struct ast_timing *i, const char *info_in)
 
 int ast_check_timing(const struct ast_timing *i)
 {
-	struct tm tm;
-	time_t t = time(NULL);
+	struct ast_tm tm;
+	struct timeval tv = ast_tvnow();
 
-	ast_localtime(&t, &tm, NULL);
+	ast_localtime(&tv, &tm, NULL);
 
 	/* If it's not the right month, return */
 	if (!(i->monthmask & (1 << tm.tm_mon)))
diff --git a/main/say.c b/main/say.c
index e401689f690609ee7a9f88043fb0d5a465e95241..85fa93abcd47c7b54b2c499b01370a010562cb23 100644
--- a/main/say.c
+++ b/main/say.c
@@ -2909,10 +2909,11 @@ static int say_date(struct ast_channel *chan, time_t t, const char *ints, const
 /* English syntax */
 int ast_say_date_en(struct ast_channel *chan, time_t t, const char *ints, const char *lang)
 {
-	struct tm tm;
+	struct ast_tm tm;
+	struct timeval tv = { t, 0 };
 	char fn[256];
 	int res = 0;
-	ast_localtime(&t,&tm,NULL);
+	ast_localtime(&tv, &tm, NULL);
 	if (!res) {
 		snprintf(fn, sizeof(fn), "digits/day-%d", tm.tm_wday);
 		res = ast_streamfile(chan, fn, lang);
@@ -2937,10 +2938,11 @@ int ast_say_date_en(struct ast_channel *chan, time_t t, const char *ints, const
 /* Danish syntax */
 int ast_say_date_da(struct ast_channel *chan, time_t t, const char *ints, const char *lang)
 {
-	struct tm tm;
+	struct timeval tv = { t, 0 };
+	struct ast_tm tm;
 	char fn[256];
 	int res = 0;
-	ast_localtime(&t,&tm,NULL);
+	ast_localtime(&tv, &tm, NULL);
 	if (!res) {
 		snprintf(fn, sizeof(fn), "digits/day-%d", tm.tm_wday);
 		res = ast_streamfile(chan, fn, lang);
@@ -2985,10 +2987,11 @@ int ast_say_date_da(struct ast_channel *chan, time_t t, const char *ints, const
 /* German syntax */
 int ast_say_date_de(struct ast_channel *chan, time_t t, const char *ints, const char *lang)
 {
-	struct tm tm;
+	struct timeval tv = { t, 0 };
+	struct ast_tm tm;
 	char fn[256];
 	int res = 0;
-	ast_localtime(&t,&tm,NULL);
+	ast_localtime(&tv, &tm, NULL);
 	if (!res) {
 		snprintf(fn, sizeof(fn), "digits/day-%d", tm.tm_wday);
 		res = ast_streamfile(chan, fn, lang);
@@ -3034,10 +3037,11 @@ int ast_say_date_de(struct ast_channel *chan, time_t t, const char *ints, const
 /* Hungarian syntax */
 int ast_say_date_hu(struct ast_channel *chan, time_t t, const char *ints, const char *lang)
 {
-	struct tm tm;
+	struct timeval tv = { t, 0 };
+	struct ast_tm tm;
 	char fn[256];
 	int res = 0;
-	ast_localtime(&t,&tm,NULL);
+	ast_localtime(&tv, &tm, NULL);
 
 	if (!res)
 		res = ast_say_number(chan, tm.tm_year + 1900, ints, lang, (char *) NULL);
@@ -3065,10 +3069,11 @@ int ast_say_date_hu(struct ast_channel *chan, time_t t, const char *ints, const
 /* French syntax */
 int ast_say_date_fr(struct ast_channel *chan, time_t t, const char *ints, const char *lang)
 {
-	struct tm tm;
+	struct timeval tv = { t, 0 };
+	struct ast_tm tm;
 	char fn[256];
 	int res = 0;
-	ast_localtime(&t,&tm,NULL);
+	ast_localtime(&tv, &tm, NULL);
 	if (!res) {
 		snprintf(fn, sizeof(fn), "digits/day-%d", tm.tm_wday);
 		res = ast_streamfile(chan, fn, lang);
@@ -3093,10 +3098,11 @@ int ast_say_date_fr(struct ast_channel *chan, time_t t, const char *ints, const
 /* Dutch syntax */
 int ast_say_date_nl(struct ast_channel *chan, time_t t, const char *ints, const char *lang)
 {
-	struct tm tm;
+	struct timeval tv = { t, 0 };
+	struct ast_tm tm;
 	char fn[256];
 	int res = 0;
-	ast_localtime(&t,&tm,NULL);
+	ast_localtime(&tv, &tm, NULL);
 	if (!res) {
 		snprintf(fn, sizeof(fn), "digits/day-%d", tm.tm_wday);
 		res = ast_streamfile(chan, fn, lang);
@@ -3121,10 +3127,11 @@ int ast_say_date_nl(struct ast_channel *chan, time_t t, const char *ints, const
 /* Thai syntax */
 int ast_say_date_th(struct ast_channel *chan, time_t t, const char *ints, const char *lang)
 {
-	struct tm tm;
+	struct timeval tv = { t, 0 };
+	struct ast_tm tm;
 	char fn[256];
 	int res = 0;
-	ast_localtime(&t,&tm,NULL);
+	ast_localtime(&tv, &tm, NULL);
 	if (!res) {
 		snprintf(fn, sizeof(fn), "digits/day-%d", tm.tm_wday);
 		res = ast_streamfile(chan, fn, lang);
@@ -3156,11 +3163,12 @@ int ast_say_date_th(struct ast_channel *chan, time_t t, const char *ints, const
 /* Portuguese syntax */
 int ast_say_date_pt(struct ast_channel *chan, time_t t, const char *ints, const char *lang)
 {
-	struct tm tm;
+	struct timeval tv = { t, 0 };
+	struct ast_tm tm;
 	char fn[256];
 	int res = 0;
 
-	ast_localtime(&t, &tm, NULL);
+	ast_localtime(&tv, &tm, NULL);
 	snprintf(fn, sizeof(fn), "digits/day-%d", tm.tm_wday);
 	if (!res)
 		res = wait_file(chan, ints, fn, lang);
@@ -3216,14 +3224,15 @@ static int say_date_with_format(struct ast_channel *chan, time_t time, const cha
 /* English syntax */
 int ast_say_date_with_format_en(struct ast_channel *chan, time_t time, const char *ints, const char *lang, const char *format, const char *timezone)
 {
-	struct tm tm;
+	struct timeval tv = { time, 0 };
+	struct ast_tm tm;
 	int res=0, offset, sndoffset;
 	char sndfile[256], nextmsg[256];
 
 	if (format == NULL)
 		format = "ABdY 'digits/at' IMp";
 
-	ast_localtime(&time,&tm,timezone);
+	ast_localtime(&tv, &tm, timezone);
 
 	for (offset=0 ; format[offset] != '\0' ; offset++) {
 		ast_debug(1, "Parsing %c (offset %d) in %s\n", format[offset], offset, format);
@@ -3351,16 +3360,15 @@ int ast_say_date_with_format_en(struct ast_channel *chan, time_t time, const cha
 				 * language to say the date, with changes in what you say, depending
 				 * upon how recent the date is. XXX */
 				{
-					struct timeval now;
-					struct tm tmnow;
-					time_t beg_today, tt;
+					struct timeval now = ast_tvnow();
+					struct ast_tm tmnow;
+					time_t beg_today;
 
 					gettimeofday(&now,NULL);
-					tt = now.tv_sec;
-					ast_localtime(&tt,&tmnow,timezone);
+					ast_localtime(&now, &tmnow, timezone);
 					/* This might be slightly off, if we transcend a leap second, but never more off than 1 second */
 					/* In any case, it saves not having to do ast_mktime() */
-					beg_today = tt - (tmnow.tm_hour * 3600) - (tmnow.tm_min * 60) - (tmnow.tm_sec);
+					beg_today = now.tv_sec - (tmnow.tm_hour * 3600) - (tmnow.tm_min * 60) - (tmnow.tm_sec);
 					if (beg_today < time) {
 						/* Today */
 						res = wait_file(chan,ints, "digits/today",lang);
@@ -3389,15 +3397,14 @@ int ast_say_date_with_format_en(struct ast_channel *chan, time_t time, const cha
 				 * upon how recent the date is. XXX */
 				{
 					struct timeval now;
-					struct tm tmnow;
-					time_t beg_today, tt;
+					struct ast_tm tmnow;
+					time_t beg_today;
 
-					gettimeofday(&now,NULL);
-					tt = now.tv_sec;
-					ast_localtime(&tt,&tmnow,timezone);
+					now = ast_tvnow();
+					ast_localtime(&now, &tmnow, timezone);
 					/* This might be slightly off, if we transcend a leap second, but never more off than 1 second */
 					/* In any case, it saves not having to do ast_mktime() */
-					beg_today = tt - (tmnow.tm_hour * 3600) - (tmnow.tm_min * 60) - (tmnow.tm_sec);
+					beg_today = now.tv_sec - (tmnow.tm_hour * 3600) - (tmnow.tm_min * 60) - (tmnow.tm_sec);
 					if (beg_today < time) {
 						/* Today */
 					} else if ((beg_today - 86400) < time) {
@@ -3458,14 +3465,15 @@ int ast_say_date_with_format_en(struct ast_channel *chan, time_t time, const cha
 /* Danish syntax */
 int ast_say_date_with_format_da(struct ast_channel *chan, time_t time, const char *ints, const char *lang, const char *format, const char *timezone)
 {
-	struct tm tm;
+	struct timeval tv = { time, 0 };
+	struct ast_tm tm;
 	int res=0, offset, sndoffset;
 	char sndfile[256], nextmsg[256];
 
 	if (!format)
 		format = "A dBY HMS";
 
-	ast_localtime(&time,&tm,timezone);
+	ast_localtime(&tv, &tm, timezone);
 
 	for (offset=0 ; format[offset] != '\0' ; offset++) {
 		ast_debug(1, "Parsing %c (offset %d) in %s\n", format[offset], offset, format);
@@ -3578,16 +3586,14 @@ int ast_say_date_with_format_da(struct ast_channel *chan, time_t time, const cha
 				 * language to say the date, with changes in what you say, depending
 				 * upon how recent the date is. XXX */
 				{
-					struct timeval now;
-					struct tm tmnow;
-					time_t beg_today, tt;
+					struct timeval now = ast_tvnow();
+					struct ast_tm tmnow;
+					time_t beg_today;
 
-					gettimeofday(&now,NULL);
-					tt = now.tv_sec;
-					ast_localtime(&tt,&tmnow,timezone);
+					ast_localtime(&now, &tmnow, timezone);
 					/* This might be slightly off, if we transcend a leap second, but never more off than 1 second */
 					/* In any case, it saves not having to do ast_mktime() */
-					beg_today = tt - (tmnow.tm_hour * 3600) - (tmnow.tm_min * 60) - (tmnow.tm_sec);
+					beg_today = now.tv_sec - (tmnow.tm_hour * 3600) - (tmnow.tm_min * 60) - (tmnow.tm_sec);
 					if (beg_today < time) {
 						/* Today */
 						res = wait_file(chan,ints, "digits/today",lang);
@@ -3605,16 +3611,14 @@ int ast_say_date_with_format_da(struct ast_channel *chan, time_t time, const cha
 				 * language to say the date, with changes in what you say, depending
 				 * upon how recent the date is. XXX */
 				{
-					struct timeval now;
-					struct tm tmnow;
-					time_t beg_today, tt;
+					struct timeval now = ast_tvnow();
+					struct ast_tm tmnow;
+					time_t beg_today;
 
-					gettimeofday(&now,NULL);
-					tt = now.tv_sec;
-					ast_localtime(&tt,&tmnow,timezone);
+					ast_localtime(&now, &tmnow, timezone);
 					/* This might be slightly off, if we transcend a leap second, but never more off than 1 second */
 					/* In any case, it saves not having to do ast_mktime() */
-					beg_today = tt - (tmnow.tm_hour * 3600) - (tmnow.tm_min * 60) - (tmnow.tm_sec);
+					beg_today = now.tv_sec - (tmnow.tm_hour * 3600) - (tmnow.tm_min * 60) - (tmnow.tm_sec);
 					if (beg_today < time) {
 						/* Today */
 					} else if ((beg_today - 86400) < time) {
@@ -3663,14 +3667,15 @@ int ast_say_date_with_format_da(struct ast_channel *chan, time_t time, const cha
 /* German syntax */
 int ast_say_date_with_format_de(struct ast_channel *chan, time_t time, const char *ints, const char *lang, const char *format, const char *timezone)
 {
-	struct tm tm;
+	struct timeval tv = { time, 0 };
+	struct ast_tm tm;
 	int res=0, offset, sndoffset;
 	char sndfile[256], nextmsg[256];
 
 	if (!format)
 		format = "A dBY HMS";
 
-	ast_localtime(&time,&tm,timezone);
+	ast_localtime(&tv, &tm, timezone);
 
 	for (offset=0 ; format[offset] != '\0' ; offset++) {
 		ast_debug(1, "Parsing %c (offset %d) in %s\n", format[offset], offset, format);
@@ -3781,16 +3786,14 @@ int ast_say_date_with_format_de(struct ast_channel *chan, time_t time, const cha
 				 * language to say the date, with changes in what you say, depending
 				 * upon how recent the date is. XXX */
 				{
-					struct timeval now;
-					struct tm tmnow;
-					time_t beg_today, tt;
+					struct timeval now = ast_tvnow();
+					struct ast_tm tmnow;
+					time_t beg_today;
 
-					gettimeofday(&now,NULL);
-					tt = now.tv_sec;
-					ast_localtime(&tt,&tmnow,timezone);
+					ast_localtime(&now, &tmnow, timezone);
 					/* This might be slightly off, if we transcend a leap second, but never more off than 1 second */
 					/* In any case, it saves not having to do ast_mktime() */
-					beg_today = tt - (tmnow.tm_hour * 3600) - (tmnow.tm_min * 60) - (tmnow.tm_sec);
+					beg_today = now.tv_sec - (tmnow.tm_hour * 3600) - (tmnow.tm_min * 60) - (tmnow.tm_sec);
 					if (beg_today < time) {
 						/* Today */
 						res = wait_file(chan,ints, "digits/today",lang);
@@ -3808,13 +3811,11 @@ int ast_say_date_with_format_de(struct ast_channel *chan, time_t time, const cha
 				 * language to say the date, with changes in what you say, depending
 				 * upon how recent the date is. XXX */
 				{
-					struct timeval now;
-					struct tm tmnow;
-					time_t beg_today, tt;
+					struct timeval now = ast_tvnow();
+					struct ast_tm tmnow;
+					time_t beg_today;
 
-					gettimeofday(&now,NULL);
-					tt = now.tv_sec;
-					ast_localtime(&tt,&tmnow,timezone);
+					ast_localtime(&now, &tmnow, timezone);
 					/* This might be slightly off, if we transcend a leap second, but never more off than 1 second */
 					/* In any case, it saves not having to do ast_mktime() */
 					beg_today = now.tv_sec - (tmnow.tm_hour * 3600) - (tmnow.tm_min * 60) - (tmnow.tm_sec);
@@ -3866,14 +3867,15 @@ int ast_say_date_with_format_de(struct ast_channel *chan, time_t time, const cha
 /* Thai syntax */
 int ast_say_date_with_format_th(struct ast_channel *chan, time_t time, const char *ints, const char *lang, const char *format, const char *timezone)
 {
-	struct tm tm;
+	struct timeval tv = { time, 0 };
+	struct ast_tm tm;
 	int res=0, offset, sndoffset;
 	char sndfile[256], nextmsg[256];
 
 	if (format == NULL)
 		format = "a 'digits/tee' e 'digits/duan' hY  I 'digits/naliga' M 'digits/natee'";
 
-	ast_localtime(&time,&tm,timezone);
+	ast_localtime(&tv, &tm, timezone);
 
 	for (offset=0 ; format[offset] != '\0' ; offset++) {
 		ast_log(LOG_DEBUG, "Parsing %c (offset %d) in %s\n", format[offset], offset, format);
@@ -3942,16 +3944,14 @@ int ast_say_date_with_format_th(struct ast_channel *chan, time_t time, const cha
 				 * language to say the date, with changes in what you say, depending
 				 * upon how recent the date is. XXX */
 				{
-					struct timeval now;
-					struct tm tmnow;
-					time_t beg_today, tt;
+					struct timeval now = ast_tvnow();
+					struct ast_tm tmnow;
+					time_t beg_today;
 
-					gettimeofday(&now,NULL);
-					tt = now.tv_sec;
-					ast_localtime(&tt,&tmnow,timezone);
+					ast_localtime(&now, &tmnow, timezone);
 					/* This might be slightly off, if we transcend a leap second, but never more off than 1 second */
 					/* In any case, it saves not having to do ast_mktime() */
-					beg_today = tt - (tmnow.tm_hour * 3600) - (tmnow.tm_min * 60) - (tmnow.tm_sec);
+					beg_today = now.tv_sec - (tmnow.tm_hour * 3600) - (tmnow.tm_min * 60) - (tmnow.tm_sec);
 					if (beg_today < time) {
 						/* Today */
 						res = wait_file(chan,ints, "digits/today",lang);
@@ -3979,16 +3979,14 @@ int ast_say_date_with_format_th(struct ast_channel *chan, time_t time, const cha
 				 * language to say the date, with changes in what you say, depending
 				 * upon how recent the date is. XXX */
 				{
-					struct timeval now;
-					struct tm tmnow;
-					time_t beg_today, tt;
+					struct timeval now = ast_tvnow();
+					struct ast_tm tmnow;
+					time_t beg_today;
 
-					gettimeofday(&now,NULL);
-					tt = now.tv_sec;
-					ast_localtime(&tt,&tmnow,timezone);
+					ast_localtime(&now, &tmnow, timezone);
 					/* This might be slightly off, if we transcend a leap second, but never more off than 1 second */
 					/* In any case, it saves not having to do ast_mktime() */
-					beg_today = tt - (tmnow.tm_hour * 3600) - (tmnow.tm_min * 60) - (tmnow.tm_sec);
+					beg_today = now.tv_sec - (tmnow.tm_hour * 3600) - (tmnow.tm_min * 60) - (tmnow.tm_sec);
 					if (beg_today < time) {
 						/* Today */
 					} else if ((beg_today - 86400) < time) {
@@ -4064,14 +4062,15 @@ int ast_say_date_with_format_he(struct ast_channel *chan, time_t time,
 	/* TODO: This whole function is cut&paste from 
 	 * ast_say_date_with_format_en . Is that considered acceptable?
 	 **/
-	struct tm tm;
+	struct timeval tv = { time, 0 };
+	struct ast_tm tm;
 	int res=0, offset, sndoffset;
 	char sndfile[256], nextmsg[256];
 
 	if (!format)
 		format = IL_DATE_STR_FULL;
 
-	ast_localtime(&time,&tm,timezone);
+	ast_localtime(&tv, &tm, timezone);
 
 	for (offset=0 ; format[offset] != '\0' ; offset++) {
 		ast_debug(1, "Parsing %c (offset %d) in %s\n", format[offset], offset, format);
@@ -4165,17 +4164,15 @@ int ast_say_date_with_format_he(struct ast_channel *chan, time_t time,
 				 * language to say the date, with changes in what you say, depending
 				 * upon how recent the date is. XXX */
 				{
-					struct timeval now;
-					struct tm tmnow;
-					time_t beg_today, tt;
+					struct timeval now = ast_tvnow();
+					struct ast_tm tmnow;
+					time_t beg_today;
 					char todo = format[offset]; /* The letter to format*/
 
-					gettimeofday(&now,NULL);
-					tt = now.tv_sec;
-					ast_localtime(&tt,&tmnow,timezone);
+					ast_localtime(&now, &tmnow, timezone);
 					/* This might be slightly off, if we transcend a leap second, but never more off than 1 second */
 					/* In any case, it saves not having to do ast_mktime() */
-					beg_today = tt - (tmnow.tm_hour * 3600) - (tmnow.tm_min * 60) - (tmnow.tm_sec);
+					beg_today = now.tv_sec - (tmnow.tm_hour * 3600) - (tmnow.tm_min * 60) - (tmnow.tm_sec);
 					if (beg_today < time) {
 						/* Today */
 						if (todo == 'Q') {
@@ -4246,14 +4243,15 @@ int ast_say_date_with_format_he(struct ast_channel *chan, time_t time,
 /* Spanish syntax */
 int ast_say_date_with_format_es(struct ast_channel *chan, time_t time, const char *ints, const char *lang, const char *format, const char *timezone)
 {
-	struct tm tm;
+	struct timeval tv = { time, 0 };
+	struct ast_tm tm;
 	int res=0, offset, sndoffset;
 	char sndfile[256], nextmsg[256];
 
 	if (format == NULL)
 		format = "'digits/es-el' Ad 'digits/es-de' B 'digits/es-de' Y 'digits/at' IMp";
 
-	ast_localtime(&time,&tm,timezone);
+	ast_localtime(&tv, &tm, timezone);
 
 	for (offset=0 ; format[offset] != '\0' ; offset++) {
 		ast_debug(1, "Parsing %c (offset %d) in %s\n", format[offset], offset, format);
@@ -4331,16 +4329,14 @@ int ast_say_date_with_format_es(struct ast_channel *chan, time_t time, const cha
 				 * language to say the date, with changes in what you say, depending
 				 * upon how recent the date is. XXX */
 				{
-					struct timeval now;
-					struct tm tmnow;
-					time_t beg_today, tt;
+					struct timeval now = ast_tvnow();
+					struct ast_tm tmnow;
+					time_t beg_today;
 
-					gettimeofday(&now,NULL);
-					tt = now.tv_sec;
-					ast_localtime(&tt,&tmnow,timezone);
+					ast_localtime(&now, &tmnow, timezone);
 					/* This might be slightly off, if we transcend a leap second, but never more off than 1 second */
 					/* In any case, it saves not having to do ast_mktime() */
-					beg_today = tt - (tmnow.tm_hour * 3600) - (tmnow.tm_min * 60) - (tmnow.tm_sec);
+					beg_today = now.tv_sec - (tmnow.tm_hour * 3600) - (tmnow.tm_min * 60) - (tmnow.tm_sec);
 					if (beg_today < time) {
 						/* Today */
 						res = wait_file(chan,ints, "digits/today",lang);
@@ -4358,16 +4354,14 @@ int ast_say_date_with_format_es(struct ast_channel *chan, time_t time, const cha
 				 * language to say the date, with changes in what you say, depending
 				 * upon how recent the date is. XXX */
 				{
-					struct timeval now;
-					struct tm tmnow;
-					time_t beg_today, tt;
+					struct timeval now = ast_tvnow();
+					struct ast_tm tmnow;
+					time_t beg_today;
 
-					gettimeofday(&now,NULL);
-					tt = now.tv_sec;
-					ast_localtime(&tt,&tmnow,timezone);
+					ast_localtime(&now, &tmnow, timezone);
 					/* This might be slightly off, if we transcend a leap second, but never more off than 1 second */
 					/* In any case, it saves not having to do ast_mktime() */
-					beg_today = tt - (tmnow.tm_hour * 3600) - (tmnow.tm_min * 60) - (tmnow.tm_sec);
+					beg_today = now.tv_sec - (tmnow.tm_hour * 3600) - (tmnow.tm_min * 60) - (tmnow.tm_sec);
 					if (beg_today < time) {
 						/* Today */
 						res = wait_file(chan,ints, "digits/today",lang);
@@ -4438,14 +4432,15 @@ oclock = heure
 */
 int ast_say_date_with_format_fr(struct ast_channel *chan, time_t time, const char *ints, const char *lang, const char *format, const char *timezone)
 {
-	struct tm tm;
+	struct timeval tv = { time, 0 };
+	struct ast_tm tm;
 	int res=0, offset, sndoffset;
 	char sndfile[256], nextmsg[256];
 
 	if (format == NULL)
 		format = "AdBY 'digits/at' IMp";
 
-	ast_localtime(&time,&tm,timezone);
+	ast_localtime(&tv, &tm, timezone);
 
 	for (offset=0 ; format[offset] != '\0' ; offset++) {
 		ast_debug(1, "Parsing %c (offset %d) in %s\n", format[offset], offset, format);
@@ -4555,16 +4550,14 @@ int ast_say_date_with_format_fr(struct ast_channel *chan, time_t time, const cha
 				 * language to say the date, with changes in what you say, depending
 				 * upon how recent the date is. XXX */
 				{
-					struct timeval now;
-					struct tm tmnow;
-					time_t beg_today, tt;
+					struct timeval now = ast_tvnow();
+					struct ast_tm tmnow;
+					time_t beg_today;
 
-					gettimeofday(&now,NULL);
-					tt = now.tv_sec;
-					ast_localtime(&tt,&tmnow,timezone);
+					ast_localtime(&now, &tmnow, timezone);
 					/* This might be slightly off, if we transcend a leap second, but never more off than 1 second */
 					/* In any case, it saves not having to do ast_mktime() */
-					beg_today = tt - (tmnow.tm_hour * 3600) - (tmnow.tm_min * 60) - (tmnow.tm_sec);
+					beg_today = now.tv_sec - (tmnow.tm_hour * 3600) - (tmnow.tm_min * 60) - (tmnow.tm_sec);
 					if (beg_today < time) {
 						/* Today */
 						res = wait_file(chan,ints, "digits/today",lang);
@@ -4582,16 +4575,14 @@ int ast_say_date_with_format_fr(struct ast_channel *chan, time_t time, const cha
 				 * language to say the date, with changes in what you say, depending
 				 * upon how recent the date is. XXX */
 				{
-					struct timeval now;
-					struct tm tmnow;
-					time_t beg_today, tt;
+					struct timeval now = ast_tvnow();
+					struct ast_tm tmnow;
+					time_t beg_today;
 
-					gettimeofday(&now,NULL);
-					tt = now.tv_sec;
-					ast_localtime(&tt,&tmnow,timezone);
+					ast_localtime(&now, &tmnow, timezone);
 					/* This might be slightly off, if we transcend a leap second, but never more off than 1 second */
 					/* In any case, it saves not having to do ast_mktime() */
-					beg_today = tt - (tmnow.tm_hour * 3600) - (tmnow.tm_min * 60) - (tmnow.tm_sec);
+					beg_today = now.tv_sec - (tmnow.tm_hour * 3600) - (tmnow.tm_min * 60) - (tmnow.tm_sec);
 					if (beg_today < time) {
 						/* Today */
 					} else if ((beg_today - 86400) < time) {
@@ -4636,14 +4627,15 @@ int ast_say_date_with_format_fr(struct ast_channel *chan, time_t time, const cha
 
 int ast_say_date_with_format_it(struct ast_channel *chan, time_t time, const char *ints, const char *lang, const char *format, const char *timezone)
 {
-	struct tm tm;
+	struct timeval tv = { time, 0 };
+	struct ast_tm tm;
 	int res=0, offset, sndoffset;
 	char sndfile[256], nextmsg[256];
 
 	if (format == NULL)
 		format = "AdB 'digits/at' IMp";
 
-	ast_localtime(&time,&tm,timezone);
+	ast_localtime(&tv, &tm, timezone);
 
 	for (offset=0 ; format[offset] != '\0' ; offset++) {
 		ast_debug(1, "Parsing %c (offset %d) in %s\n", format[offset], offset, format);
@@ -4653,7 +4645,7 @@ int ast_say_date_with_format_it(struct ast_channel *chan, time_t time, const cha
 				/* Literal name of a sound file */
 				sndoffset=0;
 				for (sndoffset=0 ; (format[++offset] != '\'') && (sndoffset < 256) ; sndoffset++)
-				sndfile[sndoffset] = format[offset];
+					sndfile[sndoffset] = format[offset];
 				sndfile[sndoffset] = '\0';
 				res = wait_file(chan,ints,sndfile,lang);
 				break;
@@ -4768,16 +4760,14 @@ int ast_say_date_with_format_it(struct ast_channel *chan, time_t time, const cha
 				 * language to say the date, with changes in what you say, depending
 				 * upon how recent the date is. XXX */
 				{
-					struct timeval now;
-					struct tm tmnow;
-					time_t beg_today, tt;
+					struct timeval now = ast_tvnow();
+					struct ast_tm tmnow;
+					time_t beg_today;
 	
-					gettimeofday(&now,NULL);
-					tt = now.tv_sec;
-					ast_localtime(&tt,&tmnow,timezone);
+					ast_localtime(&now, &tmnow, timezone);
 					/* This might be slightly off, if we transcend a leap second, but never more off than 1 second */
 					/* In any case, it saves not having to do ast_mktime() */
-					beg_today = tt - (tmnow.tm_hour * 3600) - (tmnow.tm_min * 60) - (tmnow.tm_sec);
+					beg_today = now.tv_sec - (tmnow.tm_hour * 3600) - (tmnow.tm_min * 60) - (tmnow.tm_sec);
 					if (beg_today < time) {
 						/* Today */
 						res = wait_file(chan,ints, "digits/today",lang);
@@ -4792,16 +4782,14 @@ int ast_say_date_with_format_it(struct ast_channel *chan, time_t time, const cha
 			case 'q':
 				/* Shorthand for "" (today), "Yesterday", A (weekday), or ABdY */
 				{
-					struct timeval now;
-					struct tm tmnow;
-					time_t beg_today, tt;
+					struct timeval now = ast_tvnow();
+					struct ast_tm tmnow;
+					time_t beg_today;
 	
-					gettimeofday(&now,NULL);
-					tt = now.tv_sec;
-					ast_localtime(&tt,&tmnow,timezone);
+					ast_localtime(&now, &tmnow, timezone);
 					/* This might be slightly off, if we transcend a leap second, but never more off than 1 second */
 					/* In any case, it saves not having to do ast_mktime() */
-					beg_today = tt - (tmnow.tm_hour * 3600) - (tmnow.tm_min * 60) - (tmnow.tm_sec);
+					beg_today = now.tv_sec - (tmnow.tm_hour * 3600) - (tmnow.tm_min * 60) - (tmnow.tm_sec);
 					if (beg_today < time) {
 						/* Today */
 					} else if ((beg_today - 86400) < time) {
@@ -4869,14 +4857,15 @@ int ast_say_date_with_format_it(struct ast_channel *chan, time_t time, const cha
 /* Dutch syntax */
 int ast_say_date_with_format_nl(struct ast_channel *chan, time_t time, const char *ints, const char *lang, const char *format, const char *timezone)
 {
-	struct tm tm;
+	struct timeval tv = { time, 0 };
+	struct ast_tm tm;
 	int res=0, offset, sndoffset;
 	char sndfile[256], nextmsg[256];
 
 	if (format == NULL)
 		format = "ABdY 'digits/at' IMp";
 
-	ast_localtime(&time,&tm,timezone);
+	ast_localtime(&tv, &tm, timezone);
 
 	for (offset=0 ; format[offset] != '\0' ; offset++) {
 		ast_debug(1, "Parsing %c (offset %d) in %s\n", format[offset], offset, format);
@@ -5001,16 +4990,14 @@ int ast_say_date_with_format_nl(struct ast_channel *chan, time_t time, const cha
 				 * language to say the date, with changes in what you say, depending
 				 * upon how recent the date is. XXX */
 				{
-					struct timeval now;
-					struct tm tmnow;
-					time_t beg_today, tt;
+					struct timeval now = ast_tvnow();
+					struct ast_tm tmnow;
+					time_t beg_today;
 
-					gettimeofday(&now,NULL);
-					tt = now.tv_sec;
-					ast_localtime(&tt,&tmnow,timezone);
+					ast_localtime(&now, &tmnow, timezone);
 					/* This might be slightly off, if we transcend a leap second, but never more off than 1 second */
 					/* In any case, it saves not having to do ast_mktime() */
-					beg_today = tt - (tmnow.tm_hour * 3600) - (tmnow.tm_min * 60) - (tmnow.tm_sec);
+					beg_today = now.tv_sec - (tmnow.tm_hour * 3600) - (tmnow.tm_min * 60) - (tmnow.tm_sec);
 					if (beg_today < time) {
 						/* Today */
 						res = wait_file(chan,ints, "digits/today",lang);
@@ -5025,16 +5012,14 @@ int ast_say_date_with_format_nl(struct ast_channel *chan, time_t time, const cha
 			case 'q':
 				/* Shorthand for "" (today), "Yesterday", A (weekday), or ABdY */
 				{
-					struct timeval now;
-					struct tm tmnow;
-					time_t beg_today, tt;
+					struct timeval now = ast_tvnow();
+					struct ast_tm tmnow;
+					time_t beg_today;
 
-					gettimeofday(&now,NULL);
-					tt = now.tv_sec;
-					ast_localtime(&tt,&tmnow,timezone);
+					ast_localtime(&now, &tmnow, timezone);
 					/* This might be slightly off, if we transcend a leap second, but never more off than 1 second */
 					/* In any case, it saves not having to do ast_mktime() */
-					beg_today = tt - (tmnow.tm_hour * 3600) - (tmnow.tm_min * 60) - (tmnow.tm_sec);
+					beg_today = now.tv_sec - (tmnow.tm_hour * 3600) - (tmnow.tm_min * 60) - (tmnow.tm_sec);
 					if (beg_today < time) {
 						/* Today */
 					} else if ((beg_today - 86400) < time) {
@@ -5077,11 +5062,12 @@ int ast_say_date_with_format_nl(struct ast_channel *chan, time_t time, const cha
 /* Polish syntax */
 int ast_say_date_with_format_pl(struct ast_channel *chan, time_t thetime, const char *ints, const char *lang, const char *format, const char *timezone)
 {
-	struct tm tm;
+	struct timeval tv = { thetime, 0 };
+	struct ast_tm tm;
 	int res=0, offset, sndoffset;
 	char sndfile[256], nextmsg[256];
 
-	ast_localtime(&thetime, &tm, timezone);
+	ast_localtime(&tv, &tm, timezone);
 
 	for (offset = 0 ; format[offset] != '\0' ; offset++) {
 		int remainder;
@@ -5203,14 +5189,14 @@ int ast_say_date_with_format_pl(struct ast_channel *chan, time_t thetime, const
 			case 'Q':
 				/* Shorthand for "Today", "Yesterday", or AdBY */
 				{
-					time_t tv_sec = time(NULL);
-					struct tm tmnow;
+					struct timeval now = ast_tvnow();
+					struct ast_tm tmnow;
 					time_t beg_today;
 
-					ast_localtime(&tv_sec,&tmnow, timezone);
+					ast_localtime(&now, &tmnow, timezone);
 					/* This might be slightly off, if we transcend a leap second, but never more off than 1 second */
 					/* In any case, it saves not having to do ast_mktime() */
-					beg_today = tv_sec - (tmnow.tm_hour * 3600) - (tmnow.tm_min * 60) - (tmnow.tm_sec);
+					beg_today = now.tv_sec - (tmnow.tm_hour * 3600) - (tmnow.tm_min * 60) - (tmnow.tm_sec);
 					if (beg_today < thetime) {
 						/* Today */
 						res = wait_file(chan, ints, "digits/today", lang);
@@ -5225,14 +5211,14 @@ int ast_say_date_with_format_pl(struct ast_channel *chan, time_t thetime, const
 			case 'q':
 				/* Shorthand for "" (today), "Yesterday", A (weekday), or AdBY */
 				{
-					time_t tv_sec = time(NULL);
-					struct tm tmnow;
+					struct timeval now = ast_tvnow();
+					struct ast_tm tmnow;
 					time_t beg_today;
 
-					ast_localtime(&tv_sec, &tmnow, timezone);
+					ast_localtime(&now, &tmnow, timezone);
 					/* This might be slightly off, if we transcend a leap second, but never more off than 1 second */
 					/* In any case, it saves not having to do ast_mktime() */
-					beg_today = tv_sec - (tmnow.tm_hour * 3600) - (tmnow.tm_min * 60) - (tmnow.tm_sec);
+					beg_today = now.tv_sec - (tmnow.tm_hour * 3600) - (tmnow.tm_min * 60) - (tmnow.tm_sec);
 					if (beg_today < thetime) {
 						/* Today */
 					} else if ((beg_today - 86400) < thetime) {
@@ -5293,14 +5279,15 @@ int ast_say_date_with_format_pl(struct ast_channel *chan, time_t thetime, const
 /* Portuguese syntax */
 int ast_say_date_with_format_pt(struct ast_channel *chan, time_t time, const char *ints, const char *lang, const char *format, const char *timezone)
 {
-	struct tm tm;
+	struct timeval tv = { time, 0 };
+	struct ast_tm tm;
 	int res=0, offset, sndoffset;
 	char sndfile[256], nextmsg[256];
 
 	if (format == NULL)
 		format = "Ad 'digits/pt-de' B 'digits/pt-de' Y 'digits/at' IMp";
 
-	ast_localtime(&time,&tm,timezone);
+	ast_localtime(&tv, &tm, timezone);
 
 	for (offset=0 ; format[offset] != '\0' ; offset++) {
 		ast_debug(1, "Parsing %c (offset %d) in %s\n", format[offset], offset, format);
@@ -5476,16 +5463,14 @@ int ast_say_date_with_format_pt(struct ast_channel *chan, time_t time, const cha
 				 * language to say the date, with changes in what you say, depending
 				 * upon how recent the date is. XXX */
 				{
-					struct timeval now;
-					struct tm tmnow;
-					time_t beg_today, tt;
+					struct timeval now = ast_tvnow();
+					struct ast_tm tmnow;
+					time_t beg_today;
 
-					gettimeofday(&now,NULL);
-					tt = now.tv_sec;
-					ast_localtime(&tt,&tmnow,timezone);
+					ast_localtime(&now, &tmnow, timezone);
 					/* This might be slightly off, if we transcend a leap second, but never more off than 1 second */
 					/* In any case, it saves not having to do ast_mktime() */
-					beg_today = tt - (tmnow.tm_hour * 3600) - (tmnow.tm_min * 60) - (tmnow.tm_sec);
+					beg_today = now.tv_sec - (tmnow.tm_hour * 3600) - (tmnow.tm_min * 60) - (tmnow.tm_sec);
 					if (beg_today < time) {
 						/* Today */
 						res = wait_file(chan,ints, "digits/today",lang);
@@ -5503,16 +5488,14 @@ int ast_say_date_with_format_pt(struct ast_channel *chan, time_t time, const cha
 				 * language to say the date, with changes in what you say, depending
 				 * upon how recent the date is. XXX */
 				{
-					struct timeval now;
-					struct tm tmnow;
-					time_t beg_today, tt;
+					struct timeval now = ast_tvnow();
+					struct ast_tm tmnow;
+					time_t beg_today;
 
-					gettimeofday(&now,NULL);
-					tt = now.tv_sec;
-					ast_localtime(&tt,&tmnow,timezone);
+					ast_localtime(&now, &tmnow, timezone);
 					/* This might be slightly off, if we transcend a leap second, but never more off than 1 second */
 					/* In any case, it saves not having to do ast_mktime() */
-					beg_today = tt - (tmnow.tm_hour * 3600) - (tmnow.tm_min * 60) - (tmnow.tm_sec);
+					beg_today = now.tv_sec - (tmnow.tm_hour * 3600) - (tmnow.tm_min * 60) - (tmnow.tm_sec);
 					if (beg_today < time) {
 						/* Today */
 					} else if ((beg_today - 86400) < time) {
@@ -5591,14 +5574,15 @@ int ast_say_date_with_format_pt(struct ast_channel *chan, time_t time, const cha
 /* Taiwanese / Chinese syntax */
 int ast_say_date_with_format_tw(struct ast_channel *chan, time_t time, const char *ints, const char *lang, const char *format, const char *timezone)
 {
-	struct tm tm;
+	struct timeval tv = { time, 0 };
+	struct ast_tm tm;
 	int res=0, offset, sndoffset;
 	char sndfile[256], nextmsg[256];
 
 	if (format == NULL)
 		format = "YBdAkM";
 
-	ast_localtime(&time,&tm,timezone);
+	ast_localtime(&tv, &tm, timezone);
 
 	for (offset=0 ; format[offset] != '\0' ; offset++) {
 		ast_debug(1, "Parsing %c (offset %d) in %s\n", format[offset], offset, format);
@@ -5769,16 +5753,14 @@ int ast_say_date_with_format_tw(struct ast_channel *chan, time_t time, const cha
 				 * language to say the date, with changes in what you say, depending
 				 * upon how recent the date is. XXX */
 				{
-					struct timeval now;
-					struct tm tmnow;
-					time_t beg_today, tt;
+					struct timeval now = ast_tvnow();
+					struct ast_tm tmnow;
+					time_t beg_today;
 
-					gettimeofday(&now,NULL);
-					tt = now.tv_sec;
-					ast_localtime(&tt,&tmnow,timezone);
+					ast_localtime(&now, &tmnow, timezone);
 					/* This might be slightly off, if we transcend a leap second, but never more off than 1 second */
 					/* In any case, it saves not having to do ast_mktime() */
-					beg_today = tt - (tmnow.tm_hour * 3600) - (tmnow.tm_min * 60) - (tmnow.tm_sec);
+					beg_today = now.tv_sec - (tmnow.tm_hour * 3600) - (tmnow.tm_min * 60) - (tmnow.tm_sec);
 					if (beg_today < time) {
 						/* Today */
 						res = wait_file(chan,ints, "digits/today",lang);
@@ -5796,16 +5778,14 @@ int ast_say_date_with_format_tw(struct ast_channel *chan, time_t time, const cha
 				 * language to say the date, with changes in what you say, depending
 				 * upon how recent the date is. XXX */
 				{
-					struct timeval now;
-					struct tm tmnow;
-					time_t beg_today, tt;
+					struct timeval now = ast_tvnow();
+					struct ast_tm tmnow;
+					time_t beg_today;
 
-					gettimeofday(&now,NULL);
-					tt = now.tv_sec;
-					ast_localtime(&tt,&tmnow,timezone);
+					ast_localtime(&now, &tmnow, timezone);
 					/* This might be slightly off, if we transcend a leap second, but never more off than 1 second */
 					/* In any case, it saves not having to do ast_mktime() */
-					beg_today = tt - (tmnow.tm_hour * 3600) - (tmnow.tm_min * 60) - (tmnow.tm_sec);
+					beg_today = now.tv_sec - (tmnow.tm_hour * 3600) - (tmnow.tm_min * 60) - (tmnow.tm_sec);
 					if (beg_today < time) {
 						/* Today */
 					} else if ((beg_today - 86400) < time) {
@@ -5895,11 +5875,12 @@ static int say_time(struct ast_channel *chan, time_t t, const char *ints, const
 /* English syntax */
 int ast_say_time_en(struct ast_channel *chan, time_t t, const char *ints, const char *lang)
 {
-	struct tm tm;
+	struct timeval tv = { t, 0 };
+	struct ast_tm tm;
 	int res = 0;
 	int hour, pm=0;
 
-	ast_localtime(&t, &tm, NULL);
+	ast_localtime(&tv, &tm, NULL);
 	hour = tm.tm_hour;
 	if (!hour)
 		hour = 12;
@@ -5943,10 +5924,11 @@ int ast_say_time_en(struct ast_channel *chan, time_t t, const char *ints, const
 /* German syntax */
 int ast_say_time_de(struct ast_channel *chan, time_t t, const char *ints, const char *lang)
 {
-	struct tm tm;
+	struct timeval tv = { t, 0 };
+	struct ast_tm tm;
 	int res = 0;
 
-	ast_localtime(&t, &tm, NULL);
+	ast_localtime(&tv, &tm, NULL);
 	if (!res)
 		res = ast_say_number(chan, tm.tm_hour, ints, lang, "n");
 	if (!res)
@@ -5962,10 +5944,11 @@ int ast_say_time_de(struct ast_channel *chan, time_t t, const char *ints, const
 /* Hungarian syntax */
 int ast_say_time_hu(struct ast_channel *chan, time_t t, const char *ints, const char *lang)
 {
-	struct tm tm;
+	struct timeval tv = { t, 0 };
+	struct ast_tm tm;
 	int res = 0;
 
-	ast_localtime(&t, &tm, NULL);
+	ast_localtime(&tv, &tm, NULL);
 	if (!res)
 		res = ast_say_number(chan, tm.tm_hour, ints, lang, "n");
 	if (!res)
@@ -5984,10 +5967,11 @@ int ast_say_time_hu(struct ast_channel *chan, time_t t, const char *ints, const
 /* French syntax */
 int ast_say_time_fr(struct ast_channel *chan, time_t t, const char *ints, const char *lang)
 {
-	struct tm tm;
+	struct timeval tv = { t, 0 };
+	struct ast_tm tm;
 	int res = 0;
 
-	ast_localtime(&t, &tm, NULL);
+	ast_localtime(&tv, &tm, NULL);
 
 	res = ast_say_number(chan, tm.tm_hour, ints, lang, "f");
 	if (!res)
@@ -6002,10 +5986,11 @@ int ast_say_time_fr(struct ast_channel *chan, time_t t, const char *ints, const
 /* Dutch syntax */
 int ast_say_time_nl(struct ast_channel *chan, time_t t, const char *ints, const char *lang)
 {
-	struct tm tm;
+	struct timeval tv = { t, 0 };
+	struct ast_tm tm;
 	int res = 0;
 
-	ast_localtime(&t, &tm, NULL);
+	ast_localtime(&tv, &tm, NULL);
 	if (!res)
 		res = ast_say_number(chan, tm.tm_hour, ints, lang, (char *) NULL);
 	if (!res)
@@ -6021,11 +6006,12 @@ int ast_say_time_nl(struct ast_channel *chan, time_t t, const char *ints, const
 /* Portuguese syntax */
 int ast_say_time_pt(struct ast_channel *chan, time_t t, const char *ints, const char *lang)
 {
-	struct tm tm;
+	struct timeval tv = { t, 0 };
+	struct ast_tm tm;
 	int res = 0;
 	int hour;
 
-	ast_localtime(&t, &tm, NULL);
+	ast_localtime(&tv, &tm, NULL);
 	hour = tm.tm_hour;
 	if (!res)
 		res = ast_say_number(chan, hour, ints, lang, "f");
@@ -6049,10 +6035,11 @@ int ast_say_time_pt(struct ast_channel *chan, time_t t, const char *ints, const
 /* Brazilian Portuguese syntax */
 int ast_say_time_pt_BR(struct ast_channel *chan, time_t t, const char *ints, const char *lang)
 {
-	struct tm tm;
+	struct timeval tv = { t, 0 };
+	struct ast_tm tm;
 	int res = 0;
 
-	ast_localtime(&t, &tm, NULL);
+	ast_localtime(&tv, &tm, NULL);
 
 	res = ast_say_number(chan, tm.tm_hour, ints, lang, "f");
 	if (!res) {
@@ -6078,10 +6065,11 @@ int ast_say_time_pt_BR(struct ast_channel *chan, time_t t, const char *ints, con
 /* Thai  syntax */
 int ast_say_time_th(struct ast_channel *chan, time_t t, const char *ints, const char *lang)
 {
-	struct tm tm;
+	struct timeval tv = { t, 0 };
+	struct ast_tm tm;
 	int res = 0;
 	int hour;
-	ast_localtime(&t, &tm, NULL);
+	ast_localtime(&tv, &tm, NULL);
 	hour = tm.tm_hour;
 	if (!hour)
 		hour = 24;
@@ -6095,11 +6083,12 @@ int ast_say_time_th(struct ast_channel *chan, time_t t, const char *ints, const
 /* Taiwanese / Chinese  syntax */
 int ast_say_time_tw(struct ast_channel *chan, time_t t, const char *ints, const char *lang)
 {
-	struct tm tm;
+	struct timeval tv = { t, 0 };
+	struct ast_tm tm;
 	int res = 0;
 	int hour, pm=0;
 
-	ast_localtime(&t, &tm, NULL);
+	ast_localtime(&tv, &tm, NULL);
 	hour = tm.tm_hour;
 	if (!hour)
 		hour = 12;
@@ -6166,12 +6155,13 @@ static int say_datetime(struct ast_channel *chan, time_t t, const char *ints, co
 /* English syntax */
 int ast_say_datetime_en(struct ast_channel *chan, time_t t, const char *ints, const char *lang)
 {
-	struct tm tm;
+	struct timeval tv = { t, 0 };
+	struct ast_tm tm;
 	char fn[256];
 	int res = 0;
 	int hour, pm=0;
 
-	ast_localtime(&t, &tm, NULL);
+	ast_localtime(&tv, &tm, NULL);
 	if (!res) {
 		snprintf(fn, sizeof(fn), "digits/day-%d", tm.tm_wday);
 		res = ast_streamfile(chan, fn, lang);
@@ -6232,10 +6222,11 @@ int ast_say_datetime_en(struct ast_channel *chan, time_t t, const char *ints, co
 /* German syntax */
 int ast_say_datetime_de(struct ast_channel *chan, time_t t, const char *ints, const char *lang)
 {
-	struct tm tm;
+	struct timeval tv = { t, 0 };
+	struct ast_tm tm;
 	int res = 0;
 
-	ast_localtime(&t, &tm, NULL);
+	ast_localtime(&tv, &tm, NULL);
 	res = ast_say_date(chan, t, ints, lang);
 	if (!res) 
 		ast_say_time(chan, t, ints, lang);
@@ -6246,10 +6237,11 @@ int ast_say_datetime_de(struct ast_channel *chan, time_t t, const char *ints, co
 /* Hungarian syntax */
 int ast_say_datetime_hu(struct ast_channel *chan, time_t t, const char *ints, const char *lang)
 {
-	struct tm tm;
+	struct timeval tv = { t, 0 };
+	struct ast_tm tm;
 	int res = 0;
 
-	ast_localtime(&t, &tm, NULL);
+	ast_localtime(&tv, &tm, NULL);
 	res = ast_say_date(chan, t, ints, lang);
 	if (!res) 
 		ast_say_time(chan, t, ints, lang);
@@ -6259,11 +6251,12 @@ int ast_say_datetime_hu(struct ast_channel *chan, time_t t, const char *ints, co
 /* French syntax */
 int ast_say_datetime_fr(struct ast_channel *chan, time_t t, const char *ints, const char *lang)
 {
-	struct tm tm;
+	struct timeval tv = { t, 0 };
+	struct ast_tm tm;
 	char fn[256];
 	int res = 0;
 
-	ast_localtime(&t, &tm, NULL);
+	ast_localtime(&tv, &tm, NULL);
 
 	if (!res)
 		res = ast_say_number(chan, tm.tm_mday, ints, lang, (char *) NULL);
@@ -6299,10 +6292,11 @@ int ast_say_datetime_fr(struct ast_channel *chan, time_t t, const char *ints, co
 /* Dutch syntax */
 int ast_say_datetime_nl(struct ast_channel *chan, time_t t, const char *ints, const char *lang)
 {
-	struct tm tm;
+	struct timeval tv = { t, 0 };
+	struct ast_tm tm;
 	int res = 0;
 
-	ast_localtime(&t, &tm, NULL);
+	ast_localtime(&tv, &tm, NULL);
 	res = ast_say_date(chan, t, ints, lang);
 	if (!res) {
 		res = ast_streamfile(chan, "digits/nl-om", lang);
@@ -6317,12 +6311,13 @@ int ast_say_datetime_nl(struct ast_channel *chan, time_t t, const char *ints, co
 /* Portuguese syntax */
 int ast_say_datetime_pt(struct ast_channel *chan, time_t t, const char *ints, const char *lang)
 {
-	struct tm tm;
+	struct timeval tv = { t, 0 };
+	struct ast_tm tm;
 	char fn[256];
 	int res = 0;
 	int hour, pm=0;
 
-	ast_localtime(&t, &tm, NULL);
+	ast_localtime(&tv, &tm, NULL);
 	if (!res) {
 		snprintf(fn, sizeof(fn), "digits/day-%d", tm.tm_wday);
 		res = ast_streamfile(chan, fn, lang);
@@ -6383,10 +6378,11 @@ int ast_say_datetime_pt(struct ast_channel *chan, time_t t, const char *ints, co
 /* Brazilian Portuguese syntax */
 int ast_say_datetime_pt_BR(struct ast_channel *chan, time_t t, const char *ints, const char *lang)
 {
-	struct tm tm;
+	struct timeval tv = { t, 0 };
+	struct ast_tm tm;
 	int res = 0;
 
-	ast_localtime(&t, &tm, NULL);
+	ast_localtime(&tv, &tm, NULL);
 	res = ast_say_date(chan, t, ints, lang);
 	if (!res)
 		res = ast_say_time(chan, t, ints, lang);
@@ -6396,11 +6392,12 @@ int ast_say_datetime_pt_BR(struct ast_channel *chan, time_t t, const char *ints,
 /* Thai syntax */
 int ast_say_datetime_th(struct ast_channel *chan, time_t t, const char *ints, const char *lang)
 {
-	struct tm tm;
+	struct timeval tv = { t, 0 };
+	struct ast_tm tm;
 	char fn[256];
 	int res = 0;
 	int hour;
-	ast_localtime(&t, &tm, NULL);
+	ast_localtime(&tv, &tm, NULL);
 	if (!res) {
 		snprintf(fn, sizeof(fn), "digits/day-%d", tm.tm_wday);
 		res = ast_streamfile(chan, fn, lang);
@@ -6438,12 +6435,13 @@ int ast_say_datetime_th(struct ast_channel *chan, time_t t, const char *ints, co
 /* Taiwanese / Chinese syntax */
 int ast_say_datetime_tw(struct ast_channel *chan, time_t t, const char *ints, const char *lang)
 {
-	struct tm tm;
+	struct timeval tv = { t, 0 };
+	struct ast_tm tm;
 	char fn[256];
 	int res = 0;
 	int hour, pm=0;
 
-	ast_localtime(&t, &tm, NULL);
+	ast_localtime(&tv, &tm, NULL);
 	if (!res)
 		res = ast_say_number(chan, tm.tm_year + 1900, ints, lang, (char *) NULL);
 	if (!res) {
@@ -6514,16 +6512,14 @@ static int say_datetime_from_now(struct ast_channel *chan, time_t t, const char
 int ast_say_datetime_from_now_en(struct ast_channel *chan, time_t t, const char *ints, const char *lang)
 {
 	int res=0;
-	time_t nowt;
+	struct timeval nowtv = ast_tvnow(), tv = { t, 0 };
 	int daydiff;
-	struct tm tm;
-	struct tm now;
+	struct ast_tm tm;
+	struct ast_tm now;
 	char fn[256];
 
-	time(&nowt);
-
-	ast_localtime(&t, &tm, NULL);
-	ast_localtime(&nowt,&now, NULL);
+	ast_localtime(&tv, &tm, NULL);
+	ast_localtime(&nowtv, &now, NULL);
 	daydiff = now.tm_yday - tm.tm_yday;
 	if ((daydiff < 0) || (daydiff > 6)) {
 		/* Day of month and month */
@@ -6554,16 +6550,14 @@ int ast_say_datetime_from_now_en(struct ast_channel *chan, time_t t, const char
 int ast_say_datetime_from_now_fr(struct ast_channel *chan, time_t t, const char *ints, const char *lang)
 {
 	int res=0;
-	time_t nowt;
+	struct timeval nowtv = ast_tvnow(), tv = { t, 0 };
 	int daydiff;
-	struct tm tm;
-	struct tm now;
+	struct ast_tm tm;
+	struct ast_tm now;
 	char fn[256];
 
-	time(&nowt);
-
-	ast_localtime(&t, &tm, NULL);
-	ast_localtime(&nowt, &now, NULL);
+	ast_localtime(&tv, &tm, NULL);
+	ast_localtime(&nowtv, &now, NULL);
 	daydiff = now.tm_yday - tm.tm_yday;
 	if ((daydiff < 0) || (daydiff > 6)) {
 		/* Day of month and month */
@@ -6594,16 +6588,14 @@ int ast_say_datetime_from_now_fr(struct ast_channel *chan, time_t t, const char
 int ast_say_datetime_from_now_pt(struct ast_channel *chan, time_t t, const char *ints, const char *lang)
 {
 	int res=0;
-	time_t nowt;
 	int daydiff;
-	struct tm tm;
-	struct tm now;
+	struct ast_tm tm;
+	struct ast_tm now;
+	struct timeval nowtv = ast_tvnow(), tv = { t, 0 };
 	char fn[256];
 
-	time(&nowt);
-
-	ast_localtime(&t, &tm, NULL);
-	ast_localtime(&nowt, &now, NULL);
+	ast_localtime(&tv, &tm, NULL);
+	ast_localtime(&nowtv, &now, NULL);
 	daydiff = now.tm_yday - tm.tm_yday;
 	if ((daydiff < 0) || (daydiff > 6)) {
 		/* Day of month and month */
@@ -6778,13 +6770,14 @@ static int ast_say_number_full_gr(struct ast_channel *chan, int num, const char
 
 static int ast_say_date_gr(struct ast_channel *chan, time_t t, const char *ints, const char *lang)
 {
-	struct tm tm;
+	struct ast_tm tm;
+	struct timeval tv = { t, 0 };
 	
 	char fn[256];
 	int res = 0;
 	
 
-	ast_localtime(&t,&tm,NULL);
+	ast_localtime(&tv, &tm, NULL);
 	/* W E E K - D A Y */
 	if (!res) {
 		snprintf(fn, sizeof(fn), "digits/day-%d", tm.tm_wday);
@@ -6822,11 +6815,12 @@ static int ast_say_date_gr(struct ast_channel *chan, time_t t, const char *ints,
 static int ast_say_time_gr(struct ast_channel *chan, time_t t, const char *ints, const char *lang)
 {
 
-	struct tm tm;
+	struct timeval tv = { t, 0 };
+	struct ast_tm tm;
 	int res = 0;
 	int hour, pm=0;
 
-	ast_localtime(&t, &tm, NULL);
+	ast_localtime(&tv, &tm, NULL);
 	hour = tm.tm_hour;
 
 	if (!hour)
@@ -6868,13 +6862,13 @@ static int ast_say_time_gr(struct ast_channel *chan, time_t t, const char *ints,
 
 static int ast_say_datetime_gr(struct ast_channel *chan, time_t t, const char *ints, const char *lang)
 {
-	struct tm tm;
+	struct timeval tv = { t, 0 };
+	struct ast_tm tm;
 	char fn[256];
 	int res = 0;
 
-	ast_localtime(&t, &tm, NULL);
+	ast_localtime(&tv, &tm, NULL);
 
-	
 	/* W E E K - D A Y */
 	if (!res) {
 		snprintf(fn, sizeof(fn), "digits/day-%d", tm.tm_wday);
@@ -6900,15 +6894,15 @@ static int ast_say_datetime_gr(struct ast_channel *chan, time_t t, const char *i
 
 static int ast_say_date_with_format_gr(struct ast_channel *chan, time_t time, const char *ints, const char *lang, const char *format, const char *timezone)
 {
-	
-	struct tm tm;
+	struct timeval tv = { time, 0 };
+	struct ast_tm tm;
 	int res=0, offset, sndoffset;
 	char sndfile[256], nextmsg[256];
 
 	if (!format)
 		format = "AdBY 'digits/at' IMp";
 
-	ast_localtime(&time,&tm,timezone);
+	ast_localtime(&tv, &tm, timezone);
 	
 	for (offset=0 ; format[offset] != '\0' ; offset++) {
 		ast_debug(1, "Parsing %c (offset %d) in %s\n", format[offset], offset, format);
@@ -6991,16 +6985,14 @@ static int ast_say_date_with_format_gr(struct ast_channel *chan, time_t time, co
 				 * language to say the date, with changes in what you say, depending
 				 * upon how recent the date is. XXX */
 			{
-				struct timeval now;
-				struct tm tmnow;
-				time_t beg_today, tt;
+				struct timeval now = ast_tvnow();
+				struct ast_tm tmnow;
+				time_t beg_today;
 				
-				gettimeofday(&now,NULL);
-				tt = now.tv_sec;
-				ast_localtime(&tt,&tmnow,timezone);
+				ast_localtime(&now, &tmnow, timezone);
 				/* This might be slightly off, if we transcend a leap second, but never more off than 1 second */
 				/* In any case, it saves not having to do ast_mktime() */
-				beg_today = tt - (tmnow.tm_hour * 3600) - (tmnow.tm_min * 60) - (tmnow.tm_sec);
+				beg_today = now.tv_sec - (tmnow.tm_hour * 3600) - (tmnow.tm_min * 60) - (tmnow.tm_sec);
 				if (beg_today < time) {
 					/* Today */
 					res = wait_file(chan,ints, "digits/today",lang);
@@ -7018,16 +7010,14 @@ static int ast_say_date_with_format_gr(struct ast_channel *chan, time_t time, co
 				 * language to say the date, with changes in what you say, depending
 				 * upon how recent the date is. XXX */
 			{
-				struct timeval now;
-				struct tm tmnow;
-				time_t beg_today, tt;
+				struct timeval now = ast_tvnow();
+				struct ast_tm tmnow;
+				time_t beg_today;
 				
-				gettimeofday(&now,NULL);
-				tt = now.tv_sec;
-				ast_localtime(&tt,&tmnow,timezone);
+				ast_localtime(&now, &tmnow, timezone);
 				/* This might be slightly off, if we transcend a leap second, but never more off than 1 second */
 				/* In any case, it saves not having to do ast_mktime() */
-				beg_today = tt - (tmnow.tm_hour * 3600) - (tmnow.tm_min * 60) - (tmnow.tm_sec);
+				beg_today = now.tv_sec - (tmnow.tm_hour * 3600) - (tmnow.tm_min * 60) - (tmnow.tm_sec);
 				if (beg_today < time) {
 					/* Today */
 				} else if ((beg_today - 86400) < time) {
@@ -7320,10 +7310,11 @@ tslis
 /* Georgian syntax. e.g. "oriatas xuti tslis 5 noemberi". */
 static int ast_say_date_ge(struct ast_channel *chan, time_t t, const char *ints, const char *lang)
 {
-	struct tm tm;
+	struct timeval tv = { t, 0 };
+	struct ast_tm tm;
 	char fn[256];
 	int res = 0;
-	ast_localtime(&t,&tm,NULL);
+	ast_localtime(&tv, &tm, NULL);
 
 	if (!res)
 		res = ast_say_number(chan, tm.tm_year + 1900, ints, lang, (char *) NULL);
@@ -7359,10 +7350,11 @@ static int ast_say_date_ge(struct ast_channel *chan, time_t t, const char *ints,
 /* Georgian syntax. e.g. "otxi saati da eqvsi tsuti" */
 static int ast_say_time_ge(struct ast_channel *chan, time_t t, const char *ints, const char *lang)
 {
-	struct tm tm;
+	struct timeval tv = { t, 0 };
+	struct ast_tm tm;
 	int res = 0;
 
-	ast_localtime(&t, &tm, NULL);
+	ast_localtime(&tv, &tm, NULL);
 
 	res = ast_say_number(chan, tm.tm_hour, ints, lang, (char*)NULL);
 	if (!res) {
@@ -7390,10 +7382,11 @@ static int ast_say_time_ge(struct ast_channel *chan, time_t t, const char *ints,
 /* Georgian syntax. Say date, then say time. */
 static int ast_say_datetime_ge(struct ast_channel *chan, time_t t, const char *ints, const char *lang)
 {
-	struct tm tm;
+	struct timeval tv = { t, 0 };
+	struct ast_tm tm;
 	int res = 0;
 
-	ast_localtime(&t, &tm, NULL);
+	ast_localtime(&tv, &tm, NULL);
 	res = ast_say_date(chan, t, ints, lang);
 	if (!res)
 		ast_say_time(chan, t, ints, lang);
@@ -7408,15 +7401,13 @@ static int ast_say_datetime_ge(struct ast_channel *chan, time_t t, const char *i
 static int ast_say_datetime_from_now_ge(struct ast_channel *chan, time_t t, const char *ints, const char *lang)
 {
 	int res=0;
-	time_t nowt;
 	int daydiff;
-	struct tm tm;
-	struct tm now;
+	struct ast_tm tm;
+	struct ast_tm now;
+	struct timeval tv = { t, 0 }, nowt = ast_tvnow();
 	char fn[256];
 
-	time(&nowt);
-
-	ast_localtime(&t, &tm, NULL);
+	ast_localtime(&tv, &tm, NULL);
 	ast_localtime(&nowt, &now, NULL);
 	daydiff = now.tm_yday - tm.tm_yday;
 	if ((daydiff < 0) || (daydiff > 6)) {
diff --git a/main/stdtime/localtime.c b/main/stdtime/localtime.c
index 18cd3d7d8a83aa86c8f6576297a80ecdda234275..4426feb97b3fdb5d783e36ebbb6b5580c48c26eb 100644
--- a/main/stdtime/localtime.c
+++ b/main/stdtime/localtime.c
@@ -180,25 +180,25 @@ static const char *	getsecs P((const char * strp, long * secsp));
 static const char *	getoffset P((const char * strp, long * offsetp));
 static const char *	getrule P((const char * strp, struct rule * rulep));
 static void		gmtload P((struct state * sp));
-static void		gmtsub P((const time_t * timep, long offset,
-				struct tm * tmp, const char * zone));
-static void		localsub P((const time_t * timep, long offset,
-				struct tm * tmp, const char * zone));
+static void		gmtsub P((const struct timeval * timep, long offset,
+				struct ast_tm * tmp, const char * zone));
+static void		localsub P((const struct timeval * timep, long offset,
+				struct ast_tm * tmp, const char * zone));
 static int		increment_overflow P((int * number, int delta));
 static int		normalize_overflow P((int * tensptr, int * unitsptr,
 				int base));
-static time_t		time1 P((struct tm * tmp,
-				void(*funcp) P((const time_t *,
-				long, struct tm *, const char*)),
+static time_t		time1 P((struct ast_tm * tmp,
+				void(*funcp) P((const struct timeval *,
+				long, struct ast_tm *, const char *)),
 				long offset, const char * zone));
-static time_t		time2 P((struct tm *tmp,
-				void(*funcp) P((const time_t *,
-				long, struct tm*, const char*)),
+static time_t		time2 P((struct ast_tm *tmp,
+				void(*funcp) P((const struct timeval *,
+				long, struct ast_tm *, const char *)),
 				long offset, int * okayp, const char * zone));
-static void		timesub P((const time_t * timep, long offset,
-				const struct state * sp, struct tm * tmp));
-static int		tmcomp P((const struct tm * atmp,
-				const struct tm * btmp));
+static void		timesub P((const struct timeval * timep, long offset,
+				const struct state * sp, struct ast_tm *tmp));
+static int		tmcomp P((const struct ast_tm *atmp,
+				const struct ast_tm * btmp));
 static time_t		transtime P((time_t janfirst, int year,
 				const struct rule * rulep, long offset));
 static int		tzload P((const char * name, struct state * sp));
@@ -982,15 +982,16 @@ ast_tzset P((const char *name))
 /*ARGSUSED*/
 static void
 localsub(timep, offset, tmp, zone)
-const time_t * const	timep;
+const struct timeval * const	timep;
 const long		offset;
-struct tm * const	tmp;
+struct ast_tm * const	tmp;
 const char * const	zone;
 {
 	register struct state *		sp;
 	register const struct ttinfo *	ttisp;
 	register int			i;
-	const time_t			t = *timep;
+	struct timeval	t;
+	memcpy(&t, timep, sizeof(t));
 
 	sp = lclptr;
 	/* Find the right zone record */
@@ -1019,7 +1020,7 @@ const char * const	zone;
 		gmtsub(timep, offset, tmp, zone);
 		return;
 	}
-	if (sp->timecnt == 0 || t < sp->ats[0]) {
+	if (sp->timecnt == 0 || t.tv_sec < sp->ats[0]) {
 		i = 0;
 		while (sp->ttis[i].tt_isdst)
 			if (++i >= sp->typecnt) {
@@ -1028,7 +1029,7 @@ const char * const	zone;
 			}
 	} else {
 		for (i = 1; i < sp->timecnt; ++i)
-			if (t < sp->ats[i])
+			if (t.tv_sec < sp->ats[i])
 				break;
 		i = sp->types[i - 1];
 	}
@@ -1045,12 +1046,13 @@ const char * const	zone;
 #ifdef TM_ZONE
 	tmp->TM_ZONE = &sp->chars[ttisp->tt_abbrind];
 #endif /* defined TM_ZONE */
+	tmp->tm_usec = timep->tv_usec;
 }
 
-struct tm *
+struct ast_tm *
 ast_localtime(timep, p_tm, zone)
-const time_t * const	timep;
-struct tm *p_tm;
+const struct timeval * const	timep;
+struct ast_tm *p_tm;
 const char * const	zone;
 {
 #ifdef _THREAD_SAFE
@@ -1070,9 +1072,9 @@ const char * const	zone;
 
 static void
 gmtsub(timep, offset, tmp, zone)
-const time_t * const	timep;
+const struct timeval * const	timep;
 const long		offset;
-struct tm * const	tmp;
+struct ast_tm * const	tmp;
 const char * const	zone;
 {
 #ifdef	_THREAD_SAFE
@@ -1104,10 +1106,10 @@ const char * const	zone;
 
 static void
 timesub(timep, offset, sp, tmp)
-const time_t * const			timep;
+const struct timeval * const	timep;
 const long				offset;
 register const struct state * const	sp;
-register struct tm * const		tmp;
+register struct ast_tm * const		tmp;
 {
 	register const struct lsinfo *	lp;
 	register long			days;
@@ -1124,8 +1126,8 @@ register struct tm * const		tmp;
 	i = (sp == NULL) ? 0 : sp->leapcnt;
 	while (--i >= 0) {
 		lp = &sp->lsis[i];
-		if (*timep >= lp->ls_trans) {
-			if (*timep == lp->ls_trans) {
+		if (timep->tv_sec >= lp->ls_trans) {
+			if (timep->tv_sec == lp->ls_trans) {
 				hit = ((i == 0 && lp->ls_corr > 0) ||
 					lp->ls_corr > sp->lsis[i - 1].ls_corr);
 				if (hit)
@@ -1142,10 +1144,10 @@ register struct tm * const		tmp;
 			break;
 		}
 	}
-	days = *timep / SECSPERDAY;
-	rem = *timep % SECSPERDAY;
+	days = timep->tv_sec / SECSPERDAY;
+	rem = timep->tv_sec % SECSPERDAY;
 #ifdef mc68k
-	if (*timep == 0x80000000) {
+	if (timep->tv_sec == 0x80000000) {
 		/*
 		** A 3B1 muffs the division on the most negative number.
 		*/
@@ -1196,28 +1198,16 @@ register struct tm * const		tmp;
 #ifdef TM_GMTOFF
 	tmp->TM_GMTOFF = offset;
 #endif /* defined TM_GMTOFF */
+	tmp->tm_usec = timep->tv_usec;
 }
 
 char *
-ast_ctime(timep)
-const time_t * const	timep;
-{
-/*
-** Section 4.12.3.2 of X3.159-1989 requires that
-**	The ctime funciton converts the calendar time pointed to by timer
-**	to local time in the form of a string.  It is equivalent to
-**		asctime(localtime(timer))
-*/
-	return asctime(localtime(timep));
-}
-
-char *
-ast_ctime_r(timep, buf)
-const time_t * const	timep;
+ast_ctime(timep, buf)
+const struct timeval * const	timep;
 char *buf;
 {
-        struct tm tm;
-	return asctime_r(ast_localtime(timep, &tm, NULL), buf);
+	struct ast_tm tm;
+	return asctime_r((struct tm *)ast_localtime(timep, &tm, NULL), buf);
 }
 
 /*
@@ -1267,8 +1257,8 @@ const int	base;
 
 static int
 tmcomp(atmp, btmp)
-register const struct tm * const atmp;
-register const struct tm * const btmp;
+register const struct ast_tm * const atmp;
+register const struct ast_tm * const btmp;
 {
 	register int	result;
 
@@ -1283,8 +1273,8 @@ register const struct tm * const btmp;
 
 static time_t
 time2(tmp, funcp, offset, okayp, zone)
-struct tm * const	tmp;
-void (* const		funcp) P((const time_t*, long, struct tm*, const char*));
+struct ast_tm * const	tmp;
+void (* const		funcp) P((const struct timeval *, long, struct ast_tm*, const char*));
 const long		offset;
 int * const		okayp;
 const char * const	zone;
@@ -1294,9 +1284,9 @@ const char * const	zone;
 	register int			bits;
 	register int			i, j ;
 	register int			saved_seconds;
-	time_t				newt;
-	time_t				t;
-	struct tm			yourtm, mytm;
+	struct timeval			newt = { 0, 0 };
+	struct timeval			t = { 0, 0 };
+	struct ast_tm			yourtm, mytm;
 
 	*okayp = FALSE;
 	yourtm = *tmp;
@@ -1364,7 +1354,7 @@ const char * const	zone;
 	** assuming two's complement arithmetic.
 	** If time_t is unsigned, then (1 << bits) is just above the median.
 	*/
-	t = TYPE_SIGNED(time_t) ? 0 : (((time_t) 1) << bits);
+	t.tv_sec = 0;
 	for ( ; ; ) {
 		(*funcp)(&t, offset, &mytm, zone);
 		dir = tmcomp(&mytm, &yourtm);
@@ -1372,10 +1362,10 @@ const char * const	zone;
 			if (bits-- < 0)
 				return WRONG;
 			if (bits < 0)
-				--t; /* may be needed if new t is minimal */
+				--t.tv_sec; /* may be needed if new t is minimal */
 			else if (dir > 0)
-				t -= ((time_t) 1) << bits;
-			else	t += ((time_t) 1) << bits;
+				t.tv_sec -= ((time_t) 1) << bits;
+			else	t.tv_sec += ((time_t) 1) << bits;
 			continue;
 		}
 		if (yourtm.tm_isdst < 0 || mytm.tm_isdst == yourtm.tm_isdst)
@@ -1400,7 +1390,7 @@ const char * const	zone;
 			for (j = sp->typecnt - 1; j >= 0; --j) {
 				if (sp->ttis[j].tt_isdst == yourtm.tm_isdst)
 					continue;
-				newt = t + sp->ttis[j].tt_gmtoff -
+				newt.tv_sec = t.tv_sec + sp->ttis[j].tt_gmtoff -
 					sp->ttis[i].tt_gmtoff;
 				(*funcp)(&newt, offset, &mytm, zone);
 				if (tmcomp(&mytm, &yourtm) != 0)
@@ -1417,19 +1407,19 @@ const char * const	zone;
 		return WRONG;
 	}
 label:
-	newt = t + saved_seconds;
-	if ((newt < t) != (saved_seconds < 0))
+	newt.tv_sec = t.tv_sec + saved_seconds;
+	if ((newt.tv_sec < t.tv_sec) != (saved_seconds < 0))
 		return WRONG;
-	t = newt;
+	t.tv_sec = newt.tv_sec;
 	(*funcp)(&t, offset, tmp, zone);
 	*okayp = TRUE;
-	return t;
+	return t.tv_sec;
 }
 
 static time_t
 time1(tmp, funcp, offset, zone)
-struct tm * const	tmp;
-void (* const		funcp) P((const time_t *, long, struct tm *, const char*));
+struct ast_tm * const	tmp;
+void (* const		funcp) P((const struct timeval *, long, struct ast_tm *, const char*));
 const long		offset;
 const char * const	zone;
 {
@@ -1489,7 +1479,7 @@ const char * const	zone;
 
 time_t
 ast_mktime(tmp,zone)
-struct tm * const	tmp;
+struct ast_tm * const	tmp;
 const char * const	zone;
 {
 	time_t mktime_return_value;
@@ -1504,3 +1494,62 @@ const char * const	zone;
 	return(mktime_return_value);
 }
 
+int ast_strftime(char *buf, size_t len, const char *tmp, const struct ast_tm *tm)
+{
+	size_t fmtlen = strlen(tmp) + 1;
+	char *format = ast_calloc(1, fmtlen), *fptr = format, *newfmt;
+	int decimals = -1, i, res;
+	long fraction;
+
+	if (!format)
+		return -1;
+	for (; *tmp; tmp++) {
+		if (*tmp == '%') {
+			switch (tmp[1]) {
+			case '1':
+			case '2':
+			case '3':
+			case '4':
+			case '5':
+			case '6':
+				if (tmp[2] != 'q')
+					goto defcase;
+				decimals = tmp[1] - '0';
+				tmp++;
+				/* Fall through */
+			case 'q': /* Milliseconds */
+				if (decimals == -1)
+					decimals = 3;
+
+				/* Juggle some memory to fit the item */
+				newfmt = ast_realloc(format, fmtlen + decimals);
+				if (!newfmt) {
+					ast_free(format);
+					return -1;
+				}
+				fptr = fptr - format + newfmt;
+				format = newfmt;
+				fmtlen += decimals;
+
+				/* Reduce the fraction of time to the accuracy needed */
+				for (i = 6, fraction = tm->tm_usec; i > decimals; i--)
+					fraction /= 10;
+				fptr += sprintf(fptr, "%0*ld", decimals, fraction);
+
+				/* Reset, in case more than one 'q' specifier exists */
+				decimals = -1;
+				tmp++;
+				break;
+			default:
+				goto defcase;
+			}
+		} else
+defcase:	*fptr++ = *tmp;
+	}
+	*fptr = '\0';
+#undef strftime
+	res = (int)strftime(buf, len, format, (struct tm *)tm);
+	ast_free(format);
+	return res;
+}
+
diff --git a/main/utils.c b/main/utils.c
index 65139356dc5aeb1d78d0cd3bd819fc78a5872ee5..d719837af6ea7beeda2c3009dcd0de4903d9a651 100644
--- a/main/utils.c
+++ b/main/utils.c
@@ -973,6 +973,33 @@ int ast_atomic_fetchadd_int_slow(volatile int *p, int v)
         return ret;
 }
 
+/*! \brief
+ * get values from config variables.
+ */
+int ast_get_timeval(const char *src, struct timeval *dst, struct timeval _default, int *consumed)
+{
+	long double dtv = 0.0;
+	int scanned;
+
+	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, "%Lf%n", &dtv, &scanned) > 0) {
+		dst->tv_sec = dtv;
+		dst->tv_usec = (dtv - dst->tv_sec) * 1000000.0;
+		if (consumed)
+			*consumed = scanned;
+		return 0;
+	} else
+		return -1;
+}
+
 /*! \brief
  * get values from config variables.
  */
diff --git a/res/snmp/agent.c b/res/snmp/agent.c
index d7d0955b0b92d5210f40d136758535be700a2db5..6d497183b3e3b2de18a19eafe602eb9075bfed32 100644
--- a/res/snmp/agent.c
+++ b/res/snmp/agent.c
@@ -575,14 +575,14 @@ static u_char *ast_var_Config(struct variable *vp, oid *name, size_t *length,
 	switch (vp->magic) {
 	case ASTCONFUPTIME:
 		gettimeofday(&tval, NULL);
-		long_ret = difftime(tval.tv_sec, ast_startuptime) * 100 + tval.tv_usec / 10000;
+		long_ret = difftime(tval.tv_sec, ast_startuptime.tv_sec) * 100 + tval.tv_usec / 10000 - ast_startuptime.tv_usec / 10000;
 		return (u_char *)&long_ret;
 	case ASTCONFRELOADTIME:
 		gettimeofday(&tval, NULL);
-		if (ast_lastreloadtime)
-			long_ret = difftime(tval.tv_sec, ast_lastreloadtime) * 100 + tval.tv_usec / 10000;
+		if (ast_lastreloadtime.tv_sec)
+			long_ret = difftime(tval.tv_sec, ast_lastreloadtime.tv_sec) * 100 + tval.tv_usec / 10000 - ast_lastreloadtime.tv_usec / 10000;
 		else
-			long_ret = difftime(tval.tv_sec, ast_startuptime) * 100 + tval.tv_usec / 10000;
+			long_ret = difftime(tval.tv_sec, ast_startuptime.tv_sec) * 100 + tval.tv_usec / 10000 - ast_startuptime.tv_usec / 10000;
 		return (u_char *)&long_ret;
 	case ASTCONFPID:
 		long_ret = getpid();