30#include "gwendate_p.h"
33#include <gwenhywfar/debug.h>
34#include <gwenhywfar/misc.h>
43 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31
67 if (m<1 || m>12 || d<1 || d>31 || y<0) {
73 DBG_INFO(
GWEN_LOGDOMAIN,
"Bad date values (day value too high, erroneous year=%d, month=%d, day=%d)", y, m, d);
82 gd->julian=(1461*(y+4800+(m-14)/12))/4+
83 (367*(m-2-12*((m-14)/12)))/12-
84 (3*((y+4900+(m-14)/12)/100))/4+
88 memmove(gd->asString, s, 8);
106 i=(4000*(l+1))/1461001;
109 gd->day=l-(2447*j)/80;
111 gd->month=j+2-(12*l);
112 gd->year=100*(n-49)+i+l;
118 len=snprintf(gd->asString,
sizeof(gd->asString)-1,
120 gd->year, gd->month, gd->day);
121 gd->asString[
sizeof(gd->asString)-1]=0;
122 if ((
int)(
sizeof(gd->asString)-1) < len) {
211 memmove(&ti, tp,
sizeof(ti));
215 ti.tm_year=gd->year-1900;
216 ti.tm_mon=gd->month-1;
221 assert(tt!=(time_t)-1);
264 gd->month=ogd->month;
266 gd->julian=ogd->julian;
267 memmove(gd->asString, ogd->asString,
sizeof(gd->asString));
280 if (s && strlen(s)>7) {
283 const char *originalPtr;
315 if (3==sscanf(s,
"%04d%02d%02d", &y, &m, &d)) {
341 return ((y%4==0) && (y%100!=0)) || (y%400==0);
377 result=(gd->julian)-(gd11->julian);
420 return (gd->julian+1)%7;
436 if (gd1->julian==gd0->julian)
438 else if (gd1->julian>gd0->julian)
458 return gd1->julian-gd0->julian;
481 int year, month, day;
503 if (!isdigit((
int)*p))
513 if (isdigit((
int)*p))
520 if (i==-1 && strchr(
"YMD", *t)!=
NULL) {
522 "No more digits at [%s], continuing", t);
553 "Unknown character in template, will skip in both strings");
564 "Got this date/time: %04d/%02d/%02d",
585 GWEN_DATE_TMPLCHAR *e;
605 e->maxCount=GWEN_DATE_TMPL_MAX_COUNT;
627 GWEN_DATE_TMPLCHAR *e;
629 e=GWEN_DateTmplChar_List_First(ll);
633 e=GWEN_DateTmplChar_List_Next(e);
644 GWEN_DATE_TMPLCHAR_LIST *ll)
650 if (strchr(
"YMDWw", *s)) {
651 GWEN_DATE_TMPLCHAR *e;
657 GWEN_DateTmplChar_List_Add(e, ll);
674 GWEN_DATE_TMPLCHAR *e;
677 e=GWEN_DateTmplChar_List_First(ll);
681 if (e->character==
'w') {
712 e->content=strdup(s);
719 switch (e->character) {
741 snprintf(buffer,
sizeof(buffer)-1,
"%0*d", e->maxCount, v);
742 buffer[
sizeof(buffer)-1]=0;
743 e->content=strdup(buffer);
745 clen=strlen(e->content);
748 e->nextChar=clen-(e->count);
751 e=GWEN_DateTmplChar_List_Next(e);
760 GWEN_DATE_TMPLCHAR_LIST *ll;
763 ll=GWEN_DateTmplChar_List_new();
769 if (strchr(
"YMDWw", *s)) {
770 GWEN_DATE_TMPLCHAR *e;
783 c=e->content[e->nextChar];
794 GWEN_DateTmplChar_List_free(ll);
GWEN_BUFFER * GWEN_Buffer_new(char *buffer, uint32_t size, uint32_t used, int take)
void GWEN_Buffer_free(GWEN_BUFFER *bf)
int GWEN_Buffer_AppendString(GWEN_BUFFER *bf, const char *buffer)
char * GWEN_Buffer_GetStart(const GWEN_BUFFER *bf)
int GWEN_Buffer_AppendByte(GWEN_BUFFER *bf, char c)
const char * GWEN_DB_GetCharValue(GWEN_DB_NODE *n, const char *path, int idx, const char *defVal)
int GWEN_DB_SetCharValue(GWEN_DB_NODE *n, uint32_t flags, const char *path, const char *val)
#define GWEN_DB_FLAGS_OVERWRITE_VARS
struct GWEN_DB_NODE GWEN_DB_NODE
#define DBG_DEBUG(dbg_logger, format, args...)
#define DBG_VERBOUS(dbg_logger, format, args...)
#define DBG_INFO(dbg_logger, format, args...)
#define DBG_ERROR(dbg_logger, format, args...)
#define GWEN_ERROR_GENERIC
struct GWEN_BUFFER GWEN_BUFFER
A dynamically resizeable text buffer.
GWEN_DATE * GWEN_Date_GetLastMonthEnd(const GWEN_DATE *dt)
GWEN_DATE * GWEN_Date_GetThisQuarterYearEnd(const GWEN_DATE *dt)
GWEN_DATE * GWEN_Date_GetThisYearStart(const GWEN_DATE *dt)
void GWEN_Date_SubDays(GWEN_DATE *gd, int days)
int GWEN_Date_Diff(const GWEN_DATE *gd1, const GWEN_DATE *gd0)
GWEN_DATE * GWEN_Date_fromString(const char *s)
GWEN_DATE_TMPLCHAR * GWEN_DateTmplChar_new(char c)
GWEN_DATE * GWEN_Date_GetThisMonthEnd(const GWEN_DATE *dt)
GWEN_DATE * GWEN_Date_fromStringWithTemplate(const char *s, const char *tmpl)
GWEN_DATE * GWEN_Date_GetThisMonthStart(const GWEN_DATE *dt)
GWEN_DATE * GWEN_Date_fromGmTime(time_t t)
GWEN_DATE * GWEN_Date_GetLastHalfYearStart(const GWEN_DATE *dt)
static void _writeAsString(GWEN_DATE *gd)
GWEN_DATE * GWEN_Date_fromJulian(int julian)
int GWEN_Date_IsLeapYear(int y)
GWEN_DATE_TMPLCHAR * GWEN_Date__findTmplChar(GWEN_DATE_TMPLCHAR_LIST *ll, char c)
GWEN_DATE * GWEN_Date_GetLastYearEnd(const GWEN_DATE *dt)
void GWEN_Date__fillTmplChars(const GWEN_DATE *t, GWEN_DATE_TMPLCHAR_LIST *ll)
void GWEN_Date_AddDays(GWEN_DATE *gd, int days)
int GWEN_Date_DaysInYear(const GWEN_DATE *gd)
GWEN_DATE * GWEN_Date_GetLastYearStart(const GWEN_DATE *dt)
GWEN_DATE * GWEN_Date_fromTime(const GWEN_TIME *ti)
int GWEN_Date_GetYear(const GWEN_DATE *gd)
static int _daysInMonth(int month, int year)
void GWEN_Date__sampleTmplChars(GWEN_UNUSED const GWEN_DATE *t, const char *tmpl, GWEN_UNUSED GWEN_BUFFER *buf, GWEN_DATE_TMPLCHAR_LIST *ll)
GWEN_DATE * GWEN_Date_GetThisYearEnd(const GWEN_DATE *dt)
GWEN_DATE * GWEN_Date_dup(const GWEN_DATE *ogd)
GWEN_DATE * GWEN_Date_fromGregorian(int y, int m, int d)
GWEN_DATE * GWEN_Date_GetThisHalfYearEnd(const GWEN_DATE *dt)
static GWEN_DATE * _createFromGregorianAndUseGivenString(int y, int m, int d, const char *s)
const char * GWEN_Date_GetString(const GWEN_DATE *gd)
int GWEN_Date_WeekDay(const GWEN_DATE *gd)
int GWEN_Date_toStringWithTemplate(const GWEN_DATE *t, const char *tmpl, GWEN_BUFFER *buf)
int GWEN_Date_GetMonth(const GWEN_DATE *gd)
int GWEN_Date_toDb(const GWEN_DATE *dt, GWEN_DB_NODE *db)
void GWEN_DateTmplChar_free(GWEN_DATE_TMPLCHAR *e)
void GWEN_Date_free(GWEN_DATE *gd)
int GWEN_Date_GetJulian(const GWEN_DATE *gd)
int GWEN_Date_Compare(const GWEN_DATE *gd1, const GWEN_DATE *gd0)
GWEN_DATE * GWEN_Date_GetThisHalfYearStart(const GWEN_DATE *dt)
GWEN_DATE * GWEN_Date_GetLastQuarterYearStart(const GWEN_DATE *dt)
int GWEN_Date_DaysInMonth(const GWEN_DATE *gd)
GWEN_DATE * GWEN_Date_CurrentDate(void)
time_t GWEN_Date_toLocalTime(const GWEN_DATE *gd)
GWEN_DATE * GWEN_Date_fromLocalTime(time_t t)
void GWEN_Date_setJulian(GWEN_DATE *gd, int julian)
GWEN_DATE * GWEN_Date_fromDb(GWEN_DB_NODE *db)
GWEN_DATE * GWEN_Date_GetThisQuarterYearStart(const GWEN_DATE *dt)
int GWEN_Date_GetDay(const GWEN_DATE *gd)
GWEN_DATE * GWEN_Date_GetLastQuarterYearEnd(const GWEN_DATE *dt)
GWEN_DATE * GWEN_Date_GetLastMonthStart(const GWEN_DATE *dt)
GWEN_DATE * GWEN_Date_GetLastHalfYearEnd(const GWEN_DATE *dt)
static const uint8_t daysInMonth[12]
struct GWEN_DATE GWEN_DATE
struct GWEN_TIME GWEN_TIME
GWENHYWFAR_API int GWEN_Time_toString(const GWEN_TIME *t, const char *tmpl, GWEN_BUFFER *buf)
#define GWEN_LIST_FINI(t, element)
#define GWEN_LIST_FUNCTIONS(t, pr)
#define GWEN_LIST_INIT(t, element)
#define GWEN_FREE_OBJECT(varname)
#define GWEN_NEW_OBJECT(typ, varname)