const char *MSMBSDate::format(MSString& aString_,const char *format_) const { #ifdef MS_THREAD_SAFE_FUNCTIONS struct tm tms; #endif //MS_THREAD_SAFE_FUNCTIONS MSString aString(0,((format_!=0)?strlen(format_):0)+3); // __tm_zone, a member of struct tm for the sun architecture // must be initialized if the strftime format %Z is used. Can // get this value from localtime. time_t now=time(0); struct tm *pCalendarTime=MS_LOCALTIME(&now,&tms); MSMonth m; MSDay d; MSYear y; asMonthDayYear(m,d,y); pCalendarTime->tm_sec=0; pCalendarTime->tm_min=0; pCalendarTime->tm_hour=0; pCalendarTime->tm_mday=d; pCalendarTime->tm_mon=m-1; pCalendarTime->tm_year=y-1900; pCalendarTime->tm_wday=weekDay()==7?0:weekDay(); pCalendarTime->tm_yday=dayOfYear()-1; pCalendarTime->tm_isdst=-1; int numberOfChars; while ((numberOfChars=strftime((char*)aString.string(),aString.length(),format_,pCalendarTime))==0) { aString=MSString(0,2*aString.length()); } // remove the terminating null from strftime. strftime returns the number of characters // produced not including the terminating null character. aString=MSString(aString.string(),numberOfChars); aString_=aString.string(); return aString_; }
void MSMBSDate::prevWeekday(void) { switch (weekDay()) { case Saturday: _date--; changed(); break; case Sunday: _date-=2; changed(); break; default: break; } }
void MSMBSDate::nextWeekday(void) { switch (weekDay()) { case Saturday: _date+=2; changed(); break; case Sunday: _date++; changed(); break; default: break; } }
MSBoolean MSMBSDate::isWeekend(void) const { MSBoolean rc; switch (weekDay()) { case Monday: case Tuesday: case Wednesday: case Thursday: case Friday: rc=MSFalse; break; case Saturday: case Sunday: rc=MSTrue; break; default: rc=MSFalse; break; } return rc; }
MSDate MSMBSDate::previous(MSDay desiredDayOfWeek_) const { // Renumber the desired and current day of week to start at 0 (Monday) // and end at 6 (Sunday). desiredDayOfWeek_--; MSDay thisDayOfWeek=weekDay()-1; MSJulian j=_date; // Have to determine how many days difference from current day back to // desired, if any. Special calculation under the 'if' statement to // effect the wraparound counting from Monday (0) back to Sunday (6). if (desiredDayOfWeek_>thisDayOfWeek) thisDayOfWeek+=7-desiredDayOfWeek_; else thisDayOfWeek-=desiredDayOfWeek_; j-=thisDayOfWeek; // Adjust j to set it at desired day of week. return MSMBSDate(j); }
const char* MSMBSDate::nameOfDay(void) const { return dayName(weekDay());}
/*----------------------------------------------------------------------------- * Obtenção de uma lista com as datas de mudança de hora */ int _getDstDate(const struct tm * t, struct tm * tx) { _DstRules * rules, * pr; char * isdst = tin_getDst(); int i; if (isdst == NULL) return -1; /* * Tradução das regras de formato texto para formato interno */ if (isdst[0] != '\0') { if ((rules = _getDst(isdst)) == NULL) return 0; } else return 0; /* * Interpretação das regras */ for (i = 0, pr = rules; pr->wday != 7 && tx != NULL; ++pr, ++tx, ++i) { /* * O ano em causa */ int year = pr->year == 0 ? t->tm_year : pr->year; /* * Dias desde o inicio do ano */ int yday = yearDays(year, pr->mon, pr->day); /* * Testa se a regra de mudanca de hora e' fixa ou adaptativa */ if (pr->adap) { if ( ! pr->prev) { /* Definição a partir do inicio do mês */ int wday = weekDay(t->tm_year, pr->mon, 1); yday = yearDays(year, pr->mon, 1); yday += pr->wday >= wday ? pr->wday - wday : pr->wday + 7 - wday; yday += (pr->day - 1) * 7; } else { /* Definição a partir do fim do mês */ int wday = weekDay(t->tm_year, pr->mon + 1, 1); yday = yearDays(year, pr->mon + 1, 1); yday -= wday > pr->wday ? wday - pr->wday : wday + 7 - pr->wday; yday -= (pr->day - 1) * 7; } } tx->tm_year = year; tx->tm_mon = pr->mon; tx->tm_mday = _dayMonth(year, yday); // dia no mês tx->tm_wday = (days1970(year, 0, 1) + yday + WDAY) % 7; // dia da semana tx->tm_yday = yday; tx->tm_hour = pr->hour; tx->tm_min = 0; tx->tm_sec = 0; } return i; }
int _isdst(const struct tm * t) { _DstRules * pr; static _DstRules * rules = NULL; static const char * olddst = NULL; char * isdst = tin_getDst(); if (isdst == NULL) return -1; // if (olddst != isdst) { if (isdst[0] != '\0') { if ((rules = _getDst(isdst)) == NULL) return -1; } else return -1; olddst = isdst; // } { int dst = 0; const int hour = t->tm_yday * 24 + t->tm_hour; for (pr = rules; pr->wday != 7; ++pr) if (pr->year <= t->tm_year) { /* * Dias desde o inicio do ano */ int yday = yearDays(t->tm_year, pr->mon, pr->day); /* * Testa se a regra de mudanca de hora e' fixa ou adaptativa */ if (pr->adap) { if ( ! pr->prev) { /* Definição a partir do inicio do mês */ int wday = weekDay(t->tm_year, pr->mon, 1); yday = yearDays(t->tm_year, pr->mon, 1); yday += pr->wday >= wday ? pr->wday - wday : pr->wday + 7 - wday; yday += (pr->day - 1) * 7; } else { /* Definição a partir do fim do mês */ int wday = weekDay(t->tm_year, pr->mon + 1, 1); yday = yearDays(t->tm_year, pr->mon + 1, 1); yday -= wday > pr->wday ? wday - pr->wday : wday + 7 - pr->wday; yday -= (pr->day - 1) * 7; } } /* * Se a hora presente for menor que a proxima mudanca de hora * acabar a pesquisa */ if (hour < yday * 24 + pr->hour) return dst; /* * O inicio do ano comeca sempre em hora solar * Enquanto se mantiver no mesmo ano vai invertendo */ dst = pr->year == (pr + 1)->year ? ! dst : 0; } return dst; } }