Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -48,3 +48,30 @@ target_sources(paint PRIVATE
target_link_libraries(paint PRIVATE
rp6502
)

add_executable(clock)
rp6502_executable(clock)
target_sources(clock PRIVATE
src/clock.c
)
target_link_libraries(clock PRIVATE
rp6502
)

add_executable(set-time)
rp6502_executable(set-time)
target_sources(set-time PRIVATE
src/set-time.c
)
target_link_libraries(set-time PRIVATE
rp6502
)

add_executable(print-time)
rp6502_executable(print-time)
target_sources(print-time PRIVATE
src/print-time.c
)
target_link_libraries(print-time PRIVATE
rp6502
)
86 changes: 86 additions & 0 deletions src/clock.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
#include <rp6502.h>
#include <clock.h>
#include <stdio.h>
#include <errno.h>

void test_time()
{
time_t now;
unsigned long res;
res = time(&now);
if (res == -1)
{
printf("errno: %d\n", errno);
printf("_oserror: %d\n", _oserror);
} else
{
printf("%s", ctime(&now));

}
}

void test_clock_getres()
{
struct timespec resolution;
unsigned long res;
res = clock_getres(CLOCK_REALTIME, &resolution);
if (res == -1)
{
printf("errno: %d\n", errno);
printf("_oserror: %d\n", _oserror);
} else
{
printf("Clock resolution: seconds: %lu, nanoseconds: %lu\n", resolution.tv_sec, resolution.tv_nsec);
}
}

void test_clock_gettime()
{
struct timespec now;
unsigned long res;
struct tm time_struct;
char buf[80];
res = clock_gettime(CLOCK_REALTIME, &now);
if (res == -1)
{
printf("errno: %d\n", errno);
printf("_oserror: %d\n", _oserror);
} else
{
time_struct = *localtime(&(now.tv_sec));
strftime(buf, sizeof(buf), "%a %m/%d/%Y %I:%M:%S %p", &time_struct);
printf("%s\n", buf);
}
}

void test_clock_settime()
{
struct tm current_time;
struct timespec now;
current_time.tm_hour = 5;
current_time.tm_sec = 5;
current_time.tm_min = 5;
current_time.tm_mday = 29;
current_time.tm_mon = 10;
current_time.tm_year = 123;
current_time.tm_isdst = -1;
current_time.tm_wday = 0;
current_time.tm_yday = 0;
now.tv_sec = mktime(&current_time);
now.tv_nsec = 0;
clock_settime(CLOCK_REALTIME, &now);
}

void main()
{
printf("test clock_gettime...\n");
test_clock_gettime();
printf("test clock_settime...\n");
test_clock_settime();
printf("test clock_getres...\n");
test_clock_getres();
printf("test clock_gettime...\n");
test_clock_gettime();
printf("test time...\n");
test_time();
}
23 changes: 23 additions & 0 deletions src/print-time.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#include <rp6502.h>
#include <clock.h>
#include <stdio.h>
#include <errno.h>

void main()
{
time_t now;
unsigned long res;
struct tm time_struct;
char buf[80];
res = time(&now);
if (res == -1)
{
printf("errno: %d\n", errno);
printf("_oserror: %d\n", _oserror);
} else
{
time_struct = *localtime(&now);
strftime(buf, sizeof(buf), "%a %m/%d/%Y %I:%M:%S %p", &time_struct);
printf("%s\n", buf);
}
}
72 changes: 72 additions & 0 deletions src/set-time.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
#include <rp6502.h>
#include <clock.h>
#include <stdio.h>
#include <stdlib.h>

#define IN_BUF_SIZE 8

void main()
{
struct tm current_time;
struct timespec now;
char in_buf[IN_BUF_SIZE];

current_time.tm_hour = -1;
current_time.tm_sec = -1;
current_time.tm_min = -1;
current_time.tm_mday = 0;
current_time.tm_mon = -1;
current_time.tm_year = -1;
current_time.tm_isdst = -1;
current_time.tm_wday = 0;
current_time.tm_yday = 0;


while (current_time.tm_mon < 0) {
puts("Enter month (1-12): ");
gets(in_buf);
current_time.tm_mon = atoi(in_buf) - 1;
if (current_time.tm_mon > 11)
current_time.tm_mon = -1;
}
while (!current_time.tm_mday) {
puts("Enter day (1-31): ");
gets(in_buf);
current_time.tm_mday = atoi(in_buf);
if (current_time.tm_mday > 31)
current_time.tm_mday = 0;
}
while (current_time.tm_year < 0) {
puts("Enter year: (1970-2038)");
gets(in_buf);
current_time.tm_year = atoi(in_buf) - 1900;
if (current_time.tm_year > 138 ||
current_time.tm_year < 70)
current_time.tm_year = -1;
}
puts("Local time:\n");
while (current_time.tm_hour < 0) {
puts("Enter hour (0-23): ");
gets(in_buf);
current_time.tm_hour = atoi(in_buf);
if (current_time.tm_hour > 23)
current_time.tm_hour = -1;
}
while (current_time.tm_min < 0) {
puts("Enter minute (0-59): ");
gets(in_buf);
current_time.tm_min = atoi(in_buf);
if (current_time.tm_min > 59)
current_time.tm_min = -1;
}
while (current_time.tm_sec < 0) {
puts("Enter second (0-59): ");
gets(in_buf);
current_time.tm_sec = atoi(in_buf);
if (current_time.tm_sec > 59)
current_time.tm_sec = -1;
}
now.tv_sec = mktime(&current_time);
now.tv_nsec = 0;
clock_settime(CLOCK_REALTIME, &now);
}