Finish dateTimeFromTimestampParts
and dateTimeFromTimespec
functions; TODO - write tests
This commit is contained in:
parent
c2b926daf0
commit
9eeb0660e4
1 changed files with 24 additions and 11 deletions
35
datetime.c
35
datetime.c
|
@ -85,27 +85,39 @@ Weekday dateTimeGetWeekday(DateTime *self) {
|
||||||
|
|
||||||
void dateTimeFromTimestampParts(int64_t seconds, int64_t nanoseconds, DateTime *dt) {
|
void dateTimeFromTimestampParts(int64_t seconds, int64_t nanoseconds, DateTime *dt) {
|
||||||
int64_t secs;
|
int64_t secs;
|
||||||
Month month;
|
|
||||||
int ret = 0;
|
int ret = 0;
|
||||||
|
|
||||||
dt->nanoseconds = nanoseconds;
|
dt->nanoseconds = nanoseconds;
|
||||||
if (seconds < 0) {
|
if (seconds < 0) {
|
||||||
secs = seconds;
|
secs = seconds;
|
||||||
yearNew(&dt->year, 1969);
|
yearNew(&dt->year, 1969);
|
||||||
while (secs > -yearGetSeconds(&dt->year)) {
|
while (secs < yearGetSeconds(&dt->year)) {
|
||||||
secs += yearGetSeconds(&dt->year);
|
secs += yearGetSeconds(&dt->year);
|
||||||
yearDecrement(&dt->year);
|
yearDecrement(&dt->year);
|
||||||
}
|
}
|
||||||
month = December;
|
dt->month = December;
|
||||||
while (ret == 0) {
|
while (ret == 0) {
|
||||||
if (secs > monthGetSeconds(month, &dt->year)) break;
|
if (-secs < monthGetSeconds(dt->month, &dt->year)) break;
|
||||||
secs += monthGetSeconds(month, &dt->year);
|
secs += monthGetSeconds(dt->month, &dt->year);
|
||||||
monthDecrement(&month);
|
ret = monthDecrement(&dt->month);
|
||||||
}
|
}
|
||||||
ret = 0;
|
ret = 0;
|
||||||
dt->month = month;
|
dt->day = monthGetDays(dt->month, &dt->year);
|
||||||
dt->day = monthGetDays(month, &dt->year);
|
while (dt->day > 0 && seconds < -SECONDS_PER_DAY) {
|
||||||
//todo
|
seconds += SECONDS_PER_DAY;
|
||||||
|
dt->day -=1;
|
||||||
|
}
|
||||||
|
dt->hour = 23;
|
||||||
|
while (dt->hour > 0 && seconds < -SECONDS_PER_HOUR) {
|
||||||
|
seconds += SECONDS_PER_HOUR;
|
||||||
|
dt->hour -= 1;
|
||||||
|
}
|
||||||
|
dt->minute = 59;
|
||||||
|
while (dt->minute > 0 && seconds < -60) {
|
||||||
|
seconds += 60;
|
||||||
|
dt->minute -= 1;
|
||||||
|
}
|
||||||
|
dt->second = seconds + 60;
|
||||||
} else if (seconds > 0) {
|
} else if (seconds > 0) {
|
||||||
secs = seconds;
|
secs = seconds;
|
||||||
yearNew(&dt->year, 1970);
|
yearNew(&dt->year, 1970);
|
||||||
|
@ -114,9 +126,9 @@ void dateTimeFromTimestampParts(int64_t seconds, int64_t nanoseconds, DateTime *
|
||||||
yearIncrement(&dt->year);
|
yearIncrement(&dt->year);
|
||||||
}
|
}
|
||||||
dt->month = January;
|
dt->month = January;
|
||||||
while (monthGetSeconds(dt->month, &dt->year) < secs) {
|
while (ret == 0 && monthGetSeconds(dt->month, &dt->year) < secs) {
|
||||||
secs -= (int64_t)monthGetSeconds(dt->month, &dt->year);
|
secs -= (int64_t)monthGetSeconds(dt->month, &dt->year);
|
||||||
monthIncrement(&dt->month);
|
ret = monthIncrement(&dt->month);
|
||||||
}
|
}
|
||||||
dt->day = secs / SECONDS_PER_DAY;
|
dt->day = secs / SECONDS_PER_DAY;
|
||||||
secs %= SECONDS_PER_DAY;
|
secs %= SECONDS_PER_DAY;
|
||||||
|
@ -136,4 +148,5 @@ void dateTimeFromTimestampParts(int64_t seconds, int64_t nanoseconds, DateTime *
|
||||||
}
|
}
|
||||||
|
|
||||||
void dateTimeFromTimespec(struct timespec *ts, DateTime *dt) {
|
void dateTimeFromTimespec(struct timespec *ts, DateTime *dt) {
|
||||||
|
dateTimeFromTimestampParts(ts->tv_sec, ts->tv_nsec, dt);
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue