Ejemplo n.º 1
0
bool SetTimeHelper::changeTimeToCompileTimeIfNeeded() {
    time_t tt_now = makeTime(currentTime);
    time_t tt_compiled = makeTime(compiledAt);
    if (tt_now < tt_compiled) {
        setTimeToCompileTime();
        return true;
    }

    return false;
}
Ejemplo n.º 2
0
bool DS1374RTC::setAlarm(tmElements_t &tm)
{
  timeDate_t	timeDate;
  
  // convert to 32-bit raw time
  timeDate.time = (time_t)makeTime(tm);

  Wire.beginTransmission(DS1374_CTRL_ID);
#if ARDUINO >= 100  
  Wire.write((uint8_t)0x04);		 // reset register pointer to start of alarm 
  Wire.write(timeDate.bytes[0]);	 // Write Alarm byte 0	 
  Wire.write(timeDate.bytes[1]);	 // Write Alarm byte 1
  Wire.write(timeDate.bytes[2]);	 // Write Alarm byte 2
#else  
  Wire.send(0x04);					// reset register pointer to start of alarm   
  Wire.send(timeDate.bytes[0]);		// Write TOD byte 1
  Wire.send(timeDate.bytes[1]);		// Write TOD byte 2
  Wire.send(timeDate.bytes[2]);		// Write TOD byte 3
#endif
  if (Wire.endTransmission() != 0) {
    exists = false;
    return false;
  }
  exists = true;
  return true;
}
Ejemplo n.º 3
0
time_t MSFTime::getTime()
{
  tmElements_t tm;

  if( mFixMillis == 0 )
  {
    #ifdef DEBUG
    //Serial.println("getTime - no fix");
    #endif
    return (time_t) 0; // not got a fix
  }

  tm.Year = mFixYear + 2000 - 1970;  // convert from MSF's years since 2000 into Time's years since 1970
  tm.Month = mFixMonth;              // 1-12
  tm.Day = mFixDayOfMonth;           // 1-31
  tm.Hour = mFixHour;                // 0-23
  tm.Minute = mFixMinute;            // 0-59
  tm.Second = 0;                     // 0-59

  time_t time = makeTime(tm) + (millis() - mFixMillis) / 1000; // add the time at the last fix to the interval since the last fix

  #ifdef DEBUG
  Serial.println("getTime has time");
  Serial.println(time);
  #endif

  return time;
}
Ejemplo n.º 4
0
// PUBLIC FUNCTIONS
time_t DS1307RTC::get()   // Aquire data from buffer and convert to time_t
{
	tmElements_t tm;
	if (read(tm) == false)
		return 0;
	return (makeTime(tm));
}
Ejemplo n.º 5
0
/*----------------------------------------------------------------------*
 * Read the current time from the RTC and return it as a time_t value.  *
 * Returns a zero value if an I2C error occurred (e.g. RTC              *
 * not present).                                                        *
 *----------------------------------------------------------------------*/
time_t DS3232RTC::get()
{
    tmElements_t tm;
    
    if ( read(tm) ) return 0;
    return( makeTime(tm) );
}
Ejemplo n.º 6
0
/*----------------------------------------------------------------------*
 * Convert the given DST change rule to a time_t value                  *
 * for the given year.                                                  *
 *----------------------------------------------------------------------*/
time_t Timezone::toTime_t(TimeChangeRule r, int yr)
{
    tmElements_t tm;
    time_t t;
    uint8_t m, w;            //temp copies of r.month and r.week

    m = r.month;
    w = r.week;
    if (w == 0) {            //Last week = 0
        if (++m > 12) {      //for "Last", go to the next month
            m = 1;
            yr++;
        }
        w = 1;               //and treat as first week of next month, subtract 7 days later
    }

    tm.Hour = r.hour;
    tm.Minute = 0;
    tm.Second = 0;
    tm.Day = 1;
    tm.Month = m;
    tm.Year = yr - 1970;
    t = makeTime(tm);        //first day of the month, or first day of next month for "Last" rules
    t += (7 * (w - 1) + (r.dow - weekday(t) + 7) % 7) * SECS_PER_DAY;
    if (r.week == 0) t -= 7 * SECS_PER_DAY;    //back up a week if this is a "Last" rule
    return t;
}
Ejemplo n.º 7
0
// 0x00
enum ab1815_status ab1815_get_timet(struct ab1815_t *clock, time_t *time)
{
		struct tmElements_t time_el;
		enum ab1815_status to_ret = ab1815_get_time(clock, &time_el);
		*time = makeTime(&time_el);
		return to_ret;
};
Ejemplo n.º 8
0
// PUBLIC FUNCTIONS
bool DS1374RTC::readAlarm(time_t &t)
{
  // TODO get rid of the unnecessary time conversions here
  tmElements_t tm;
  if (readAlarm(tm) == false) return false;
  t = makeTime(tm);
  return true;
}
Ejemplo n.º 9
0
time_t timeToSecondsFromNow(tmElements_t *t_then)
{
    time_t t, then;

    t = now();
    then = makeTime((*t_then));

    return (time_t) (then - t);
}
Ejemplo n.º 10
0
Astro::Astro(void)
{
  TimeElements j2k_tm={0, 0, 12, 1, 1, 1, 30}; /* 1 Jan 2000 */
  j2k_t = makeTime(j2k_tm);
  /* I'm still cheating here. Should save these in NVM */
  LAT = -25.75 * M_PI / 180.0;
  LON = 28.19 * M_PI / 180.0;
  TZone = 2;
}
Ejemplo n.º 11
0
// PUBLIC FUNCTIONS
time_t DS1307RTC::get()   // Aquire data from buffer and convert to time_t
{
  #ifdef __STM32F1__
    DS1307RTC_INIT_WIRE();
  #endif
  tmElements_t tm;
  if (read(tm) == false) return 0;
  return(makeTime(tm));
}
Ejemplo n.º 12
0
	char *convert_date(char *twitter_date, int utc_offset)
	{
		// Twitter date format: Sat, 12 Jan 2012 23:34:45 +0000
		//                      0123456789012345678901234567890
		//                      0         1         2         3
		
		// Format in other parts of API: Sat Jan 12 23:34:45 +0000 2012
		// Apparently not in search API, however.
		
		// Extract year.
		unsigned int year = strtol(twitter_date + 12, NULL, 10);

		// Extract abbreviated name of month.
		char *month_name = (char *) calloc(3 + 1, sizeof(char));
		memcpy(month_name, twitter_date + 8, 3 * sizeof(char));
		
		// Calculate month's number.
		uint8_t month = 0;
		while (strcmp(month_name, month_names[month]) != 0 && month < 11) {
			++month;
		}
		++month; // Put date within interval 1-12 instead of 0-11.
		free(month_name);

		// Convert to time structure.
		TimeElements utc_date;
		utc_date.Year = year - 1970; // Offset from 1970 as required by TimeElements.
		utc_date.Month = month;
		utc_date.Day = strtol(twitter_date + 5, NULL, 10); // Extract day.
		utc_date.Hour = strtol(twitter_date + 17, NULL, 10); // Extract hour.
		utc_date.Minute = strtol(twitter_date + 20, NULL, 10); // Extract minute.
		utc_date.Second = 0;

		// Convert to timestamp and apply UTC offset.
		time_t utc_time = makeTime(utc_date);
		time_t other_time = utc_time + utc_offset;

		// Convert back into time structure.
		TimeElements other_date;
		breakTime(other_time, other_date);

		// Length of minimal format string:
		// "- d/m hh:mm"
		//  12345678901
		uint8_t result_length = 11;
		
		// Consider days and months with two digits.
		if (other_date.Day >= 10) { ++result_length; }
		if (other_date.Month >= 10) { ++result_length; }

		// Create the final string.
		char *result = (char *) calloc(result_length + 1, sizeof(char));
		sprintf(result, DATE_FORMAT, other_date.Day, other_date.Month, other_date.Hour, other_date.Minute);

		return result;
	}
Ejemplo n.º 13
0
bool SetTimeHelper::setTimeTo(tmElements_t tm, time_t offset) {
    time_t timeToSet = makeTime(tm);
    setTime(timeToSet);
#if TEENSYDUINO
    Teensy3Clock.set(timeToSet);
    return true;
#else
    return (bool) RTC.write(tm);
#endif
}
Ejemplo n.º 14
0
int Controller::calculateProgress()
{
  int progress = 0; // return variable
  time_t current = now(); // save current time
  tmElements_t elements; // create start time for comparsion
  breakTime(current, elements); // break time into elements
  elements.Second = 0; // set set start time
  elements.Hour = (int)*_times;
  elements.Minute = (int)*(_times+1);
  time_t start = makeTime(elements); // convert back to timestamp
  
  if (current > start) // if the cycle has started today
  {
    elements.Hour = (unsigned int)*(_times+2); // reuse elements to create stop time timestamp
    elements.Minute = (unsigned int)*(_times+3);
    
    time_t stop = makeTime(elements); // make the timestamp
    
    if (current < stop) {
      progress = (current-start)*100/(stop-start); // calculate progress
    }
    else
    {
      progress = 100;
    }
  }
  else
  {
    //breakTime(nextMidnight(current), elements); // lets make an elements object holding next day's date
    elements.Hour = (unsigned int)*(_times+4); // set the rewind time
    elements.Minute = (unsigned int)*(_times+5);
    
    time_t rewind = makeTime(elements); // make the timestamp
    
    if (current < rewind)
    {
      progress = 100;
    }
  }
  return progress;
}
Ejemplo n.º 15
0
    time_t LocalTime::asNative() const
    {
        struct tm ekT = {0};
        makeLocalTime(timeSinceEpoch.tv_sec, ekT);
        struct tm utcT = {0};
        time_t start = time(NULL);
        makeUTCTime(start, utcT);
        utcT.tm_isdst = ekT.tm_isdst;

        // Compute offset from GMT and localtime, including DST
        time_t offset = makeTime(&utcT) - start;
        return timeSinceEpoch.tv_sec - offset;
    }
Ejemplo n.º 16
0
 LocalTime::LocalTime(const int year, const int month, const int dayOfMonth, const int hour, const int min, const int sec)
     : Time(time(NULL), 0)
 {
     struct tm tmt;
     tmt.tm_year = year;
     tmt.tm_mon = month;
     tmt.tm_mday = dayOfMonth;
     tmt.tm_hour = hour;
     tmt.tm_min = min;
     tmt.tm_sec = sec;
     tmt.tm_isdst = -1;
     timeSinceEpoch.tv_sec = makeTime(&tmt);
 }
Ejemplo n.º 17
0
void J1ClockKit::Crown::save( tmElements_t &tm ) {
    this->cleanup();
    
    time_t t = makeTime( this->timeElements );
    this->timeElements.Wday = weekday( t );
    
    tm.Second = this->timeElements.Second;
    tm.Minute = this->timeElements.Minute;
    tm.Hour   = this->timeElements.Hour;
    tm.Wday   = this->timeElements.Wday;
    tm.Day    = this->timeElements.Day;
    tm.Month  = this->timeElements.Month;
    tm.Year   = this->timeElements.Year;
}
Ejemplo n.º 18
0
void J1ClockKit::Crown::load( tmElements_t &tm ) {
    this->timeElements.Second = tm.Second;
    this->timeElements.Minute = tm.Minute;
    this->timeElements.Hour   = tm.Hour;
    this->timeElements.Day    = tm.Day;
    this->timeElements.Month  = tm.Month;
    this->timeElements.Year   = tm.Year;
    
    time_t t = makeTime( tm );
    this->timeElements.Wday   = weekday( t );
    
    this->field = (tmByteFields) min( max( (int)this->field, (int)tmMinute ), (int)tmYear );
    this->prepare();
}
Ejemplo n.º 19
0
void setTime(int hr,int min,int sec,int dy, int mnth, int yr){
 // year can be given as full four digit year or two digts (2010 or 10 for 2010);  
 //it is converted to years since 1970
  if( yr > 99)
      yr = yr - 1970;
  else
      yr += 30;  
  tm.Year = yr;
  tm.Month = mnth;
  tm.Day = dy;
  tm.Hour = hr;
  tm.Minute = min;
  tm.Second = sec;
  setTime(makeTime(tm));
}
Ejemplo n.º 20
0
Alarm::Alarm()
{
	// fill in structure with current time
	time_t n = now();
	tmElements_t alarmSetting;
	breakTime(n, alarmSetting);

	// set alarm value to 00:00:00 (HH:MM:SS).
	alarmSetting.Hour   = 0;
	alarmSetting.Minute = 0;
	alarmSetting.Second = 0;

	setAlarmTime(makeTime(alarmSetting));
	disableAlarm();
}
Ejemplo n.º 21
0
void setRtcAlarm(byte hour, byte minute)
{	
	// copy time_t object, modify some fields, 
	// inherit month, day, dayOfWeek and Year.
	time_t justNow = now();
	tmElements_t alarmSetting;
	breakTime(justNow, alarmSetting);

	alarmSetting.Hour = hour;
	alarmSetting.Minute = minute;
	alarmSetting.Second = 0;
	wkAlarm.setAlarmTime(makeTime(alarmSetting));

	printAlarmStatus();
}
Ejemplo n.º 22
0
void Clock::set(int y,int m,int d,int h,int n,int s)
{
  tmElements_t tm;
  time_t t;
  
  tm.Hour = h;  
  tm.Minute = n;
  tm.Second = s;
  tm.Month = m;
  tm.Day = d;
  tm.Year = CalendarYrToTm(y); //TM.Year is a byte so can't take year > 255
  t = makeTime(tm);
  
  this->_rtc->set(t);   // set the RTC and the system time to the received value
  setTime(t); 
}
Ejemplo n.º 23
0
time_t getDS1302Time() {
	Time time;
	do {
		time = rtc.getTime();
		delay(100);
	} while (time.year < 2013);
	tmElements_t t;
	t.Day = time.date;
	t.Hour = time.hour;
	t.Minute = time.min;
	t.Month = time.mon;
	t.Second = time.sec;
	t.Wday = time.dow;
	t.Year = time.year - 1970;
	return makeTime(t);
}
Ejemplo n.º 24
0
/*
 * getTime
 */
time_t DS1339::getTime()   // Aquire data from buffer and convert to time_t
{
  byte buffer[7];
  tmElements_t tm;

   if(readBytes(buffer, 0, 7) == 7) {
     tm.Second = bcd2dec(buffer[0] & DS1339_SEC_MASK);
     tm.Minute = bcd2dec(buffer[1]);
     tm.Hour =  bcd2dec(buffer[2] & DS1339_HR_MASK);  // mask assumes 24hr clock
     tm.Wday = bcd2dec(buffer[3]);
     tm.Day = bcd2dec(buffer[4]);
     tm.Month = bcd2dec(buffer[5]);
     tm.Year = y2kYearToTm((bcd2dec(buffer[6])));
   }

  return(makeTime(tm));
}
Ejemplo n.º 25
0
QTime
FirebirdColumn::getTime()
{
    if (isNull()) return QTime();
    struct tm tm;

    switch (_var->sqltype & ~1) {
    case SQL_TYPE_TIME:
	_procs->isc_decode_sql_time((ISC_TIME*)_var->sqldata, &tm);
	tm.tm_year = 100;
	tm.tm_mon = 0;
	tm.tm_mday = 1;
	return makeTime(&tm);
    default:
	break;
    }

    qWarning("Sqlda::getTime: invalid type: %d", _var->sqltype);
    return QTime();
}
Ejemplo n.º 26
0
bool DS1374RTC::setTime(tmElements_t &tm)
{
  timeDate_t	timeDate;
  
  // convert to 32-bit raw time
  timeDate.time = (time_t)makeTime(tm);

  Wire.beginTransmission(DS1374_CTRL_ID);
#if ARDUINO >= 100  
  Wire.write((uint8_t)0x00);		 // reset register pointer  
  Wire.write(timeDate.bytes[0]);	 // Write TOD byte 0	 
  Wire.write(timeDate.bytes[1]);	 // Write TOD byte 1
  Wire.write(timeDate.bytes[2]);	 // Write TOD byte 2
  Wire.write(timeDate.bytes[3]);	 // Write TOD byte 3	  
//  Wire.write(0);			 // Write Alarm byte 0 - TODO Alarm
//  Wire.write(0);			 // Write Alarm byte 1
//  Wire.write(0);			 // Write Alarm byte 2 
//  Wire.write(0);			 // Write Control Register - TODO allow selection of options
#else  
  Wire.send(0x00);			// reset register pointer  
  Wire.send(timeDate.bytes[0]);		// Write TOD byte 1
  Wire.send(timeDate.bytes[1]);		// Write TOD byte 2
  Wire.send(timeDate.bytes[2]);		// Write TOD byte 3
  Wire.send(timeDate.bytes[3]);		// Write TOD byte 4	
//  Wire.send(0);				// Write Alarm byte 0 
//  Wire.send(0);				// Write Alarm byte 1	
//  Wire.send(0);				// Write Alarm byte 2
//  Wire.send(0);				// Write Control Register
 
#endif
  if (Wire.endTransmission() != 0) {
    exists = false;
    return false;
  }
  exists = true;
  return true;
}
Ejemplo n.º 27
0
// Function: Get current time
time_t SensorDs1307::getData()   // Aquire data from buffer and convert to time_t
{
  tmElements_t tm;
  if (read(tm) == false) return 0;
  return(makeTime(tm));
}
Ejemplo n.º 28
0
const t_aTime makeUT(const t_aTime &aTime)
{
    return makeTime(jd(aTime) - aTime.utcOffset / 3600.0 / 24.0, 0);
}
Ejemplo n.º 29
0
QTime
makeTime(time_t time)
{
    return makeTime(localtime(&time));
}
Ejemplo n.º 30
0
/*
    Parse the a date/time string and return the result in *time. Missing date items may be provided
    via the defaults argument. This is a tolerant parser. It is not validating and will do its best
    to parse any possible date string.
 */
PUBLIC int websParseDateTime(WebsTime *time, char *dateString, struct tm *defaults)
{
    TimeToken       *tt;
    struct tm       tm;
    char            *str, *next, *token, *cp, *sep;
    int64           value;
    int             kind, hour, min, negate, value1, value2, value3, alpha, alpha2, alpha3;
    int             dateSep, offset, zoneOffset, explicitZone, fullYear;

    if (!dateString) {
        dateString = "";
    }
    offset = 0;
    zoneOffset = 0;
    explicitZone = 0;
    sep = ", \t";
    cp = 0;
    next = 0;
    fullYear = 0;

    /*
        Set these mandatory values to -1 so we can tell if they are set to valid values
        WARNING: all the calculations use tm_year with origin 0, not 1900. It is fixed up below.
     */
    tm.tm_year = -MAXINT;
    tm.tm_mon = tm.tm_mday = tm.tm_hour = tm.tm_sec = tm.tm_min = tm.tm_wday = -1;
    tm.tm_min = tm.tm_sec = tm.tm_yday = -1;

#if ME_UNIX_LIKE && !CYGWIN
    tm.tm_gmtoff = 0;
    tm.tm_zone = 0;
#endif

    /*
        Set to -1 to try to determine if DST is in effect
     */
    tm.tm_isdst = -1;
    str = slower(dateString);

    /*
        Handle ISO dates: "2009-05-21t16:06:05.000z
     */
    if (strchr(str, ' ') == 0 && strchr(str, '-') && str[slen(str) - 1] == 'z') {
        for (cp = str; *cp; cp++) {
            if (*cp == '-') {
                *cp = '/';
            } else if (*cp == 't' && cp > str && isdigit((uchar) cp[-1]) && isdigit((uchar) cp[1]) ) {
                *cp = ' ';
            }
        }
    }
    token = stok(str, sep, &next);

    while (token && *token) {
        if (snumber(token)) {
            /*
                Parse either day of month or year. Priority to day of month. Format: <29> Jan <15> <2014>
             */
            value = atoi(token);
            if (value > 3000) {
                *time = value;
                return 0;
            } else if (value > 32 || (tm.tm_mday >= 0 && tm.tm_year == -MAXINT)) {
                if (value >= 1000) {
                    fullYear = 1;
                }
                tm.tm_year = (int) value - 1900;
            } else if (tm.tm_mday < 0) {
                tm.tm_mday = (int) value;
            }

        } else if ((*token == '+') || (*token == '-') ||
                ((strncmp(token, "gmt", 3) == 0 || strncmp(token, "utc", 3) == 0) &&
                ((cp = strchr(&token[3], '+')) != 0 || (cp = strchr(&token[3], '-')) != 0))) {
            /*
                Timezone. Format: [GMT|UTC][+-]NN[:]NN
             */
            if (!isalpha((uchar) *token)) {
                cp = token;
            }
            negate = *cp == '-' ? -1 : 1;
            cp++;
            hour = getNum(&cp, timeSep);
            if (hour >= 100) {
                hour /= 100;
            }
            min = getNum(&cp, timeSep);
            zoneOffset = negate * (hour * 60 + min);
            explicitZone = 1;

        } else if (isalpha((uchar) *token)) {
            if ((tt = (TimeToken*) hashLookupSymbol(timeTokens, token)) != 0) {
                kind = tt->value & TOKEN_MASK;
                value = tt->value & ~TOKEN_MASK;
                switch (kind) {

                case TOKEN_DAY:
                    tm.tm_wday = (int) value;
                    break;

                case TOKEN_MONTH:
                    tm.tm_mon = (int) value;
                    break;

                case TOKEN_OFFSET:
                    /* Named timezones or symbolic names like: tomorrow, yesterday, next week ... */
                    /* Units are seconds */
                    offset += (int) value;
                    break;

                case TOKEN_ZONE:
                    zoneOffset = (int) value;
                    explicitZone = 1;
                    break;

                default:
                    /* Just ignore unknown values */
                    break;
                }
            }

        } else if ((cp = strchr(token, timeSep)) != 0 && isdigit((uchar) token[0])) {
            /*
                Time:  10:52[:23]
                Must not parse GMT-07:30
             */
            tm.tm_hour = getNum(&token, timeSep);
            tm.tm_min = getNum(&token, timeSep);
            tm.tm_sec = getNum(&token, timeSep);

        } else {
            dateSep = '/';
            if (strchr(token, dateSep) == 0) {
                dateSep = '-';
                if (strchr(token, dateSep) == 0) {
                    dateSep = '.';
                    if (strchr(token, dateSep) == 0) {
                        dateSep = 0;
                    }
                }
            }
            if (dateSep) {
                /*
                    Date:  07/28/2014, 07/28/08, Jan/28/2014, Jaunuary-28-2014, 28-jan-2014
                    Support order: dd/mm/yy, mm/dd/yy and yyyy/mm/dd
                    Support separators "/", ".", "-"
                 */
                value1 = getNumOrSym(&token, dateSep, TOKEN_MONTH, &alpha);
                value2 = getNumOrSym(&token, dateSep, TOKEN_MONTH, &alpha2);
                value3 = getNumOrSym(&token, dateSep, TOKEN_MONTH, &alpha3);

                if (value1 > 31) {
                    /* yy/mm/dd */
                    tm.tm_year = value1;
                    tm.tm_mon = value2;
                    tm.tm_mday = value3;

                } else if (value1 > 12 || alpha2) {
                    /*
                        dd/mm/yy
                        Cannot detect 01/02/03  This will be evaluated as Jan 2 2003 below.
                     */
                    tm.tm_mday = value1;
                    tm.tm_mon = value2;
                    tm.tm_year = value3;

                } else {
                    /*
                        The default to parse is mm/dd/yy unless the mm value is out of range
                     */
                    tm.tm_mon = value1;
                    tm.tm_mday = value2;
                    tm.tm_year = value3;
                }
            }
        }
        token = stok(NULL, sep, &next);
    }

    /*
        Y2K fix and rebias
     */
    if (0 <= tm.tm_year && tm.tm_year < 100 && !fullYear) {
        if (tm.tm_year < 50) {
            tm.tm_year += 2000;
        } else {
            tm.tm_year += 1900;
        }
    }
    if (tm.tm_year >= 1900) {
        tm.tm_year -= 1900;
    }

    /*
        Convert back to origin 0 for months
     */
    if (tm.tm_mon > 0) {
        tm.tm_mon--;
    }

    /*
        Validate and fill in missing items with defaults
     */
    validateTime(&tm, defaults);
    *time = makeTime(&tm);
    *time += -(zoneOffset * SEC_PER_MIN);
    *time += offset;
    return 0;
}