Library for DS1307 Real Time Clock

Rest of "ds1337.cpp":

void DS1337::clockSetWithUTS(uint32_t unixTimeStamp, boolean correctedTime)
{
      uint16_t      leapCorrection = 0;
      uint32_t      tt;
      uint8_t            thisDate;
      int                  ii;
#if defined(RTC_DST_TYPE)
      uint16_t      thisYear;
#endif
      uint16_t      year;
      const uint16_t      monthcount[] = {0, 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365};
      
      /**
       * Calculate GMT and DST
      **/      
#if defined(RTC_GMT_OFFSET)
      if (!correctedTime) unixTimeStamp = unixTimeStamp + (RTC_GMT_OFFSET * 3600);
#endif

      // Years
      tt                  = unixTimeStamp / 3600 / 24 / 365;
      year            = tt + 1970;
      
#if defined(RTC_DST_TYPE)
      if (!correctedTime) {
            thisYear      = year;
      }
#endif
      
      // Set the century bit
      if (tt > 30) {
            rtc_bcd[DS1337_MTH]      = (rtc_bcd[DS1337_MTH] | DS1337_LO_CNTY);
            tt-= 30;
      } else {
            rtc_bcd[DS1337_MTH]      = (rtc_bcd[DS1337_MTH] & ~DS1337_LO_CNTY);
      }
      
      // Set the year
      rtc_bcd[DS1337_YR]            = binToBcd(tt);
      
      // Number of days left in the year
      tt = (unixTimeStamp%31536000 / 3600 / 24) + 1;
      
      // leap year correction
      for (year--; year > 1970; year--) {
            if (isleap(year))
            {
                  leapCorrection++;
                  tt--;
            }
      }
      
      // Set the month
      for (ii = 1; ii < 12; ii++)
      {
            if (monthcount[ii+1] > (tt + ((ii == 2 && isleap(thisYear)) * 1)))
            {
                  rtc_bcd[DS1337_MTH]            = (binToBcd(ii) & ~DS1337_LO_CNTY) | (rtc_bcd[DS1337_MTH] & DS1337_LO_CNTY);
                  break;
            }
      }
      
      // Date
#if defined(RTC_DST_TYPE)
      if (!correctedTime) {
            thisDate = tt - monthcount[ii];
      }
#endif
      
      rtc_bcd[DS1337_DATE]      = binToBcd(tt - monthcount[ii]);
      
      // Day of the week
      rtc_bcd[DS1337_DOW]            = ((tt)%7 + 1) & DS1337_LO_DOW;
      
      // Hour
      tt = unixTimeStamp%86400 / 3600;
      rtc_bcd[DS1337_HR]            = binToBcd(tt);
      
#if defined(RTC_DST_TYPE)
      if (!correctedTime) {
            uint8_t dstStartMo, dstStopMo, dstStart, dstStop;
            
      #ifndef RTC_CHECK_OLD_DST
            dstStart      =  (31-((thisYear * 5 / 4) + 1) % 7);
      #if RTC_DST_TYPE == 1
             dstStop            =  (31-((thisYear * 5 / 4) + 1) % 7);      // EU DST
      #else
            dstStop            = 7 - ((1 + thisYear * 5 / 4) % 7);            // US DST
      #endif
            dstStartMo      = 3;
            dstStopMo      = 11;
      #else
            if (thisYear < 2006) {
                  dstStart      = (2+6 * thisYear - (thisYear / 4) ) % 7 + 1;
                  dstStop            = 14 - ((1 + thisYear * 5 / 4) % 7);
                  dstStartMo      = 4;
                  dstStopMo      = 10;
            } else {
                  dstStart      =  (31-((thisYear * 5 / 4) + 1) % 7);
            #if RTC_DST_TYPE == 1
                   dstStop            =  (31-((thisYear * 5 / 4) + 1) % 7);      // EU DST
            #else
                  dstStop            = 7 - ((1 + thisYear * 5 / 4) % 7);            // US DST
            #endif
                  dstStartMo      = 3;
                  dstStopMo      = 11;
            }
      #endif
            if (ii >= dstStartMo && ii <= dstStopMo)
            {
                  if (ii < dstStopMo)
                  {
                        if (ii > dstStartMo || thisDate > dstStart || thisDate == dstStart && tt >= 2)
                        {
                              clockSetWithUTS(unixTimeStamp + 3600, true);
                              return;
                        }
                  } else {
                        if (thisDate < dstStop || thisDate == dstStop && tt < 2)
                        {
                              clockSetWithUTS(unixTimeStamp + 3600, true);
                              return;
                        }
                  }
            }
      }
#endif
      
      // Minutes
      tt = unixTimeStamp%3600 / 60;
      rtc_bcd[DS1337_MIN]            = binToBcd(tt);
      
      // Seconds
      tt = (unixTimeStamp%3600)%60;
      rtc_bcd[DS1337_SEC]            = binToBcd(tt);
      
      // Stop the clock
      //clockStop();
      
      // Save buffer to the RTC
      clockSave();
      
      // Restart the oscillator
      //clockStart();
}

void DS1337::printRegisters(void)
{
      for(int ii=0;ii<0x10;ii++)
      {
            SPrint("0x");
            Serial.print(ii, HEX);
            SPrint(" ");
            Serial.println(getRegister(ii), BIN);
      }

      delay(200);
}

EDIT: No need to stop/start the oscillator when reading or writing to the ds1337 as the IC as in internal buffer and it will read/write only when all of the memory is read for the operation. Also, if the scan mod is used the clock is always started when the address is found and the osc. is stopped.