30 #define DISABLE_DEBUGLOG 33 #include "gwentime_p.h" 34 #include <gwenhywfar/gwentime.h> 35 #include <gwenhywfar/debug.h> 97 "Overflow when adding %u seconds", secs);
113 "Underflow when subtracting %u seconds",
178 int day, month, year;
205 "Creating time from this: %04d/%02d/%02d - %02d:%02d:%02d (%d)",
206 year, month, day, hour, min, sec, inUtc);
220 int year, month, day;
244 if (!isdigit((
int)*p))
254 if (isdigit((
int)*p))
261 if (i==-1 && strchr(
"YMDhms", *t)!=
NULL) {
263 "No more digits at [%s], continuing", t);
318 "Unknown character in template, will skip in both strings");
331 "Got this date/time: %04d/%02d/%02d, %02d:%02d:%02d",
332 year, month-1, day, hour, min, sec);
335 gwt=
GWEN_Time_new(year, month-1, day, hour, min, sec, inUtc);
379 memmove(&ti, tp,
sizeof(ti));
388 ti.tm_year=year-1900;
394 assert(tt!=(time_t)-1);
412 const uint32_t hoursecs=60*60;
413 const uint32_t daysecs=24*hoursecs;
414 const uint32_t yearsecs=365*daysecs;
415 const uint32_t monthDays[12]= {
421 result=(year-1970)*yearsecs;
423 for (i=1970; i<year; i++)
429 isLeap=((((year % 4)==0) &&
430 ((year % 100)!=0)) ||
433 for (i=0; i<month; i++)
437 result+=monthDays[i]*daysecs;
439 result+=(day-1)*daysecs;
440 result+=(hour*hoursecs);
456 newT->msecs=t->msecs;
478 d=1000.0*((double)(t1->secs)-(double)(t0->secs));
479 d+=(double)((
double)(t1->msecs)-(
double)(t0->msecs));
493 d=(double)(t1->secs)-(double)(t0->secs);
494 d+=((double)((
double)(t1->msecs)-(
double)(t0->msecs)))/1000.0;
504 if (t1->secs<t0->secs)
506 else if (t1->secs>t0->secs)
509 if (t1->msecs<t0->msecs)
511 else if (t1->msecs>t0->msecs)
530 return (
double)((t->secs*1000)+(t->msecs));
606 *year=tb->tm_year+1900;
629 *year=tb->tm_year+1900;
637 struct tm GWEN_Time_toTm(const
GWEN_TIME *t)
661 GWEN_TIME_TMPLCHAR *e;
684 GWEN_TIME_TMPLCHAR *e;
686 e=GWEN_TimeTmplChar_List_First(ll);
690 e=GWEN_TimeTmplChar_List_Next(e);
701 GWEN_TIME_TMPLCHAR_LIST *ll)
707 if (strchr(
"YMDhms", *s)) {
708 GWEN_TIME_TMPLCHAR *e;
714 GWEN_TimeTmplChar_List_Add(e, ll);
730 GWEN_TIME_TMPLCHAR_LIST *ll,
733 GWEN_TIME_TMPLCHAR *e;
734 int year, month, day, hour, minute, second;
745 e=GWEN_TimeTmplChar_List_First(ll);
750 switch (e->character) {
778 snprintf(buffer,
sizeof(buffer)-1,
"%0*d", GWEN_TIME_TMPL_MAX_COUNT, v);
779 buffer[
sizeof(buffer)-1]=0;
780 e->content=strdup(buffer);
781 e->nextChar=strlen(e->content)-(e->count);
782 e=GWEN_TimeTmplChar_List_Next(e);
792 GWEN_TIME_TMPLCHAR_LIST *ll;
795 ll=GWEN_TimeTmplChar_List_new();
801 if (strchr(
"YMDhms", *s)) {
802 GWEN_TIME_TMPLCHAR *e;
815 c=e->content[e->nextChar++];
824 GWEN_TimeTmplChar_List_free(ll);
struct GWEN_TIME GWEN_TIME
double GWEN_Time_Diff(const GWEN_TIME *t1, const GWEN_TIME *t0)
int GWEN_Time_GetBrokenDownUtcDate(const GWEN_TIME *t, int *days, int *month, int *year)
void GWEN_Time__fillTmplChars(const GWEN_TIME *t, GWEN_TIME_TMPLCHAR_LIST *ll, int useUtc)
#define GWEN_DB_FLAGS_OVERWRITE_VARS
int GWEN_Time_AddSeconds(GWEN_TIME *ti, uint32_t secs)
struct GWEN_DB_NODE GWEN_DB_NODE
int GWEN_Time_GetBrokenDownDate(const GWEN_TIME *t, int *days, int *month, int *year)
int GWEN_Time_toDb(const GWEN_TIME *t, GWEN_DB_NODE *db)
#define GWEN_ERROR_INVALID
double GWEN_Time_DiffSeconds(const GWEN_TIME *t1, const GWEN_TIME *t0)
int GWEN_Time_SubSeconds(GWEN_TIME *ti, uint32_t secs)
int GWEN_Time_Compare(const GWEN_TIME *t1, const GWEN_TIME *t0)
void GWEN_Time__sampleTmplChars(GWEN_UNUSED const GWEN_TIME *t, const char *tmpl, GWEN_UNUSED GWEN_BUFFER *buf, GWEN_TIME_TMPLCHAR_LIST *ll)
uint32_t GWEN_Time_Seconds(const GWEN_TIME *t)
#define GWEN_FREE_OBJECT(varname)
#define DBG_VERBOUS(dbg_logger, format, args...)
double GWEN_Time_Milliseconds(const GWEN_TIME *t)
GWEN_TIME * GWEN_Time_new(int year, int month, int day, int hour, int min, int sec, int inUtc)
void GWEN_Time_free(GWEN_TIME *t)
GWEN_TIME * GWEN_Time_fromString(const char *s, const char *tmpl)
int GWEN_Time_GetBrokenDownTime(const GWEN_TIME *t, int *hours, int *mins, int *secs)
#define GWEN_NEW_OBJECT(typ, varname)
int GWEN_Time_toUtcString(const GWEN_TIME *t, const char *tmpl, GWEN_BUFFER *buf)
GWEN_TIME * GWEN_Time_fromDb(GWEN_DB_NODE *db)
#define DBG_DEBUG(dbg_logger, format, args...)
static GWEN_TIME * GWEN_Time__fromString(const char *s, const char *tmpl, int inUtc)
GWEN_TIME * GWEN_Time_fromUtcString(const char *s, const char *tmpl)
time_t GWEN_Time_toTime_t(const GWEN_TIME *t)
uint32_t GWEN_Time__mktimeUtc(int year, int month, int day, int hour, int min, int sec)
GWEN_TIME * GWEN_Time_fromSeconds(uint32_t secs)
int GWEN_Buffer_AppendByte(GWEN_BUFFER *bf, char c)
GWEN_DB_NODE * GWEN_DB_GetGroup(GWEN_DB_NODE *n, uint32_t flags, const char *path)
struct GWEN_BUFFER GWEN_BUFFER
A dynamically resizeable text buffer.
int GWEN_Time__GetCurrentTime(GWEN_TIME *ti)
GWEN_TIME * GWEN_Time_dup(const GWEN_TIME *t)
#define DBG_ERROR(dbg_logger, format, args...)
GWEN_TIME_TMPLCHAR * GWEN_Time__findTmplChar(GWEN_TIME_TMPLCHAR_LIST *ll, char c)
void GWEN_Time__SetSecsAndMSecs(GWEN_TIME *ti, uint32_t secs, uint32_t msecs)
#define DBG_INFO(dbg_logger, format, args...)
GWEN_TIME * GWEN_CurrentTime(void)
GWEN_TIME_TMPLCHAR * GWEN_TimeTmplChar_new(char c)
#define GWEN_LIST_INIT(t, element)
int GWEN_DB_GetIntValue(GWEN_DB_NODE *n, const char *path, int idx, int defVal)
void GWEN_TimeTmplChar_free(GWEN_TIME_TMPLCHAR *e)
int GWEN_Time_toString(const GWEN_TIME *t, const char *tmpl, GWEN_BUFFER *buf)
int GWEN_Time_GetBrokenDownUtcTime(const GWEN_TIME *t, int *hours, int *mins, int *secs)
int GWEN_DB_SetIntValue(GWEN_DB_NODE *n, uint32_t flags, const char *path, int val)
#define GWEN_LIST_FUNCTIONS(t, pr)
#define GWEN_PATH_FLAGS_NAMEMUSTEXIST
int GWEN_Time__toString(const GWEN_TIME *t, const char *tmpl, GWEN_BUFFER *buf, int useUtc)
#define GWEN_LIST_FINI(t, element)
int GWEN_Buffer_AppendString(GWEN_BUFFER *bf, const char *buffer)
#define GWEN_DB_FLAGS_DEFAULT