C language Linux under Gettimeofday function Windows replacement scheme

Source: Internet
Author: User

* 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 (&LT);
        newtime = localtime (&LT);
        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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.