]> git.ipfire.org Git - thirdparty/libarchive.git/commitdiff
Make the current time be an argument to get_date(), to facilitate
authorTim Kientzle <kientzle@gmail.com>
Mon, 23 Feb 2009 01:47:28 +0000 (20:47 -0500)
committerTim Kientzle <kientzle@gmail.com>
Mon, 23 Feb 2009 01:47:28 +0000 (20:47 -0500)
testing.  In particular, this should make it easier to test
relative time names such as "3am next tuesday".

SVN-Revision: 701

tar/bsdtar.c
tar/getdate.c
tar/test/test_getdate.c

index 3fbb4e7dc9ede1203700ddec28e9971732443d0f..2bdfb68f94d4c4e2a686897a1d070abac6d3e89d 100644 (file)
@@ -82,7 +82,7 @@ __FBSDID("$FreeBSD: src/usr.bin/tar/bsdtar.c,v 1.93 2008/11/08 04:43:24 kientzle
 #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,
@@ -103,6 +103,7 @@ main(int argc, char **argv)
        char                     option_o;
        char                     possible_help_request;
        char                     buff[16];
+       time_t                   now;
 
        /*
         * Use a pointer for consistency, but stack-allocated storage
@@ -132,6 +133,8 @@ main(int argc, char **argv)
                        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)
@@ -306,7 +309,7 @@ main(int argc, char **argv)
                 * 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:
                        {
@@ -320,7 +323,7 @@ main(int argc, char **argv)
                        }
                        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:
                        {
index 37be8d54f7c05411400f0c0e0ae7154d68493f37..5854f48176dd734cb2f94a18695ac74f107fdf2d 100644 (file)
@@ -39,7 +39,7 @@ __FBSDID("$FreeBSD$");
 #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
@@ -883,7 +883,7 @@ difftm (struct tm *a, struct tm *b)
  * 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;
@@ -893,7 +893,6 @@ get_date(char *p)
        struct tm       gmt, *gmt_ptr;
        time_t          Start;
        time_t          tod;
-       time_t          nowtime;
        long            tzone;
 
        /* Clear out the parsed token array. */
@@ -902,11 +901,9 @@ get_date(char *p)
        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;
@@ -914,7 +911,7 @@ get_date(char *p)
        /* 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;
@@ -964,7 +961,7 @@ get_date(char *p)
                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;
index deaedf63aaa8691f8a7fe9c21defaaf9eb36e644..7b1c260acc5844d22a942d3d2840c0dcfc3014ec 100644 (file)
@@ -31,7 +31,7 @@ __FBSDID("$FreeBSD: src/usr.bin/tar/test/test_getdate.c,v 1.2 2008/05/26 17:10:1
  * Verify that the getdate() function works.
  */
 
-time_t get_date(const char *);
+time_t get_date(time_t, const char *);
 
 DEFINE_TEST(test_getdate)
 {
@@ -40,31 +40,32 @@ 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. */
 }