Refactor old date parsing into single method

We've managed to duplicate this four times at this point, so make it a
method in util.c instead.

Signed-off-by: Dan McGee <dan@archlinux.org>
This commit is contained in:
Dan McGee 2011-01-07 20:35:43 -06:00
parent d288240426
commit 4bc6ed56aa
5 changed files with 18 additions and 46 deletions

View File

@ -28,7 +28,6 @@
#include <stdint.h> /* intmax_t */
#include <sys/stat.h>
#include <dirent.h>
#include <ctype.h>
#include <time.h>
#include <limits.h> /* PATH_MAX */
#include <locale.h> /* setlocale */
@ -570,33 +569,13 @@ int _alpm_local_db_read(pmdb_t *db, pmpkg_t *info, pmdbinfrq_t inforeq)
goto error;
}
_alpm_strtrim(line);
char first = tolower((unsigned char)line[0]);
if(first > 'a' && first < 'z') {
struct tm tmp_tm = {0}; /* initialize to null in case of failure */
setlocale(LC_TIME, "C");
strptime(line, "%a %b %e %H:%M:%S %Y", &tmp_tm);
info->builddate = mktime(&tmp_tm);
setlocale(LC_TIME, "");
} else {
info->builddate = atol(line);
}
info->builddate = _alpm_parsedate(line);
} else if(strcmp(line, "%INSTALLDATE%") == 0) {
if(fgets(line, sizeof(line), fp) == NULL) {
goto error;
}
_alpm_strtrim(line);
char first = tolower((unsigned char)line[0]);
if(first > 'a' && first < 'z') {
struct tm tmp_tm = {0}; /* initialize to null in case of failure */
setlocale(LC_TIME, "C");
strptime(line, "%a %b %e %H:%M:%S %Y", &tmp_tm);
info->installdate = mktime(&tmp_tm);
setlocale(LC_TIME, "");
} else {
info->installdate = atol(line);
}
info->installdate = _alpm_parsedate(line);
} else if(strcmp(line, "%PACKAGER%") == 0) {
if(fgets(line, sizeof(line), fp) == NULL) {
goto error;

View File

@ -24,7 +24,6 @@
#include <stdlib.h>
#include <string.h>
#include <limits.h>
#include <ctype.h>
#include <locale.h> /* setlocale */
#include <errno.h>
@ -203,16 +202,7 @@ static int parse_descfile(struct archive *a, pmpkg_t *newpkg)
} else if(strcmp(key, "license") == 0) {
newpkg->licenses = alpm_list_add(newpkg->licenses, strdup(ptr));
} else if(strcmp(key, "builddate") == 0) {
char first = tolower((unsigned char)ptr[0]);
if(first > 'a' && first < 'z') {
struct tm tmp_tm = {0}; /* initialize to null in case of failure */
setlocale(LC_TIME, "C");
strptime(ptr, "%a %b %e %H:%M:%S %Y", &tmp_tm);
newpkg->builddate = mktime(&tmp_tm);
setlocale(LC_TIME, "");
} else {
newpkg->builddate = atol(ptr);
}
newpkg->builddate = _alpm_parsedate(ptr);
} else if(strcmp(key, "packager") == 0) {
STRDUP(newpkg->packager, ptr, RET_ERR(PM_ERR_MEMORY, -1));
} else if(strcmp(key, "arch") == 0) {

View File

@ -21,7 +21,6 @@
#include "config.h"
#include <errno.h>
#include <ctype.h>
#include <locale.h>
#include <limits.h>
@ -313,17 +312,7 @@ static int sync_db_read(pmdb_t *db, struct archive *archive, struct archive_entr
READ_AND_STORE(pkg->arch);
} else if(strcmp(line, "%BUILDDATE%") == 0) {
READ_NEXT(line);
char first = tolower((unsigned char)line[0]);
if(first > 'a' && first < 'z') {
/* initialize to null in case of failure */
struct tm tmp_tm = {0};
setlocale(LC_TIME, "C");
strptime(line, "%a %b %e %H:%M:%S %Y", &tmp_tm);
pkg->builddate = mktime(&tmp_tm);
setlocale(LC_TIME, "");
} else {
pkg->builddate = atol(line);
}
pkg->builddate = _alpm_parsedate(line);
} else if(strcmp(line, "%PACKAGER%") == 0) {
READ_AND_STORE(pkg->packager);
} else if(strcmp(line, "%CSIZE%") == 0) {

View File

@ -923,4 +923,17 @@ unsigned long _alpm_hash_sdbm(const char *str)
return(hash);
}
long _alpm_parsedate(const char *line)
{
if(isalpha((unsigned char)line[0])) {
/* initialize to null in case of failure */
struct tm tmp_tm = { 0 };
setlocale(LC_TIME, "C");
strptime(line, "%a %b %e %H:%M:%S %Y", &tmp_tm);
setlocale(LC_TIME, "");
return(mktime(&tmp_tm));
}
return(atol(line));
}
/* vim: set ts=2 sw=2 noet: */

View File

@ -95,6 +95,7 @@ int _alpm_test_md5sum(const char *filepath, const char *md5sum);
int _alpm_archive_fgets(struct archive *a, struct archive_read_buffer *b);
int _alpm_splitname(const char *target, pmpkg_t *pkg);
unsigned long _alpm_hash_sdbm(const char *str);
long _alpm_parsedate(const char *line);
#ifndef HAVE_STRSEP
char *strsep(char **, const char *);