Working with bash and date

Hello all,

I'm trying to substract 1 minute from the current date and take the hour and minute (for filename purpose).

1) If I want hour an minute from current time I can use:

timetmp=$(date +"%H:%M")

2) To substract 1 minute from current time I can use:

timetmp=$(date --date "$dte -1 minute")

Well now, how can I extract the minute and hour from example 2 like on example 1?

Could some1 please help me with this (i hope) simple trouble?
Thanks

timetmp=$(date --date "$dte -1 minute" +"%H:%M")
1 Like

omg... I realize right now that it was very simple!!! :wall:

I used:
timetmp=$(date +%H%M --date "$dte -1 minute")

Btw, thanks Chubler_XL! :wink:

BTW if you're doing a bit of date calculations a good trick is to convert your dates to seconds past epoch they you can add/subtract seconds as you like using shell, or even find the difference between two dates.

From date to epoch time date -d Jan-10-2011 +%s
From epoch time to date date -d @1294581600 +%b-%d-%Y

1 Like

If you have not GNU date, then ex. ksh93 is a solution:

epoc=$(printf "%(%s)T" "2010-10-24")
epoc=$(printf "%(%s)T" "2010-10-24 00:00:00")
epoc=$(printf "%(%s)T" "10/24/2010 00:00:00")
# day is 86400 s (60*60*24)
((yesterday=epoc-86400))
printf  "%(%Y-%m-%d)T"     "#$yesterday"
1 Like

Guys, please help.
I make all my test under ubuntu and redhat 5 and all working fine.
The problem is that my script should run under SunOS where the bash version is:
GNU Bash-2.05

SunOS version is
SunOS rmcs 5.9 Generic_118558-09 sun4u sparc SUNW,Sun-Fire-V440

So the previous commands doesn't works. It show me the HHMM but it doesn't substract any time!!!

root@rms /# timetmp=$(date +%H%M --date "$dte -1 minute")
root@rms /# echo $timetmp
1307
root@rms /# date
Thu Jan 20 13:07:31 CET 2011

Is there any other commands??

date under solaris doesn't have some of the nice features...

you could try:

#  date;perl -e 'print scalar(localtime(time()-60))."\n"'
Thu Jan 20 12:24:32 GMT 2011
Thu Jan 20 12:23:32 2011

or have a look at the FAQ: http://www.unix.com/answers-frequently-asked-questions/13785-yesterdays-date-date-arithmetic.html

HTH

If you have a C compiler available on solaris I've written a little C program that prints a time with secs adjustment (positive or negative) you can also specify the output format but it defaults to %d/%b/%Y %T

dateadj.c

#include <time.h>
#include <string.h>
#include <stdlib.h>
int main(int argc, char *argv[]) {
   long long adj = 0;
   char res[1024], fmt[1024] = "%d/%b/%Y %T";
   time_t secs_now;
   struct tm *time_now = (struct tm *)malloc(sizeof(struct tm));
   if (argc > 1) adj=atoll(argv[argc-1]);
   if (argc > 2) strcpy(fmt, argv[1]);
   if (argc > 1 && *argv[argc-1] == '@') secs_now=atoll(argv[argc-1]+1);
   else secs_now = time(NULL)+adj;
   *time_now = *localtime(&secs_now);
   strftime(res, 1024, fmt, time_now);
   free(time_now);
   puts(res);
   return 0;
}

Compile with cc -o dateadj dateadj.c , or replace cc with gcc if you have it.

Some usage examples:

$ ./dateadj
21/Jan/2011 08:15:22
$ ./dateadj -60
21/Jan/2011 08:14:26
$ ./dateadj '%H%M' -60
0814
$ ./dateadj @1294581600 
10/Jan/2011 00:00:00

@Chubler_XL: Thanks. unfortunately I don't have a C compiler on board, but I'll keep your code just in case! :wink:

@Tytalus: Thanks for your feedback, your example works:
root@rms# perl -e 'print scalar(localtime(time()-60))."\n"'
Thu Jan 20 15:19:51 2011

Now, how can I extract the hour an minute values??? :confused:

---------- Post updated at 11:08 AM ---------- Previous update was at 09:25 AM ----------

Solved myself! :slight_smile:

This is for the minute:

timetmp=$(perl -e 'print scalar(localtime(time()-60))'|awk '{printf $4}' |awk -F":" '{printf $2}')

Btw, thanks to all for tips! :wink: