Skip to content

Instantly share code, notes, and snippets.

@mundry
Last active August 29, 2015 14:01
Show Gist options
  • Save mundry/b80a396e38d7a2322311 to your computer and use it in GitHub Desktop.
Save mundry/b80a396e38d7a2322311 to your computer and use it in GitHub Desktop.
C program to print the current local time to standard output.
#include <stdio.h>
#include <string.h>
#include <time.h>
#define TIME_STRING_LENGTH 23
int main(void) {
time_t currentTime;
char currTimeStr[TIME_STRING_LENGTH];
struct tm* tmInfo;
/* Obtain current time as seconds elapsed since the Epoch. */
time(&currentTime);
tmInfo = localtime(&currentTime);
strftime(currTimeStr, TIME_STRING_LENGTH, "[%Y-%m-%d %H:%M:%S] ", tmInfo);
printf("%s\n", currTimeStr);
printf("%ld\n", strlen(currTimeStr));
return 0;
}
TARGET := local-time
$(TARGET): local-time.c
gcc -Werror -Wall -O2 -o $@ $<
run: $(TARGET)
@./$(TARGET)
clean:
$(RM) $(TARGET)
.PHONY: run
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment