api:examples:calculate_date
Recalculating Calendar Time to Julian Days and Back
All times in Skylark services are measured in frames when they are passed as integers, and in Julian days when they are passed as double integers. Recalculating calendar time to the Julian day and back can be implemented by the following code:
unsigned greg_to_jul( int y, int m, int d ) { unsigned c, ya; if ( y <= 99 ) y += 1900; if ( m > 2 ) { m -= 3; } else { m += 9; y--; } c = y; // NOTE: Sym C++ 6.0 bug c /= 100; ya = y - 100*c; return 1721119 + d + (146097*c)/4 + (1461*ya)/4 + (153*m+2)/5; }
void jul_to_greg( unsigned jd, int &y, int &m, int &d ) { unsigned x; unsigned j = jd - 1721119; y = (j*4 - 1)/146097; j = j*4 - 146097*y - 1; x = j/4; j = (x*4 + 3) / 1461; y = 100*y + j; x = (x*4) + 3 - 1461*j; x = (x + 4)/4; m = (5*x - 3)/153; x = 5*x - 3 - 153*m; d = (x + 5)/5; if ( m < 10 ) { m += 3; } else { m -= 9; y++; } }
double secs_to_time(double s) { return s/(24*3600); }
double time_to_secs(double t) { return t*(24*3600); }
double ymdf_to_time(int y, int m, int d, int f, double frame_time) { int jd = greg_to_jul(y, m, d); int df = (int)(24*3600/frame_time + 0.5); return jd + ((double)f)/df; }
void time_to_ymdf(double t, double frame_time, int &y, int &m, int &d, int &f) { int fd = (int)(24*3600/frame_time + 0.5f); int jd = (int) t; jul_to_greg( jd, y, m, d); f = (int)((t - jd)*fd + 0.5); }
api/examples/calculate_date.txt · Last modified: 2024/12/13 07:44 by Dmitriy Sazhin