OS X - Development and Darwin Discussion and questions about development for Mac OS X.

Problem with times() function


Post Reply New Thread Subscribe

 
Thread Tools
ankur17

 
Member Since: Mar 29, 2011
Posts: 7
ankur17 is on a distinguished road

ankur17 is offline
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
xstep

 
xstep's Avatar
 
Member Since: Jun 25, 2005
Location: On the road
Posts: 3,231
xstep is a name known to allxstep is a name known to allxstep is a name known to allxstep is a name known to allxstep is a name known to allxstep is a name known to allxstep is a name known to all
Mac Specs: 2011 MBP, i7, 16GB RAM, MBP 2.16Ghz Core Duo, 2GB ram, Dual 867Mhz MDD, 1.75GB ram, ATI 9800 Pro vid

xstep is offline
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
Dysfunction

 
Dysfunction's Avatar
 
Member Since: Mar 17, 2008
Location: Tucson, AZ
Posts: 6,533
Dysfunction has a brilliant futureDysfunction has a brilliant futureDysfunction has a brilliant futureDysfunction has a brilliant futureDysfunction has a brilliant futureDysfunction has a brilliant futureDysfunction has a brilliant futureDysfunction has a brilliant futureDysfunction has a brilliant futureDysfunction has a brilliant futureDysfunction has a brilliant future
Mac Specs: 2008 and 2011 15" mbps, late 11 iMac, iPhone 4s, and too many ipods and other stuff

Dysfunction is online now
From the sysconf man page on another Unix box..

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
ankur17

 
Member Since: Mar 29, 2011
Posts: 7
ankur17 is on a distinguished road

ankur17 is offline
Any idea about how to use value which times() returns?
it dsnt seems to be epoch time..ryt?


Thanks..
QUOTE Thanks
Dysfunction

 
Dysfunction's Avatar
 
Member Since: Mar 17, 2008
Location: Tucson, AZ
Posts: 6,533
Dysfunction has a brilliant futureDysfunction has a brilliant futureDysfunction has a brilliant futureDysfunction has a brilliant futureDysfunction has a brilliant futureDysfunction has a brilliant futureDysfunction has a brilliant futureDysfunction has a brilliant futureDysfunction has a brilliant futureDysfunction has a brilliant futureDysfunction has a brilliant future
Mac Specs: 2008 and 2011 15" mbps, late 11 iMac, iPhone 4s, and too many ipods and other stuff

Dysfunction is online now
From the man 3 page of times...

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
ankur17

 
Member Since: Mar 29, 2011
Posts: 7
ankur17 is on a distinguished road

ankur17 is offline
You mean to say that if we will divide the value returned by times() by sysconf(_SC_CLK_TCK) then we will get epoch time in seconds?
Please correct me if I am wrong..


Thanks
QUOTE Thanks
Dysfunction

 
Dysfunction's Avatar
 
Member Since: Mar 17, 2008
Location: Tucson, AZ
Posts: 6,533
Dysfunction has a brilliant futureDysfunction has a brilliant futureDysfunction has a brilliant futureDysfunction has a brilliant futureDysfunction has a brilliant futureDysfunction has a brilliant futureDysfunction has a brilliant futureDysfunction has a brilliant futureDysfunction has a brilliant futureDysfunction has a brilliant futureDysfunction has a brilliant future
Mac Specs: 2008 and 2011 15" mbps, late 11 iMac, iPhone 4s, and too many ipods and other stuff

Dysfunction is online now
Quote:
Originally Posted by ankur17 View Post
You mean to say that if we will divide the value returned by times() by sysconf(_SC_CLK_TCK) then we will get epoch time in seconds?
Please correct me if I am wrong..


Thanks
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
ankur17

 
Member Since: Mar 29, 2011
Posts: 7
ankur17 is on a distinguished road

ankur17 is offline
yeah thats true. But I was concerned about what times() returns and how the return value should be utilized, because we have some legacy code here in which times() was used which is creating this situation in OSX.
QUOTE Thanks
xstep

 
xstep's Avatar
 
Member Since: Jun 25, 2005
Location: On the road
Posts: 3,231
xstep is a name known to allxstep is a name known to allxstep is a name known to allxstep is a name known to allxstep is a name known to allxstep is a name known to allxstep is a name known to all
Mac Specs: 2011 MBP, i7, 16GB RAM, MBP 2.16Ghz Core Duo, 2GB ram, Dual 867Mhz MDD, 1.75GB ram, ATI 9800 Pro vid

xstep is offline
Quote:
Originally Posted by ankur17 View Post
You mean to say that if we will divide the value returned by times() by sysconf(_SC_CLK_TCK) then we will get epoch time in seconds?
Please correct me if I am wrong..


Thanks
You'd think from the descriptions that would be true. I tried it and it didn't work. Not unexpected given what the value of CLK_TCK is.

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
ankur17

 
Member Since: Mar 29, 2011
Posts: 7
ankur17 is on a distinguished road

ankur17 is offline
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
xstep

 
xstep's Avatar
 
Member Since: Jun 25, 2005
Location: On the road
Posts: 3,231
xstep is a name known to allxstep is a name known to allxstep is a name known to allxstep is a name known to allxstep is a name known to allxstep is a name known to allxstep is a name known to all
Mac Specs: 2011 MBP, i7, 16GB RAM, MBP 2.16Ghz Core Duo, 2GB ram, Dual 867Mhz MDD, 1.75GB ram, ATI 9800 Pro vid

xstep is offline
Quote:
Originally Posted by ankur17 View Post
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.
Yep. That seems to work.


Quote:
Originally Posted by ankur17 View Post
But anyways output of times() dsnt seems to make any sense, isnt it?
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
ankur17

 
Member Since: Mar 29, 2011
Posts: 7
ankur17 is on a distinguished road

ankur17 is offline
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
xstep

 
xstep's Avatar
 
Member Since: Jun 25, 2005
Location: On the road
Posts: 3,231
xstep is a name known to allxstep is a name known to allxstep is a name known to allxstep is a name known to allxstep is a name known to allxstep is a name known to allxstep is a name known to all
Mac Specs: 2011 MBP, i7, 16GB RAM, MBP 2.16Ghz Core Duo, 2GB ram, Dual 867Mhz MDD, 1.75GB ram, ATI 9800 Pro vid

xstep is offline
Quote:
Originally Posted by ankur17 View Post
Do we have any API in mac which returns actual CPU ticks elapsed since an arbitrary point in past say epoch..??
I don't get why you'd want to use this value which seems to have variable influences on the value; CPU, OS, possibly the precise machine.

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


« installing g++-2.95 on snow leopard | Developing Mac Applications »
Thread Tools

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off
Forum Jump

Similar Threads
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
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
X

Welcome to Mac-Forums.com

Create your username to jump into the discussion!

New members like you have made this community the ultimate source for your Mac since 2003!


(4 digit year)

Already a member?