From 2dc50471ca17eaeb24b45abce4c0f8944cd781d5 Mon Sep 17 00:00:00 2001 From: Frank Gevaerts Date: Tue, 11 Aug 2009 19:30:19 +0000 Subject: Consolidate day of week calculation git-svn-id: svn://svn.rockbox.org/rockbox/trunk@22258 a1c6a512-1295-4272-9138-f99709370657 --- apps/screens.c | 9 +-------- firmware/common/timefuncs.c | 7 +++++-- firmware/include/timefuncs.h | 2 +- firmware/usbstack/usb_storage.c | 22 ++++++++++++---------- 4 files changed, 19 insertions(+), 21 deletions(-) diff --git a/apps/screens.c b/apps/screens.c index 9f1596cb7a..4ec35f9f57 100644 --- a/apps/screens.c +++ b/apps/screens.c @@ -511,7 +511,6 @@ bool set_time_screen(const char* title, struct tm *tm) int button; unsigned int i, j, s; int cursorpos = 0; - unsigned int julianday; unsigned int realyear; unsigned int width; unsigned int min, max; @@ -556,13 +555,7 @@ bool set_time_screen(const char* title, struct tm *tm) tm->tm_mday = daysinmonth[tm->tm_mon]; /* calculate day of week */ - julianday = tm->tm_mday; - for(i = 0; (int)i < tm->tm_mon; i++) { - julianday += daysinmonth[i]; - } - - tm->tm_wday = (realyear + julianday + (realyear - 1) / 4 - - (realyear - 1) / 100 + (realyear - 1) / 400 + 7 - 1) % 7; + set_day_of_week(tm); /* put all the numbers we want from the tm struct into an easily printable buffer */ diff --git a/firmware/common/timefuncs.c b/firmware/common/timefuncs.c index e59534eeba..774fba9ab0 100644 --- a/firmware/common/timefuncs.c +++ b/firmware/common/timefuncs.c @@ -194,12 +194,15 @@ time_t mktime(struct tm *t) } #endif -int day_of_week(int m, int d, int y) +void set_day_of_week(struct tm *tm) { + int y=tm->tm_year+1900; + int d=tm->tm_mday; + int m=tm->tm_mon; static const char mo[] = { 0, 3, 2, 5, 0, 3, 5, 1, 4, 6, 2, 4 }; if(m == 0 || m == 1) y--; - return (d + mo[m] + y + y/4 - y/100 + y/400) % 7; + tm->tm_wday = (d + mo[m] + y + y/4 - y/100 + y/400) % 7; } void yearday_to_daymonth(int yd, int y, int *d, int *m) diff --git a/firmware/include/timefuncs.h b/firmware/include/timefuncs.h index 52d4608287..e9ef8075ac 100644 --- a/firmware/include/timefuncs.h +++ b/firmware/include/timefuncs.h @@ -29,7 +29,7 @@ struct tm *get_time(void); int set_time(const struct tm *tm); bool valid_time(const struct tm *tm); -int day_of_week(int m, int d, int y); +void set_day_of_week(struct tm *tm); void yearday_to_daymonth(int yd, int y, int *d, int *m); #if CONFIG_RTC time_t mktime(struct tm *t); diff --git a/firmware/usbstack/usb_storage.c b/firmware/usbstack/usb_storage.c index 94f4f71dbb..a3664b074b 100644 --- a/firmware/usbstack/usb_storage.c +++ b/firmware/usbstack/usb_storage.c @@ -608,14 +608,13 @@ void usb_storage_transfer_complete(int ep,int dir,int status,int length) } break; case RECEIVING_TIME: - tm.tm_year=(tb.transfer_buffer[0]<<8)+tb.transfer_buffer[1]; + tm.tm_year=(tb.transfer_buffer[0]<<8)+tb.transfer_buffer[1] - 1900; tm.tm_yday=(tb.transfer_buffer[2]<<8)+tb.transfer_buffer[3]; tm.tm_hour=tb.transfer_buffer[5]; tm.tm_min=tb.transfer_buffer[6]; tm.tm_sec=tb.transfer_buffer[7]; - yearday_to_daymonth(tm.tm_yday,tm.tm_year,&tm.tm_mday,&tm.tm_mon); - tm.tm_wday=day_of_week(tm.tm_mon,tm.tm_mday,tm.tm_year); - tm.tm_year -= 1900; + yearday_to_daymonth(tm.tm_yday,tm.tm_year + 1900,&tm.tm_mday,&tm.tm_mon); + set_day_of_week(&tm); set_time(&tm); send_csw(UMS_STATUS_GOOD); break; @@ -1081,16 +1080,19 @@ static void handle_scsi(struct command_block_wrapper* cbw) break; case SCSI_WRITE_BUFFER: - if(cbw->command_block[1]==1 - && cbw->command_block[2]==0 - && cbw->command_block[3]==0x0c + if(cbw->command_block[1]==1 /* mode = vendor specific */ + && cbw->command_block[2]==0 /* buffer id = 0 */ + + && cbw->command_block[3]==0x0c /* offset (3 bytes) */ && cbw->command_block[4]==0 && cbw->command_block[5]==0 - && cbw->command_block[6]==0 + + /* Some versions of itunes set the parameter list length to 0. + * Technically it should be 0x0c, which is what libgpod sends */ + && cbw->command_block[6]==0 /* parameter list (3 bytes) */ && cbw->command_block[7]==0 - /* Some versions of itunes set the next byte to 0. Technically - * it should be 0x0c */ && (cbw->command_block[8]==0 || cbw->command_block[8]==0x0c) + && cbw->command_block[9]==0) receive_time(); break; -- cgit v1.2.3