gettimeofday

Why changing timezone from shell does not affect gettimeofday() even after reboot?

空扰寡人 提交于 2019-12-12 03:06:58
问题 I have changed on Ubuntu timezone using dpkg-reconfigure tzdata from UTC+2 to UTC+0 but running C code gettimeofday() still showing tz_minuteswest and tv_sec in previous timezone even after reboot. Only after running C code below once gettimeofday() starts to showing UTC+0 time: #include <stdlib.h> #include <stdio.h> #include <sys/time.h> int main() { struct timeval tv; struct timezone tz; setenv("TZ", "UTC", 1); tzset(); gettimeofday(&tv, &tz); tv.tv_sec -= 7200; tz.tz_minuteswest = 0;

getting chrono time in specific way

前提是你 提交于 2019-12-11 22:44:58
问题 I have following C code: uint64_t combine(uint32_t const sec, uint32_t const usec){ return (uint64_t) sec << 32 | usec; }; uint64_t now3(){ struct timeval tv; gettimeofday(&tv, NULL); return combine((uint32_t) tv.tv_sec, (uint32_t) tv.tv_usec); } What this do it combine 32 bit timestamp, and 32 bit "something", probably micro/nanoseconds into single 64 bit integer. I have really hard time to rewrite it with C++11 chrono. This is what I did so far, but I think this is wrong way to do it. auto

Timeval struct usage

给你一囗甜甜゛ 提交于 2019-12-11 19:36:18
问题 I needed to calculate the time it takes to run a certain function and ran into the following code,record & output the execution time of a piece of code in nanoseconds And also is there any difference between struct timeval timer_spent & timeval timer_spent /* Put this line at the top of the file: */ #include <sys/time.h> /* Put this right before the code you want to time: */ struct timeval timer_start, timer_end; gettimeofday(&timer_start, NULL); /* Put this right after the code you want to

Python clock function on FreeBSD

你说的曾经没有我的故事 提交于 2019-12-11 14:38:40
问题 While testing Pythons time.clock() function on FreeBSD I've noticed it always returns the same value, around 0.156 The time.time() function works properly but I need a something with a slightly higher resolution. Does anyone the C function it's bound to and if there is an alternative high resolution timer? I'm not profiling so the TimeIt module is not really appropriate here. 回答1: time.clock() returns the processor time. That is, how much time the current process has used on the processor. So

Strange results while measuring delta time on Linux

人盡茶涼 提交于 2019-12-08 05:08:02
问题 Update: fixed delta calculations in code, still the issue remains Folks, could you please explain why I'm getting very strange results from time to time using the the following code: #include <unistd.h> #include <sys/time.h> #include <stdio.h> int main() { struct timeval start, end; long mtime1, mtime2, diff; while(1) { gettimeofday(&start, NULL); usleep(2000); gettimeofday(&end, NULL); mtime1 = (start.tv_sec * 1000 + start.tv_usec/1000.0); mtime2 = (end.tv_sec * 1000 + end.tv_usec/1000.0);

Strange results while measuring delta time on Linux

非 Y 不嫁゛ 提交于 2019-12-06 16:38:08
Update: fixed delta calculations in code, still the issue remains Folks, could you please explain why I'm getting very strange results from time to time using the the following code: #include <unistd.h> #include <sys/time.h> #include <stdio.h> int main() { struct timeval start, end; long mtime1, mtime2, diff; while(1) { gettimeofday(&start, NULL); usleep(2000); gettimeofday(&end, NULL); mtime1 = (start.tv_sec * 1000 + start.tv_usec/1000.0); mtime2 = (end.tv_sec * 1000 + end.tv_usec/1000.0); diff = mtime2 - mtime1; if(diff > 10) printf("WTF: %ld\n", diff); } return 0; } (You can compile and run

iOS后台倒计时

笑着哭i 提交于 2019-12-04 16:44:50
场景 我们经常遇到这样的场景,比如电商类App到零点的时候开始抢购,比如商品限购倒计时等等。这种场景下需要我们将客户端的时间与服务器保持一致,最重要的是,要防止用户通过断网修改系统时间,来影响客户端的逻辑。下面是我个人的分析和实现步骤,只为了帮助有同样需求的人,知识有限,欢迎大神们补充。 如果不想看分析的同学,可以直接调到 “奉上代码” 处查看具体实现。 分析 研究之前,对京东做了一下抓包,数据如下 京东抓包秒杀数据 12345 "miaoshaInfo": { "title": "京东秒杀", "miaoshaRemainTime": "79836", "miaosha": true }, 数据中可以看出,京东秒杀商品返回了秒杀剩余的时间。通过进入后台,再次进入前台,以及断网修改时间的尝试,发现并不影响倒计时的运行。 仿“京东秒杀”实现思路 1、程序进入后台时计时器不停止,这种做法网上有较多案例。例如: iOS 后台完成倒计时的功能 这种方案简书里面就有很多,有兴趣的同学可以在搜索一下。对于这种方案,我个人觉得有一定的审核风险,并没有使用。 2、在程序进入后台和进入前台时分别记录时间,程序进入前台获得时间差 IntervalTime,然后在定时器响应的时候获得正确的剩余时间(miaoshaRemainTime - IntervalTime)。我用的是这个方案,不过这个方案有个缺点

How to get the current time in milliseconds from C in Linux?

匿名 (未验证) 提交于 2019-12-03 07:36:14
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: How do I get the current time on Linux in milliseconds? 回答1: This can be achieved using the clock_gettime function. In the current version of POSIX, gettimeofday is marked obsolete . This means it may be removed from a future version of the specification. Application writers are encouraged to use the clock_gettime function instead of gettimeofday . Here is an example of how to use clock_gettime : #define _POSIX_C_SOURCE 200809L #include #include #include #include void print_current_time_with_ms (void) { long ms; // Milliseconds time_t s; //

gettimeofday() C++ Inconsistency

匿名 (未验证) 提交于 2019-12-03 02:29:01
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: I'm doing a project that involves comparing programming languages. I'm computing the Ackermann function. I tested Java, Python, and Ruby, and got responses between 10 and 30 milliseconds. But C++ seems to take 125 milliseconds. Is this normal, or is it a problem with the gettimeofday() ? Gettimeofday() is in time.h. I'm testing on a (virtual) Ubuntu Natty Narwhal 32-bit. I'm not short processing power (Quad-core 2.13 GHz Intel Xeon). My code is here: #include <iostream> #include <sys/time.h> using namespace std; int a(int m,int n) { if (m ==

how to use gettimeofday() or something equivalent with Visual Studio C++ 2008?

匿名 (未验证) 提交于 2019-12-03 02:13:02
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: Could someone please help me to use gettimeofday() function with Visual Studio C++ 2008 on Windows XP? here is a code that I found somewhere on the net: #include < time.h > #include <windows.h> #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) {