Dave's Brain

Browse - Programming Tips - How can I quickly make a nice timestamp string?

Date: 2010jan19
Language: C/C++
Level: beginner
OS: Linux

Q.  How can I quickly make a nice timestamp string?

A.  Here's a function that does that:

#include <time.h> // eg "Wed Jun 30 21:49:08 1993" static void timeStamp(char *szTime, const size_t size) { time_t now; szTime[0] = '\0'; if (size < 25) return; now = time(NULL); ctime_r(&now, szTime); szTime[24] = '\0'; // Zap the newline } void exampleUse() { char szTime[100]; timeStamp(szTime, sizeof(szTime)); printf("It is now %s\n", szTime); }

Add a comment

Sign in to add a comment
Copyright © 2008-2017, dave - Code samples on Dave's Brain is licensed under the Creative Commons Attribution 2.5 License. However other material, including English text has all rights reserved.