2005-05-01 00:19:56 +04:00
|
|
|
#include "cache.h"
|
|
|
|
|
2009-08-31 06:26:46 +04:00
|
|
|
static const char *usage_msg = "\n"
|
|
|
|
" test-date show [time_t]...\n"
|
|
|
|
" test-date parse [date]...\n"
|
|
|
|
" test-date approxidate [date]...\n";
|
|
|
|
|
|
|
|
static void show_dates(char **argv, struct timeval *now)
|
2005-05-01 00:19:56 +04:00
|
|
|
{
|
2012-04-23 16:30:23 +04:00
|
|
|
struct strbuf buf = STRBUF_INIT;
|
2009-08-31 06:26:46 +04:00
|
|
|
|
|
|
|
for (; *argv; argv++) {
|
|
|
|
time_t t = atoi(*argv);
|
2012-04-23 16:30:23 +04:00
|
|
|
show_date_relative(t, 0, now, &buf);
|
|
|
|
printf("%s -> %s\n", *argv, buf.buf);
|
2009-08-31 06:26:46 +04:00
|
|
|
}
|
2012-04-23 16:30:23 +04:00
|
|
|
strbuf_release(&buf);
|
2009-08-31 06:26:46 +04:00
|
|
|
}
|
2005-05-01 00:19:56 +04:00
|
|
|
|
2009-08-31 06:26:46 +04:00
|
|
|
static void parse_dates(char **argv, struct timeval *now)
|
|
|
|
{
|
2014-08-27 11:57:08 +04:00
|
|
|
struct strbuf result = STRBUF_INIT;
|
|
|
|
|
2009-08-31 06:26:46 +04:00
|
|
|
for (; *argv; argv++) {
|
2010-07-06 11:54:33 +04:00
|
|
|
unsigned long t;
|
2010-07-04 14:48:35 +04:00
|
|
|
int tz;
|
2005-05-01 00:19:56 +04:00
|
|
|
|
2014-08-27 11:57:08 +04:00
|
|
|
strbuf_reset(&result);
|
|
|
|
parse_date(*argv, &result);
|
|
|
|
if (sscanf(result.buf, "%lu %d", &t, &tz) == 2)
|
2010-07-04 14:48:35 +04:00
|
|
|
printf("%s -> %s\n",
|
convert "enum date_mode" into a struct
In preparation for adding date modes that may carry extra
information beyond the mode itself, this patch converts the
date_mode enum into a struct.
Most of the conversion is fairly straightforward; we pass
the struct as a pointer and dereference the type field where
necessary. Locations that declare a date_mode can use a "{}"
constructor. However, the tricky case is where we use the
enum labels as constants, like:
show_date(t, tz, DATE_NORMAL);
Ideally we could say:
show_date(t, tz, &{ DATE_NORMAL });
but of course C does not allow that. Likewise, we cannot
cast the constant to a struct, because we need to pass an
actual address. Our options are basically:
1. Manually add a "struct date_mode d = { DATE_NORMAL }"
definition to each caller, and pass "&d". This makes
the callers uglier, because they sometimes do not even
have their own scope (e.g., they are inside a switch
statement).
2. Provide a pre-made global "date_normal" struct that can
be passed by address. We'd also need "date_rfc2822",
"date_iso8601", and so forth. But at least the ugliness
is defined in one place.
3. Provide a wrapper that generates the correct struct on
the fly. The big downside is that we end up pointing to
a single global, which makes our wrapper non-reentrant.
But show_date is already not reentrant, so it does not
matter.
This patch implements 3, along with a minor macro to keep
the size of the callers sane.
Signed-off-by: Jeff King <peff@peff.net>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
2015-06-25 19:55:02 +03:00
|
|
|
*argv, show_date(t, tz, DATE_MODE(ISO8601)));
|
2010-07-04 14:48:35 +04:00
|
|
|
else
|
|
|
|
printf("%s -> bad\n", *argv);
|
2009-08-31 06:26:46 +04:00
|
|
|
}
|
2014-08-27 11:57:08 +04:00
|
|
|
strbuf_release(&result);
|
2009-08-31 06:26:46 +04:00
|
|
|
}
|
2005-11-15 11:07:04 +03:00
|
|
|
|
2009-08-31 06:26:46 +04:00
|
|
|
static void parse_approxidate(char **argv, struct timeval *now)
|
|
|
|
{
|
|
|
|
for (; *argv; argv++) {
|
|
|
|
time_t t;
|
|
|
|
t = approxidate_relative(*argv, now);
|
convert "enum date_mode" into a struct
In preparation for adding date modes that may carry extra
information beyond the mode itself, this patch converts the
date_mode enum into a struct.
Most of the conversion is fairly straightforward; we pass
the struct as a pointer and dereference the type field where
necessary. Locations that declare a date_mode can use a "{}"
constructor. However, the tricky case is where we use the
enum labels as constants, like:
show_date(t, tz, DATE_NORMAL);
Ideally we could say:
show_date(t, tz, &{ DATE_NORMAL });
but of course C does not allow that. Likewise, we cannot
cast the constant to a struct, because we need to pass an
actual address. Our options are basically:
1. Manually add a "struct date_mode d = { DATE_NORMAL }"
definition to each caller, and pass "&d". This makes
the callers uglier, because they sometimes do not even
have their own scope (e.g., they are inside a switch
statement).
2. Provide a pre-made global "date_normal" struct that can
be passed by address. We'd also need "date_rfc2822",
"date_iso8601", and so forth. But at least the ugliness
is defined in one place.
3. Provide a wrapper that generates the correct struct on
the fly. The big downside is that we end up pointing to
a single global, which makes our wrapper non-reentrant.
But show_date is already not reentrant, so it does not
matter.
This patch implements 3, along with a minor macro to keep
the size of the callers sane.
Signed-off-by: Jeff King <peff@peff.net>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
2015-06-25 19:55:02 +03:00
|
|
|
printf("%s -> %s\n", *argv, show_date(t, 0, DATE_MODE(ISO8601)));
|
2009-08-31 06:26:46 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int main(int argc, char **argv)
|
|
|
|
{
|
|
|
|
struct timeval now;
|
|
|
|
const char *x;
|
|
|
|
|
|
|
|
x = getenv("TEST_DATE_NOW");
|
|
|
|
if (x) {
|
|
|
|
now.tv_sec = atoi(x);
|
|
|
|
now.tv_usec = 0;
|
2005-05-01 00:19:56 +04:00
|
|
|
}
|
2009-08-31 06:26:46 +04:00
|
|
|
else
|
|
|
|
gettimeofday(&now, NULL);
|
|
|
|
|
|
|
|
argv++;
|
|
|
|
if (!*argv)
|
|
|
|
usage(usage_msg);
|
|
|
|
if (!strcmp(*argv, "show"))
|
|
|
|
show_dates(argv+1, &now);
|
|
|
|
else if (!strcmp(*argv, "parse"))
|
|
|
|
parse_dates(argv+1, &now);
|
|
|
|
else if (!strcmp(*argv, "approxidate"))
|
|
|
|
parse_approxidate(argv+1, &now);
|
|
|
|
else
|
|
|
|
usage(usage_msg);
|
2005-05-01 00:19:56 +04:00
|
|
|
return 0;
|
|
|
|
}
|