summaryrefslogtreecommitdiff
path: root/src/pmdate
diff options
context:
space:
mode:
authorIgor Pashev <pashev.igor@gmail.com>2014-10-26 12:33:50 +0400
committerIgor Pashev <pashev.igor@gmail.com>2014-10-26 12:33:50 +0400
commit47e6e7c84f008a53061e661f31ae96629bc694ef (patch)
tree648a07f3b5b9d67ce19b0fd72e8caa1175c98f1a /src/pmdate
downloadpcp-debian/3.9.10.tar.gz
Debian 3.9.10debian/3.9.10debian
Diffstat (limited to 'src/pmdate')
-rw-r--r--src/pmdate/GNUmakefile31
-rw-r--r--src/pmdate/pmdate.c152
2 files changed, 183 insertions, 0 deletions
diff --git a/src/pmdate/GNUmakefile b/src/pmdate/GNUmakefile
new file mode 100644
index 0000000..b5ede4f
--- /dev/null
+++ b/src/pmdate/GNUmakefile
@@ -0,0 +1,31 @@
+#
+# Copyright (c) 2000,2004 Silicon Graphics, Inc. All Rights Reserved.
+#
+# This program is free software; you can redistribute it and/or modify it
+# under the terms of the GNU General Public License as published by the
+# Free Software Foundation; either version 2 of the License, or (at your
+# option) any later version.
+#
+# This program is distributed in the hope that it will be useful, but
+# WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
+# or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
+# for more details.
+#
+
+TOPDIR = ../..
+include $(TOPDIR)/src/include/builddefs
+
+CFILES = pmdate.c
+LLDLIBS = $(PCPLIB)
+CMDTARGET = pmdate$(EXECSUFFIX)
+
+default: $(CMDTARGET)
+
+include $(BUILDRULES)
+
+install: default
+ $(INSTALL) -m 755 $(CMDTARGET) $(PCP_BIN_DIR)/$(CMDTARGET)
+
+default_pcp: default
+
+install_pcp: install
diff --git a/src/pmdate/pmdate.c b/src/pmdate/pmdate.c
new file mode 100644
index 0000000..acec4a6
--- /dev/null
+++ b/src/pmdate/pmdate.c
@@ -0,0 +1,152 @@
+/*
+ * Copyright (c) 1995-2002 Silicon Graphics, Inc. All Rights Reserved.
+ *
+ * This program is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License as published by the
+ * Free Software Foundation; either version 2 of the License, or (at your
+ * option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
+ * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
+ * for more details.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with this program; if not, write to the Free Software Foundation, Inc.,
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
+ *
+ * Display offset date
+ */
+
+#include "pmapi.h"
+#include "impl.h"
+
+#define usage "Usage: pmdate { +valueS | -valueS } ... format\n\
+\n\
+where the scale \"S\" is one of: S (seconds), M (minutes), H (hours), \n\
+d (days), m (months) or y (years)\n"
+
+int
+main(int argc, char *argv[])
+{
+ time_t now;
+ int need;
+ char *buf;
+ char *p;
+ char *pend;
+ struct tm *tmp;
+ int sgn;
+ int val;
+ int mo_delta = 0;
+ int yr_delta = 0;
+
+ __pmSetProgname(argv[0]);
+
+ if (argc < 2) {
+ fprintf(stderr, usage);
+ exit(1);
+ }
+
+ if (strcmp(argv[1], "-?") == 0 || strcmp(argv[1], "--help") == 0) {
+ fprintf(stderr, usage);
+ exit(1);
+ }
+
+ time(&now);
+
+ while (argc > 2) {
+ p = argv[1];
+ if (*p == '+')
+ sgn = 1;
+ else if (*p == '-')
+ sgn = -1;
+ else {
+ fprintf(stderr, "%s: incorrect leading sign for offset (%s), must be \"+\" or \"-\"\n",
+ pmProgname, argv[1]);
+ exit(1);
+ }
+ p++;
+
+ val = (int)strtol(p, &pend, 10);
+ switch (*pend) {
+ case 'S':
+ now += sgn * val;
+ break;
+ case 'M':
+ now += sgn * val * 60;
+ break;
+ case 'H':
+ now += sgn * val * 60 * 60;
+ break;
+ case 'd':
+ now += sgn * val * 24 * 60 * 60;
+ break;
+ case 'm':
+ mo_delta += sgn*val;
+ break;
+ case 'y':
+ yr_delta += sgn*val;
+ break;
+ case '\0':
+ fprintf(stderr, "%s: missing scale after offset (%s)\n", pmProgname, argv[1]);
+ exit(1);
+ case '?':
+ fprintf(stderr, usage);
+ exit (1);
+ default:
+ fprintf(stderr, "%s: unknown scale after offset (%s)\n", pmProgname, argv[1]);
+ exit(1);
+ }
+
+ argv++;
+ argc--;
+ }
+
+ tmp = localtime(&now);
+
+ if (yr_delta) {
+ /*
+ * tm_year is years since 1900 and yr_delta is relative (not
+ * absolute), so this is Y2K safe
+ */
+ tmp->tm_year += yr_delta;
+ }
+ if (mo_delta) {
+ /*
+ * tm_year is years since 1900 and the tm_year-- and
+ * tm_year++ is adjusting for underflow and overflow in
+ * tm_mon as a result of relative month delta, so this
+ * is Y2K safe
+ */
+ tmp->tm_mon += mo_delta;
+ while (tmp->tm_mon < 0) {
+ tmp->tm_mon += 12;
+ tmp->tm_year--;
+ }
+ while (tmp->tm_mon > 12) {
+ tmp->tm_mon -= 12;
+ tmp->tm_year++;
+ }
+ }
+
+ /*
+ * Note: 256 is _more_ than enough to accommodate the longest
+ * value for _every_ %? lexicon that strftime() understands
+ */
+ need = strlen(argv[1]) + 256;
+ if ((buf = (char *)malloc(need)) == NULL) {
+ fprintf(stderr, "%s: malloc failed\n", pmProgname);
+ exit(1);
+ }
+
+ if (strftime(buf, need, argv[1], tmp) == 0) {
+ fprintf(stderr, "%s: format too long\n", pmProgname);
+ exit(1);
+ }
+ else {
+ buf[need-1] = '\0';
+ printf("%s\n", buf);
+ exit(0);
+ }
+
+}