Trouble calculating difference in number of days


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Trouble calculating difference in number of days
# 1  
Old 01-01-2015
Trouble calculating difference in number of days

Hi all,

I have a requirement to calculate the difference of number of days of time stamp of a file and system date and if the difference is greater than 15 days it should prompt as previous month file otherwise current month file.

Below is the code i used and it is working fine till now. (You can try some date instead of $Publish_Time, it actually carries time stamp of a file)

Code:
Sys_Time=`date |tr -s " "|cut -d" " -f2,3,4|cut -d":" -f1,2`
Sys_Time=`date +%s -d "$Sys_Time"`
Publish_Time=`date +%s -d "$Time_Stamp_cntrl_file"`
Time_Stamp_Diff=`m_eval $Sys_Time-$Publish_Time`
Time_Stamp_Diff=`m_eval $Time_Stamp_Diff/86400`
if [[ $Time_Stamp_Diff -ge 15 ]]; then
echo "file is as of previous month"
else
echo "current month file"
fi

From today since there is change in year, time stamp difference is going negative value and hence even with Dec 1 time stamp it is prompting as current month file.

Any help plz
# 2  
Old 01-01-2015
Your logic escapes me, and it might be quite locale dependent. Here's a somewhat easier approach:
Code:
touch -d"-15days" TMP
[ file -nt TMP ] && echo newer || echo older

# 3  
Old 01-01-2015
Way too many steps. What shell are you using?

Example in bash:

Code:
#!/bin/bash
#get some random filename just as an example
filename=/usr
mod_time=`stat -c %Y $filename`
curr_time=`date +%s`
diff_in_sec=$(( $curr_time - $mod_time ))
diff_in_days=$(( $diff_in_sec / 86400 ))
echo Diff in days:  $diff_in_days

And I wonder how reliable the use of a time difference greater then 15 full days is in determining "previous month" given that months range from 28 to 31 days long.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Calculating Time difference Between two Rows in Linux

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. working in media company on a project OS: RHEl7 tried command: awk 'function... (2 Replies)
Discussion started by: vivekn
2 Replies

2. Shell Programming and Scripting

Need difference between days

Hi, I am not comfortable with dates and I fail to crack this. I have two strings Date1="Apr 22 23:59:59 2016 GMT" Date2="Dec 1 15:08:40 UTC 2015" I need to find the difference in days between the two dates which in this example is approx 140 days. Is there an easy way to get the... (3 Replies)
Discussion started by: mohtashims
3 Replies

3. UNIX for Advanced & Expert Users

Help with Calculating time difference between many directories in UNIX

A report needs to come some what similar to this No of elements Stream Batch No Load time A B C D A,B,C im able to get quite easily wc -l /usr/local/intranet/areas/prod/output/SRGW_0?/*/MESSAGE_T.dat O/P of above command. A B C ... (1 Reply)
Discussion started by: peckenson
1 Replies

4. Shell Programming and Scripting

Calculating 7 days ago date for the given Argument

Hi I have shell script and I am facing the below issue to integrate the date calculation to the the script. If I give the $1 as the date(20110701) then I need to get the 7 days ago date for the same format.(20110624). At first I thought its a simple one to handle and I did a search in the... (10 Replies)
Discussion started by: filter
10 Replies

5. Shell Programming and Scripting

Calculating number of records by field

Hi, I have CSV file which looks like below, i want to calulate number of records for each brand say SOLO_UNBEATABLE E and SOLO_UNBEATABLE F combined and record count is say 20 . i want to calculate for each brand, and here only first record will have all data and rest of record for the brand... (2 Replies)
Discussion started by: raghavendra.cse
2 Replies

6. Shell Programming and Scripting

Calculating the difference between dates

Hello! i need to find files lower and bigger that one date i pass, i search in the man find, but i didn't find anything, the only that i find is the parameter -mtime, in this parameter i can pass a number of days, but i need to know the difference between dates, any built-in function for do... (15 Replies)
Discussion started by: claw82
15 Replies

7. UNIX for Dummies Questions & Answers

Calculating the Number of Rows and Average

Hi All I like to know how can we calculate the number of rows and the average of the values present in the file. I will not know what will be the rowcount, which will be dynamic in nature of the file. eg. 29 33 48 30 28 (6 Replies)
Discussion started by: pk_eee
6 Replies

8. Shell Programming and Scripting

parsing and calculating difference.

Hi, I have a file with the contents as following Access Time: Thu Nov 6 16:43:45 2008 Modify Time: Thu Nov 6 16:43:45 2008 Change Time: Thu Nov 6 16:43:45 2008 Access Time: Thu Nov 6 16:43:02 2008 Modify Time: Thu Nov 6 16:44:01 2008 Change Time: Thu Nov 6 16:44:01 2008 I need... (3 Replies)
Discussion started by: meetmano143
3 Replies

9. Shell Programming and Scripting

calculating a number

Hello all :) I need some help; I'm running the sp_spaceused command on various tables and saving the output to a file. So, I have an input file that has 3 rows - each row has 7 columns. I would like to 1) sort the file on the 4th column, 2) take the 4th column in the first row and add 25% to... (2 Replies)
Discussion started by: stonemonolith
2 Replies

10. Shell Programming and Scripting

calculating the time difference, when the script was executed and the currenent file

Hi, I has created the shell script in HP_UX 11.23 and using the command, echo $(date +%Y%m%d%H%M%S) > $DIR/alert, placing the time of running the script into a file alert. I want to compare the time in the above file alert with the current time.If difference is more than 5 min, then print the... (7 Replies)
Discussion started by: velocitnitin
7 Replies
Login or Register to Ask a Question