timestamp.sh

 1: #! /bin/sh -
 2: #
 3: # POSIX script to display the current "Unix time",
 4: # that is the number of seconds since midnight 1/1/1970.
 5: # Note shell arithmetic can't be used as the values overflow.
 6: #
 7: # $Id: timestamp.sh,v 1.1 2009/10/15 15:57:31 wpollock Exp $
 8: # Written 10/2009 by Wayne Pollock, Tampa Florida USA
 9: 
10: set -- $(date -u '+%S %M %H %j %Y')
11: SEC="$1"
12: MIN="$2"
13: HR="$3"
14: DAY=$(( $4 - 1 ))  # Day of year in range [0..365]
15: YR=$(( $5 - 1900 ))
16: 
17: TIME=$(printf '%s\n' "$SEC + $MIN*60 + $HR*3600 + $DAY*86400 + \
18:   ($YR-70)*31536000 + ( ($YR-69)/4)*86400 - \
19:   ( ($YR-1)/100)*86400 + ( ($YR+299)/400)*86400" |bc)
20: 
21: printf '%s\n' "$TIME"
22: 
23: # Geoff Clare <netnews@gclare.org.uk> posted this POSIX solution
24: # on 10/13/2009 on comp.unix.shell "Re: Compute seconds since
25: # 1st Jan 1970.":
26: #
27: # FILE=/tmp/timestamp-$$  # mktemp is not POSIX but see mktemp.sh
28: # trap 'rm $FILE; trap - 0; exit' 0 1 2 3 15
29: # touch $FILE
30: # printf '%d\n' 0"$(pax -wxcpio $FILE |
31: #    dd bs=1 skip=48 count=11 2>/dev/null)"