文章详情

  • 游戏榜单
  • 软件榜单
关闭导航
热搜榜
热门下载
热门标签
php爱好者> php文档>Port Vxworks clock_gettime to gettimeofday

Port Vxworks clock_gettime to gettimeofday

时间:2009-05-05  来源:soararing

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;
            }
        }
    }
}
相关阅读 更多 +
排行榜 更多 +
大武道最新版

大武道最新版

休闲益智 下载
宝宝巴士手机版(babybus)

宝宝巴士手机版(babybus)

休闲益智 下载
宝宝巴士快乐启蒙游戏

宝宝巴士快乐启蒙游戏

休闲益智 下载