c语言之linux下gettimeofday函数windows替换方案
* 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(<);
newtime = localtime(<);
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替换方案的更多相关文章
- linux下sprintf_s函数的替代
error code: ]; sprintf_s(buf, , "predicted position:(%3d, %3d)", predict_pt.x, predict_pt. ...
- linux select函数:Linux下select函数的使用详解【转】
本文转载自;http://www.bkjia.com/article/28216.html Linux下select函数的使用 Linux下select函数的使用 一.Select 函数详细介绍 Se ...
- linux下syscall函数,SYS_gettid,SYS_tgkill
出处:http://blog.chinaunix.net/uid-28458801-id-4630215.html linux下syscall函数,SYS_gettid,SYS_tgkill ...
- 对于linux下system()函数的深度理解(整理)
原谅: http://blog.sina.com.cn/s/blog_8043547601017qk0.html 这几天调程序(嵌入式linux),发现程序有时就莫名其妙的死掉,每次都定位在程序中不同 ...
- Linux下c函数dlopen实现加载动态库so文件代码举例
dlopen()是一个强大的库函数.该函数将打开一个新库,并把它装入内存.该函数主要用来加载库中的符号,这些符号在编译的时候是不知道的.这种机制使得在系统中添加或者删除一个模块时,都不需要重新编译了. ...
- 转:对于linux下system()函数的深度理解(整理)
这几天调程序(嵌入式linux),发现程序有时就莫名其妙的死掉,每次都定位在程序中不同的system()函数,直接在shell下输入system()函数中调用的命令也都一切正常.就没理这个bug,以为 ...
- 【C/C++】Linux下system()函数引发的错误
http://my.oschina.net/renhc/blog/54582 [C/C++]Linux下system()函数引发的错误 恋恋美食 恋恋美食 发布时间: 2012/04/21 11:3 ...
- 如何在linux下制作一个windows的可启动u盘?
如何在linux下制作一个windows的可启动u盘? 情景是这样的,有一个windows10的iso,现在想通过U盘安装,要求即支持UEFI(启动引导器),又支持Legacy(启动引导器),因为有一 ...
- [转帖]Linux下fork函数及pthread函数的总结
Linux下fork函数及pthread函数的总结 https://blog.csdn.net/wangdd_199326/article/details/76180514 fork Linux多进程 ...
随机推荐
- [Git] Git把Tag推送到远程仓库
转载: http://blog.csdn.net/hustpzb/article/details/8056518 用git tag来给工程打上标签,但是这个命令只是在本地仓库打标签而已, 为了能把标签 ...
- virtualenv、virtualenvwrapper安装和使用;Mac os的特殊性
[sudo] pip install virtualenv 或者[sudo] pip3 install virtualenv [sudo]可用可不用 pip/pip3 install virtuale ...
- http://blog.csdn.net/tkwxty/article/details/34474501
http://blog.csdn.net/tkwxty/article/details/34474501
- 前端传递参数,由于控制器层类实现了struts2的ModelDriven而产生的一个异常
产生的异常如下: ognl.MethodFailedException: Method "setId" failed for object com.aliyun.pcitcAliy ...
- yum是什么?(linux命令)
一. yum是什么 yum = Yellow dog Updater, Modified 主要功能是更方便的添加/删除/更新RPM包. 它能自动解决包的倚赖性问题. 它能便于管理大量系统的更新问题 注 ...
- C#开发微信公众平台-就这么简单(转载)
写在前面 服务号和订阅号 URL配置 创建菜单 查询.删除菜单 接受消息 发送消息(图文.菜单事件响应) 示例Demo下载 后记 最近公司在做微信开发,其实就是接口开发,网上找了很多资料,当然园友也写 ...
- Django的信号机制详解
Django提供一种信号机制.其实就是观察者模式,又叫发布-订阅(Publish/Subscribe) .当发生一些动作的时候,发出信号,然后监听了这个信号的函数就会执行. Django内置了一些信号 ...
- Struts2数据类型转换之批量数据转换
前面我们实现了从字符串到User对象的转换.如果表单中有多个User数据,我们可以批量转换. 我们把input.jsp修改为: <h1>使用分号隔开username password< ...
- Unity网游开发生存指南—蒸汽之城
Posted by amy on 2013.03.07 文 / 王楠(梦加网络 游戏制作人) 前段时间关于Unity是否适合国内手游/网游创业团队的讨论非常火爆,本文从<蒸汽之城>的开发历 ...
- Uni2D 入门
原地址:http://blog.csdn.net/kakashi8841/article/details/17599505