Funny and strange UNIX happenings and C programming tips.

Posted: May 16, 2017. At: 6:06 PM. This was 5 months ago. Post ID: 3823

Funny and cool Linux tips

1337 or Leet in the UNIX time.

-01:44:57-- gordon@deusexmachina [~]$ date +%s
1337096699

The missing days in 1752. I have mentioned this before, but it is worth mentioning again.

-01:45:31-- gordon@deusexmachina [~]$ cal 9 1752
   September 1752     
Su Mo Tu We Th Fr Sa  
       1  2 14 15 16  
17 18 19 20 21 22 23  
24 25 26 27 28 29 30

A couple of visitors to my website wanted to know about the time() function in time.h, so here is a simple C program for printing a verbose time and date.

#include <time.h>	/* For time function (random seed). */
#include <stdio.h>      /* For extra functions. printf().   */
#include <stdlib.h>     /* For getenv();                    */
 
#define format "The time and date is: %A %d %B %Y. The time is: %H:%M:%S, %Z."
 
int print_time(void) {
	struct tm *ptr;
	time_t tm;
	char length[60];
	tm = time(NULL);
	ptr = localtime(&tm);
	strftime(length, 100, format, ptr);
	printf("%s\n", length);
}
 
int main(void)
{
	print_time();
	return 0;
}

And another version with colored output for the Linux terminal.

#include <time.h>
#include <stdio.h>
#include <stdlib.h>
 
#define format "The time and date is: %A %d %B %Y. The time is: %H:%M:%S, %Z."
 
int main(void) {
 
	struct tm *ptr;
	time_t tm;
	char length[60];
	char *logname;
	tm = time(NULL);
	ptr = localtime(&tm); // UNIX Epoch.
	strftime(length, 100, format, ptr);
	logname = getenv("LOGNAME");
 
/*
 *  Formatting the strftime string.
 */
 
	printf("\E[1;33m--Welcome %s.\E[0m\n", logname);
	printf("\E[1;32m--%s\E[0m\n", length);
	printf("--There have been %i seconds since Epoch.\n", tm);
	return 0;
 
}

No comments have been made. Use this form to start the conversation :)

Leave a Reply