-
Notifications
You must be signed in to change notification settings - Fork 0
/
date.c
58 lines (56 loc) · 1.26 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
57
58
#include<stdio.h>
#include<time.h>
#include<string.h>
//1 argument--simple date
//2 argument-- u date
//3 argument-- local time
int main(int argc,char* argv[]){
char* t=strtok(argv[0]," ");//date
t=strtok(NULL," "); //command
if(t==NULL){
time_t t=time(NULL);
time(&t);
printf("%s\n",ctime(&t));
}
else if(!strcmp(t,"-u")){
time_t t=time(NULL);
if(t==-1){
perror("Error is");
}
else{
struct tm *pt;
char b1[4],b2[4],b3[4];
pt=gmtime(&t);
if(pt==NULL){
perror("Error is");
}
else{
strftime(b1,4,"%a",pt);
strftime(b2,4,"%b",pt);
strftime(b3,4,"%z",pt);
printf("%s %s %02d:%02d:%02d %d\n",b1,b2,pt->tm_hour,pt->tm_min,pt->tm_sec,pt->tm_year+1900);
}
}
}
else if(!strcmp(t,"-r")){
time_t t=time(NULL);
if(t==-1){
perror("Error is");
}
else{
struct tm *pt;
char b1[4],b2[4],b3[6];
pt=localtime(&t);
if(pt==NULL){
perror("Error is");
}
else{
strftime(b1,4,"%a",pt);
strftime(b2,4,"%b",pt);
strftime(b3,6,"%z",pt);
printf("%s, %02d %s %d %02d:%02d:%02d %s\n",b1,pt->tm_mday,b2,pt->tm_year+1900,pt->tm_hour,pt->tm_min,pt->tm_sec,b3);
}
}
}
return 0;
}