void Date::setDateAsWeekday(int yy,int nn,int ww) { int d; if(nn>0) { setDate(yy,1,1); int w=getWeekday(); if(ww>=w) { d=(nn-1)*7+ww-w; } else { d=(nn-1)*7+7+ww-w; } } else { setDate(yy,12,31); int w=getWeekday(); if(ww<=w) { d=(nn+1)*7+ww-w; } else { d=(nn+1)*7-7+ww-w; } } addDays(d); }
void Date::setDateAsWeekday(int yy,int mm,int nn,int ww) { int d; setDate(yy,mm,1); if(nn>0) { int w=getWeekday(); if(ww>=w) { d=(nn-1)*7+ww-w; } else { d=(nn-1)*7+7+ww-w; } } else { setDate(yy,mm,getDaysOfMonth()); int w=getWeekday(); if(ww<=w) { d=(nn+1)*7+ww-w; } else { d=(nn+1)*7-7+ww-w; } } addDays(d); }
// #3: Sun Nov 6 08:49:37 1994 ;ANSI C's asctime() format time_t atotime3 ( char *s ) { // this time structure, once filled, will help yield a time_t struct tm t; // DAY OF WEEK t.tm_wday = getWeekday ( s ); // need getLongWeekday()? while ( isalpha(*s) ) s++; while ( isspace(*s) ) s++; // MONTH t.tm_mon = getMonth ( s ); while ( *s && ! isdigit (*s) ) s++; // DAY OF MONTH t.tm_mday = atol ( s ); while ( *s && ! isalpha (*s) ) s++; // TIME getTime ( s , &t.tm_sec , &t.tm_min , &t.tm_hour ); while ( *s && ! isspace (*s) ) s++; while ( isspace (*s) ) s++; // YEAR t.tm_year = atol ( s ) - 1900 ; // # of years since 1900 // unknown if we're in daylight savings time t.tm_isdst = -1; // translate using mktime time_t tt = mktime ( &t ); return tt; }
// #1: Sun, 06 Nov 1994 08:49:37 GMT ;RFC 822, updated by RFC 1123 time_t atotime1 ( char *s ) { // this time structure, once filled, will help yield a time_t struct tm t; // DAY OF WEEK t.tm_wday = getWeekday ( s ); while ( *s && ! isdigit(*s) ) s++; // DAY OF MONTH t.tm_mday = atol ( s ); while ( *s && ! isalpha (*s) ) s++; // MONTH t.tm_mon = getMonth ( s ); while ( *s && ! isdigit (*s) ) s++; // YEAR t.tm_year = atol ( s ) - 1900 ; // # of years since 1900 while ( isdigit (*s) ) s++; while ( isspace (*s) ) s++; // TIME getTime ( s , &t.tm_sec , &t.tm_min , &t.tm_hour ); // unknown if we're in daylight savings time t.tm_isdst = -1; // translate using mktime time_t local = mktime ( &t ); // now, convert to utc //time_t utc = time(NULL); // get time here locally //time_t here = localtime(&utc); // what is the diff? //long delta = here - utc; // modify our time to make it into utc //return local - delta; return local; }
// #2: Sunday, 06-Nov-94 08:49:37 GMT ;RFC 850,obsoleted by RFC1036 static time_t atotime2 ( const char *s ) { // this time structure, once filled, will help yield a time_t struct tm t; // DAY OF WEEK t.tm_wday = getWeekday ( s ); // need getLongWeekday()? while ( *s && ! isdigit ( *s ) ) s++; // DAY OF MONTH t.tm_mday = atol ( s ); while ( *s && ! isalpha (*s) ) s++; // MONTH t.tm_mon = getMonth ( s ); while ( *s && ! isdigit (*s) ) s++; // YEAR t.tm_year = atol ( s ) ; // # of years since 1900 while ( isdigit (*s) ) s++; while ( isspace (*s) ) s++; // TIME getTime ( s , &t.tm_sec , &t.tm_min , &t.tm_hour ); // unknown if we're in daylight savings time t.tm_isdst = -1; // translate using mktime time_t global = timegm ( &t ); // skip HH:MM:SS while ( ! isspace (*s) ) s++; // skip spaces while ( isspace (*s) ) s++; // convert local time to "utc" or whatever timezone "s" points to, // which is usually gmt or utc int32_t tzoff = getTimeZone ( s ) ; if ( tzoff != BADTIMEZONE ) global += tzoff; return global; }
void print_time(time_t tim) { std::cout << getWeekday(tim) << std::endl; std::cout << getSec(tim) << std::endl; std::cout << getMin(tim) << std::endl; std::cout << getHour(tim) << std::endl; std::cout << getYear(tim) << std::endl; std::cout << getDay(tim) << std::endl; std::cout << getMonth(tim) << std::endl; }
// #1: Sun, 06 Nov 1994 08:49:37 GMT ;RFC 822, updated by RFC 1123 time_t atotime1 ( const char *s ) { // this time structure, once filled, will help yield a time_t struct tm t; // DAY OF WEEK t.tm_wday = getWeekday ( s ); while ( *s && ! isdigit(*s) ) s++; // DAY OF MONTH t.tm_mday = atol ( s ); while ( *s && ! isalpha (*s) ) s++; // MONTH t.tm_mon = getMonth ( s ); while ( *s && ! isdigit (*s) ) s++; // YEAR t.tm_year = atol ( s ) - 1900 ; // # of years since 1900 while ( isdigit (*s) ) s++; while ( isspace (*s) ) s++; // TIME getTime ( s , &t.tm_sec , &t.tm_min , &t.tm_hour ); // unknown if we're in daylight savings time t.tm_isdst = -1; // translate using mktime time_t global = timegm ( &t ); // skip HH:MM:SS while ( *s && ! isspace (*s) ) s++; // no timezone following??? fix core. if ( ! *s ) return global; // skip spaces while ( isspace (*s) ) s++; // convert local time to "utc" or whatever timezone "s" points to, // which is usually gmt or utc int32_t tzoff = getTimeZone ( s ) ; if ( tzoff != BADTIMEZONE ) global += tzoff; return global; // now, convert to utc //time_t utc = time(NULL); // get time here locally //time_t here = localtime(&utc); // what is the diff? //int32_t delta = here - utc; // modify our time to make it into utc //return local - delta; }
void setTime(TimeVal* timeVal) { /* WRITE: 0xD0 ACK<< DS1307 write address WRITE: 0x00 ACK<< location to write WRITE: 0x00 ACK<< 0 seconds, reset CH bit to 0 WRITE: 0x30 ACK<< 30 minutes WRITE: 0x09 ACK<< 9 hours, 24 hour clock mode WRITE: 0x02 ACK<< Monday (day 2) WRITE: 0x07 ACK<< 7th (date) WRITE: 0x09 ACK<< September (month) WRITE: 0x09 ACK<< '09 (year) WRITE: 0x00 ACK<< Set control register to 0' I2C STOP BIT */ // Compute the BCD data uint8_t yearByte = toBCD(timeVal->year - 2000); uint8_t monthByte = toBCD(timeVal->month); uint8_t dateByte = toBCD(timeVal->date); uint8_t dayByte = toBCD(getWeekday(timeVal) + 1); uint8_t hourByte = toBCD(timeVal->hour); uint8_t minByte = toBCD(timeVal->min); uint8_t secByte = toBCD(timeVal->sec); // Set device address and write mode i2c_start_wait(DEV_DS1307 + I2C_WRITE); // Write at address 0 i2c_write(0x00); i2c_write(secByte); // Seconds, CH bit i2c_write(minByte); // Minutes i2c_write(hourByte); // Hours, 12/24h clock mode i2c_write(dayByte); // Weekday i2c_write(dateByte); // Date i2c_write(monthByte); // Month i2c_write(yearByte); // Year // Set control register to 0 i2c_write(0x00); // Stop, release bus i2c_stop(); }
void printCalendar(int year, int month) { int i,j,weekday; int months[2][13]={{0,31,28,31,30,31,30,31,31,30,31,30,31}, //平年 {0,31,29,31,30,31,30,31,31,30,31,30,31}}; //闰年 weekday = getWeekday(year,month,1); printf(" %4d年%2d月 \n",year,month); printf("日 一 二 三 四 五 六\n"); for(i = 0; i < weekday; i++) { printf(" "); } for(j = 1; j <= months[isLeapYear(year)][month]; j++, i = (i + 1) % 7) { printf("%2d%s",j,i == 6 ? "\n":" "); } printf("\n\n"); }
const char* getDayStr(TimeVal* time) { return dayNames[getWeekday(time)]; }