Weird date difference problem


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Weird date difference problem
# 8  
Old 03-12-2008
Hi Jadu,
I did see your previous post yesterday and tried doing this - but I do not have GNU date and E1 just returns me today's date.
IS there any other way - the deadline for this is fast approaching.Smilie
Thanks...
Quote:
Originally Posted by jaduks
This is a bit similar to an older post: https://www.unix.com/shell-programmin...two-dates.html

I would suggest convert both the dates to "epoch" seconds, then do a awk operation to convert the epoch diff secs to num days.

something like:

Code:
$ cat days.sh
D1=`echo "SHIP_606400_2008233202_20080206_070308083544.xml"|awk -F_ '{print $4}'`
E1=`date +%s -d"$D1"`
E2=`date +%s` #present time
((diff_sec=E2-E1))
echo - | awk -v SECS=$diff_sec '{printf "Number of days : %d",SECS/(60*60*24)}'

Code:
$ sh days.sh
Number of days : 35

//Jadu
# 9  
Old 03-12-2008
Okay, play with this:

Code:
$ date -d "20080206" +%s
1202274000

$ date +%s
1205330860

In that case, subtract 1202274000 from 1205330860. The result is 3056860. Divide that by 86,400 (24 * 60 * 60) to convert seconds to days. In this case, it's 35.38 days.

The point being that if you can extract the date from your filename (which you have already done), you can use the "+%s" formatting (seconds since epoch) to compare the current date/time (in epoch) to it. Take the difference and divide it by days (24 * 60 * 60) and see if that's less than 30.

You can probably modify the script you originally posted with this information fairly quickly and be up and running.

Please post the result to the thread when you get it working (or nearly so).

ShawnMilo
# 10  
Old 03-12-2008
Hi Shawn,
OK i tried this - and this is the error that I got -

Code:
date: illegal option -- d
usage:  date [-u] mmddHHMM[[cc]yy][.SS]
        date [-u] [+format]
        date -a [-]sss[.fff]

Is it a version thing?

Thanks for all your help.
# 11  
Old 03-12-2008
Quote:
Originally Posted by meeraKh
Hi Shawn,
OK i tried this - and this is the error that I got -

Code:
date: illegal option -- d
usage:  date [-u] mmddHHMM[[cc]yy][.SS]
        date [-u] [+format]
        date -a [-]sss[.fff]

Is it a version thing?

Thanks for all your help.
What's your Unix/Linux version? I'm on Ubuntu Linux, myself. Worst case, we can do this is a Python or Perl script. But I think we're already close to solving this.

Type:
Code:
man date

and see if there's an option you can use to pass it a date as a string instead of having it default to the current system time.

ShawnMilo
# 12  
Old 03-12-2008
Thanks Shawn
The unix version i am using is

Code:
SunOS retailtest 5.9 Generic_122300-13 sun4u sparc SUNW,Sun-Fire-V240

tried getting help on the date command and the only 2 options available are -a and -u.

Also, have never written a Perl script in my life.... Smilie

Thanks again for all your help

Last edited by meeraKh; 03-12-2008 at 12:57 PM.. Reason: Added Code tags
# 13  
Old 03-12-2008
Let's try it in Python.

Code:
 $ ./timeComparison.py SHIP_606400_2008233202_20080206_070308083544.xml 
35.4838506172

Code:

Code:
#!/usr/bin/env python

import time
import sys

one_day = 24 * 60 * 60

#Assume the first argument is the file to check
file = sys.argv[1]

#split the filename into an array
sections = file.split("_")

#the date we want is the fourth section of the filename
file_date = sections[3]

file_date = int(time.mktime(time.strptime(file_date, '%Y%m%d')))

now = time.time()


age_in_days = (now - file_date) / one_day

print age_in_days

# 14  
Old 03-12-2008
Thanks Shawn - but having problems with this too.
I created a new file, timeComparison.py using the vi editor, (actually just copied your code there) and then trid running the commant, and it gave me an error:

Code:
/usr/bin/env: No such file or directory


I am creating this in the bin directory, where i usually create my shell scripts:
Code:
/u01/app/rms9.0/prd/oracle/proc/bin


Thanks again for your help
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Weird awk problem

Hi, I have a simple awk script: BEGIN{} { $a=$2-$1; print $a } END{if(NR==0){ print "0" } } to which I provide the following input 2.9 14 22.2 27 (4 Replies)
Discussion started by: jamie_123
4 Replies

2. Shell Programming and Scripting

Weird difference in script execution

Hi, So I have a very simple script which loops over 5 times and prints the iterator value. #!/bin/sh START=1 END=5 for i in $(eval echo "{$START..$END}") do echo "$i" done If I save this script in a .sh file and run it in the terminal, the output I get is {1..5} (4 Replies)
Discussion started by: jamie_123
4 Replies

3. Shell Programming and Scripting

A very weird problem about getting a random string

Hi, guys Here is my script and it's an imaginary script to recast a string randomly. #!/bin/bash # scriptname: recast_string # purpose: recast a string randomly like this "abc123" --> "21a3cb" or "a31b2c" function recast() { local original_string=$1 local... (4 Replies)
Discussion started by: franksunnn
4 Replies

4. Programming

A weird problem with POSIX function

Hi all, Sorry for the title because I didn't find a proper name for it. My question is about POSIX functions, such as timer_create(), mq_open() and pthread_create(). void test_queue() { struct mq_attr attr; attr.mq_maxmsg = 10; attr.mq_msgsize = 64; mq_unlink("/my_test_queue");... (6 Replies)
Discussion started by: bus147
6 Replies

5. Shell Programming and Scripting

awk weird problem.

awk 'BEGIN{print 1.2.3.4}' 1.20.30.4 Can anyone explain why has extra "0" in the IP address? (3 Replies)
Discussion started by: newoz
3 Replies

6. Infrastructure Monitoring

Weird dependency problem!

Hi, I want to install net-snmp-devel package but i have following dependecy problem. It's very odd, i don't get it. One of packages is depended on the other one, the other one is depended on the previous one as well. :S :S Could you help me please? Here are the steps: # ls -l total... (4 Replies)
Discussion started by: oduth
4 Replies

7. UNIX for Advanced & Expert Users

Really weird delete problem

Hi, I've Ubuntu 8.04, and it has some files that I just cannot delete. I've tried everything, inode, fsck etc. Here is what the ls -li outputs root@ubuntu:/home/luser/.local/share/Trash/files/junk# ls -l ls: cannot access TRUNK_: No such file or directory ls: cannot access 2006_output.mv:... (11 Replies)
Discussion started by: nitin
11 Replies

8. Solaris

Weird crontab problem

Greetings To All! I am running Solaris 10 in a sparc environment. Here is the deal: In /var/spool/cron/crontabs, there is a cron user named "sys". If I do a crontab -l sys, it returns: # 0 * * * 0-6 /usr/lib/sa/sa1 # 20,40 8-17 * * 1-5 /usr/lib/sa/sa1 # 5 18 * * 1-5 /usr/lib/sa/sa2... (8 Replies)
Discussion started by: RobSand
8 Replies

9. Shell Programming and Scripting

Weird problem with output from "date '+3600*%H+60*%M+%S' "

Hi, I came across a script a few months ago that allowed you to use the following script to include the current time into your prompt (useful from auditting purposes): # Set Prompt typeset -RZ2 _x1 _x2 _x3 let SECONDS=$(date '+3600*%H+60*%M+%S')... (5 Replies)
Discussion started by: m223464
5 Replies

10. UNIX for Dummies Questions & Answers

Weird Problem???

I have a problem I don't understand... I am trying to declare a variable, and then output the results of that variable, couldn't be simpler #!/bin/ksh VAR='Oranges' if then echo "Found Lemons" elif then echo "Found Oranges" fi The output shouold clearly be "Found Oranges", but... (2 Replies)
Discussion started by: danhodges99
2 Replies
Login or Register to Ask a Question