linux 计算时间差
2013-05-21 11:41
197 查看
为了计算留存,需要知道多个从时间差,来获取不同的时间点。 以下代码在输入值与当前值,在同一月份时,不会有问题。但是如果是夸月份回溯数据,那么就会出现较大的问题。
#Date variables for
if [ -n "$1" ]; then
TODAY=`date -d "" +"%Y%m%d"`
base=$(($TODAY-$1))
elif [ -z "${DAYAGO1}" ]; then
base=0
fi
arr=($base $(($base+1)) $(($base+2)) $(($base+8)) $(($base+31)))
dag=(0 1 2 8 31)
for i in {0..4}; do
echo $i
echo ${dag[$i]}
echo ${arr[$i]}
export "DAYAGO${dag[$i]}"=`date -d "${arr[$i]} days ago" +"%Y%m%d"`
done
为了修正bug,可以参考
http://stackoverflow.com/questions/3385003/shell-script-to-get-difference-in-two-dates
There's a solution that almost works: use the
But the following displays 0 in some locations:
Because of daylight savings time, there are only 23 hours between those times. You need to add at least one hour (and at most 23) to be safe.
Or you can tell
(POSIX says to call the reference timezone is UTC, but it also says not to count leap seconds, so the number of seconds in a day is always exactly 86400 in a GMT+xx timezone.)
最终结果就接近完美了,为:
#Date variables for
if [ -n "$1" ]; then
TODAY=`date -d "" +"%Y%m%d"`
BACKDAY=$1
base=$((($(date -d $TODAY +%s) - $(date -d $BACKDAY +%s) + 43200) / 86400))
elif [ -z "${DAYAGO1}" ]; then
base=1
fi
arr=($(($base)) $(($base+1)) $(($base+7)) $(($base+30)))
dag=(1 2 8 31)
for i in {0..3}; do
echo ${dag[$i]}
echo ${arr[$i]}
export "DAYAGO${dag[$i]}"=`date -d "${arr[$i]} days ago" +"%Y%m%d"`
done
shell 中的时间计算转为秒做相减运算
NOW=`date +"%Y-%m-%d %H:%M:%S"`
Back=$(ls -lt * "$dir"| line | awk '{print $6,$7,$8}') #获取文件的修改时间
dn=`date -d "$NOW" +%s` #把当前时间转化为毫秒时间
db=`date -d "$Back" +%s`
ddif=`expr $dn- $db` #计算2个时间的差
另外一种,换个思路,从前往后推算也是可以的:
day='20120201'
this_day=$day
today='20120311'
i=-1
while [ $t_day != $today ]
do
i=$(($i + 1))
t_day=`date +%Y%0m%0d --date="$day $i day"`
echo $t_day
done
#Date variables for
if [ -n "$1" ]; then
TODAY=`date -d "" +"%Y%m%d"`
base=$(($TODAY-$1))
elif [ -z "${DAYAGO1}" ]; then
base=0
fi
arr=($base $(($base+1)) $(($base+2)) $(($base+8)) $(($base+31)))
dag=(0 1 2 8 31)
for i in {0..4}; do
echo $i
echo ${dag[$i]}
echo ${arr[$i]}
export "DAYAGO${dag[$i]}"=`date -d "${arr[$i]} days ago" +"%Y%m%d"`
done
为了修正bug,可以参考
http://stackoverflow.com/questions/3385003/shell-script-to-get-difference-in-two-dates
There's a solution that almost works: use the
%sdate format of GNU date, which prints the number of seconds since 1970-01-01 00:00. These can be subtracted to find the time difference between two dates.
echo $(( ($(date -d 2010-06-01 +%s) - $(date -d 2010-05-15 +%s)) / 86400))
But the following displays 0 in some locations:
echo $((($(date -d 2010-03-29 +%s) - $(date -d 2010-03-28 +%s)) / 86400))
Because of daylight savings time, there are only 23 hours between those times. You need to add at least one hour (and at most 23) to be safe.
echo $((($(date -d 2010-03-29 +%s) - $(date -d 2010-03-28 +%s) + 43200) / 86400))
Or you can tell
dateto work in a timezone without DST.
echo $((($(date -u -d 2010-03-29 +%s) - $(date -u -d 2010-03-28 +%s)) / 86400))
(POSIX says to call the reference timezone is UTC, but it also says not to count leap seconds, so the number of seconds in a day is always exactly 86400 in a GMT+xx timezone.)
最终结果就接近完美了,为:
#Date variables for
if [ -n "$1" ]; then
TODAY=`date -d "" +"%Y%m%d"`
BACKDAY=$1
base=$((($(date -d $TODAY +%s) - $(date -d $BACKDAY +%s) + 43200) / 86400))
elif [ -z "${DAYAGO1}" ]; then
base=1
fi
arr=($(($base)) $(($base+1)) $(($base+7)) $(($base+30)))
dag=(1 2 8 31)
for i in {0..3}; do
echo ${dag[$i]}
echo ${arr[$i]}
export "DAYAGO${dag[$i]}"=`date -d "${arr[$i]} days ago" +"%Y%m%d"`
done
shell 中的时间计算转为秒做相减运算
NOW=`date +"%Y-%m-%d %H:%M:%S"`
Back=$(ls -lt * "$dir"| line | awk '{print $6,$7,$8}') #获取文件的修改时间
dn=`date -d "$NOW" +%s` #把当前时间转化为毫秒时间
db=`date -d "$Back" +%s`
ddif=`expr $dn- $db` #计算2个时间的差
另外一种,换个思路,从前往后推算也是可以的:
day='20120201'
this_day=$day
today='20120311'
i=-1
while [ $t_day != $today ]
do
i=$(($i + 1))
t_day=`date +%Y%0m%0d --date="$day $i day"`
echo $t_day
done
相关文章推荐
- Linux C语言之计算程序段运行时间
- linux 计算程序运行时间
- linux 日期时间计算
- linux 通过自己计算时间差的方法来定时 毫秒
- Linux下时间计算
- Linux系统使用time计算命令执行的时间
- Linux之父Linus说:并行计算基本上就是浪费大家的时间
- 计算机时间、unix时间、linux时间、java时间为何以1970年1月1日为原点?从1970年1月1日开始计算?
- Linux下计算程序运行时间的两种方法
- linux 时间戳及时间差计算
- Linux下计算程序运行时间
- C语言计算时间函数 & 理解linux time命令的输出中“real”“user”“sys”的真正含义
- Linux下计算程序运行时间的两种方法
- linux c语言 计算函数执行时间
- Linux下计算程序运行时间的一个简单方法
- Linux 计算时间差
- 详解Linux下用date命令查看和计算包含时区的时间戳
- Linux之父Linus说:并行计算基本上就是浪费大家的时间
- linux计算时间精确到微妙
- linux计算时间