Sponsored Content
Top Forums Shell Programming and Scripting want to create directory with the previous date Post 302146090 by prowla on Sunday 18th of November 2007 01:45:14 PM
Old 11-18-2007
Aha - date arithmetic.
You could set up an array with 28/29/30/31 days lookups for the months and leapyear calculations.

I once wrote a C program called timecalc. Here it is, if it's of any use:
Code:
/*
 * Name:
 *	timecalc.c
 *
 * Purpose:
 *	Time calculations.
 #
 * Usage:
 *	timecalc [+|-]n [seconds|minutes|hours|days|weeks]
 *
 * History:
 *	050713	GZB	Original.
 *
 */

static char *gsVersion="@(#)timecalc.c GZB 050713";

#include <time.h>
#include <stdlib.h>
#include <sys/time.h>

int gbDebug=0;
int gbVerbose=0;
double gfTimeDiff=0;	/* What to change the time by. */
int giTimeDiff=0;	/* What to change the time by. */
int giTimeUnits=1;	/* Time units scaled to seconds. */
char gsDateSep[10]="/";	/* Date separator. */
char gsTimeSep[10]=":";	/* Time separator. */
char gsDtoTSep[10]=" ";	/* Separator between date & time. */


Usage()
{
	printf("usage: timecalc [+|-]n [seconds|minutes|hours|days|weeks]\n");
	exit(1);
}


CommandArgs(argc,argv)
	int argc;
	char **argv;
{
	int i;

	if (gbDebug) printf("CommandArgs()\n");

	for ( i=1; i<argc; i++ )
	{
		if (gbDebug) printf("%d/%d\n", i, argc);

		switch (*argv[i])
		{
			case '+': gfTimeDiff=atof(argv[i]+1); break;
			case '-': gfTimeDiff=-atof(argv[i]+1); break;
			case 's': giTimeUnits=1; break;
			case 'm': giTimeUnits=60; break;
			case 'h': giTimeUnits=60*60; break;
			case 'd': giTimeUnits=60*60*24; break;
			case 'w': giTimeUnits=60*60*24*7; break;
			case 'D': gbDebug=1; break;
			case 'V': gbVerbose=1; break;
			/* case 'T': *gsDateSep='\0'; *gsTimeSep='\0'; break; */
			case 'T': strcpy(gsDateSep,""); strcpy(gsTimeSep,""); break;
			default: Usage(); break;
		}
	}

	/*
	giTimeDiff *= giTimeUnits;
	if (gbDebug) printf( "%d %d\n", giTimeDiff, giTimeUnits);
	*/

	giTimeDiff = (int)(gfTimeDiff * (double)giTimeUnits);
	if (gbDebug) printf( "%d %f %d\n", giTimeDiff, gfTimeDiff, giTimeUnits);
}


PrintTime(tp)
	struct tm *tp;
{
	printf("%04d%s%02d%s%02d%s%02d%s%02d%s%02d",
		tp->tm_year+1900,
		gsDateSep,
		tp->tm_mon+1,
		gsDateSep,
		tp->tm_mday,
		gsDtoTSep,
		tp->tm_hour,
		gsTimeSep,
		tp->tm_min,
		gsTimeSep,
		tp->tm_sec
		);
}


main(argc,argv)
	int argc;
	char **argv;
{
	time_t	t;
	time_t	tnew;
	struct tm *tp;

	t = (time_t)time((time_t *)0);
	if (gbDebug) printf("Seconds since epoch = %d\n", t);

	CommandArgs(argc,argv);

	tnew = t + giTimeDiff;
	if (gbDebug) printf("Seconds adjusted by %d = %d\n", giTimeDiff, tnew);

	if (gbVerbose)
	{
		tp = localtime(&t);
		PrintTime(tp);
		printf("\n");
		/* printf(" -> "); */
	}

	tp = localtime(&tnew);
	PrintTime(tp);
	printf("\n");
}

All usual disclaimers, non-warranties, and so-on apply!

Alternatively, you could try and make some clever use of the "cal" (calendar) program.
 

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

create directory named current date

Since this site solved my problems before, I am back for more (solutions) I down load via a script every day a file that has the same name as the file of the day before. I want to move that file to its own directory like: /archive/jul30 How do I capture the systems date in a script an... (2 Replies)
Discussion started by: flowrats
2 Replies

2. UNIX for Dummies Questions & Answers

Directory create date

Hi, How do you find the create date of a directory? I can see the modification date using ls -l but I'm looking for the create date. Many thanks Helen (2 Replies)
Discussion started by: Bab00shka
2 Replies

3. UNIX for Dummies Questions & Answers

Create Year directory, date subdirectory and archive the file

Hi, After checking all the UNIX threads, I am able to come up with a solution so far. I am working on a shell script where it moves the files to a certain directory. The conditions to check are 1) Check if the file exists in the current directory. 2) Check if the destination directory... (2 Replies)
Discussion started by: madhunk
2 Replies

4. UNIX for Dummies Questions & Answers

Create a directory using current date

Hi, I have a question, is there any way I can, when i create a directory, put the current date on it so that the directory name will be "name-current date"? just curious (3 Replies)
Discussion started by: aric87
3 Replies

5. Shell Programming and Scripting

Create a directory named as the current date

I am preparing a shell script to backup a few config files on a daily basis, with a retention of 30 days. Need some help with syntax/examples: The shell script (running as cron) would require the following: 1. create a sub-directory within a specified (backup) directory, in the format... (3 Replies)
Discussion started by: FeNiCrC_Neil
3 Replies

6. Shell Programming and Scripting

finding the previous day date and creating a file with date

Hi guys, I had a scenario... 1. I had to get the previous days date in yyyymmdd format 2. i had to create a file with Date inthe format yyyymmdd.txt format both are different thanks guys in advance.. (4 Replies)
Discussion started by: apple2685
4 Replies

7. Shell Programming and Scripting

Help with getting last date of previous month and first date of previous 4th month from current date

I have requirment to get last date of previous month and the first date of previous 4th month: Example: Current date: 20130320 (yyyymmdd) Last date of previous month: 20130228 (yyyymmdd) First date of previous 4th month: 20121101 (yyyymmdd) In my shell --date, -d, -v switches are not... (3 Replies)
Discussion started by: machomaddy
3 Replies

8. Shell Programming and Scripting

Create a directory with previous date

Hi All, can any body tell me how to create a new directory with previous time stamp in HP-UX?. e.g i am creating a file liketouch -t 07022013 kkk -rw-r----- 1 feeds sys 0 Jul 2 20:13 kkksame how can i create a directory? please help me. Thanks & Regards, Krupa (4 Replies)
Discussion started by: krupasindhu18
4 Replies

9. Shell Programming and Scripting

List files with date, create directory, move to the created directory

Hi all, i have a folder, with tons of files containing as following, on /my/folder/jobs/ some_name_2016-01-17-22-38-58_some name_0_0.zip.done some_name_2016-01-17-22-40-30_some name_0_0.zip.done some_name_2016-01-17-22-48-50_some name_0_0.zip.done and these can be lots of similar files,... (6 Replies)
Discussion started by: charli1
6 Replies

10. Shell Programming and Scripting

Bash to create new directory by date followed by identifier and additional subdirectories

I have a bash that downloads a list and if that list has data in it then a new main directory is created (with the date) with several subdirectories (example1, example2, example3). My question is in that list there are portion of specific file types (.vcf.gz) - identifier towards the end that have... (0 Replies)
Discussion started by: cmccabe
0 Replies
PARSE_TIME(3)						   BSD Library Functions Manual 					     PARSE_TIME(3)

NAME
parse_time, print_time_table, unparse_time, unparse_time_approx, -- parse and unparse time intervals LIBRARY
The roken library (libroken, -lroken) SYNOPSIS
#include <parse_time.h> int parse_time(const char *timespec, const char *def_unit); void print_time_table(FILE *f); size_t unparse_time(int seconds, char *buf, size_t len); size_t unparse_time_approx(int seconds, char *buf, size_t len); DESCRIPTION
The parse_time() function converts a the period of time specified in into a number of seconds. The timespec can be any number of <number unit> pairs separated by comma and whitespace. The number can be negative. Number without explicit units are taken as being def_unit. The unparse_time() and unparse_time_approx() does the opposite of parse_time(), that is they take a number of seconds and express that as human readable string. unparse_time produces an exact time, while unparse_time_approx restricts the result to only include one units. print_time_table() prints a descriptive list of available units on the passed file descriptor. The possible units include: second, s minute, m hour, h day week seven days month 30 days year 365 days Units names can be arbitrarily abbreviated (as long as they are unique). RETURN VALUES
parse_time() returns the number of seconds that represents the expression in timespec or -1 on error. unparse_time() and unparse_time_approx() return the number of characters written to buf. if the return value is greater than or equal to the len argument, the string was too short and some of the printed characters were discarded. EXAMPLES
#include <stdio.h> #include <parse_time.h> int main(int argc, char **argv) { int i; int result; char buf[128]; print_time_table(stdout); for (i = 1; i < argc; i++) { result = parse_time(argv[i], "second"); if(result == -1) { fprintf(stderr, "%s: parse error ", argv[i]); continue; } printf("-- "); printf("parse_time = %d ", result); unparse_time(result, buf, sizeof(buf)); printf("unparse_time = %s ", buf); unparse_time_approx(result, buf, sizeof(buf)); printf("unparse_time_approx = %s ", buf); } return 0; } $ ./a.out "1 minute 30 seconds" "90 s" "1 y -1 s" 1 year = 365 days 1 month = 30 days 1 week = 7 days 1 day = 24 hours 1 hour = 60 minutes 1 minute = 60 seconds 1 second -- parse_time = 90 unparse_time = 1 minute 30 seconds unparse_time_approx = 1 minute -- parse_time = 90 unparse_time = 1 minute 30 seconds unparse_time_approx = 1 minute -- parse_time = 31535999 unparse_time = 12 months 4 days 23 hours 59 minutes 59 seconds unparse_time_approx = 12 months BUGS
Since parse_time() returns -1 on error there is no way to parse "minus one second". Currently "s" at the end of units is ignored. This is a hack for English plural forms. If these functions are ever localised, this scheme will have to change. HEIMDAL
October 31, 2004 HEIMDAL
All times are GMT -4. The time now is 07:50 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy