Tom's Guide | Tom's Hardware | Tom's Games
![]() |
![]() |
![]() |
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.

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=20030521112958diff=$(($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 20030521082958difference 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.

If you just want to retain the last 6 digits from
variable $ts, declare it usingtypeset -R6 ts
The current time is date +%H%M%S, e.g:
timenow=$(date +%H%M%S)
print Time difference: $((timenow - ts)) seconds

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 timestampdateecho $timestampdate #prints nothing!
-------------
Any way around this/what am i doing wrong?
Thanks,
elpico.

Hi:
Interesting. your code above works fine on my Solaris 7 box. Have you tried reversing the lines:
typeset -R6 timestampdate
timestampdate=20030521091625Regards,
Nails

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".

![]() |
![]() |
![]() |

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