📄 dt.c
字号:
return FALSE; return ((timespan1->time != timespan2->time) || (timespan1->month != timespan2->month));} /* timespan_ne() */booltimespan_lt(TimeSpan *timespan1, TimeSpan *timespan2){ double span1, span2; if (!PointerIsValid(timespan1) || !PointerIsValid(timespan2)) return FALSE; if (TIMESPAN_IS_INVALID(*timespan1) || TIMESPAN_IS_INVALID(*timespan2)) return FALSE; span1 = timespan1->time; if (timespan1->month != 0) span1 += (timespan1->month * (30.0 * 86400)); span2 = timespan2->time; if (timespan2->month != 0) span2 += (timespan2->month * (30.0 * 86400)); return span1 < span2;} /* timespan_lt() */booltimespan_gt(TimeSpan *timespan1, TimeSpan *timespan2){ double span1, span2; if (!PointerIsValid(timespan1) || !PointerIsValid(timespan2)) return FALSE; if (TIMESPAN_IS_INVALID(*timespan1) || TIMESPAN_IS_INVALID(*timespan2)) return FALSE; span1 = timespan1->time; if (timespan1->month != 0) span1 += (timespan1->month * (30.0 * 86400)); span2 = timespan2->time; if (timespan2->month != 0) span2 += (timespan2->month * (30.0 * 86400)); return span1 > span2;} /* timespan_gt() */booltimespan_le(TimeSpan *timespan1, TimeSpan *timespan2){ double span1, span2; if (!PointerIsValid(timespan1) || !PointerIsValid(timespan2)) return FALSE; if (TIMESPAN_IS_INVALID(*timespan1) || TIMESPAN_IS_INVALID(*timespan2)) return FALSE; span1 = timespan1->time; if (timespan1->month != 0) span1 += (timespan1->month * (30.0 * 86400)); span2 = timespan2->time; if (timespan2->month != 0) span2 += (timespan2->month * (30.0 * 86400)); return span1 <= span2;} /* timespan_le() */booltimespan_ge(TimeSpan *timespan1, TimeSpan *timespan2){ double span1, span2; if (!PointerIsValid(timespan1) || !PointerIsValid(timespan2)) return FALSE; if (TIMESPAN_IS_INVALID(*timespan1) || TIMESPAN_IS_INVALID(*timespan2)) return FALSE; span1 = timespan1->time; if (timespan1->month != 0) span1 += (timespan1->month * (30.0 * 86400)); span2 = timespan2->time; if (timespan2->month != 0) span2 += (timespan2->month * (30.0 * 86400)); return span1 >= span2;} /* timespan_ge() *//* timespan_cmp - 3-state comparison for timespan */inttimespan_cmp(TimeSpan *timespan1, TimeSpan *timespan2){ double span1, span2; if (!PointerIsValid(timespan1) || !PointerIsValid(timespan2)) return 0; if (TIMESPAN_IS_INVALID(*timespan1)) { return TIMESPAN_IS_INVALID(*timespan2) ? 0 : 1; } else if (TIMESPAN_IS_INVALID(*timespan2)) return -1; span1 = timespan1->time; if (timespan1->month != 0) span1 += (timespan1->month * (30.0 * 86400)); span2 = timespan2->time; if (timespan2->month != 0) span2 += (timespan2->month * (30.0 * 86400)); return (span1 < span2) ? -1 : (span1 > span2) ? 1 : 0;} /* timespan_cmp() *//*---------------------------------------------------------- * "Arithmetic" operators on date/times. * datetime_foo returns foo as an object (pointer) that * can be passed between languages. * datetime_xx is an internal routine which returns the * actual value. *---------------------------------------------------------*/DateTime *datetime_smaller(DateTime *datetime1, DateTime *datetime2){ DateTime *result; DateTime dt1, dt2; if (!PointerIsValid(datetime1) || !PointerIsValid(datetime2)) return NULL; dt1 = *datetime1; dt2 = *datetime2; result = palloc(sizeof(DateTime)); if (DATETIME_IS_RELATIVE(dt1)) dt1 = SetDateTime(dt1); if (DATETIME_IS_RELATIVE(dt2)) dt2 = SetDateTime(dt2); if (DATETIME_IS_INVALID(dt1)) *result = dt2; else if (DATETIME_IS_INVALID(dt2)) *result = dt1; else *result = ((dt2 < dt1) ? dt2 : dt1); return result;} /* datetime_smaller() */DateTime *datetime_larger(DateTime *datetime1, DateTime *datetime2){ DateTime *result; DateTime dt1, dt2; if (!PointerIsValid(datetime1) || !PointerIsValid(datetime2)) return NULL; dt1 = *datetime1; dt2 = *datetime2; result = palloc(sizeof(DateTime)); if (DATETIME_IS_RELATIVE(dt1)) dt1 = SetDateTime(dt1); if (DATETIME_IS_RELATIVE(dt2)) dt2 = SetDateTime(dt2); if (DATETIME_IS_INVALID(dt1)) *result = dt2; else if (DATETIME_IS_INVALID(dt2)) *result = dt1; else *result = ((dt2 > dt1) ? dt2 : dt1); return result;} /* datetime_larger() */TimeSpan *datetime_mi(DateTime *datetime1, DateTime *datetime2){ TimeSpan *result; DateTime dt1, dt2; if (!PointerIsValid(datetime1) || !PointerIsValid(datetime2)) return NULL; dt1 = *datetime1; dt2 = *datetime2; result = palloc(sizeof(TimeSpan)); if (DATETIME_IS_RELATIVE(dt1)) dt1 = SetDateTime(dt1); if (DATETIME_IS_RELATIVE(dt2)) dt2 = SetDateTime(dt2);#ifdef DATEDEBUG printf("datetime_mi- evaluate %f - %f\n", dt1, dt2);#endif if (DATETIME_IS_INVALID(dt1) || DATETIME_IS_INVALID(dt2)) { DATETIME_INVALID(result->time); } else result->time = JROUND(dt1 - dt2); result->month = 0; return result;} /* datetime_mi() *//* datetime_pl_span() * Add a timespan to a datetime data type. * Note that timespan has provisions for qualitative year/month * units, so try to do the right thing with them. * To add a month, increment the month, and use the same day of month. * Then, if the next month has fewer days, set the day of month * to the last day of month. */DateTime *datetime_pl_span(DateTime *datetime, TimeSpan *span){ DateTime *result; DateTime dt; int tz; char *tzn; if ((!PointerIsValid(datetime)) || (!PointerIsValid(span))) return NULL; result = palloc(sizeof(DateTime));#ifdef DATEDEBUG printf("datetime_pl_span- add %f to %d %f\n", *datetime, span->month, span->time);#endif if (DATETIME_NOT_FINITE(*datetime)) { *result = *datetime; } else if (TIMESPAN_IS_INVALID(*span)) { DATETIME_INVALID(*result); } else { dt = (DATETIME_IS_RELATIVE(*datetime) ? SetDateTime(*datetime) : *datetime);#ifdef ROUND_ALL dt = JROUND(dt + span->time);#else dt += span->time;#endif if (span->month != 0) { struct tm tt, *tm = &tt; double fsec; if (datetime2tm(dt, &tz, tm, &fsec, &tzn) == 0) {#ifdef DATEDEBUG printf("datetime_pl_span- date was %04d-%02d-%02d %02d:%02d:%02d\n", tm->tm_year, tm->tm_mon, tm->tm_mday, tm->tm_hour, tm->tm_min, tm->tm_sec);#endif tm->tm_mon += span->month; if (tm->tm_mon > 12) { tm->tm_year += ((tm->tm_mon - 1) / 12); tm->tm_mon = (((tm->tm_mon - 1) % 12) + 1); } else if (tm->tm_mon < 1) { tm->tm_year += ((tm->tm_mon / 12) - 1); tm->tm_mon = ((tm->tm_mon % 12) + 12); } /* adjust for end of month boundary problems... */ if (tm->tm_mday > day_tab[isleap(tm->tm_year)][tm->tm_mon - 1]) tm->tm_mday = (day_tab[isleap(tm->tm_year)][tm->tm_mon - 1]);#ifdef DATEDEBUG printf("datetime_pl_span- date becomes %04d-%02d-%02d %02d:%02d:%02d\n", tm->tm_year, tm->tm_mon, tm->tm_mday, tm->tm_hour, tm->tm_min, tm->tm_sec);#endif if (tm2datetime(tm, fsec, &tz, &dt) != 0) elog(ERROR, "Unable to add datetime and timespan", NULL); } else DATETIME_INVALID(dt); } *result = dt; } return result;} /* datetime_pl_span() */DateTime *datetime_mi_span(DateTime *datetime, TimeSpan *span){ DateTime *result; TimeSpan tspan; if (!PointerIsValid(datetime) || !PointerIsValid(span)) return NULL; tspan.month = -span->month; tspan.time = -span->time; result = datetime_pl_span(datetime, &tspan); return result;} /* datetime_mi_span() */TimeSpan *timespan_um(TimeSpan *timespan){ TimeSpan *result; if (!PointerIsValid(timespan)) return NULL; result = palloc(sizeof(TimeSpan)); result->time = -(timespan->time); result->month = -(timespan->month); return result;} /* timespan_um() */TimeSpan *timespan_smaller(TimeSpan *timespan1, TimeSpan *timespan2){ TimeSpan *result; double span1, span2; if (!PointerIsValid(timespan1) || !PointerIsValid(timespan2)) return NULL; result = palloc(sizeof(TimeSpan)); if (TIMESPAN_IS_INVALID(*timespan1)) { result->time = timespan2->time; result->month = timespan2->month; } else if (TIMESPAN_IS_INVALID(*timespan2)) { result->time = timespan1->time; result->month = timespan1->month; } else { span1 = timespan1->time; if (timespan1->month != 0) span1 += (timespan1->month * (30.0 * 86400)); span2 = timespan2->time; if (timespan2->month != 0) span2 += (timespan2->month * (30.0 * 86400));#ifdef DATEDEBUG printf("timespan_smaller- months %d %d times %f %f spans %f %f\n", timespan1->month, timespan2->month, timespan1->time, timespan2->time, span1, span2);#endif if (span2 < span1) { result->time = timespan2->time; result->month = timespan2->month; } else { result->time = timespan1->time; result->month = timespan1->month; } } return result;} /* timespan_smaller() */TimeSpan *timespan_larger(TimeSpan *timespan1, TimeSpan *timespan2){ TimeSpan *result; double span1, span2; if (!PointerIsValid(timespan1) || !PointerIsValid(timespan2)) return NULL; result = palloc(sizeof(TimeSpan)); if (TIMESPAN_IS_INVALID(*timespan1)) { result->time = timespan2->time; result->month = timespan2->month; } else if (TIMESPAN_IS_INVALID(*timespan2)) { result->time = timespan1->time; result->month = timespan1->month; } else { span1 = timespan1->time; if (timespan1->month != 0) span1 += (timespan1->month * (30.0 * 86400)); span2 = timespan2->time; if (timespan2->month != 0) span2 += (timespan2->month * (30.0 * 86400));#ifdef DATEDEBUG printf("timespan_larger- months %d %d times %f %f spans %f %f\n", timespan1->month, timespan2->month, timespan1->time, timespan2->time, span1, span2);#endif if (span2 > span1) { result->time = timespan2->time; result->month = timespan2->month; } else { result->time = timespan1->time; result->month = timespan1->month; } } return result;} /* timespan_larger() */TimeSpan *timespan_pl(TimeSpan *span1, TimeSpan *span2){ TimeSpan *result; if ((!PointerIsValid(span1)) || (!PointerIsValid(span2))) return NULL; result = palloc(sizeof(TimeSpan)); result->month = (span1->month + span2->month); result->time = JROUND(span1->time + span2->time); return result;} /* timespan_pl() */TimeSpan *timespan_mi(TimeSpan *span1, TimeSpan *span2){ TimeSpan *result; if ((!PointerIsValid(span1)) || (!PointerIsValid(span2))) return NULL; result = palloc(sizeof(TimeSpan)); result->month = (span1->month - span2->month); result->time = JROUND(span1->time - span2->time); return result;} /* timespan_mi() */TimeSpan *timespan_div(TimeSpan *span1, float8 *arg2){ TimeSpan *result; if ((!PointerIsValid(span1)) || (!PointerIsValid(arg2))) return NULL; if (!PointerIsValid(result = palloc(sizeof(TimeSpan)))) elog(ERROR, "Memory allocation failed, can't divide timespans", NULL); if (*arg2 == 0.0) elog(ERROR, "timespan_div: divide by 0.0 error"); result->month = rint(span1->month / *arg2); result->time = JROUND(span1->time / *arg2); return result;} /* timespan_div() *//* datetime_age() * Calculate time difference while retaining year/month fields. * Note that this does not result in an accurate absolute time span * since year and month are out of context once the arithmetic * is done. */TimeSpan *datetime_age(DateTime *datetime1, DateTime *datetime2){ TimeSpan *result; DateTime dt1, dt2; double fsec, fsec1, fsec2; struct tm tt, *tm = &tt; struct tm tt1, *tm1 = &tt1; struct tm tt2, *tm2 = &tt2; if (!PointerIsValid(datetime1) || !PointerIsValid(datetime2)) return NULL; result = palloc(sizeof(TimeSpan)); dt1 = *datetime1; dt2 = *datetime2; if (DATETIME_IS_RELATIVE(dt1)) dt1 = SetDateTime(dt1); if (DATETIME_IS_RELATIVE(dt2)) dt2 = SetDateTime(dt2); if (DATETIME_IS_INVALID(dt1) || DATETIME_IS_INVALID(dt2)) { DATETIME_INVALID(result->time); } else if ((datetime2tm(dt1, NULL, tm1, &fsec1, NULL) == 0) && (datetime2tm(dt2, NULL, tm2, &fsec2, NULL) == 0)) { fsec = (fsec1 - fsec2); tm->tm_sec = (tm1->tm_sec - tm2->tm_sec);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -