Time Display
A C function that formats the current local time as HH:MM:SS using time, localtime, and strftime.
The portable C way to get a formatted clock string. time gives you the seconds since the epoch, localtime breaks it into a struct tm in the machine's time zone, and strftime renders whatever layout you ask for - here %H:%M:%S. The result is copied into one slot of the familiar 64-deep static buffer ring so repeated calls stay safe, matching the pattern used throughout the older NetworkDLS code. Handy in log lines, status bars, or any place you want a human-readable timestamp without pulling in the Win32 date APIs.
#include <stdio.h>
#include <time.h>
static int StrCnt;
static char StrFunction [64][257];
extern char *timef (int i);
int main()
{
printf("%s\n",timef(0));
return 0;
}
extern char *timef (int i)
{
static struct tm *tp;
long elapse_time;
if (++StrCnt == 64) StrCnt = 0;
time(&elapse_time);
tp=localtime(&elapse_time);
strftime(StrFunction[StrCnt],256,"%H:%M:%S",tp);
return StrFunction[StrCnt];
}
Swap the format string for anything strftime understands - %d/%m/%Y %H:%M:%S for a full timestamp, %A for the weekday name, and so on. For UTC instead of local time, use gmtime in place of localtime.