Last active
July 4, 2024 16:21
-
-
Save fenrir-naru/d78cd54fe2595deb4bd5e80efbf39881 to your computer and use it in GitHub Desktop.
weekday C function for 8bit MCU
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
require 'rspec' | |
require 'inline' | |
require 'date' | |
module Date_C | |
inline{|builder| | |
builder.add_type_converter('unsigned char', 'NUM2CHR', 'CHR2FIX') | |
builder.include("<time.h>") #builder.prefix("#include <time.h>") | |
builder.c_singleton(open(File::join(File::dirname(__FILE__), 'dow_time_h.c')).read) | |
builder.c_singleton(open(File::join(File::dirname(__FILE__), 'dow_u8.c')).read) | |
} | |
end | |
describe Date_C do | |
it "dow" do | |
(1980..2099).each{|y| | |
(1..12).each{|m| | |
wday = Date::new(y, m, 1).wday | |
expect(Date_C::dow(y - 1900, m - 1, 1)).to equal(wday) | |
expect(Date_C::dow_time_h(y - 1900, m - 1, 1)).to equal(wday) | |
} | |
} | |
end | |
end |
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
unsigned int dow_time_h(unsigned int y1900, unsigned int m1, unsigned int d){ | |
struct tm t_tm = {0}; | |
time_t rawtime; | |
t_tm.tm_year = y1900; | |
t_tm.tm_mon = m1; | |
t_tm.tm_mday = d; | |
rawtime = mktime(&t_tm); | |
return (t_tm = *localtime(&rawtime)).tm_wday; | |
} |
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
unsigned char dow(unsigned char y1900, unsigned char m1, unsigned char d){ | |
unsigned char y = y1900 - 76; // y > 1980 | |
static const unsigned char tbl[] = {5, 1, 0, 3, 5, 1, 3, 6, 2, 4, 0, 2}; | |
if(m1 < 2){y -= 1;} | |
return ((y + y/4 + tbl[m1] + d) % 7); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment