Hi!

I'd like to provide a gettimeofday() function for systems that do not
have it (at least Win32). 

I already wrote a module that works, see below. But this module
overwrites the existing gettimeofday module. I do not know how to
integrate the changes into the existing module because of the autoconf
and M4 magic that is involved.

I have two questions: Would you consider integrating such changes into
the existing module at all? If so, how can this be done?

Regards,
Martin
Description:
gettimeofday() function: return current time.

Files:
lib/gettimeofday.h
lib/gettimeofday.c
m4/gettimeofday.m4

Depends-on:

configure.ac:
gl_FUNC_GETTIMEOFDAY

Makefile.am:

Include:
"gettimeofday.h"

License:
LGPL

Maintainer:
all

#serial 1

# Copyright (C) 2005 Free Software Foundation, Inc.
# This file is free software; the Free Software Foundation
# gives unlimited permission to copy and/or distribute it,
# with or without modifications, as long as this notice is preserved.

AC_DEFUN([gl_FUNC_GETTIMEOFDAY],
[
  AC_LIBSOURCES([gettimeofday.c, gettimeofday.h])
  gl_PREREQ_GETTIMEOFDAY
  AC_REPLACE_FUNCS(gettimeofday)
])

# Prerequisites for lib/gettimeofday.c
AC_DEFUN([gl_PREREQ_GETTIMEOFDAY], 
[
  AC_REQUIRE([AC_HEADER_TIME])

  AC_CHECK_TYPE([suseconds_t], ,
    [AC_DEFINE([suseconds_t], [int],
       [Define to `int' if `suseconds_t' is missing.])
    ],
    [
#    if TIME_WITH_SYS_TIME
#     include <sys/time.h>
#     include <time.h>
#    else
#     if HAVE_SYS_TIME_H
#      include <sys/time.h>
#     else
#      include <time.h>
#     endif
#    endif
    ])
  
  AC_CACHE_CHECK([for struct timeval], fu_cv_sys_struct_timeval,
    [AC_TRY_COMPILE(
      [
#      if TIME_WITH_SYS_TIME
#       include <sys/time.h>
#       include <time.h>
#      else
#       if HAVE_SYS_TIME_H
#        include <sys/time.h>
#       else
#        include <time.h>
#       endif
#      endif
      ],
      [static struct timeval x; x.tv_sec = x.tv_usec;],
      fu_cv_sys_struct_timeval=yes,
      fu_cv_sys_struct_timeval=no)
    ])
 
  if test $fu_cv_sys_struct_timeval = yes; then
    AC_DEFINE(HAVE_STRUCT_TIMEVAL, 1,
              [Define if struct timeval is declared in <time.h> or 
<sys/time.h>. ])
  fi
])
#ifndef GETTIMEOFDAY_H
#define GETTIMEOFDAY_H

#include <config.h>

#if TIME_WITH_SYS_TIME
# include <sys/time.h>
# include <time.h>
#else
# if HAVE_SYS_TIME_H
#  include <sys/time.h>
# else
#  include <time.h>
# endif
#endif

#ifndef HAVE_STRUCT_TIMEVAL
struct timeval 
{
    time_t tv_sec;
    suseconds_t tv_usec;
};
#endif

#ifndef HAVE_GETTIMEOFDAY
int gettimeofday(struct timeval *tp, void *tzp);
#endif

#endif /* GETTIMEOFDAY_H */
#include <config.h>

#ifdef WIN32
#include <sys/timeb.h>
#endif

#include "gettimeofday.h"

int gettimeofday(struct timeval *tp, void *tzp)
{
#ifdef WIN32
    
    struct _timeb timebuffer;
    
    _ftime(&timebuffer);
    tp->tv_sec = timebuffer.time;
    tp->tv_usec = timebuffer.millitm * 1000;
    
    return 0;
    
#else
    
    tp->tv_sec = time(NULL);
    tp->tv_usec = 0;

    return 0;
    
#endif
}
_______________________________________________
bug-gnulib mailing list
bug-gnulib@gnu.org
http://lists.gnu.org/mailman/listinfo/bug-gnulib

Reply via email to