| OS X - Development and Darwin Discussion and questions about development for Mac OS X. |
| Post Reply | New Thread | Subscribe |
|
|
Thread Tools |
![]() Member Since: Mar 29, 2011
Posts: 7
![]() |
Hi,
I have below code to get current time in clock ticks: int main(void) { clock_t clk; struct tms tm; clk = times(&tm); printf("Output from times(): %u\n", clk); } It returns 1860906285 on MAC 10.4 which is "Mon, 05 Jun 2028 16:49:23 GMT" as per Epoch Converter - Unix Timestamp Converter I have no clue why times() is returning this value. Any help would be appreciated. Thanks |
| QUOTE Thanks | |
![]() Member Since: Jun 25, 2005
Location: On the road
Posts: 3,231
![]() ![]() ![]() ![]() ![]() ![]() ![]() Mac Specs: 2011 MBP, i7, 16GB RAM, MBP 2.16Ghz Core Duo, 2GB ram, Dual 867Mhz MDD, 1.75GB ram, ATI 9800 Pro vid
|
Reading the man pages for time() and times() indicates they return different values. Specifically, time() returns seconds since start time in 1970. The times() function returns something referred to as CLK_TCK's. The wording indicates that for every second returned, you get more than one CLK_TCK. I haven't found out what that value actually is.
I'd say, use time() instead of times(). CameraTime - Time lapse photography for novice and advanced users. When asking questions, post the version of your software. You'll receive better answers. Please post your results to the thread as it is good feedback.
|
| QUOTE Thanks | |
![]() Member Since: Mar 17, 2008
Location: Tucson, AZ
Posts: 6,533
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Mac Specs: 2008 and 2011 15" mbps, late 11 iMac, iPhone 4s, and too many ipods and other stuff
|
The value of CLK_TCK can be variable and it should not be assumed that CLK_TCK is a compile-time constant. While OS X's man page does not include this note, I believe it's likely to be the same. mike This machine kills fascists Got # ? phear the command line! |
||||
| QUOTE Thanks | |||||
![]() Member Since: Mar 17, 2008
Location: Tucson, AZ
Posts: 6,533
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Mac Specs: 2008 and 2011 15" mbps, late 11 iMac, iPhone 4s, and too many ipods and other stuff
|
The times() function returns the value of time in CLK_TCK's of a second since 0 hours, 0 minutes, 0 seconds, January 1, 1970, Coordinated Universal Time. So, it would appear we have a variable, rather than a constant, that's based on epoch time. where as time() would be a constant based on epoch as: The time() function returns the value of time in seconds since 0 hours, 0 minutes, 0 seconds, January 1, 1970, Coordinated Universal Time, without including leap seconds. If an error occurs, time() returns the value (time_t)-1. I'm not sure I'd base a date off clock ticks when a fixed method is readily available mike This machine kills fascists Got # ? phear the command line! |
| QUOTE Thanks | |
![]() Member Since: Mar 17, 2008
Location: Tucson, AZ
Posts: 6,533
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Mac Specs: 2008 and 2011 15" mbps, late 11 iMac, iPhone 4s, and too many ipods and other stuff
|
I mean to say I'd just use time() since it returns what you want in the first place, which is time (in seconds) based on epoch.
mike This machine kills fascists Got # ? phear the command line! |
| QUOTE Thanks | |
![]() Member Since: Jun 25, 2005
Location: On the road
Posts: 3,231
![]() ![]() ![]() ![]() ![]() ![]() ![]() Mac Specs: 2011 MBP, i7, 16GB RAM, MBP 2.16Ghz Core Duo, 2GB ram, Dual 867Mhz MDD, 1.75GB ram, ATI 9800 Pro vid
|
Quote:
Here is what I played with. Code:
#include <stdio.h>
#include <string.h>
#include <time.h>
#include <sys/times.h>
#include <limits.h>
/* typedef__darwin_clock_tclock_t; found in types.h */
/* typedef unsigned long __darwin_clock_t found in _types.h */
int main(void)
{
clock_t clk;
// unsigned long clk;
struct tms tm;
printf("CLK_CKK: %u\n", CLK_TCK);
// The times() function conforms to IEEE Std 1003.1-1988 (``POSIX.1'').
clk = times(&tm); // clock_t times(struct tms *buffer);
printf("Output from times(): %lu\n", clk);
double clkf = (double) clk / (double) CLK_TCK;
printf("Output from times() / CLK_TCK : %f\n", clkf);
// The time() function conforms to IEEE Std 1003.1-2001 (``POSIX.1'').
time_t clk2;
time_t tloc;
clk2 = time(&tloc);
printf("Output from time(): %lu\n", clk2);
}
CameraTime - Time lapse photography for novice and advanced users. When asking questions, post the version of your software. You'll receive better answers. Please post your results to the thread as it is good feedback.
|
|
| QUOTE Thanks | ||
![]() Member Since: Mar 29, 2011
Posts: 7
![]() |
I think you should not directly divide it by CLK_TCK.
If you try to get output of times() at two time instances, then divide the difference of times() output by CLK_TCK then you will get correct seconds elapsed. But anyways output of times() dsnt seems to make any sense, isnt it? |
| QUOTE Thanks | |
![]() Member Since: Jun 25, 2005
Location: On the road
Posts: 3,231
![]() ![]() ![]() ![]() ![]() ![]() ![]() Mac Specs: 2011 MBP, i7, 16GB RAM, MBP 2.16Ghz Core Duo, 2GB ram, Dual 867Mhz MDD, 1.75GB ram, ATI 9800 Pro vid
|
Quote:
Yea, I don't get it either. Not really looking further though since it doesn't affect anything I'm working on. CameraTime - Time lapse photography for novice and advanced users. When asking questions, post the version of your software. You'll receive better answers. Please post your results to the thread as it is good feedback.
|
|
| QUOTE Thanks | ||
![]() Member Since: Mar 29, 2011
Posts: 7
![]() |
One thing I noticed is that times() has different behavior on MAC and Linux.
On MAC 10.4 its like: 1- Get times(struct tms *tp) value on say Jun 2 2011 2- Again get times(struct tms *tp) value on Jun 5 2011 3- Subtract the later with earlier and divide it by CLK_TCK, you will get the seconds elapsed i.e. 72hrs (259382 Seconds) On Linux RH 2.4 Its like: 1- Get times(NULL) value on say Jun 2 2011 2- Again get times(NULL) value on Jun 5 2011 3- Subtract the later with earlier and divide it by CLK_TCK, you will get the exact CPU clock ticks (not seconds) elapsed i.e. (something like 10-12 seconds..depends). I need to implement later functionality in MAC to get actual usage which should be independent of manual date/time change. Since times(NULL) is obsolete in MAC so we have tried to use times(struct tms *tm), but its not working the way I need it to. Do we have any API in mac which returns actual CPU ticks elapsed since an arbitrary point in past say epoch..?? |
| QUOTE Thanks | |
![]() Member Since: Jun 25, 2005
Location: On the road
Posts: 3,231
![]() ![]() ![]() ![]() ![]() ![]() ![]() Mac Specs: 2011 MBP, i7, 16GB RAM, MBP 2.16Ghz Core Duo, 2GB ram, Dual 867Mhz MDD, 1.75GB ram, ATI 9800 Pro vid
|
Quote:
It sounds to me that what you want is time function that is more fine grained than the seconds count your getting back. Something say that consistently counts in milliseconds. Unfortunately I'm not familiar enough with the time functions to know which one to use. Using 'man time' lead me to gettimeofday(). There may be others more appropriate. CameraTime - Time lapse photography for novice and advanced users. When asking questions, post the version of your software. You'll receive better answers. Please post your results to the thread as it is good feedback.
|
|
| QUOTE Thanks | ||
| Post Reply | New Thread | Subscribe |
| Thread Tools | |
|
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
|
|
|
|
|||||||
Thread |
Thread Starter |
Forum |
Replies |
Last Post |
| Problem with preview of .Nef file in Bridge | Pierpi | Images, Graphic Design, and Digital Photography | 5 | 03-29-2011 08:59 AM |
| iTunes problem with network songs | jmaurin | OS X - Apps and Games | 0 | 01-02-2011 07:06 PM |
| Airport Utility accessing problem to TC | Ippo 2001 | Internet, Networking, and Wireless | 4 | 01-02-2011 11:03 AM |
| Final Cut Pro 7 keyboard shortcuts problem | zevimetal | OS X - Apps and Games | 0 | 11-27-2009 05:36 AM |
| Problem with launching x11 applications | kousik | OS X - Development and Darwin | 8 | 11-19-2009 12:42 AM |
All times are GMT -4. The time now is 01:42 AM.
Powered by vBulletin