* Copyright (C) 2008 mymtom (mymtom@hotmail.com)
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are 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 IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE 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 WAY
 * OUT OF THE USE OF THIS 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, 128, "%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 + 20, 3, "%03d", (int)(tv.tv_usec / 1000));
    (void)printf("%s\n", buf);

return (0);
}

http://blog.sina.com.cn/s/blog_48526a5f0100iqyn.html

c语言之linux下gettimeofday函数windows替换方案的更多相关文章

  1. linux下sprintf_s函数的替代

    error code: ]; sprintf_s(buf, , "predicted position:(%3d, %3d)", predict_pt.x, predict_pt. ...

  2. linux select函数:Linux下select函数的使用详解【转】

    本文转载自;http://www.bkjia.com/article/28216.html Linux下select函数的使用 Linux下select函数的使用 一.Select 函数详细介绍 Se ...

  3. linux下syscall函数,SYS_gettid,SYS_tgkill

    出处:http://blog.chinaunix.net/uid-28458801-id-4630215.html     linux下syscall函数,SYS_gettid,SYS_tgkill  ...

  4. 对于linux下system()函数的深度理解(整理)

    原谅: http://blog.sina.com.cn/s/blog_8043547601017qk0.html 这几天调程序(嵌入式linux),发现程序有时就莫名其妙的死掉,每次都定位在程序中不同 ...

  5. Linux下c函数dlopen实现加载动态库so文件代码举例

    dlopen()是一个强大的库函数.该函数将打开一个新库,并把它装入内存.该函数主要用来加载库中的符号,这些符号在编译的时候是不知道的.这种机制使得在系统中添加或者删除一个模块时,都不需要重新编译了. ...

  6. 转:对于linux下system()函数的深度理解(整理)

    这几天调程序(嵌入式linux),发现程序有时就莫名其妙的死掉,每次都定位在程序中不同的system()函数,直接在shell下输入system()函数中调用的命令也都一切正常.就没理这个bug,以为 ...

  7. 【C/C++】Linux下system()函数引发的错误

    http://my.oschina.net/renhc/blog/54582 [C/C++]Linux下system()函数引发的错误 恋恋美食  恋恋美食 发布时间: 2012/04/21 11:3 ...

  8. 如何在linux下制作一个windows的可启动u盘?

    如何在linux下制作一个windows的可启动u盘? 情景是这样的,有一个windows10的iso,现在想通过U盘安装,要求即支持UEFI(启动引导器),又支持Legacy(启动引导器),因为有一 ...

  9. [转帖]Linux下fork函数及pthread函数的总结

    Linux下fork函数及pthread函数的总结 https://blog.csdn.net/wangdd_199326/article/details/76180514 fork Linux多进程 ...

随机推荐

  1. t-sql 笔记(1)

    Toad-for-SQL-Server-Freeware 1.查询哪些数据库对象使用了某个表 SELECT b.[name], a.[definition] FROM sys.all_sql_modu ...

  2. es6类声明,class总结

    1.class的基本写法 class a{ // 传入参数或者写入固定参数 constructor(a,b){ this.a=a this.b=b } // 可直接调用的计算后的参数 get c(){ ...

  3. Oracle RMAN 备份及不完全恢复(删除archievelog)

    RMAN备份命令 backup Database format='/home/oracle/backup/bak_full_%U_%T' tag='bak_full'; sql 'alter syst ...

  4. BUPT复试专题—解析表达式(2015)

    题目描述 输入一个字符串形式的表达式,该表达式中包括整数,四则运算符(+.-.*./),括号,三角函数(sin(x).cos(x).tan(x)),底数函数(lg(x).ln(x)),计算该表达式的值 ...

  5. mui 上拉加载更多

    看起来很简单的东西,实践过程中还是出现了很多麻烦,比如上拉时,状态条跑到了顶部,因为内容没有添加到容器中,再比如下拉的回调函数使用问题,this的传递. html实现部分: <div class ...

  6. 通过ngxtop实时监控webserver的访问情况 / 解决ImportError: No module named _sqlite3问题

    通过ngxtop实时监控webserver的访问情况 2014-04-03      0个评论    来源:通过ngxtop实时监控web server的访问情况   收藏    我要投稿 关于对ng ...

  7. GoldenGate Lag For Huge Insert

    前些天客户的ogg延迟到达8小时左右.于是我当时用logdump追踪了一下: 看进程状态: send extsa staus EXTRACT ZBDBA (PID 2269368) Current s ...

  8. Jmeter-接口测试(二)

    接口测试我们前面已经讲过,此博不做重复,我们主要讲讲如何利用Jmeter做接口测试及参数化. 一.新建项目 1.运行Jmeter.bat打开Jmeter 2.添加线程组(测试计划->添加-> ...

  9. MvcPager帮助文档 - MvcAjaxOptions 类

    表示用于 MvcPager 在 Ajax 分页模式下的选项设置,该类继承自 AjaxOptions. 公共属性: 名称 说明 默认值 AllowCache 获取或设置一个值,该值指示是否在Ajax分页 ...

  10. sublime text3 修改左边栏背景颜色为编辑栏颜色

    用Package Control安装Theme-Afterglow插件: Ctrl+Shift+P -> install ,如图 点击Install Package,在弹出框中输入Theme-A ...