See the little c program below that displays dates after applying your
favorite offset.
mktime() and strftime() do all of the heavy lifting.
example:
*> dateoff -d -1 -h -1 "%Y/%m/%d %H:%M:%S"
*
2013/03/13 14:20:33
(display the local time less a day and an hour)
Kirk Wolf
Dovetailed Technologies
http://dovetail.com
#define _LARGE_TIME_API
#define _XOPEN_SOURCE_EXTENDED 1
#include <stdlib.h>
#include <stdio.h>
#include <time.h>
/*
dateoff - display current date/time after applying optional offsets
to build: cc -o dateoff dateoff.c
Kirk Wolf - 2013/03/14
License: public domain
*/
void usage() {
fprintf(stderr, "usage: dateoff [-y|m|d|h|n|s] [-]<offset>] <format>\n");
}
#define RC_SYNTAX_ERROR 1
#define RC_SYSCALL_ERROR 2
int main(int argc, char**argv) {
int days_off = 0;
int years_off = 0;
int months_off = 0;
int hours_off = 0;
int minutes_off = 0;
int seconds_off = 0;
char* format;
time64_t timeval;
struct tm* bdt;
char formatted[100];
extern char* optarg;
int opt;
if (argc < 2) {
usage();
return RC_SYNTAX_ERROR;
}
format = argv[--argc]; /* last argument is a strftime() format string */
while ((opt = getopt(argc, argv, ":y:m:d:h:n:s:")) != -1) {
switch(opt) {
case 'y':
years_off = atoi(optarg);
break;
case 'm':
months_off = atoi(optarg);
break;
case 'd':
days_off = atoi(optarg);
break;
case 'h':
hours_off = atoi(optarg);
break;
case 'n':
minutes_off = atoi(optarg);
break;
case 's':
seconds_off = atoi(optarg);
break;
case ':':
fprintf(stderr, "Missing argument for option: %c\n", optopt);
usage();
exit(RC_SYNTAX_ERROR);
default:
fprintf(stderr, "Unknown option: %c\n", optopt);
usage();
exit(RC_SYNTAX_ERROR);
}
}
if (time64(&timeval) < 0) {
perror("time64");
return RC_SYSCALL_ERROR;
}
if (!(bdt = localtime64(&timeval))) {
perror("localtime64");
return RC_SYSCALL_ERROR;
}
bdt->tm_mday += days_off;
bdt->tm_year += years_off;
bdt->tm_mon += months_off;
bdt->tm_hour += hours_off;
bdt->tm_min += minutes_off;
bdt->tm_sec += seconds_off;
timeval = mktime64(bdt);
if (timeval < 0) {
perror("mktime64");
return RC_SYSCALL_ERROR;
}
if (!strftime(formatted, sizeof(formatted)-1, format, bdt) ) {
perror("strftime");
return RC_SYSCALL_ERROR;
}
printf("%s\n",formatted);
return 0;
}
----------------------------------------------------------------------
For IBM-MAIN subscribe / signoff / archive access instructions,
send email to [email protected] with the message: INFO IBM-MAIN