k9core

Coreutils for *nix operating systems [fork]
git clone git://bvnf.space/k9core.git
Log | Files | Refs | LICENSE

date.c (701B)


      1
      2
      3
      4
      5
      6
      7
      8
      9
     10
     11
     12
     13
     14
     15
     16
     17
     18
     19
     20
     21
     22
     23
     24
     25
     26
     27
     28
     29
     30
     31
     32
     33
     34
     35
     36
     37
     38
     39
     40
     41
     42
     43
     44
     45
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <unistd.h>

int
main(int argc, char *argv[])
{
	int c;
	int u = 0;
	char buffer[256];
	time_t now;
	now = time(NULL);
	char FORMAT[256] = "%c";
	struct tm *timeinfo;

	while((c = getopt(argc, argv, "u")) != -1) {
		switch(c) {
			case 'u':
				u = 1;
				break;
		}
	}

	timeinfo = localtime(&now);

	if(u)
		timeinfo = gmtime(&now);

	if(timeinfo == NULL) {
		fprintf(stderr, "date: %s\n", strerror(errno));
		return 1;
	}

	if(argc > optind && argv[optind][0] == '+') {
		argv[optind]++;
		strcpy(FORMAT, argv[optind]);
	}

	strftime(buffer, 256, FORMAT, timeinfo);
	puts(buffer);
	return 0;
}