subtract minutes from time


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting subtract minutes from time
# 1  
Old 04-21-2010
subtract minutes from time

i have the time 20100421043335 in format (date +%Y%m%d%H%M%S),and i want to be able to get the previous time 2 minutes ago,which is

20100421043135
# 2  
Old 04-21-2010
if you have GNU date
Code:
D="20100421043335"
date -d "${D:0:8} ${D:8:2}:${D:10:2}:${D:12:2} 2 minutes ago"

# 3  
Old 04-21-2010
Frans,
i am having difficulty using it in a script.
Please how do i go about it
# 4  
Old 04-21-2010
type
Code:
date -d "now 2 minutes ago"

if that works : you have GNU date and the snippet should work.
For use it in a script, show us first how your script looks like.
# 5  
Old 04-21-2010
Frans,
my script looks like this

#!/bin/ksh
D=`date +%Y%m%d%H%M%S`
D1=`date -d "now 2 minutes ago"`
echo $D1

But it does not work for me. i need D1 to be 2 minutes ago
# 6  
Old 04-21-2010
If you are using ksh93 the following works
Code:
printf "%(%Y%m%d%H%M%S)T" "${D:0:4}-${D:4:2}-${D:6:2} ${D:8:2}:${D:10:2}:${D:12:2} 2 minutes ago"

If neither GNU date or ksh93 are available to you the following short C utility will do the job
(see usage string for arguments):
Code:
#include <stdio.h>
#include <time.h>
#include <unistd.h>
#include <stdlib.h>


int
main(int argc, char* argv[])
{
   struct tm tm1, *tm2;

   char buf[80];
   char format[50];
   char tmp[80];
   char date[80];
   char seconds[10];
   char *s, *d;

   time_t t1, t2;

   int c;
   int debug = 0;
   int errflg = 0;

   extern char *optarg;
   extern int optind, optopt;

   while ((c = getopt(argc, argv, "dh")) != -1) {
        switch(c) {
        case 'd':
            debug = 1;
            break;
        case 'h':
            errflg++;
            break;
        case '?':
            fprintf(stderr, "Unknown option: -%c\n", optopt);
            errflg++;
        }
   }
   if (errflg || (argc < 2)) {
       fprintf(stderr, "Usage: subtime [-d] datestr seconds\n");
       exit(1);
   }

   strcpy(tmp, argv[optind]);
   optind++;
   strcpy(seconds, argv[optind]);

   strcpy(format, "%Y %m %d %H %M %S");

   /* reformat date string to place a space betwen each token */
   /* strptime specification requires a space */
   s = tmp;
   d = date;
   c = 0;
   while(*s) {
      if (c == 4 || c == 6 || c == 8 || c == 10 || c == 12 || c == 14) {
         *d++ = ' ';
      }
      c++;
      *d++ = *s++;
   }
   *d = '\0';

   if (debug) {
       fprintf(stderr, "Format: %s\n", format);
       fprintf(stderr, "Date: %s\n", date);
       fprintf(stderr, "Seconds: %s\n", seconds);
   }

   if (!strptime(date, format, &tm1)) {
       fprintf(stderr, "strptime() error\n");
       exit(1);
   }
   strftime(buf, 50, "%a %Y %H:%M:%S", &tm1);

   if ((t1 = mktime(&tm1)) == -1) {
      fprintf(stderr, "mktime() error\n");
      exit(1);
   }

   t1 = t1 - atoi(seconds);
   tm2 = localtime(&t1);

   if (strftime(buf, sizeof(buf), "%Y%m%d%H%M%S", tm2) == 0) {
      fprintf(stderr, "strftime() error\n");
      exit(1);
   }

   printf("%s", buf);

   exit(0);
}

# 7  
Old 04-21-2010
i dont seem to get the printf code working.
may be you can help by completing the code i sent,so i can echo out the new date.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Check file creation Time minutes and if file older then 5 minutes execute some stuff

Hello all, Info: System RedHat 7.5 I need to create a script that based on the creation time, if the file is older then 5 minutes then execute some stuff, if not exit. I thought to get the creation time and minutes like this. CreationTime=$(stat -c %y /tmp/test.log | awk -F" " '{ print... (3 Replies)
Discussion started by: charli1
3 Replies

2. Shell Programming and Scripting

Subtract time in two line

INPUT: 16:45:51 10051 77845 16:45:51 10051 77845 16:46:52 10051 77846 16:46:53 10051 77846 Match the last PID then subtract second line time with first line. Please help me with any command or script. (3 Replies)
Discussion started by: vivekn
3 Replies

3. UNIX for Dummies Questions & Answers

Subtract minutes from date

Hi, I am reading a particular date from a file using below command WFLWDATE=$(sed '2q;d' FileA.prm) The echo command outputs the correct date in variable WFLWDATE Now I want to subtract 5 minutes from this variable. I am on AIX and unable to get anything working as expected. Can you... (1 Reply)
Discussion started by: vrupatel
1 Replies

4. Shell Programming and Scripting

Date command - subtract from given time

the given time is: 12:13:00 how do i subtract a 10 minutes from any given time? date '12:13:00' '-10 min' also tried this: date +12:13:00 '-10 min' (2 Replies)
Discussion started by: SkySmart
2 Replies

5. Shell Programming and Scripting

Add or Subtract the hours,minutes or seconds in the the time variable

Hello All, I am working on script where I need to add hours,minutes or seconds in the time.Time is not the current but it could be future time.I thought I can store that time in variable and add hours.minutes or second but I am not able to add that in the time that is stores in a variable. Time... (9 Replies)
Discussion started by: anuragpgtgerman
9 Replies

6. Shell Programming and Scripting

Subtract two rows (in Time format)

Hello all, I have written sth like this: #!/bin/bash grep -e XXX -e YYYY myfile.log | grep -v ZZZ | awk '{print $1 " " $2 ";" $3 ";" $9 ";" $11}' > myfile.csv sed -i '1iDate;Time;From;To' myfile.csv => it is clear that it converts log to csv and add a header. Now I want to subtract row... (4 Replies)
Discussion started by: frhling
4 Replies

7. Shell Programming and Scripting

How to subtract time by 10 minutes in datecalc tool

Hi guys. I am trying to subtract 10 minutes from the current Unix system date and time. I have the datecalc provided here but it is mainly the date and not the time. Please check on how can i subtract 10 minutes from the current time using datecalc or any other shell scripting that will... (2 Replies)
Discussion started by: bantiloe
2 Replies

8. Shell Programming and Scripting

Perl - Extract 12 hour time, convert to 24 and subtract 15 minutes?

OK, I am by no means a programmer... I have been given the task to do some automation scripts. I have got most of it working from snippets I have found on the Web. One requirement has me stumped. The initial timing file created by the user is a comma delimited in the following format.... (4 Replies)
Discussion started by: autotuner
4 Replies

9. Shell Programming and Scripting

Add/Subtract Time

need some help on the below requirement: File1: SV,22,20100501140000,JFK,RUH SV,29,20100501073000,BOM,RUH SV,29,20100501073000,SIN,RUH third filed is datetime which is of the format (yyyymmddhh24miss) File2 JFK,+,0500 BLR,-,0530 SIN,-,0800 for every line of file 1, take 4... (9 Replies)
Discussion started by: ssantoshss
9 Replies

10. Shell Programming and Scripting

Subtract Time

Hello, Im writing a script using the ksh shell. I have 2 variables in the script: CURRTIME PREVTIME Example, if CURRTIME=13:00, I want to somehow calculate what the time was an hour ago so that PREVTIME=12:00 Right now I have the following: CURRTIME=`date +%H:%M` How can I... (4 Replies)
Discussion started by: xadamz23
4 Replies
Login or Register to Ask a Question