-
Notifications
You must be signed in to change notification settings - Fork 0
/
date.c
56 lines (42 loc) · 1.03 KB
/
date.c
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
46
47
48
49
50
51
52
53
54
55
56
#include <stdio.h>
#include <dirent.h>
#include <string.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <time.h>
#include <stdlib.h>
#define sz 100
int main(int argc, char **argv)
{
char *flag = argv[1], *args = argv[2];
if(strlen(args) != 0){
printf("%s\n", "Error: Date command has no arguments!");
return 0;
}
if(strlen(flag) == 0){
time_t t = time(NULL);
struct tm *tm = localtime(&t);
char *s = (char*)malloc(sz * sizeof(char));
strftime(s, sz, "%A, %b %d, %Y, %T %Z", tm);
printf("%s\n\n", s);
}
else if(strlen(flag) == 1){
if(flag[0] == 'g'){
time_t t = time(NULL);
struct tm *tm = gmtime(&t);
char *s = (char*)malloc(sz * sizeof(char));
strftime(s, sz, "%A, %b %d, %Y, %T %Z", tm);
printf("%s\n\n", s);
}
else if(flag[0] == 'u'){
time_t t = time(NULL);
struct tm *tm = gmtime(&t);
char *s = (char*)malloc(sz * sizeof(char));
strftime(s, sz, "%A, %b %d, %Y, %T", tm);
printf("%s UTC\n", s);
}
else
printf("Invalid flag!\n\n");
}
return 0;
}