uint16_t DS3231::dayInYear(uint16_t year, uint8_t month, uint8_t day) { uint16_t fromDate; uint16_t toDate; fromDate = date2days(year, 1, 1); toDate = date2days(year, month, day); return (toDate - fromDate); }
uint32_t DateTime::unixtime(void) const { uint32_t t; uint16_t days = date2days(yOff, m, d); t = time2long(days, hh, mm, ss); t += SECONDS_FROM_1970_TO_2000; // seconds from 1970 to 2000 return t; }
uint32_t read_unix_time() { DS1307 time; read_ds1307(&time); uint32_t unixtime = time2long(date2days(time.year, time.month, time.day), time.hour, time.minute, time.second); return unixtime + SECONDS_FROM_1970_TO_2000; }
uint32_t DateTime::secondstime( void ) const { uint32_t t; uint16_t days = date2days( yOff, m, d ); t = time2long( days, hh, mm, ss ); return t; }
uint32_t DateTime::unixtime(void) const { uint32_t t; uint16_t days = date2days(year_, month_, day_); t = time2long(days, hour_, minute_, second_); t += SECONDS_FROM_1970_TO_2000; return t; }
uint32_t DateTime::unixtime(void) const { uint32_t t; uint16_t days = date2days(Year, Month, Day); t = time2long(days, Hour, Minute, Second); t += SECONDS_FROM_1970_TO_2000; // seconds from 1970 to 2000 return t; }
uint32_t DS3231::unixtime(void) { uint32_t u; u = time2long(date2days(t.year, t.month, t.day), t.hour, t.minute, t.second); u += 946681200; return u; }
w_millis date2millis(w_date date) { w_int days = date2days(date); w_millis millis; millis = (w_millis)(days * MSECS_PER_DAY); millis += date->msec; /* ** we started the day so we delete those milliseconds ... */ millis -= (w_millis)MSECS_PER_DAY; return millis; }
BOOL EVAL::EvalFunction(INT nFunction, INT argc, double * argv, double * pValue) { switch( nFunction ) { case EVAL_FUNCTION_ABS: if( argc == 1 ) { * pValue = fabs( argv[0] ); return TRUE; } break; case EVAL_FUNCTION_MOD: if( argc == 2 ) { * pValue = fmod( argv[0], argv[1] ); return TRUE; } break; case EVAL_FUNCTION_CEIL: if( argc == 1 ) { * pValue = ceil( argv[0] ); return TRUE; } break; case EVAL_FUNCTION_FLOOR: if( argc == 1 ) { * pValue = floor( argv[0] ); return TRUE; } break; case EVAL_FUNCTION_ROUND: if( argc == 1 ) { * pValue = floor( argv[0] + 0.5 ); return TRUE; } break; case EVAL_FUNCTION_MIN: if( argc == 2 ) { * pValue = argv[0] < argv[1] ? argv[0] : argv[1]; return TRUE; } break; case EVAL_FUNCTION_MAX: if( argc == 2 ) { * pValue = argv[0] > argv[1] ? argv[0] : argv[1]; return TRUE; } break; case EVAL_FUNCTION_ACOS: if( argc == 1 ) { * pValue = acos(argv[0]); return TRUE; } break; case EVAL_FUNCTION_ASIN: if( argc == 1 ) { * pValue = asin(argv[0]); return TRUE; } break; case EVAL_FUNCTION_ATAN: if( argc == 1 ) { * pValue = atan(argv[0]); return TRUE; } break; case EVAL_FUNCTION_ATAN2: if( argc == 2 ) { * pValue = atan2(argv[0], argv[1]); return TRUE; } break; case EVAL_FUNCTION_COS: if( argc == 1 ) { * pValue = cos(argv[0]); return TRUE; } break; case EVAL_FUNCTION_SIN: if( argc == 1 ) { * pValue = sin(argv[0]); return TRUE; } break; case EVAL_FUNCTION_TAN: if( argc == 1 ) { * pValue = tan(argv[0]); return TRUE; } break; case EVAL_FUNCTION_COSH: if( argc == 1 ) { * pValue = cosh(argv[0]); return TRUE; } break; case EVAL_FUNCTION_SINH: if( argc == 1 ) { * pValue = sinh(argv[0]); return TRUE; } break; case EVAL_FUNCTION_TANH: if( argc == 1 ) { * pValue = tanh(argv[0]); return TRUE; } break; case EVAL_FUNCTION_EXP: if( argc == 1 ) { * pValue = exp(argv[0]); return TRUE; } break; case EVAL_FUNCTION_LOG: if( argc == 1 ) { * pValue = log(argv[0]); return TRUE; } break; case EVAL_FUNCTION_LOG10: if( argc == 1 ) { * pValue = log10(argv[0]); return TRUE; } break; case EVAL_FUNCTION_POW: if( argc == 2 ) { * pValue = pow(argv[0], argv[1]); return TRUE; } break; case EVAL_FUNCTION_SQR: if( argc == 1 ) { * pValue = argv[0] * argv[0]; return TRUE; } break; case EVAL_FUNCTION_SQRT: if( argc == 1 ) { * pValue = sqrt(argv[0]); return TRUE; } break; case EVAL_FUNCTION_TODAY: if( argc == 0 ) { * pValue = (double)today(); return TRUE; } break; case EVAL_FUNCTION_YEARDAYS: if( argc == 1 ) { * pValue = (double)yeardays((int)argv[0]); return TRUE; } break; case EVAL_FUNCTION_MONTHDAYS: if( argc == 2 ) { * pValue = (double)monthdays((int)argv[0], (int)argv[1]); return TRUE; } break; case EVAL_FUNCTION_DATE2DAYS: if( argc == 1 ) { * pValue = (double)date2days((int)argv[0]); return TRUE; } break; case EVAL_FUNCTION_DAYS2DATE: if( argc == 1 ) { * pValue = (double)days2date((int)argv[0]); return TRUE; } break; case EVAL_FUNCTION_EOMDAY: if( argc == 2 ) { * pValue = (double)eomday((int)argv[0], (int)argv[1]); return TRUE; } break; case EVAL_FUNCTION_EOMDATE: if( argc == 2 ) { * pValue = (double)eomdate((int)argv[0], (int)argv[1]); return TRUE; } break; case EVAL_FUNCTION_WEEKDAY: if( argc == 1 ) { * pValue = (double)weekday((int)argv[0]); return TRUE; } break; case EVAL_FUNCTION_ISBIZDATE: if( argc == 1 ) { * pValue = (double)isbizdate((int)argv[0], HDC_SUNSAT ); return TRUE; } if( argc == 2 ) { * pValue = (double)isbizdate((int)argv[0], (int)argv[1]); return TRUE; } break; case EVAL_FUNCTION_NBIZDATE: if( argc == 1 ) { * pValue = (double)nbizdate((int)argv[0], HDC_SUNSAT ); return TRUE; } if( argc == 2 ) { * pValue = (double)nbizdate((int)argv[0], (int)argv[1]); return TRUE; } break; case EVAL_FUNCTION_PBIZDATE: if( argc == 1 ) { * pValue = (double)pbizdate((int)argv[0], HDC_SUNSAT ); return TRUE; } if( argc == 2 ) { * pValue = (double)pbizdate((int)argv[0], (int)argv[1]); return TRUE; } break; case EVAL_FUNCTION_ADDDAYS: if( argc == 2 ) { * pValue = (double)adddays((int)argv[0], (int)argv[1]); return TRUE; } break; case EVAL_FUNCTION_ADDMONTHS: if( argc == 2 ) { * pValue = (double)addmonths((int)argv[0], (int)argv[1], EMR_ACTUAL ); return TRUE; } if( argc == 3 ) { * pValue = (double)addmonths((int)argv[0], (int)argv[1], (int)argv[2]); return TRUE; } break; case EVAL_FUNCTION_ADDTERMS: if( argc == 3 ) { * pValue = (double)addterms((int)argv[0], (int)argv[1], (int)argv[2], EMR_ACTUAL ); return TRUE; } if( argc == 4 ) { * pValue = (double)addterms((int)argv[0], (int)argv[1], (int)argv[2], (int)argv[3]); return TRUE; } break; case EVAL_FUNCTION_DAYS360: if( argc == 2 ) { * pValue = (double)days360((int)argv[0], (int)argv[1], 0 /* USA */ ); return TRUE; } if( argc == 3 ) { * pValue = (double)days360((int)argv[0], (int)argv[1], (int)argv[2]); return TRUE; } break; case EVAL_FUNCTION_DAYS365: if( argc == 2 ) { * pValue = (double)days365((int)argv[0], (int)argv[1]); return TRUE; } break; case EVAL_FUNCTION_DAYSACT: if( argc == 2 ) { * pValue = (double)daysact((int)argv[0], (int)argv[1]); return TRUE; } break; case EVAL_FUNCTION_DAYSBET: if( argc == 2 ) { * pValue = (double)daysbet((int)argv[0], (int)argv[1], DCB_ACTACT ); return TRUE; } if( argc == 3 ) { * pValue = (double)daysbet((int)argv[0], (int)argv[1], (int)argv[2]); return TRUE; } break; case EVAL_FUNCTION_MONTHSBET: if( argc == 2 ) { * pValue = (double)monthsbet((int)argv[0], (int)argv[1]); return TRUE; } break; case EVAL_FUNCTION_TERMSBET: if( argc == 3 ) { * pValue = (double)termsbet((int)argv[0], (int)argv[1], (int)argv[2]); return TRUE; } break; case EVAL_FUNCTION_TERMFRAC: if( argc == 3 ) { * pValue = termfrac((int)argv[0], (int)argv[1], (int)argv[2], DCB_ACTACT, EMR_ACTUAL ); return TRUE; } if( argc == 4 ) { * pValue = termfrac((int)argv[0], (int)argv[1], (int)argv[2], (int)argv[3], EMR_ACTUAL ); return TRUE; } if( argc == 5 ) { * pValue = termfrac((int)argv[0], (int)argv[1], (int)argv[2], (int)argv[3], (int)argv[4]); return TRUE; } break; case EVAL_FUNCTION_YEARFRAC: if( argc == 2 ) { * pValue = yearfrac((int)argv[0], (int)argv[1], DCB_ACTACT, EMR_ACTUAL ); return TRUE; } if( argc == 3 ) { * pValue = yearfrac((int)argv[0], (int)argv[1], (int)argv[2], EMR_ACTUAL ); return TRUE; } if( argc == 4 ) { * pValue = yearfrac((int)argv[0], (int)argv[1], (int)argv[2], (int)argv[3]); return TRUE; } break; } return FALSE; }
uint8_t DateTime::dayOfWeek() const { uint16_t day = date2days(yOff, m, d); return (day + 6) % 7; // Jan 1, 2000 is a Saturday, i.e. returns 6 }
uint8_t DateTime::dayOfWeek() const { return (date2days(yOff, m, d) + 6) % 7; // Jan 1, 2000 is a Sat, returns 6 }
uint32_t DateTime::get() const { return time2long(date2days(yOff, m, d), hh, mm, ss); }
int main(int argc, char *argv[]) { struct timeval tv; char help_msg[1024] = "Usage: tzconfig [-s <timezone>] [-x <timezone name is encoded according to IEEE 1003.1 (POSIX)>] [-d yyyy-mm-dd] [-j <days of this year>] [-m <[J]<days of this year>] [-i] [-h]\n\n"; char *tz, *tzname, *date, dst[64], *tmp; int c, days; strcat(help_msg, "tzconfig command summary\n"); strcat(help_msg, "\ttzconfig is a function to setup/get timezone infomation.\n"); strcat(help_msg, "\t-s:setup datetime for specified timezone.\n"); strcat(help_msg, "\t\t<timezone>(input format):'<+ | ->xx:xx' (x=[0~9]).\n"); strcat(help_msg, "\t-x:setup datetime for specified timezone name.\n"); strcat(help_msg, "\t\t<timezone name>(input format):'std offset dst [offset],start[/time],end[/time]' .\n"); strcat(help_msg, "\t-d:To convert date to days.\n"); strcat(help_msg, "\t-j:To convert days to date.\n"); strcat(help_msg, "\t\t leap year : 0 ~ 365 \n"); strcat(help_msg, "\t\t otherwise : 1 ~ 365 \n"); strcat(help_msg, "\t-m:To convert 'Jn' to 'Mm.w.d' format. \n"); strcat(help_msg, "\t\t where 'Jn' is specifies the Julian day, with n between 1 and 365. February 29 is never counted, even in leap years. \n"); strcat(help_msg, "\t\t where 'n' is the days of this year which means the Julian day, with n between 0 and 365. February 29 is counted in leap years. \n"); strcat(help_msg, "\t-i:To get the local timezone information.\n"); strcat(help_msg, "\t-h:To show this help message.\n"); if(argc <= 1 || ((isgraph(*argv[1]) || ispunct(*argv[1])) && *argv[1]!='-')) fprintf(stderr, "%s", help_msg); while ((c = getopt(argc, argv, "S:X:s:x:D:d:J:j:M:m:I0:i0:h0:H0:?0:0:-")) != -1){ switch (c) { case 'S': case 's': tz = optarg; setTZ(tz); break; case 'X': case 'x': tzname = optarg; setTZName(tzname); break; case 'D': case 'd': date = optarg; days = date2days(date); printf("%d\n", days); break; case 'J': case 'j': days = atoi(optarg); date = StrDup("yyyy-mm-dd"); days2date(days, date); printf("%s\n", date); StrFree(date); break; case 'I': case 'i': getTZ(&tz); printf("本地時區是:UTC%s\n", tz); break; case 'M': case 'm': tmp = StrDup(optarg); if(strstr(tmp, "J")){ days = atoi(strmhead(tmp, "J", -1)); if(days != 59){ days = days2date(days, dst); jds2mds(days, dst); } else printf("Invalid !!!\n"); } else{ days = atoi(optarg); /* step 1 : to find the date by days */ days = days2date(days, dst); /* step 2 : to find the result by Julian day & date */ jds2mds(days, dst); } printf("%s\n", dst); StrFree(tmp); break; case 'H': case 'h': case '?': default: fprintf(stderr, "%s", help_msg); exit(0); break; } } return 0; }
long DateTime::get()const{ uint16_t days=date2days(yOff, m ,d); return time2long(days, hh, mm, ss); }
void DataWarehouse::init( uint16_t nowYear, uint8_t nowMonth, uint8_t nowDate, uint8_t nowHour ) { uint16_t year; uint8_t month; uint8_t date; uint8_t todayIndex; boolean valid; #if _DEBUG for ( uint8_t i = 0; i < 20; i++ ) { Serial.print(i); Serial.print(": "); Serial.println( eeprom_read_byte((const uint8_t *)i), HEX); } #endif valid = true; if ( DATA_VALID == eeprom_read_byte((const uint8_t *)STORE_ADDR_VALIDFLAG ) ) { todayIndex = eeprom_read_byte((const uint8_t *)STORE_ADDR_TODAYINDEX); if ( todayIndex < DATA_SAVE_DAYS ) { uint32_t nowHours, storeHours; year = eeprom_read_word((const uint16_t *)STORE_ADDR_YEAR(todayIndex)); month = eeprom_read_byte((const uint8_t *)STORE_ADDR_MONTH(todayIndex)); date = eeprom_read_byte((const uint8_t *)STORE_ADDR_DAYDATE(todayIndex)); storeHours = days2hours( date2days( year, month, date ), END_HOURTIME_ONEDAY); nowHours = days2hours( date2days( nowYear, nowMonth, nowDate ), nowHour); if ( (nowHours - storeHours) > 12 ) { valid = false; } } else { valid = false; } } else { valid = false; } if ( !valid ) { uint8_t i,j,k; eeprom_write_byte( (uint8_t *)STORE_ADDR_VALIDFLAG, ~DATA_VALID ); for ( i = 0; i < DATA_SAVE_DAYS; i++ ) { writeDateInfo( i, INVALID_UINT16, INVALID_UINT8, INVALID_UINT8); for ( j = 0; j < DATA_TYPE_MAXNUM; j++ ) { for ( k = 0; k < SAMPLE_TIMES_PERDAY; k++ ) { writeData( INVALID_INT16, i, j, k ); } } } todayIndex = 0; eeprom_write_byte((uint8_t *)STORE_ADDR_TODAYINDEX, todayIndex); writeDateInfo( todayIndex, nowYear, nowMonth, nowDate ); //record start date, only used internally eeprom_write_byte( (uint8_t *)STORE_ADDR_VALIDFLAG, DATA_VALID ); } #if _DEBUG for ( uint8_t i = 0; i < 20; i++ ) { Serial.print(i); Serial.print(": "); Serial.println( eeprom_read_byte((const uint8_t *)i), HEX); } #endif }
uint32_t MTD_FLASHMEM DateTime::getUnixDateTime() const { uint16_t days = date2days(year, month, day); return time2long(days, hours, minutes, seconds) + SECONDS_FROM_1970_TO_2000; }
uint16_t DateTime::dayOfWeek() const { uint16_t day = date2days(year_, month_, day_); // Jan 1, 2000 is a Saturday, i.e. returns 6 return (day + 6) % 7; }
/****************************************************************** * Number of days from now to some date */ uint16_t RTClib::dateDiff(uint8_t day, uint8_t month, uint8_t year){ return date2days(day, month, year) - date2days(d, m, y); }
uint16_t MTD_FLASHMEM DateTime::dayOfYear() const { return date2days(year, month, day) - date2days(year, 1, 1) + 1; }
// 0=sunday...6=saturday uint8_t MTD_FLASHMEM DateTime::dayOfWeek() const { return (date2days(year, month, day) + 6) % 7; }