Init commit, the date parser works but nothing else
This commit is contained in:
69
13date.cpp
Normal file
69
13date.cpp
Normal file
@@ -0,0 +1,69 @@
|
||||
#include <cstdio>
|
||||
#include <cstdlib>
|
||||
#include "13date.h"
|
||||
|
||||
Date::Date(double timestamp, float offset)
|
||||
{
|
||||
this->timestamp=timestamp;
|
||||
this->offset=offset;
|
||||
this->components=(int*)malloc(sizeof(int)*7);
|
||||
this->update_components();
|
||||
}
|
||||
|
||||
Date::~Date()
|
||||
{
|
||||
free(this->components);
|
||||
}
|
||||
|
||||
bool Date::leapyear()
|
||||
{
|
||||
return Date::leapyear(this->components[0]);
|
||||
}
|
||||
|
||||
void Date::update_components()
|
||||
{
|
||||
this->update_components(this->timestamp);
|
||||
}
|
||||
|
||||
void Date::update_components(double timestamp)
|
||||
{
|
||||
int* c=this->components;
|
||||
c[2]=timestamp/86400; // 60*60*24
|
||||
c[5]=timestamp-(long)c[2]*86400; // Gotta use longs for these subtractions to avoid 2038 bug. May reoccur at uhhh.... I don't have a tool that can convert 2^63-1 to a date. An estimate from this suggests it should be in the year 5,881,580, but that's when the bug really reoccurs, which is much sooner. Please don't use my calendar after 5 million AD.
|
||||
c[6]=(timestamp-(long)timestamp)*1000; // Don't need to subtract days, just take the float portion
|
||||
// YMD handling, gotta deal with leap years
|
||||
c[2]-=365*31+8+(c[5]<0); // This is the number of days between 1970-01-01 and 2001-01-01. Subtract them to move the epoch/zero-point. Also handle non-zero timestamps, this is a fucking off-by-one generating machine.
|
||||
c[0]=(c[2]+(c[2]<0))/365-(c[2]<0); // Make C++'s negative division/modulo behave like python's, because it's more useful for dates. Old version c[2]/365-(c[2]<0) would bug out when c[2]==365, that's when C++ flips value, but not when python - and dates - do. The perfect off-by-one-error, buried within a division. Could potentially fix it with some +not (c[2]%365) instead.
|
||||
c[2]%=365; c[2]+=365; c[2]%=365; // C++ modulo over n is bound (-n,n), this maps the negative portion into the positive without flipping it around.
|
||||
c[0]+=1-(c[0]>0); // Actually the +1 has to happen now (again, because C++ negative divisions), but the +2k can't happen until after the next line - BUT ONLY WHEN THE YEAR IS NEGATIVE! Or zero, for some reason.
|
||||
c[2]-=c[0]/4-c[0]/100+c[0]/400-(c[0]<=0); // Initial leap-year adjustment, this can potentially take day count outside 0-365 range. This didn't need anything fancy for handling y2k in python ver but it does in C++ ver, because of that difference in negative division.
|
||||
c[0]+=2000+(c[0]>0); // Adjust to real year value, ~~the +1 has to happen now but~~ the +2k doesn't matter, but why not do it all at once.
|
||||
while(c[2]<0 and c[0]>2000){c[2]+=365+this->leapyear(--c[0]);} // Honestly could just build up the year from days like this to begin with
|
||||
while(c[2]>364+this->leapyear(c[0]) and c[0]<2000){c[2]-=365+this->leapyear(c[0]++);} // But I prefer not to, this only iterates once every ~1500 years instead of once per year.
|
||||
c[1]=c[2]/28+1;
|
||||
c[2]=c[2]%28+1;
|
||||
// HMS handling, no big
|
||||
// Still bugged in negative time
|
||||
c[5]=(c[5]+86400)%86400;
|
||||
c[3]=c[5]/3600;
|
||||
c[5]%=3600;
|
||||
c[4]=c[5]/60;
|
||||
c[5]%=60;
|
||||
}
|
||||
|
||||
char* Date::isostring(bool apply_offset)
|
||||
{
|
||||
char* buff=(char*)malloc(sizeof(char)*40);
|
||||
int* c=this->components;
|
||||
if (!apply_offset)
|
||||
{
|
||||
sprintf(buff,"%d-%02d-%02dT%02d:%02d:%02d.%dZ",c[0],c[1],c[2],c[3],c[4],c[5],c[6]);
|
||||
}
|
||||
else
|
||||
{ // I should probably make a method specifically for adding/removing offset, it's a lot of work to do it twice in one call.
|
||||
this->update_components(this->timestamp+3600*this->offset);
|
||||
sprintf(buff,"%d-%02d-%02dT%02d:%02d:%02d.%d+%05.2f",c[0],c[1],c[2],c[3],c[4],c[5],c[6],this->offset);
|
||||
this->update_components();
|
||||
}
|
||||
return buff;
|
||||
}
|
||||
26
13date.h
Normal file
26
13date.h
Normal file
@@ -0,0 +1,26 @@
|
||||
#ifndef THIRTEENDATE
|
||||
#define THIRTEENDATE
|
||||
class Date
|
||||
{
|
||||
private:
|
||||
void update_components();
|
||||
void update_components(double timestamp);
|
||||
|
||||
public:
|
||||
double timestamp;
|
||||
float offset;
|
||||
int* components; // year, month, day, hour, minute, second, millisecond
|
||||
|
||||
Date(double timestamp,float offset=0);
|
||||
~Date();
|
||||
|
||||
bool leapyear();
|
||||
static bool leapyear(int year)
|
||||
{
|
||||
while(year<0){year+=400;}
|
||||
return (bool)(not (year%4)-not (year%100)+not (year%400));
|
||||
}
|
||||
char* isostring(bool apply_offset=true);
|
||||
};
|
||||
|
||||
#endif
|
||||
BIN
13organiser
Executable file
BIN
13organiser
Executable file
Binary file not shown.
44
bullshit
Normal file
44
bullshit
Normal file
@@ -0,0 +1,44 @@
|
||||
x=days-(365*31+8) # set 2001-01-01 as the epoch
|
||||
|
||||
l=x/146097-x/36524+x/1461 # number of leap-days to remove before calculating current year
|
||||
+x<0 # add one if date is negative
|
||||
|
||||
y=(x-l)/365
|
||||
|
||||
d=(x-(y*365+l)) // day in current year
|
||||
*((x<0)*-2+1) // inverted if negative
|
||||
+(365+bool(y%4)-bool(y%100)+bool(y%400))*(x<0) // so that we can add 365/366 - based on leapyear status - to it to make it count the year backwards
|
||||
|
||||
|
||||
|
||||
def leapyear(year):
|
||||
return (not year%4)-(not year%100)+(not year%400)
|
||||
|
||||
def mkdate(days_since_1970):
|
||||
epoch=days_since_1970-(365*31+8) # 2001-01-01
|
||||
year=epoch//365
|
||||
day=epoch%365
|
||||
day-=year//4-year//100+year//400
|
||||
year+=2001
|
||||
while day<0 and year>2000: year-=1; day+=365+leapyear(year)
|
||||
# It already behaves correctly in 2000 somehow, so deliberately leave it out of adjustments.
|
||||
while day>364+leapyear(year) and year<2000: day-=365+leapyear(year); year+=1
|
||||
month=day//28+1
|
||||
day=day%28+1
|
||||
return year,month,day
|
||||
|
||||
def derp(time):
|
||||
a=mkdate(time//86400)
|
||||
b=datetime.date.fromtimestamp(time)
|
||||
print(a[0]==b.year,a,b)
|
||||
|
||||
for n in sorted([1762613223,13601088000,13601174400,978393600,946684800,1072915200,1104364800,1104451200,1104537600,4102444800,4133894400,915148800,946512000,946598400,978134400,978220800,13600915200,13601001600,978307200,946771200,852076800,851990400,883526400,0,-86400,-31536000,31449600,-56771020800,-62135596800,-62104147200,-62135510400,-62134819200,-62104060800,-2208988800,-59011459200,-30610224000,-30641760000,-46388678400,-54277948800,-50333356800,-48502972800,-47650982400,-46420214400,-47019830400,-46704211200,-46546444800,-46609516800,-46641139200,-46641052800]):
|
||||
derp(n)
|
||||
|
||||
def test():
|
||||
dd=datetime.date(1970,1,1)
|
||||
for n in range(3470,3530):
|
||||
for m in range(20,32):
|
||||
d=datetime.date(n,12,m)
|
||||
a=mkdate((d-dd).days)
|
||||
print(a[0]==d.year,a,d)
|
||||
1
calendar_event.cpp
Normal file
1
calendar_event.cpp
Normal file
@@ -0,0 +1 @@
|
||||
#include "calendar_event.h"
|
||||
11
calendar_event.h
Normal file
11
calendar_event.h
Normal file
@@ -0,0 +1,11 @@
|
||||
#ifndef CALENDAREVENT
|
||||
#define CALENDAREVENT
|
||||
|
||||
#include "13date.h"
|
||||
|
||||
class Event
|
||||
{
|
||||
public:
|
||||
Date *date_start, *date_end, *date_stamp;
|
||||
};
|
||||
#endif
|
||||
15
main.cpp
Normal file
15
main.cpp
Normal file
@@ -0,0 +1,15 @@
|
||||
#include <cstdio>
|
||||
//#include <cstdlib>
|
||||
#include <string>
|
||||
#include "13date.h"
|
||||
|
||||
int main(int arg_count,char** args)
|
||||
{
|
||||
printf("%d %d\n",-32/4, -31/4);
|
||||
if (arg_count<2) { printf("Must provide a timestamp\n"); return 1; }
|
||||
Date *d=new Date(std::stod(args[1]),9);
|
||||
printf("%s\n",d->isostring());
|
||||
printf("%s\n",d->isostring(false));
|
||||
delete d;
|
||||
return 0;
|
||||
}
|
||||
Reference in New Issue
Block a user