-
Notifications
You must be signed in to change notification settings - Fork 8
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Implemented date
#38
Draft
Eike-Flath
wants to merge
8
commits into
proh14:main
Choose a base branch
from
Eike-Flath:date
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Implemented date
#38
Changes from 3 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
40e77ab
Implemented date
Eike-Flath 2ceb359
prevented date from parsing invalid dates
Eike-Flath 8b14fa5
use clock_settime (which is specified by POSIX) instead of settimeofday
Eike-Flath 42370e1
added malloc/realloc call checks
Eike-Flath 0a5be13
Merge branch 'proh14:main' into date
Eike-Flath 0ca95d7
implemented --help and started on --iso-8601
Eike-Flath bdaa0a3
Added %N, %:z, --iso-8601 is fully implemented
Eike-Flath fea3e1b
Added RFC options
Eike-Flath File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,119 @@ | ||
#define _POSIX_C_SOURCE 200112L // for setenv | ||
|
||
#include <ctype.h> | ||
#include <errno.h> | ||
#include <stdbool.h> | ||
#include <stdio.h> | ||
#include <stdlib.h> | ||
#include <string.h> | ||
#include <time.h> | ||
|
||
#define NAME "date (canoutils)" | ||
#define VERSION "1.0.0" | ||
#define AUTHOR "Eike Flath" | ||
|
||
#include "version_info.h" | ||
|
||
#define INITIAL_BUF_SIZE 512 | ||
|
||
static struct tm *get_time(void) { | ||
time_t t; | ||
if (time(&t) == -1) { | ||
fprintf(stderr, "date: failed to retrieve time: %s\n", strerror(errno)); | ||
exit(EXIT_FAILURE); | ||
} | ||
return localtime(&t); | ||
} | ||
|
||
static int set_time(char *str) { | ||
size_t n; | ||
for (n = 0; str[n]; n++) { | ||
if (!isdigit(str[n])) { | ||
fprintf(stderr, "date: invalid date '%s'\n", str); | ||
return EXIT_FAILURE; | ||
} | ||
} | ||
|
||
#define s(i) (str[i] - '0') | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. For readability purposes, you may want to avoid doing define within functions |
||
struct tm tm; | ||
memcpy(&tm, get_time(), sizeof tm); | ||
bool century_specified = false; | ||
switch (n) { | ||
case 12: | ||
tm.tm_year = 100 * (10 * s(n - 4) + s(n - 3)); | ||
century_specified = true; | ||
// fallthrough | ||
case 10: | ||
if (century_specified) { | ||
tm.tm_year += 10 * s(n - 2) + s(n - 1); | ||
} else { | ||
int yy = 10 * s(n - 2) + s(n - 1); | ||
int cc = yy >= 69 ? 19 : 20; | ||
tm.tm_year = 100 * cc + yy; | ||
} | ||
tm.tm_year -= 1900; | ||
// fallthrough | ||
case 8: | ||
tm.tm_mon = 10 * s(0) + s(1) - 1; // tm.tm_mon is in [0,11] | ||
tm.tm_mday = 10 * s(2) + s(3); | ||
tm.tm_hour = 10 * s(4) + s(5); | ||
tm.tm_min = 10 * s(6) + s(7); | ||
tm.tm_isdst = -1; | ||
break; | ||
default: | ||
fprintf(stderr, "date: invalid date '%s'\n", str); | ||
return EXIT_FAILURE; | ||
} | ||
#undef s | ||
time_t t = mktime(&tm); | ||
if (t == -1) { | ||
fprintf(stderr, "date: '%s' does not specify a valid time\n", str); | ||
return EXIT_FAILURE; | ||
} | ||
struct timespec ts = {.tv_sec = t}; | ||
if (clock_settime(CLOCK_REALTIME, &ts)) { | ||
fprintf(stderr, "date: cannot set date: %s\n", strerror(errno)); | ||
return EXIT_FAILURE; | ||
} | ||
return EXIT_SUCCESS; | ||
} | ||
|
||
int main(int argc, char **argv) { | ||
char *operand = "+%a %b %e %H:%M:%S %Z %Y"; | ||
bool parse_options = true; | ||
for (int i = 1; i < argc; i++) { | ||
if (parse_options) { | ||
if (!strcmp(argv[i], "--")) { | ||
parse_options = false; | ||
continue; | ||
} | ||
if (!strcmp(argv[i], "--version")) { | ||
print_version(); | ||
return EXIT_SUCCESS; | ||
} | ||
if (!strcmp(argv[i], "-u")) { | ||
if (setenv("TZ", "UTC", 1) == -1) { | ||
fprintf(stderr, "date: cannot setenv TZ: %s\n", strerror(errno)); | ||
return EXIT_FAILURE; | ||
} | ||
continue; | ||
} | ||
} | ||
operand = argv[i]; | ||
} | ||
|
||
if (operand[0] != '+') | ||
return set_time(operand); | ||
|
||
struct tm *tm = get_time(); | ||
|
||
int main(void) { | ||
printf("Hello, World!\n"); | ||
return 0; | ||
} | ||
size_t cap = INITIAL_BUF_SIZE; | ||
char *buf = malloc(cap); | ||
Eike-Flath marked this conversation as resolved.
Show resolved
Hide resolved
|
||
while (!strftime(buf, cap, &operand[1], tm)) { | ||
cap *= 2; | ||
buf = realloc(buf, cap); | ||
} | ||
printf("%s\n", buf); | ||
free(buf); | ||
return EXIT_SUCCESS; | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You will need to create your own
setenv
. Fear not, it is simpler than it seems.Take a look at glibc implementaion or muscl one