Computing.Net > Forums > Unix > Time diff and date formatting prob

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

Time diff and date formatting prob

Reply to Message Icon

Name: elpico
Date: May 21, 2003 at 05:54:04 Pacific
OS: Solaris 8/Unix
CPU/Ram: -
Comment:

Hi all,

I'm using ksh, and have a problem over date formatting and getting the difference between them.

First of all, I need to remove some information to get the time from it. A timestamp from my log looks like the following: 20030521112958

- But I need the last 6 digits only, and then I need to find the difference in time between that and the current date -

All I can think of is to convert the timestamp time into seconds, and compare it with the current date, also compared into seconds - Surely there must be an easier way!

Any suggestions appreciated -
elpcio.



Sponsored Link
Ads by Google

Response Number 1
Name: LANkrypt0
Date: May 21, 2003 at 06:24:21 Pacific
Reply:

If that is a static length number this can be much easier. Note: I had to change the date on your log to adjust for time differences (assuming that the dates in your logs are of times in the past)

The script is something along the lines of:

#!/bin/ksh
currdate=$(date +"%Y%m%d%H%M%S")
statdate=20030521112958

diff=$(($currdate-$statdate))

Doing that will give you a number, for instance when I tried it my values ended up being:

currdate was 20030521091625
statdate was 20030521082958

difference ended up being 8667
but remember that is based on 100 numbers,
so I simply subtracted 40 from 86 leaving me with 46 minutes and 40 from 67 leaving me with 27 seconds, and my difference was 46 minuts and 27 seconds.

Of course this can all be scripted to change automatically. But definitely easier then converting everything to seconds, getting the difference, etc.


0

Response Number 2
Name: WilliamRobertson
Date: May 21, 2003 at 06:25:16 Pacific
Reply:

If you just want to retain the last 6 digits from
variable $ts, declare it using

typeset -R6 ts

The current time is date +%H%M%S, e.g:

timenow=$(date +%H%M%S)
print Time difference: $((timenow - ts)) seconds


0

Response Number 3
Name: elpico
Date: May 21, 2003 at 06:45:16 Pacific
Reply:

Thanks guys - Some great food for thought with those ideas!

elpico.


0

Response Number 4
Name: elpico
Date: May 21, 2003 at 08:02:52 Pacific
Reply:

Hi guys,

Sorry to trouble you again, but I haven't used typeset before, and while the script works on the command prompt, when I put it into a script in returns blank...

Eg;

#!/usr/bin/ksh

timestampdate=20030521091625
typeset -R6 timestampdate

echo $timestampdate #prints nothing!

-------------

Any way around this/what am i doing wrong?

Thanks,
elpico.


0

Response Number 5
Name: nails
Date: May 21, 2003 at 15:21:36 Pacific
Reply:

Hi:

Interesting. your code above works fine on my Solaris 7 box. Have you tried reversing the lines:

typeset -R6 timestampdate
timestampdate=20030521091625

Regards,


Nails


0

Related Posts

See More



Response Number 6
Name: WilliamRobertson
Date: May 22, 2003 at 09:38:03 Pacific
Reply:

Should work either way around. Could there be something about the script causing this?

The typeset command is a shell built-in, so documentation is under "man ksh".


0

Sponsored Link
Ads by Google
Reply to Message Icon






Post Locked

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.


Go to Unix Forum Home


Sponsored links

Ads by Google


Results for: Time diff and date formatting prob

change date format using shell www.computing.net/answers/unix/change-date-format-using-shell/6741.html

Prb in Yest'days Date Format www.computing.net/answers/unix/prb-in-yestdays-date-format/4559.html

UNIX/changing date format www.computing.net/answers/unix/unixchanging-date-format/8440.html