-
Notifications
You must be signed in to change notification settings - Fork 25
birthday/date implementation #27
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
Open
aryianj
wants to merge
7
commits into
USF-OS:main
Choose a base branch
from
aryianj:main
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.
Open
Changes from 4 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
4ea9964
birthday/time/date
aryianj 5d1c299
add help guide
aryianj 0fb2d44
fixing bug
aryianj b3e9eab
Update birthday-date.md
aryianj dd57a24
CR changes
aryianj 9c6929b
added testing
aryianj a893b10
update .md
aryianj 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 hidden or 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
This file contains hidden or 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 |
|---|---|---|
| @@ -0,0 +1,31 @@ | ||
| ---- FogOSv2 Birthday, Date, Time Help Guide ---- | ||
| -- Running -- | ||
| Run make, make qemu. | ||
|
|
||
| -- Makefile -- | ||
| Add 'birthday' and 'date' to UPROGS. | ||
| Build 'birthday' and 'date' with 'time' object. | ||
|
|
||
| -- Kernel Activity -- | ||
| Clock is a necessary system call. | ||
|
|
||
| -- Birthday -- | ||
| birthday | birthday with no flags. If the birthday is set, then it will tell the user "happy | ||
| happy", otherwise it will tell the user not yet! | ||
| birthday '-s' [MM/DD] | birthday with -s flag will set the birthday. Must be in MM/DD format | ||
| where both are integers. | ||
|
|
||
| -- Date -- | ||
| date | only command for date. This will return the date in format: MMM DD HH:MM:SS PDT YYYY | ||
| Note: MMM is a string. The rest are integers besides PDT. | ||
|
|
||
| -- Time -- | ||
| time.c and time.h are required for date and time to work. Time is not a usable command. | ||
|
|
||
| -- Testing -- | ||
| To test, try: | ||
| 1. Calling 'birthday' before it is set. | ||
| 2. Setting 'birthday' with parameters M/DD. | ||
| 3. Setting 'birthday' with an impossible date. | ||
| 4. Setting 'birthday' with today's date and calling it. | ||
| 5. Setting 'birthday' with your birthday and calling it. |
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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 |
|---|---|---|
|
|
@@ -20,3 +20,4 @@ | |
| #define SYS_link 19 | ||
| #define SYS_mkdir 20 | ||
| #define SYS_close 21 | ||
| #define SYS_clock 22 | ||
This file contains hidden or 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
This file contains hidden or 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
Binary file not shown.
|
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. Suggestion: could handle wrong format input |
This file contains hidden or 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 |
|---|---|---|
| @@ -0,0 +1,133 @@ | ||
| #include "kernel/types.h" | ||
| #include "kernel/stat.h" | ||
| #include "user/user.h" | ||
| #include "kernel/fcntl.h" | ||
| #include "kernel/param.h" | ||
| #include "time.h" | ||
|
|
||
| static const int days_per_month[] = { | ||
| 31, 28, 31, 30, 31, 30, | ||
| 31, 31, 30, 31, 30, 31 | ||
| }; | ||
|
|
||
| int validate_birthdate(char buf[]) { | ||
| if (strlen(buf) != 5) { // not MM/DD | ||
| return 0; | ||
| } | ||
|
|
||
| int i = 0, month = 0, day = 0; | ||
| while (buf[i] != '\0') { | ||
| if ((buf[i] == '/') ||(buf[i] >= '1' && buf[i] <= '9')) { | ||
| if (i < 2) { | ||
| month = month * 10 + (buf[i] - '0'); | ||
| } else if (i > 2) { | ||
| day = day * 10 + (buf[i] - '0'); | ||
| } | ||
| } | ||
| ++i; | ||
| } | ||
|
|
||
| if ((month < 1) || (month > 12) || (day > days_per_month[month-1] && month != 2 && day != 29) || (day < 1)) { | ||
| return 0; | ||
| } | ||
|
|
||
| return 1; | ||
| } | ||
|
|
||
| void print_usage() { | ||
| printf("Usage:\n"); | ||
| printf("birthday # if set, check if it's your birthday\n"); | ||
| printf("birthday -s MM/DD # (re)set birthday\n"); | ||
| } | ||
|
|
||
| int main(int argc, char *argv[]) { | ||
| /* | ||
| birthday command in use | ||
| $birthday - No arguments, if birthday is set, return predefined | ||
| statements. else, ask to set birthday. | ||
| $birthday -s [MM/DD] - Set birthday | ||
| */ | ||
|
|
||
| struct stat st; // check if birthday file exists | ||
| int fd; // for open, write, close | ||
| char buf[MAXPATH]; // for write | ||
|
|
||
| int month = 0, day = 0, i = 0; // for comparisons | ||
|
|
||
| int exists = (stat("birthday.txt", &st) == 0); | ||
|
|
||
| // proper length and checking flag | ||
| if ((argc == 3) && (argv[1][0] == '-' && argv[1][1] == 's')) { | ||
| if (!validate_birthdate(argv[2])) { | ||
| print_usage(); | ||
| return -1; | ||
| } | ||
|
|
||
| fd = open("birthday.txt", O_CREATE | O_RDWR | O_TRUNC); | ||
| if (fd < 0) { // error | ||
| return -1; | ||
| } | ||
| write(fd, argv[2], strlen(argv[2])); | ||
| close(fd); | ||
| return 0; | ||
| } | ||
| if (argc == 1 && exists) { | ||
| fd = open("birthday.txt", O_RDWR); | ||
| if (fd < 0) { // error | ||
| return -1; | ||
| } | ||
| int n = read(fd, buf, sizeof(buf) - 1); | ||
|
|
||
| if(n < 0){ | ||
| close(fd); | ||
| return -1; | ||
| } | ||
| buf[n] = '\0'; | ||
|
|
||
| while (buf[i] != '\0') { | ||
| if (i < 2) { | ||
| month = month * 10 + (buf[i] - '0'); | ||
| } else if (i > 2) { | ||
| day = day * 10 + (buf[i] - '0'); | ||
| } | ||
| ++i; | ||
| } | ||
|
|
||
| uint64 c = clock() / 1000000000ULL; | ||
| uint64 local_date = c - (7 * 3600); | ||
| datetime date = to_datetime(local_date); | ||
|
|
||
| if (date.month == month && date.day == day) { | ||
| printf( | ||
| " ( (\n" | ||
| " ( ) ( ) (\n" | ||
| " ( Y Y ( )\n" | ||
| " ( ) | | | | Y\n" | ||
| " Y | | | | | |\n" | ||
| " | | | |.-----| |---.___ | |\n" | ||
| " | | .--| |,~~~~~| |~~~,,,,'-| |\n" | ||
| " | |-,,~~'-'___ '-' ~~| |._\n" | ||
| " .| |~ // ___ '-',,'.\n" | ||
| " /,'-' <_// // _ __ ~,\\\n" | ||
| " / ; ,-, \\_> <<_______________;_)\n" | ||
| " | ; {(_)} _, . |================|\n" | ||
| " | '-._ ~~,,, | ,, |\n" | ||
| " | '-.__ ~~~~~~~~~~~|________________| \n" | ||
| " |\\ `'----------|\n" | ||
| " | '=._ |\n" | ||
| " : '=.__ |\n" | ||
| " \\ `'==========|\n" | ||
| "snd '-._ |\n" | ||
| " '-.__ |\n" | ||
| " ``''--------|\n" | ||
| ); | ||
| printf("Happy Birthday!\n"); | ||
| } else { | ||
| printf("Uh oh. It's not your birthday yet :(\n"); | ||
| } | ||
| return 0; | ||
| } | ||
|
|
||
| print_usage(); // otherwise, fail. | ||
| return -1; | ||
| } |
Binary file not shown.
|
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. |
This file contains hidden or 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 |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| #include "kernel/types.h" | ||
| #include "kernel/stat.h" | ||
| #include "user/user.h" | ||
| #include "time.h" | ||
|
|
||
| static const char* month_name[] = { | ||
| "Jan", "Feb", "Mar", "Apr", "May", "June", | ||
| "July", "Aug", "Sep", "Oct", "Nov", "Dec" | ||
| }; | ||
|
|
||
| int main() { | ||
| uint64 c = clock() / 1000000000ULL; | ||
| uint64 local_date = c - (7 * 3600); | ||
| datetime date = to_datetime(local_date); | ||
|
|
||
| printf("%s %d %d:%d:%d PDT %d\n", | ||
| month_name[date.month-1], date.day, | ||
| date.hour, date.minute, date.second, | ||
| date.year); | ||
| return 0; | ||
| } |
This file contains hidden or 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 |
|---|---|---|
| @@ -0,0 +1,61 @@ | ||
| #include "kernel/types.h" | ||
| #include "kernel/stat.h" | ||
| #include "user/user.h" | ||
| #include "time.h" | ||
|
|
||
| int is_leap(int year) { | ||
| return (year % 4 == 0 && (year % 100 != 0 || year % 400 == 0)); | ||
| } | ||
|
|
||
| static const int days_per_month[] = { | ||
| 31, 28, 31, 30, 31, 30, | ||
| 31, 31, 30, 31, 30, 31 | ||
| }; | ||
|
|
||
| datetime to_datetime(uint64 timestamp) { | ||
| datetime date; | ||
| // 86400 seconds in a day | ||
| uint64 secs = timestamp % 86400; // seconds passed today | ||
| uint64 days = timestamp / 86400; // number of days since Jan 1, 1970 | ||
|
|
||
| date.hour = secs / 3600; // gets the hour | ||
| if (date.hour > 12) { | ||
| date.hour -= 12; // i don't like military time. | ||
| } | ||
| date.minute = (secs % 3600) / 60; // remaining minutes | ||
| date.second = secs % 60; // remaining seconds | ||
|
|
||
| // unix time starts in 1970 | ||
| date.year = 1970; | ||
|
|
||
| while (1) { | ||
| int days_per_yr = is_leap(date.year) ? 366 : 365; | ||
| if (days >= days_per_yr) { | ||
| // subtract days until it fits within a year | ||
| days -= days_per_yr; | ||
| date.year++; | ||
| } else { | ||
| break; | ||
| } | ||
| } | ||
|
|
||
| int month = 0; | ||
| while (1) { | ||
| int m_days = days_per_month[month]; | ||
| if (month == 1 && is_leap(date.year)) { | ||
| m_days = 29; | ||
| } | ||
| if (days >= m_days) { | ||
| // subtract days until it fits in a month | ||
| days -= m_days; | ||
| month++; | ||
| } else { | ||
| break; | ||
| } | ||
| } | ||
| date.month = month + 1; // corrects month | ||
| date.day = days + 1; // corrects day | ||
|
|
||
| return date; | ||
| } | ||
|
|
This file contains hidden or 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 |
|---|---|---|
| @@ -0,0 +1,18 @@ | ||
| #ifndef TIME_H | ||
| #define TIME_H | ||
|
|
||
| #include "kernel/types.h" | ||
|
|
||
| typedef struct datetime { | ||
| int year; | ||
| int month; | ||
| int day; | ||
| int hour; | ||
| int minute; | ||
| int second; | ||
| } datetime; | ||
|
|
||
| int is_leap(int year); | ||
| datetime to_datetime(uint64 timestamp); | ||
|
|
||
| #endif |
This file contains hidden or 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
This file contains hidden or 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 |
|---|---|---|
|
|
@@ -42,3 +42,4 @@ sub entry { | |
| entry("sbrk"); | ||
| entry("pause"); | ||
| entry("uptime"); | ||
| entry("clock"); | ||
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.
I would maybe try to make the .md file more organized and easy to read