The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > UNIX for Dummies Questions & Answers > Answers to Frequently Asked Questions > Tips and Tutorials
Google UNIX.COM
Home Forums Register Rules & FAQ Members List Arcade Search Today's Posts Mark Forums Read


Tips and Tutorials Helpful articles from our Users.


Other UNIX.COM Threads You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Processing a log file based on date/time input and the date/time on the log file primp Shell Programming and Scripting 4 03-16-2008 07:23 AM
Get date and time for past 1 hour from current date spch2o Shell Programming and Scripting 4 12-21-2007 05:55 AM
a simple way of converting a date in seconds to normal date travian HP-UX 2 11-23-2006 08:25 AM
simple bash script to ftp? satnamx Shell Programming and Scripting 1 04-21-2006 08:18 AM
Simple Bash Script xaphalanx Shell Programming and Scripting 3 12-21-2005 11:54 AM

 
 
Submit Tools LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 10-08-2006
Registered User
 

Join Date: Oct 2006
Location: Belgium
Posts: 167
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiReddit! Stumble this Post!Spurl this Post!
Simple date and time calulation in BASH

The GNU date command in full of goodies but not when it comes to calculate a date or time difference. Here is what I came up with after looking to more than one solution.

Code should be self explaining.

Code:
#!/bin/bash

date2stamp () {
    date --utc --date "$1" +%s
}

stamp2date (){
    date --utc --date "1970-01-01 $1 sec" "+%Y-%m-%d %T"
}

dateDiff (){
    case $1 in
        -s)   sec=1;      shift;;
        -m)   sec=60;     shift;;
        -h)   sec=3600;   shift;;
        -d)   sec=86400;  shift;;
        *)    sec=86400;;
    esac
    dte1=$(date2stamp $1)
    dte2=$(date2stamp $2)
    diffSec=$((dte2-dte1))
    if ((diffSec < 0)); then abs=-1; else abs=1; fi
    echo $((diffSec/sec*abs))
}

# USAGE # # # # # # # # # # # # # # # # # # # # # # # # # # # # # 

# convert a date into a UNIX timestamp
    stamp=$(date2stamp "2006-10-01 15:00")
    echo $stamp

# from timestamp to date
    stamp2date $stamp

# calculate the number of days between 2 dates
    # -s in sec. | -m in min. | -h in hours  | -d in days (default)
    dateDiff -s "2006-10-01" "2006-10-32"
    dateDiff -m "2006-10-01" "2006-10-32"
    dateDiff -h "2006-10-01" "2006-10-32"
    dateDiff -d "2006-10-01" "2006-10-32"
    dateDiff  "2006-10-01" "2006-10-32"

# number of seconds between two times
    dateDiff -s "17:55" "23:15:07"
    dateDiff -m "17:55" "23:15:07"
    dateDiff -h "17:55" "23:15:07"

# number of minutes from now until the end of the year
    dateDiff -m "now" "2006-12-31 24:00:00 CEST"

# Other standard goodies from GNU date not too well documented in the man pages
    # assign a value to the variable dte for the examples below
    dte="2006-10-01 06:55:55"
    echo $dte

    # add 2 days, one hour and 5 sec to any date
    date --date "$dte  2 days 1 hour 5 sec"

    # substract from any date
    date --date "$dte 3 days 5 hours 10 sec ago"
    date --date "$dte -3 days -5 hours -10 sec"

    # or any mix of +/-. What will be the date in 3 months less 5 days
    date --date "now +3 months -5 days"

    # time conversions into ISO-8601 format (RFC-3339 internet recommended format)
    date --date "sun oct 1 5:45:02PM" +%FT%T%z
    date --iso-8601=seconds --date "sun oct 1 5:45:02PM"
    date --iso-8601=minutes

    # time conversions into RFC-822 format
    date --rfc-822 --date "sun oct 1 5:45:02PM"
Warning: the non-standard format %s might not work on all version of date. Works fine of my GNU date 5.2.1

Last edited by ripat : 10-09-2006 at 07:57 AM. Reason: Added some options to the function dateDiff
Google UNIX.COM
Forum Sponsor
 



Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT -7. The time now is 04:32 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008 The CEP Blog All Rights Reserved -Ad Management by RedTyger

Search Engine Optimization by vBSEO 3.1.0

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102