# Help needed regarding c program

**URL:** <https://community.unix.com/t/help-needed-regarding-c-program/177872>\
**Category:** Programming\
**Created:** [November 19, 2007, 3:57am UTC](https://community.unix.com/t/help-needed-regarding-c-program/177872 "2007-11-19T03:57:22Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![dwgi32](https://community.unix.com/letter_avatar/dwgi32/32/6_22734026db12803ebb3e059622ac3534.png) [@dwgi32](https://community.unix.com/u/dwgi32)\
**Post date:** [November 19, 2007, 3:57am UTC](https://community.unix.com/t/help-needed-regarding-c-program/177872/1 "2007-11-19T03:57:22Z")

</div>

Hi,

Currently, i have an application that does logging of messages into a text file and i record the timing for the messages in a [hh:mm:ss] format. However, i need to log the messages up to millisec level and the struct tm i am using now only support up to sec, is there any other way to get millisec?

Cheers

---

<div class="post-metadata">

**Author:** ![fpmurphy](https://community.unix.com/user_avatar/community.unix.com/fpmurphy/32/110_2.png) [@fpmurphy](https://community.unix.com/u/fpmurphy)\
**Post date:** [November 19, 2007, 6:43am UTC](https://community.unix.com/t/help-needed-regarding-c-program/177872/2 "2007-11-19T06:43:41Z")

</div>

You can use the gettimeofday() and the timeval structure to achieve what you want.

#include \<stdio.h\>  
#include \<time.h\>  
#include \<sys/time.h\>

int  
main(void)  
{  
char buf[30];  
struct timeval tv;  
time\_t ct;  
int ms;

```
gettimeofday\(&tv, NULL\);
ct = tv.tv_sec;
strftime\(buf, 30, "%m/%d/%y %T.", localtime\(&ct\)\);
ms = \(tv.tv_usec % 1000000\) / 1000;

printf\("Timestamp: %s%d\\n", buf, ms\);

```

}

---

<div class="post-metadata">

**Author:** ![dwgi32](https://community.unix.com/letter_avatar/dwgi32/32/6_22734026db12803ebb3e059622ac3534.png) [@dwgi32](https://community.unix.com/u/dwgi32)\
**Post date:** [November 19, 2007, 9:44am UTC](https://community.unix.com/t/help-needed-regarding-c-program/177872/3 "2007-11-19T09:44:44Z")

</div>

Hi fpmurphy,

It works thanks a lot for the help.

Cheers  
dwgi32
