It is currently Tue, 07 Dec 2021 02:14:25 GMT



 
Author Message
 Get time between 2 date
Someone can tell me if in unix .. a command exist for calcul the time
between 2 date

ex :  date_start ( 2001-01-12 13:24:35 )     date_end ( 2001-01-25
13:34:37 )

the result should be 13D 00:10:02

or if someone have a script for that .. i will appreciate
thank



 Sun, 20 Jul 2003 20:15:01 GMT   
 Get time between 2 date

SG,

I think that you have to use the expr command to calcul and format text
to obtain the result you expect

sorry for my English



 Tue, 22 Jul 2003 17:29:17 GMT   
 Get time between 2 date

Well, if you want to check for valid data, the script gets long.
The input requires a complete date and time specification, even if you
just want the difference between two different times on the same day.
And if you just want the difference between two dates, you still have
to put in the full time specs.  Maybe version 2.0 will fix that :-)

Usage:
timediff YYYYMMDDThh:mm:ss YYYYMMMMThh:mm:ss
T is a required separator

Examples:

    $ timediff 2001-02-04T00:00:00 2001-02-03T21:56:07
    -0D02:03:53
    $ timediff 2001-02-03T00:00:00 2001-02-03T21:56:07
    0D21:56:07
    $ timediff 1970-01-01T00:00:00 2001-02-03T22:30:17
    11356D22:30:17

Have fun!

---Robert

####----BEGIN
#!/bin/ksh -p
#
# @(#) timediff:
# @(#) script to determine time between two dates
# @(#) 2001FEB03 -- Robert Katz
#
function _invalid {
    print -u2 invalid input

function _Usage {
    print -u2 Usage:
    print -ru2 "${0##*/} YYYY-MM-DDThh:mm:ss YYYY-MM-DDThh:mm:ss"
    print -u2 "\tT is a required separator."
    print -ru2 -- "${0##*/}: finds the number of days, hours, minutes, and"
    print -u2 "\tseconds between two dates.  Each date must be valid"
    print -u2 "\twithin the Gregorian calendar, which started on 1582-10-15."
    print -u2 "\tNeither date can be later than 9999-12-31."
    exit 1

function _invalid_Usage {
    _invalid
    _Usage

if (($# != 2))
then
    (($# != 0)) && _invalid
    _Usage
fi

set -A SDATE 1582 10 15
set -A DAYS 0 31 28 31 30 31 30 31 31 30 31 30 31
set -A TOTAL 0 306 337 0 31 61 92 122 153 184 214 245 275
integer DAYS SDATE TOTAL Y M D Dayofyear day hour min sec
typeset -L4 y
typeset -R2 d s
typeset -L2 h

integer i=0
for input
do
    date=${input%T*}
    time=${input#*T}
    ((${#date} != 10)) || ((${#time} != 8)) && _invalid_Usage
    y=$date
    d=$date
    h=$time
    s=$time
    m=${date#$y-%-$d}
    min=${time#$h:%:$s}
    for arg in y m d h min s
    do
        [[ $(eval print -r -- \$$arg) != +([[:digit:]]) ]] && _invalid_Usage
    done
    Y[i]=$y
    M[i]=$m
    D[i]=$d
    H[i]=$h
    MIN[i]=$min
    S[i]=$s
    integer Y M D H MIN S
    DAYS[2]=28+$((Y[i] % 4 == 0 && Y[i] % 25 != 0 || Y[i] % 16 == 0))
    (( (Y[i] < SDATE[0] || M[i] == 0 || M[i] > 12 || D[i] == 0 ||
        D[i] > DAYS[M[i]]) || (Y[i] == SDATE[0] && (M[i] < SDATE[1])) ||
        (M[i] == SDATE[1] && D[i] < SDATE[2]) ||
        H[i] > 23 || MIN[i] > 59 || S[i] > 59
    )) && _invalid_Usage
    ((Y[i] -= (14 - M[i]) / 12))
    Dayofyear[i]=TOTAL[M[i]]+D[i]
    i=i+1
done

day=$((365 * (Y[1] - Y[0]) + Y[1] / 4 - Y[0] / 4
    - (Y[1] / 100 - Y[0] / 100) + Y[1] / 400 - Y[0] / 400
    + Dayofyear[1] - Dayofyear[0]))
if ((S[1] < S[0]))
then
    ((S[1] += 60))
    ((MIN[1] -= 1))
fi
if ((MIN[1] < MIN[0]))
then
    ((MIN[1] += 60))
    ((H[1] -= 1))
fi
if ((H[1] < H[0]))
then
    ((H[1] += 24))
    ((day -= 1))
fi
sec=S[1]-S[0]
min=MIN[1]-MIN[0]
hour=H[1]-H[0]
if ((day < 0))
then
    if ((sec > 0))
    then
        ((sec = 60 - sec))
        ((min += 1))
    fi
    if ((min > 0))
    then
        ((min = 60 - min))
        ((hour += 1))
    fi
    if ((hour > 0))
    then
        ((hour = 24 - hour))
        ((day += 1))
    fi
    ((day == 0)) && sign=-
fitypeset -Z2 hour min sec
print -- $sign${day}D$hour:$min:$sec
####----END



 Thu, 24 Jul 2003 12:15:34 GMT   
 Get time between 2 date

[...]

Excellent work! but I got some error when trying it.
unexpected `:'

I don't know ksh script so the only thing I can think of is to
provide you with the detail:

$ ksh -x timediff 2001-02-04T00:00:00 2001-02-03T21:56:07
+ let 2 != 2
+ set -A SDATE 1582 10 15
+ set -A DAYS 0 31 28 31 30 31 30 31 31 30 31 30 31
+ set -A TOTAL 0 306 337 0 31 61 92 122 153 184 214 245 275
+ typeset -i DAYS SDATE TOTAL Y M D Dayofyear day hour min sec
+ typeset -L4 y
+ typeset -R2 d s
+ typeset -L2 h
+ typeset -i i=0
+ date=2001-02-04
+ time=00:00:00
+ let 10 != 10
+ let 8 != 8
+ y=2001-02-04
+ d=2001-02-04
+ h=00:00:00
+ s=00:00:00
+ m=2001-02-04
+ min=00:00:00
timediff[nn]: 00:00:00: unexpected `:'

--
Tong (remove underscore(s) to reply)
  http://members.xoom.com/suntong001/
  - All free contribution & collection & music from the heavens



 Fri, 25 Jul 2003 04:49:30 GMT   
 Get time between 2 date

Okay, try this version instead:

---Robert

####----BEGIN
#!/bin/ksh -p
#
# @(#) timediff:
# @(#) script to determine time between two dates
# @(#) 2001FEB05 -- Robert Katz
#
function _invalid {
    print -u2 invalid input

function _Usage {
    print -u2 Usage:
    print -ru2 "${0##*/} YYYY-MM-DDThh:mm:ss YYYY-MM-DDThh:mm:ss"
    print -u2 "\tT is a required separator."
    print -ru2 -- "${0##*/}: finds the number of days, hours, minutes, and"
    print -u2 "\tseconds between two dates.  Each date must be valid"
    print -u2 "\twithin the Gregorian calendar, which started on 1582-10-15."
    print -u2 "\tNeither date can be later than 9999-12-31."
    exit 1

function _invalid_Usage {
    _invalid
    _Usage

if (($# != 2))
then
    (($# != 0)) && _invalid
    _Usage
fi

set -A SDATE 1582 10 15
set -A DAYS 0 31 28 31 30 31 30 31 31 30 31 30 31
set -A TOTAL 0 306 337 0 31 61 92 122 153 184 214 245 275
integer DAYS SDATE TOTAL Y M D Dayofyear day Y M D H MIN S
typeset -L4 y
typeset -R2 d s
typeset -L2 h

integer i=0
for input
do
    date=${input%T*}
    time=${input#*T}
    ((${#date} != 10)) || ((${#time} != 8)) && _invalid_Usage
    y=$date
    d=$date
    h=$time
    s=$time
    m=${date#$y-}
    m=${m%-$d}
    min=${time#$h:}
    min=${min%:$s}
    for arg in y m d h min s
    do
        [[ $(eval print -r -- \$$arg) != +([[:digit:]]) ]] && _invalid_Usage
    done
    Y[i]=$y
    M[i]=$m
    D[i]=$d
    H[i]=$h
    MIN[i]=$min
    S[i]=$s
    DAYS[2]=28+$((Y[i] % 4 == 0 && Y[i] % 25 != 0 || Y[i] % 16 == 0))
    (( (Y[i] < SDATE[0] || M[i] == 0 || M[i] > 12 || D[i] == 0 ||
        D[i] > DAYS[M[i]]) || (Y[i] == SDATE[0] && (M[i] < SDATE[1])) ||
        (M[i] == SDATE[1] && D[i] < SDATE[2]) ||
        H[i] > 23 || MIN[i] > 59 || S[i] > 59
    )) && _invalid_Usage
    ((Y[i] -= (14 - M[i]) / 12))
    Dayofyear[i]=TOTAL[M[i]]+D[i]
    i=i+1
done

day=$((365 * (Y[1] - Y[0]) + Y[1] / 4 - Y[0] / 4
    - (Y[1] / 100 - Y[0] / 100) + Y[1] / 400 - Y[0] / 400
    + Dayofyear[1] - Dayofyear[0]))
if ((S[1] < S[0]))
then
    ((S[1] += 60))
    ((MIN[1] -= 1))
fi
if ((MIN[1] < MIN[0]))
then
    ((MIN[1] += 60))
    ((H[1] -= 1))
fi
if ((H[1] < H[0]))
then
    ((H[1] += 24))
    ((day -= 1))
fi
typeset -i sec min hour
sec=S[1]-S[0]
min=MIN[1]-MIN[0]
hour=H[1]-H[0]
if ((day < 0))
then
    if ((sec > 0))
    then
        ((sec = 60 - sec))
        ((min += 1))
    fi
    if ((min > 0))
    then
        ((min = 60 - min))
        ((hour += 1))
    fi
    if ((hour > 0))
    then
        ((hour = 24 - hour))
        ((day += 1))
    fi
    ((day == 0)) && sign=-
fi
typeset -Z2 hour min sec
print -- $sign${day}D$hour:$min:$sec
####----END



 Sat, 26 Jul 2003 06:31:56 GMT   
 
   [ 5 post ] 

Similar Threads

1. run time date and syslog date/time conflict

2. date and time of compile time (__DATE__ in Borland C++

3. date/time of a file compare with current date/time

4. Date/time keeps getting adjusted?

5. Getting current date and time.

6. Getting Time & Date of files Exercise

7. getting system date/time from another system

8. Help getting date and time of a file

9. Korn shell date cmd - getting yesterday's date

10. System date and file dates not showing in same time zone


 
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group.
Designed by ST Software