Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1324892
  • 博文数量: 281
  • 博客积分: 8800
  • 博客等级: 中将
  • 技术积分: 3345
  • 用 户 组: 普通用户
  • 注册时间: 2006-05-17 22:31
文章分类

全部博文(281)

文章存档

2013年(1)

2012年(18)

2011年(16)

2010年(44)

2009年(86)

2008年(41)

2007年(10)

2006年(65)

我的朋友

分类: LINUX

2009-05-05 10:34:57

Actually, VxWorks can use the following thing to replace gettimeofday.

    struct timespec tp;
    ret = clock_gettime(CLOCK_REALTIME, &tp);
    srand( tp.tv_sec+tp.tv_nsec);


However, you can implement gettimeofday as following

int gettimeofday(struct timeval *tv, struct timezone *tz)
{
    int ret;
    struct timespec tp;

    if  ( (ret=clock_gettime(CLOCK_REALTIME, &tp))==0)
    {
        tv->tv_sec  = tp.tv_sec;
        tv->tv_usec = (tp.tv_nsec + 500) / 1000;  //将纳秒转成微妙

        if (tz != NULL)
        {
            getTimeZone(tp.tv_sec, &tz->tz_minuteswest, &tz->tz_dsttime);
        }
    }
     return ret;
}

static void getTimeZone(time_t t, int *min, int *dst)
{
    struct tm dstTm;
    char *tz = getenv(TZ_ENV);
    *dst = (localtime_r(&t, &dstTm)==OK) ? dstTm.tm_isdst : 0;

    if (tz)
    {   /* see VxWorks timeLib develop guide */
      /*
name_of_zone:<(unused)>:time_in_minutes_from_UTC:daylight_start:daylight_end
*/
      /* mmddhh */
        const char *p = tz;
        int i = 0;
        while ( (i < 2) && (p = strchr(p, ':')) )
        {
           ++p;
           ++i;
        }
        if (p)
        {
            if (sscanf(p, "%d", min)!=1)
            {
                *min = 0;
            }
        }
    }
}
阅读(2304) | 评论(1) | 转发(0) |
给主人留下些什么吧!~~

creasyimm2012-06-25 18:12:58

tv->tv_usec = (tp.tv_nsec + 500) / 1000;  //将纳秒转成微妙
多加500是什么意思?