#endif
/* External function to parse a date/time string (from getdate.y) */
-time_t get_date(const char *);
+time_t get_date(time_t, const char *);
static void long_help(struct bsdtar *);
static void only_mode(struct bsdtar *, const char *opt,
char option_o;
char possible_help_request;
char buff[16];
+ time_t now;
/*
* Use a pointer for consistency, but stack-allocated storage
bsdtar->progname = *argv;
}
+ time(&now);
+
if (setlocale(LC_ALL, "") == NULL)
bsdtar_warnc(bsdtar, 0, "Failed to set default locale");
#if defined(HAVE_NL_LANGINFO) && defined(HAVE_D_MD_ORDER)
* TODO: Add corresponding "older" options to reverse these.
*/
case OPTION_NEWER_CTIME: /* GNU tar */
- bsdtar->newer_ctime_sec = get_date(bsdtar->optarg);
+ bsdtar->newer_ctime_sec = get_date(now, bsdtar->optarg);
break;
case OPTION_NEWER_CTIME_THAN:
{
}
break;
case OPTION_NEWER_MTIME: /* GNU tar */
- bsdtar->newer_mtime_sec = get_date(bsdtar->optarg);
+ bsdtar->newer_mtime_sec = get_date(now, bsdtar->optarg);
break;
case OPTION_NEWER_MTIME_THAN:
{
#include <time.h>
/* This file defines a single public function. */
-time_t get_date(char *);
+time_t get_date(time_t now, char *);
/* Basic time units. */
#define EPOCH 1970
* TODO: tokens[] array should be dynamically sized.
*/
time_t
-get_date(char *p)
+get_date(time_t now, char *p)
{
struct token tokens[256];
struct gdstate _gds;
struct tm gmt, *gmt_ptr;
time_t Start;
time_t tod;
- time_t nowtime;
long tzone;
/* Clear out the parsed token array. */
memset(&_gds, 0, sizeof(_gds));
gds = &_gds;
- (void)time (&nowtime);
-
/* Look up the current time. */
memset(&local, 0, sizeof(local));
- tm = localtime (&nowtime);
+ tm = localtime (&now);
if (tm == NULL)
return -1;
local = *tm;
/* Look up UTC if we can and use that to determine the current
* timezone offset. */
memset(&gmt, 0, sizeof(gmt));
- gmt_ptr = gmtime (&nowtime);
+ gmt_ptr = gmtime (&now);
if (gmt_ptr != NULL) {
/* Copy, in case localtime and gmtime use the same buffer. */
gmt = *gmt_ptr;
if (Start < 0)
return -1;
} else {
- Start = nowtime;
+ Start = now;
if (!gds->HaveRel)
Start -= local.tm_hour * HOUR + local.tm_min * MINUTE
+ local.tm_sec;
* Verify that the getdate() function works.
*/
-time_t get_date(const char *);
+time_t get_date(time_t, const char *);
DEFINE_TEST(test_getdate)
{
time_t dayStart = now
- ((tm.tm_hour * 60L + tm.tm_min) * 60L + tm.tm_sec);
- assertEqualInt(get_date("Jan 1, 1970 UTC"), 0);
- assertEqualInt(get_date("7:12:18-0530 4 May 1983"), 420900138);
- assertEqualInt(get_date("2004/01/29 513 mest"), 1075345980);
- assertEqualInt(get_date("99/02/17 7pm utc"), 919278000);
- assertEqualInt(get_date("02/17/99 7:11am est"), 919253460);
+ assertEqualInt(get_date(now, "Jan 1, 1970 UTC"), 0);
+ assertEqualInt(get_date(now, "7:12:18-0530 4 May 1983"), 420900138);
+ assertEqualInt(get_date(now, "2004/01/29 513 mest"), 1075345980);
+ assertEqualInt(get_date(now, "99/02/17 7pm utc"), 919278000);
+ assertEqualInt(get_date(now, "02/17/99 7:11am est"), 919253460);
/* It's important that we handle ctime() format. */
- assertEqualInt(get_date("Sun Feb 22 17:38:26 PST 2009"), 1235353106);
+ assertEqualInt(get_date(now, "Sun Feb 22 17:38:26 PST 2009"),
+ 1235353106);
/* Basic relative offsets. */
- assertEqualInt(get_date("tomorrow"), now + 24 * 60 * 60);
- assertEqualInt(get_date("yesterday"), now - 24 * 60 * 60);
- assertEqualInt(get_date("now + 1 hour"), now + 60 * 60);
- assertEqualInt(get_date("now + 1 hour + 1 minute"),
+ assertEqualInt(get_date(now, "tomorrow"), now + 24 * 60 * 60);
+ assertEqualInt(get_date(now, "yesterday"), now - 24 * 60 * 60);
+ assertEqualInt(get_date(now, "now + 1 hour"), now + 60 * 60);
+ assertEqualInt(get_date(now, "now + 1 hour + 1 minute"),
now + 60 * 60 + 60);
/* "tuesday" is the start of the first tuesday today or later */
- assertEqualInt(get_date("tuesday"),
+ assertEqualInt(get_date(now, "tuesday"),
dayStart + ((2 - tm.tm_wday + 7) % 7) * 24 * 60 * 60);
/* "next tuesday" is one week after "tuesday" */
- assertEqualInt(get_date("next tuesday"),
+ assertEqualInt(get_date(now, "next tuesday"),
dayStart + (((2 - tm.tm_wday + 7) % 7) + 7) * 24 * 60 * 60);
/* "last tuesday" is one week before "tuesday" */
- assertEqualInt(get_date("last tuesday"),
+ assertEqualInt(get_date(now, "last tuesday"),
dayStart + (((2 - tm.tm_wday + 7) % 7) - 7) * 24 * 60 * 60);
- assertEqualInt(get_date("tomorrow 5:16am"),
+ assertEqualInt(get_date(now, "tomorrow 5:16am"),
dayStart + 24 * 60 * 60 + 5 * 60 * 60 + 16 * 60);
- assertEqualInt(get_date("5:16am tomorrow"),
+ assertEqualInt(get_date(now, "5:16am tomorrow"),
dayStart + 24 * 60 * 60 + 5 * 60 * 60 + 16 * 60);
/* TODO: Lots more tests here. */
}