gettimeofday windows上的实现

2023-11-07

gettimeofday是Linux上的函数,在windows的实现,这里直接转doubango工程中的tsk_time.c 源文件种的实现,可以参考;


#include "tsk_time.h"



#include "tsk_debug.h"


#if TSK_UNDER_WINDOWS
# include <Winsock2.h> // timeval
# include <windows.h>
#elif defined(__SYMBIAN32__)
# include <_timeval.h> 
#else
# include <sys/time.h>
#endif


#include <time.h>
#if defined (__APPLE__)
# include <mach/mach.h>
# include <mach/mach_time.h>
#endif


/**@defgroup tsk_time_group Datetime functions.
*/


#if !HAVE_GETTIMEOFDAY
#if TSK_UNDER_WINDOWS


/* For windows implementation of "gettimeofday" Thanks to "http://www.cpp-programming.net/c-tidbits/gettimeofday-function-for-windows" */
#if defined(_MSC_VER) || defined(_MSC_EXTENSIONS)
#define DELTA_EPOCH_IN_MICROSECS 11644473600000000Ui64
#else
#define DELTA_EPOCH_IN_MICROSECS 11644473600000000ULL
#endif


struct timezone 
{  
int  tz_minuteswest; // minutes W of Greenwich  
int  tz_dsttime;     // type of dst correction
};


int gettimeofday(struct timeval *tv, struct timezone *tz) 
{  
FILETIME ft;
uint64_t tmpres = 0;  
static int tzflag = 0; 


if(tv)   
{    
#ifdef _WIN32_WCE
SYSTEMTIME st;
GetSystemTime(&st);
SystemTimeToFileTime(&st, &ft);
#else
GetSystemTimeAsFileTime(&ft);
#endif


tmpres |= ft.dwHighDateTime;   
tmpres <<= 32; 
tmpres |= ft.dwLowDateTime;


/*converting file time to unix epoch*/   
tmpres /= 10;  /*convert into microseconds*/  
tmpres -= DELTA_EPOCH_IN_MICROSECS;  
tv->tv_sec = (long)(tmpres / 1000000UL); 
tv->tv_usec = (long)(tmpres % 1000000UL); 
}


if (tz){   
if (!tzflag){
#if !TSK_UNDER_WINDOWS_RT
_tzset();
#endif
tzflag++;  
}   
tz->tz_minuteswest = _timezone / 60;
tz->tz_dsttime = _daylight;
}


return 0; 
}


#else
#pragma error "You MUST provide an implement for 'gettimeofday'"
#endif /* WIN32 */


#endif /* !HAVE_GETTIMEOFDAY */


/**@ingroup tsk_time_group
* The tsk_gettimeofday() function shall obtain the current time, expressed as seconds and microseconds since EPOCH (00:00:00 UTC on 1 January 1970). 
* The resolution of the system clock is unspecified.
* @param tv The current time, expressed as seconds and microseconds since EPOCH(00:00:00 UTC on 1 January 1970).
* @param tz The timezone.
* @retval The tsk_gettimeofday() function shall return 0 and no value shall be reserved to indicate an error.
*/
int tsk_gettimeofday(struct timeval *tv, struct timezone *tz)
{
return gettimeofday(tv, tz);
}


/**@ingroup tsk_time_group
*/
uint64_t tsk_gettimeofday_ms()
{
struct timeval tv;
tsk_gettimeofday(&tv, tsk_null);
return (((uint64_t)tv.tv_sec)*(uint64_t)1000) + (((uint64_t)tv.tv_usec)/(uint64_t)1000);
}


/**@ingroup tsk_time_group
* Gets the number of milliseconds in @a tv
* @retval The number of milliseconds
*/
uint64_t tsk_time_get_ms(const struct timeval* tv)
{
if(!tv){
TSK_DEBUG_ERROR("Invalid parameter");
return 0;
}
return (((uint64_t)tv->tv_sec)*(uint64_t)1000) + (((uint64_t)tv->tv_usec)/(uint64_t)1000);

}





这个是网络上常见的实现;
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);
}

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

gettimeofday windows上的实现 的相关文章

  • 【廖雪峰python入门笔记】列表生成式

    1 生成列表 要生成list 1 2 3 4 5 6 7 8 9 10 我们可以用range 1 11 gt gt gt range 1 11 1 2 3 4 5 6 7 8 9 10 但如果要生成 1x1 2x2 3x3 10x10 怎么
  • Web系统大规模并发:电商秒杀与抢购

    http blog jobbole com 91754 一 大规模并发带来的挑战 在过去的工作中 我曾经面对过5w每秒的高并发秒杀功能 在这个过程中 整个Web系统遇到了很多的问题和挑战 如果Web系统不做针对性的优化 会轻而易举地陷入到异
  • Android图片加载优化方案

    1 前言 在电商APP中 图片在整个页面中占比最大 清晰高质量的图片能够明显提升转化率 但是APP运行环境错综复杂 往往我们会遇到 图片压缩导致模糊 列表加载长时间显示空白图 查看大图黑屏过久 甚至因为图片过大导致crash等 如下效果展示
  • Java编程思想课后练习题——第三章-操作符

    本文主要依据 Java编程思想 的示例及课后练习 通过个人编写 同时参考答案的代码写法 主要目的是用于自己熟悉编码风格 同时可以给更多人提供参考 只完成了部分练习 练习1 package 操作符 import static net mind
  • VScode安装

    1 下载安装VScode 下载地址 Visual Studio Code Code Editing Redefined 浏览器下载很慢 我们进入下载页面 复制下载链接 https az764295 vo msecnd net stable
  • ubunutu20.04 pycharm使用anaconda下环境(主要是pytorch)

    新建pycharm项目和anaconda环境 打开pycharm new project new environment using conda 修改环境名 项目名 python版本选择3 9 我的电脑pyTorch在3 9上跑通了 其他版

随机推荐

  • SAP应付模块详解

    本文介绍以下内容 应付模块的基础知识 主数据 供应商 发票处理 付款及清账 预付款 应付票据 其他特别总账业务 供应商余额查询 定期处理 月末及年末年初的操作 应付模块报表 应付模块设计的流程清单和方案要点 由于应付模块和应收模块在很多方面
  • LaTex 之 数学运算符号

    属于号 in 开根号 sqrt 求和符号 sum 积分符号 int min max 大于等于 小于等于号 导言区使用两个宏包 usepackage amsmath usepackage amssymb 大于等于号 geqslant or g
  • Code Review的亲身实践

    Code Review 中文叫代码审查 指的是完成了部分功能的代码开发之后 在代码真正合并到仓库主分支之前 邀请同事帮你进行代码的审核和检查 检查代码的质量 规范 设计等等方面的过程 代码审查的好处 知识共享 进行代码审查的好处很多 其中一
  • Elasticsearch入门初探-单机多节点集群

    实时搜索引擎Elasticsearch 简称ES 是一个基于Apache Lucene 的开源搜索引擎 无论在开源还是专有领域 Lucene可以被认为是迄今为止最先进 性能最好 这里分享Elasticsearch入门在单机下如何配置多节点集
  • 数据权限——Mybatis拦截器实现

    一 需求背景介绍 1 需求介绍 需要实现数据权限管理 包含角色 普通用户 组长 管理员 其中普通用户只能看到自己创建的项目 组长能看到自己所管理的普通用户创建的项目 管理员能看到所有项目 相关表为 项目表 包含责任人owner字段 owne
  • iconfont下载到本地使用教程

    1 选择Symbol下载至本地 2 将压缩包进行解压 将压缩包如下后缀的文件放入项目中 iconfont css iconfont ttf iconfont woff iconfont woff2 通过import进行导入到index cs
  • 使用python-docx生成word文档

    基于python docx生成自定义word文档 源代码 from docx import Document from docx shared import Pt Inches from docx oxml ns import qn fro
  • Linux 字节序与字节对齐优化

    1 字节序跟Linux Windows无关 是由CPU构架决定 同一个CPU不管装的是Windows 或 Linux 字节序都是一样的 2 字节对齐 Linux 全用 attribute packed 作用于结构体 类似于pragma pa
  • 【深度学习】——循环神经网络RNN及实例气温预测、单层lstm股票预测

    引言 密集连接网络和卷积神经网络都有主要的特点 那就是它们没有记忆 它们单独处理每个输入 在输入和输入之间没有保存任何状态 举个例子 当你在阅读一个句子的时候 你需要记住之前的内容 我们才能动态的了解这个句子想表达的含义 生物智能已渐进的方
  • easyexcel poi根据模板导出Excel

    1 导入依赖
  • 最大和的连续子数组

    1 题目背景 给你一个整数数组nums 请你找出一个具有最大和的连续子数组 子数组最少包含一个元素 返回其最大和 子数组是数组中的一个连续部分 2 代码实现 public class Solution public static void
  • 关于HTTP常见状态码

    http状态码 HTTP Status Code 表示网页服务器超文本传输协议响应状态的3位数代码 是服务器用来告诉客户端当前请求响应的状态 通过状态码判断服务器运行状态 分类的第一位数字来表示 1xx表示消息 2xx表示成功 3xx表示重
  • 指向函数的指针 ------ 函数指针(function pointer)

    函数具有可赋值给指针的物理内存地址 一个函数的函数名就是一个指针 它指向函数的代码 一个函数的地址是该函数的进入点 也是调用函数的地址 函数的调用可以通过函数名 也可以通过指向函数的指针来调用 函数指针还允许将函数作为变元传递给其他函数 不
  • 读写一致 && MySQL&&Redis

    存储一致性 CPU存储 L1L2Cache等等 不懂CPU如何读写内存还敢说自己是程序员 知乎 zhihu com 基础知识 1 什么是cache line cache line 位于 CPU 与内存之间 CPU想要获得数据的时候 先从CP
  • Typora+PicGo+Alist 私人图床教程

    前置资源 已安装好typora 直接官网下载即可 Typora 官方中文站 typoraio cn 安装好PicGo 稳定版优先 推荐稳定版2 3 1 PicGo 腾讯云COS 下载链接 下载速度快 https picgo 12517503
  • 人民币兑换python

    输入一个人民币的整数值 100以内以元为单位 编程找到用10元 5元 2元 1元表示的总数量的最小组合方式 输入形式 从控制台输入一个整数值 表示以元为单位的人民币币值 输出形式 向控制台输出四个整数 以空格分隔 分别表示兑换成的10元 5
  • linux安装minio以及springboot整合使用

    文章目录 1 linux安装minio 2 springboot整合minio使用 1 linux安装minio 1 新建文件夹 mkdir home minio 数据文件夹 mkdir home minio data 创建日志文件夹 mk
  • python-selenium-pytest-allure UI自动化测试

    一 下载及安装插件 python selenium pytest allure 辅助 pyautoit pymysql 二 代码框架 1 基础文件 main py if name main pytest main alluredir all
  • 台式电脑没鼠标怎么移动光标_台式电脑没有鼠标怎么操作电脑

    没鼠标只能靠键盘操作了 以下都是键盘快捷键命令F6 选择操作区域光标键 选择目标backspac 退回上一级目录enter 进入 运行 table 项目选择单独按Windows 显示或隐藏 开始 功能表Windows BREAK 显示 系统
  • gettimeofday windows上的实现

    gettimeofday是Linux上的函数 在windows的实现 这里直接转doubango工程中的tsk time c 源文件种的实现 可以参考 include tsk time h include tsk debug h if TS