Chinaunix首页 | 论坛 | 博客

MyL

  • 博客访问: 10190
  • 博文数量: 4
  • 博客积分: 1410
  • 博客等级: 上尉
  • 技术积分: 50
  • 用 户 组: 普通用户
  • 注册时间: 2010-02-08 14:20
文章分类

全部博文(4)

文章存档

2010年(4)

我的朋友
最近访客

分类: LINUX

2010-02-08 14:39:35

自己动手shell实现时间差计算(精确到毫秒)

 

脚本

 


#! /bin/bash

if [ $# -eq 0 ]

then
        echo "Usage : ./timecount.sh file"
        echo "e.g. ./timecount.sh time1"
        exit 1
fi
c=1
cf=0
for i in $(sed 's/[- ]/:/g' $1)
do
        year[$c]=$(echo "$i"|cut -d: -f1)
        month[$c]=$(echo "$i"|cut -d: -f2)
        day[$c]=$(echo "$i"|cut -d: -f3)
        hour[$c]=$(echo "$i"|cut -d: -f4)
        min[$c]=$(echo "$i"|cut -d: -f5)
        sec[$c]=$(echo "$i"|cut -d: -f6)
        msec[$c]=$(echo "$i"|cut -d: -f7)
# test info

# echo "$c,${year[$c]},${month[$c]},${day[$c]},${hour[$c]},${min[$c]}

,${sec[$c]},${msec[$c]}"

        ((c+=1))
done
#msec

#test info
#echo "$(( ${msec[2]} - ${msec[1]} )) haha_msec"

if [ $(( ${msec[2]} - ${msec[1]} )) -lt 0 ] ; then
                msectime="$(( ${msec[2]} + 1000 - ${msec[1]} ))"
                cf=1
else
                msectime="$(( ${msec[2]} - ${msec[1]} ))"
                cf=0
fi
#test info
#echo "this is msec"
#echo "${msectime}"


#sec
#test info
#echo "$(( ${sec[2]} - $cf - ${sec[1]} )) haha_sec"

if [ $(( ${sec[2]} - $cf - ${sec[1]} )) -lt 0 ] ; then
                sectime="$(( ${sec[2]} - $cf + 60 - ${sec[1]} ))"
                cf=1
else
                sectime="$(( ${sec[2]} - $cf - ${sec[1]} ))"
                cf=0
fi
#test info
#echo "this is sec"
#echo "$sectime"

#min
#test info
#echo "$(( ${min[2]} - $cf - ${min[1]} )) haha_min"

if [ $(( ${min[2]} - $cf - ${min[1]} )) -lt 0 ] ; then
                mintime="$(( ${min[2]} - $cf + 60 - ${min[1]} ))"
                cf=1
else
                mintime="$(( ${min[2]} - $cf - ${min[1]} ))"
                cf=0
fi
#test info
#echo "this is min"
#echo "$mintime"

#hour

#test info
#echo "$(( ${hour[2]} - $cf - ${hour[1]} )) haha_hour"

if [ $(( ${hour[2]} - $cf - ${hour[1]} )) -lt 0 ] ; then
                hourtime="$(( ${hour[2]} - $cf + 60 - ${hour[1]} ))"
                cf=1
else
                hourtime="$(( ${hour[2]} - $cf - ${hour[1]} ))"
                cf=0
fi
#test info
#echo "this is hour"
#echo "$hourtime"

#day
#this part need the function moudle "day" in the same directory with this script
daytime=$(./day ${year[1]} ${month[1]} ${day[1]} ${year[2]} ${month[2]} $(( ${day[2]} - $cf )))
#test info
#echo "this is day"
#echo "$daytime"
echo
echo "the time during"
echo "${year[1]}/${month[1]}/${day[1]} ${hour[1]}:${min[1]}:${sec[1]}:${msec[1]}"
echo "and"
echo "${year[2]}/${month[2]}/${day[2]} ${hour[2]}:${min[2]}:${sec[2]}:${msec[2]}"
echo -n "is : "
echo


 

天数计算day.c:

 

 

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#ifndef __SDATE__
#define __SDATE__
struct SDate {
 short int year;
 short int month;
 short int day;
};
typedef struct SDate Sdate;
#endif
int IsLeapYear(short year);
int DaysOfOneYear(Sdate sd);
int SDateToAbsDays(Sdate sd);
int IsLeapYear(short year) {
 return year%4==0&&year%100||year%400==0;
}
int DaysOfOneYear(Sdate sd) {
 switch(sd.month-1) {
 case 11:
  sd.day+=30;
 case 10:
  sd.day+=31;
 case 9:
  sd.day+=30;
 case 8:
  sd.day+=31;
 case 7:
  sd.day+=31;
 case 6:
  sd.day+=30;
 case 5:
  sd.day+=31;
 case 4:
  sd.day+=30;
 case 3:
  sd.day+=31;
 case 2:
  sd.day+=IsLeapYear(sd.year)?29:28;
 case 1:
  sd.day+=31;
 }
 return sd.day;
}

int SDateToAbsDays(Sdate sd) {
 int years = sd.year -1;
 int days = years*365 + years/4 - years/100 + years/400;
 days+=DaysOfOneYear(sd);
 return days;
}
int main(int argc, char *argv[])
{
        Sdate d1,d2;
        int day=0;
        argv[0]="day";
        d1.year=atoi(argv[1]);
        d1.month=atoi(argv[2]);
        d1.day=atoi(argv[3]);
        d2.year=atoi(argv[4]);
        d2.month=atoi(argv[5]);
        d2.day=atoi(argv[6]);
        day=SDateToAbsDays(d2)-SDateToAbsDays(d1);
        
        printf("%d",day);
}


 

timecount源代码下载

 

阅读(3602) | 评论(0) | 转发(0) |
0

上一篇:没有了

下一篇:数据库备份

给主人留下些什么吧!~~