33 #define DISABLE_DEBUGLOG
36 #include "gwentime_p.h"
37 #include <gwenhywfar/gwentime.h>
38 #include <gwenhywfar/debug.h>
82 "Overflow when adding %u seconds", secs);
97 "Underflow when subtracting %u seconds",
159 int day, month, year;
186 "Creating time from this: %04d/%02d/%02d - %02d:%02d:%02d (%d)",
187 year, month, day, hour, min, sec, inUtc);
200 int year, month, day;
224 if (!isdigit((
int)*p))
234 if (isdigit((
int)*p))
241 if (i==-1 && strchr(
"YMDhms", *t)!=
NULL) {
243 "No more digits at [%s], continuing", t);
298 "Unknown character in template, will skip in both strings");
311 "Got this date/time: %04d/%02d/%02d, %02d:%02d:%02d",
312 year, month-1, day, hour, min, sec);
315 gwt=
GWEN_Time_new(year, month-1, day, hour, min, sec, inUtc);
356 memmove(&ti, tp,
sizeof(ti));
365 ti.tm_year=year-1900;
371 assert(tt!=(time_t)-1);
388 const uint32_t hoursecs=60*60;
389 const uint32_t daysecs=24*hoursecs;
390 const uint32_t yearsecs=365*daysecs;
391 const uint32_t monthDays[12]= {
397 result=(year-1970)*yearsecs;
399 for (i=1970; i<year; i++)
405 isLeap=((((year % 4)==0) &&
406 ((year % 100)!=0)) ||
409 for (i=0; i<month; i++)
413 result+=monthDays[i]*daysecs;
415 result+=(day-1)*daysecs;
416 result+=(hour*hoursecs);
431 newT->msecs=t->msecs;
451 d=1000.0*((double)(t1->secs)-(double)(t0->secs));
452 d+=(double)((
double)(t1->msecs)-(
double)(t0->msecs));
465 d=(double)(t1->secs)-(double)(t0->secs);
466 d+=((double)((
double)(t1->msecs)-(
double)(t0->msecs)))/1000.0;
475 if (t1->secs<t0->secs)
477 else if (t1->secs>t0->secs)
480 if (t1->msecs<t0->msecs)
482 else if (t1->msecs>t0->msecs)
500 return (
double)((t->secs*1000)+(t->msecs));
572 *year=tb->tm_year+1900;
594 *year=tb->tm_year+1900;
620 GWEN_TIME_TMPLCHAR *e;
641 GWEN_TIME_TMPLCHAR *e;
643 e=GWEN_TimeTmplChar_List_First(ll);
647 e=GWEN_TimeTmplChar_List_Next(e);
658 GWEN_TIME_TMPLCHAR_LIST *ll) {
663 if (strchr(
"YMDhms", *s)) {
664 GWEN_TIME_TMPLCHAR *e;
670 GWEN_TimeTmplChar_List_Add(e, ll);
686 GWEN_TIME_TMPLCHAR_LIST *ll,
688 GWEN_TIME_TMPLCHAR *e;
689 int year, month, day, hour, minute, second;
700 e=GWEN_TimeTmplChar_List_First(ll);
705 switch(e->character) {
733 snprintf(buffer,
sizeof(buffer)-1,
"%0*d", GWEN_TIME_TMPL_MAX_COUNT, v);
734 buffer[
sizeof(buffer)-1]=0;
735 e->content=strdup(buffer);
736 e->nextChar=strlen(e->content)-(e->count);
737 e=GWEN_TimeTmplChar_List_Next(e);
746 GWEN_TIME_TMPLCHAR_LIST *ll;
749 ll=GWEN_TimeTmplChar_List_new();
755 if (strchr(
"YMDhms", *s)) {
756 GWEN_TIME_TMPLCHAR *e;
769 c=e->content[e->nextChar++];
778 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
struct tm GWEN_Time_toTm(const GWEN_TIME *t)
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...)
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)
GWEN_TIME * GWEN_Time__fromString(const char *s, const char *tmpl, int inUtc)
#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