wc: Added wc
[mutos-utils.git] / date.c
blobadb68d7799a8312af09e8b6138211b0f67c72b5e
1 /*
2 Copyright © 2013 Alastair Stuart
4 This program is open source software: you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation, either version 3 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
15 #include <stdio.h>
16 #include <time.h>
17 #include <stdbool.h>
18 #include <string.h>
20 #define VERSION "0.01"
22 struct {
23 bool iso;
24 bool utc;
25 } flags;
27 char string[128+1] = {'\0'};
28 char* format = "%a %b %d %X %Z %Y";
30 int main(int argc, char* argv[])
32 flags.iso = false;
33 flags.utc = false;
35 // flag parsing
36 int arg = 0;
37 for (arg = 1; arg < argc; arg++)
39 if (strcmp("--iso", argv[arg]) == 0 || strcmp("-I", argv[arg]) == 0) {
40 flags.iso = true;
41 } else if (strcmp("--utc", argv[arg]) == 0 || strcmp("-u", argv[arg]) == 0) {
42 flags.utc = true;
43 } else if (strcmp("--help", argv[arg]) == 0 || strcmp("-h", argv[arg]) == 0) {
44 printf("Usage: %s [options ...] [+format]\n", argv[0]);
45 printf("\n"
46 " -I, --iso Print time in ISO 8601 format.\n"
47 " -u, --utc Print time in UTC instead of localtime.\n"
48 "\n"
49 " -h, --help Print this message.\n"
50 " -v, --version Show version info.\n");
51 return 0;
52 } else if (strcmp("--version", argv[arg]) == 0 || strcmp("-v", argv[arg]) == 0) {
53 printf("date (mutos) v"VERSION"\n");
54 return 0;
55 } else {
56 // no more flags left
57 break;
61 if ((argc - arg) > 1) {
62 fprintf(stderr, "%s: too many arguments\n",
63 argv[0]);
64 return 1;
65 } else if ((argc - arg) == 1) {
66 if (argv[arg][0] != '+') {
67 fprintf(stderr, "%s: invalid format\n",
68 argv[0]);
69 return 1;
70 } else if (flags.iso){
71 fprintf(stderr, "%s: multiple formats given\n",
72 argv[0]);
73 return 1;
74 } else {
75 format = argv[arg] + 1;
79 time_t curr_time = time(NULL);
80 struct tm *now = NULL;
81 if (flags.utc) {
82 now = gmtime(&curr_time);
83 } else {
84 now = localtime(&curr_time);
87 if (flags.iso) {
88 format = "%Y-%m-%dT%X%z";
91 strftime(string, 128, format, now);
93 printf("%s\n", string);
95 return 0;