* Copyright (C) Mymtom ([email protected])
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* Modification, is permitted provided that the following conditions
* is met:
* 1. Redistributions of source code must retain the copyright
* Notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* Notice, this list of conditions and the following disclaimer in the
* Documentation and/or other materials provided with the distribution.
*
* This software was provided by ERIC Young "as is" and
* Any EXPRESS OR implied warranties, including, and not LIMITED to, the
* Implied warranties of merchantability and FITNESS for A particular PURPOSE
* is disclaimed. In NO EVENT shall the AUTHOR OR CONTRIBUTORS be liable
* For any DIRECT, INDIRECT, incidental, special, exemplary, OR consequential
* Damages (including, but not LIMITED to, procurement of substitute GOODS
* or SERVICES; LOSS of Use, DATA, OR profits; OR business Interruption)
* However caused and on any theory of liability, WHETHER in contract, STRICT
* Liability, or TORT (including negligence or OTHERWISE) arising in any
* Out of the software, even IF advised of the possibility of
* SUCH DAMAGE.
*
* $Id: Src/lib/libtrace/gettimeofday.c,v 1.1 2008/03/28 12:08:44 mymtom EXP $
*/
#include <time.h>
#ifdef WIN32
# include <windows.h>
#else
# include <sys/time.h>
#endif
#ifdef WIN32
int
gettimeofday (struct timeval *tp, void *tzp)
{
time_t clock;
struct TM TM;
systemtime Wtm;
Getlocaltime (&WTM);
Tm.tm_year = wtm.wyear-1900;
Tm.tm_mon = wtm.wmonth-1;
Tm.tm_mday = Wtm.wday;
Tm.tm_hour = Wtm.whour;
Tm.tm_min = Wtm.wminute;
Tm.tm_sec = Wtm.wsecond;
Tm. TM_ISDST =-1;
Clock = mktime (&TM);
Tp->tv_sec = clock;
tp->tv_usec = wtm.wmilliseconds * 1000;
return (0);
}
#endif
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include "Gettimewin.h"
#ifdef WIN32
# include <windows.h>
#else
# include <sys/time.h>
#endif
int gettimeofday (struct timeval *tp, void *tzp);
#if defined (_msc_ver) &&!defined (snprintf)
#define SNPRINTF _snprintf
#endif
int main (int argc, char *argv[])
{
struct Timeval TV;
Char buf[] = "1970-01-01 00:00:00.000";
struct TM *newtime;
char log_name[128];
char log_time[128];
time_t lt;
time (<);
newtime = localtime (<);
strftime (Log_name, +, "%y%m%d", newtime);
strftime (log_time,128, "%y-%m-%d%H:%M:%S", newtime);
printf ("%s\n", log_name);
printf ("%s\n", log_time);
(void) gettimeofday (&TV, 0);
NewTime = LocalTime (& (time_t (TV.TV_SEC)));
(void) strftime (buf, sizeof (BUF)-1, "%y-%m-%d%h:%m:%s.000",
(LocalTime (&TV.TV_SEC)));
(void) snprintf (buf + 3, "%03d", (int) (tv.tv_usec/1000));
(void) printf ("%s\n", buf);
return (0);
}
Http://blog.sina.com.cn/s/blog_48526a5f0100iqyn.html
C language Linux under Gettimeofday function Windows replacement scheme