![]() |
| | #1 |
| Registered User Join Date: Mar 2008
Posts: 24
| Question about Function: int gettimeofday Many Thanks |
| sweetorangepie is offline | |
| | #2 |
| Kernel hacker Join Date: Jul 2007 Location: Farncombe, Surrey, England
Posts: 15,686
| Something like this? Code: #include <sys/time.h>
#include <stdio.h>
int main()
{
struct timeval t;
if (gettimeofday(&t, NULL) != 0)
perror("gettimeofday");
printf("Number of seconds since Jan 1 1970: %ld\n", (long)t.tv_sec);
return 0;
}
__________________ Compilers can produce warnings - make the compiler programmers happy: Use them! Please don't PM me for help - and no, I don't do help over instant messengers. |
| matsp is offline | |
| | #3 |
| C++ Witch Join Date: Oct 2003 Location: Singapore
Posts: 11,338
| gettimeofday is not a standard C function, but something *nix based, so I am moving this thread to the closest forum you can get to *nix here.
__________________ C + C++ Compiler: MinGW port of GCC Build + Version Control System: SCons + Bazaar Look up a C/C++ Reference and learn How To Ask Questions The Smart Way |
| laserlight is offline | |
| | #4 |
| Registered User Join Date: Mar 2008
Posts: 24
| matsp i've just tried what you suggested and it compiles but nothing happens? I'm not sure if what you suggested will do what I need, what I need is something that will get the current system date and time. is gettimeofday the right function to use or is there something else/better? Thanks for the help, much appreciated |
| sweetorangepie is offline | |
| | #5 |
| Registered User Join Date: Oct 2006
Posts: 298
| try this: Code: #include <glib.h>
#include <time.h>
#include <stdio.h>
#include <stdlib.h>
int GetTimeAsString(char* str, size_t str_len)
{
time_t t;
char* time_str;
tm *my_tm;
t = time(NULL);
my_tm = localtime(&t);
time_str = g_strdup_printf("%d/%02d/%02d %02d:%02d:%02d", my_tm->tm_year + 1900,
my_tm->tm_mon + 1,
my_tm->tm_mday,
my_tm->tm_hour,
my_tm->tm_min,
my_tm->tm_sec);
if (strlen(time_str) > (len - 1)) return -1;
else
{
memset(str, 0, strlen(time_str) + 1);
strcpy(str, time_str);
return 0;
}
}
|
| Elkvis is offline | |
| | #6 |
| CSharpener Join Date: Oct 2006
Posts: 5,325
| and what about freeing the time_str string? or better snprintf directly into the provided buffer to avoid unneeded copy...
__________________ If I have eight hours for cutting wood, I spend six sharpening my axe. |
| vart is offline | |
| | #7 |
| Registered User Join Date: Oct 2006
Posts: 298
| certainly something that would be necessary in production code, but for the purposes of illustration, not a deal breaker. I prefer to err on the side of caution in the case of filling a user-provided buffer with data. if there isn't enough space in which to put the string, it will return an error (-1). and the program can try again with a bigger buffer. obviously it's up to the person implementing this in his/her code to decide which way is better for him/her. my goal was to simply offer one possible solution to the problem. |
| Elkvis is offline | |
| | #8 | |
| Kernel hacker Join Date: Jul 2007 Location: Farncombe, Surrey, England
Posts: 15,686
| Quote:
I'm not sure what you expect to happen, and what you see. You should get a printout from my code - it may not "do" much, but you should get the text string and a large-ish number after it. What is the right function for your code: I can't tell you that. gettimeofday will give you the current time (and the timezone if you supply the second parameter). But there are other time functions, such as localtime() and there are functions to format the time into text, which I'm sure you'll need. (but you should be able to use for example strftime with the time_t tv_sec value that comes out of gettimeofday() too) Have a look at: http://www.hmug.org/man/3/time.php http://www.hmug.org/man/3/ctime.php http://www.hmug.org/man/3/strftime.php These functions have the added advantage that they will work on most systems, whether they are Unix/Linux ones, or for example Windows. On the other hand, gettimeofday() gives you a more precise time with it's microsecond value tv_usecs. If you need very precise time-stamps in your log, then that's the key. -- Mats
__________________ Compilers can produce warnings - make the compiler programmers happy: Use them! Please don't PM me for help - and no, I don't do help over instant messengers. | |
| matsp is offline | |
![]() |
| Thread Tools | |
| Display Modes | |
|
Similar Threads | ||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Compiling sample DarkGDK Program | Phyxashun | Game Programming | 6 | 01-27-2009 03:07 AM |
| getting a headache | sreetvert83 | C++ Programming | 41 | 09-30-2005 05:20 AM |
| Binary Search Trees Part III | Prelude | A Brief History of Cprogramming.com | 16 | 10-02-2004 03:00 PM |
| How do you search & sort an array? | sketchit | C Programming | 30 | 11-03-2001 05:26 PM |
| Hi, could someone help me with arrays? | goodn | C Programming | 20 | 10-18-2001 09:48 AM |