瀏覽代碼

Use tmElements_t struct (offset from 2000) to set and get time

tags/v1.2.0
Bertrand Lemasle 7 年之前
父節點
當前提交
84e09577ae
共有 3 個檔案被更改,包括 23 行新增26 行删除
  1. +4
    -4
      GpsTracker/Rtc.cpp
  2. +7
    -7
      GpsTracker/Time2.cpp
  3. +12
    -15
      GpsTracker/Time2.h

+ 4
- 4
GpsTracker/Rtc.cpp 查看文件

@@ -61,10 +61,10 @@ namespace rtc {

void getTime(tmElements_t &time) {
hardware::i2c::powerOn();
RTC.readTime();
RTC.readTime(time);
hardware::i2c::powerOff();

details::readTimeFromRegisters(time);
//details::readTimeFromRegisters(time);
VERBOSE_FORMAT("getTime", "%d/%d/%d %d:%d:%d", tmYearToCalendar(time.Year), time.Month, time.Day, time.Hour, time.Minute, time.Second);
}

@@ -76,10 +76,10 @@ namespace rtc {

void setTime(const tmElements_t &time) {
VERBOSE_FORMAT("setTime", "%d/%d/%d %d:%d:%d", tmYearToCalendar(time.Year), time.Month, time.Day, time.Hour, time.Minute, time.Second);
details::writeTimeToRegisters(time);
//details::writeTimeToRegisters(time);

hardware::i2c::powerOn();
RTC.writeTime();
RTC.writeTime(time);
hardware::i2c::powerOff();
}



+ 7
- 7
GpsTracker/Time2.cpp 查看文件

@@ -15,7 +15,7 @@
#define numberOfMinutes(_time_) ((_time_ / SECS_PER_MIN) % SECS_PER_MIN)
#define numberOfHours(_time_) (( _time_% SECS_PER_DAY) / SECS_PER_HOUR)
#define dayOfWeek(_time_) ((( _time_ / SECS_PER_DAY + 4) % DAYS_PER_WEEK)+1) // 1 = Sunday
#define elapsedDays(_time_) ( _time_ / SECS_PER_DAY) // this is number of days since Jan 1 1970
#define elapsedDays(_time_) ( _time_ / SECS_PER_DAY) // this is number of days since Jan 1 2000
#define elapsedSecsToday(_time_) (_time_ % SECS_PER_DAY) // the number of seconds since last midnight
// The following macros are used in calculating alarms and assume the clock is set to a date later than Jan 1 1971
// Always set the correct time before settting alarms
@@ -32,8 +32,8 @@
#define daysTotimestamp_t ((D)) ( (D) * SECS_PER_DAY) // fixed on Jul 22 2011
#define weeksTotimestamp_t ((W)) ( (W) * SECS_PER_WEEK)

// leap year calulator expects year argument as years offset from 1970
#define LEAP_YEAR(Y) ( ((1970+Y)>0) && !((1970+Y)%4) && ( ((1970+Y)%100) || !((1970+Y)%400) ) )
// leap year calulator expects year argument as years offset from 2000
#define LEAP_YEAR(Y) ( ((2000+Y)>0) && !((2000+Y)%4) && ( ((2000+Y)%100) || !((2000+Y)%400) ) )

static const uint8_t monthDays[] = { 31,28,31,30,31,30,31,31,30,31,30,31 }; // API starts months from 1, this array starts from 0

@@ -41,7 +41,7 @@ __attribute__((__optimize__("O2")))
void breakTime(const timestamp_t timeInput, tmElements_t &tm) {
// break the given timestamp_t into time components
// this is a more compact version of the C library localtime function
// note that year is offset from 1970 !!!
// note that year is offset from 2000 !!!

uint8_t year;
uint8_t month, monthLength;
@@ -62,7 +62,7 @@ void breakTime(const timestamp_t timeInput, tmElements_t &tm) {
while ((unsigned)(days += (LEAP_YEAR(year) ? 366 : 365)) <= time) {
year++;
}
tm.Year = year; // year is offset from 1970
tm.Year = year; // year is offset from 2000

days -= LEAP_YEAR(year) ? 366 : 365;
time -= days; // now it is days in this year, starting at 0
@@ -97,13 +97,13 @@ void breakTime(const timestamp_t timeInput, tmElements_t &tm) {
__attribute__((__optimize__("O2")))
timestamp_t makeTimestamp(const tmElements_t &tm) {
// assemble time elements into timestamp_t
// note year argument is offset from 1970 (see macros in time.h to convert to other formats)
// note year argument is offset from 2000 (see macros in time.h to convert to other formats)
// previous version used full four digit year (or digits since 2000),i.e. 2009 was 2009 or 9

int i;
uint32_t seconds;

// seconds from 1970 till 1 jan 00:00:00 of the given year
// seconds from 2000 till 1 jan 00:00:00 of the given year
seconds = tm.Year*(SECS_PER_DAY * 365);
for (i = 0; i < tm.Year; i++) {
if (LEAP_YEAR(i)) {


+ 12
- 15
GpsTracker/Time2.h 查看文件

@@ -1,26 +1,23 @@
#pragma once

#include <Arduino.h>
#include <Time3.h>


//convenience macros to convert to and from tm years
#define tmYearToCalendar(Y) ((Y) + 1970) // full four digit year
#define CalendarYrToTm(Y) ((Y) - 1970)
#define tmYearToY2k(Y) ((Y) - 30) // offset is from 2000
#define y2kYearToTm(Y) ((Y) + 30)

/*============================================================================*/

typedef unsigned long timestamp_t;
//typedef unsigned long timestamp_t;

typedef struct {
uint8_t Second;
uint8_t Minute;
uint8_t Hour;
//uint8_t Wday; // day of week, sunday is day 1
uint8_t Day;
uint8_t Month;
uint8_t Year; // offset from 1970;
} tmElements_t;
//typedef struct {
// uint8_t Second;
// uint8_t Minute;
// uint8_t Hour;
// //uint8_t Wday; // day of week, sunday is day 1
// uint8_t Day;
// uint8_t Month;
// uint8_t Year; // offset from 1970;
//} tmElements_t;

/* low level functions to convert to and from system time */
void breakTime(const timestamp_t time, tmElements_t &tm); // break timestamp_t into elements


Loading…
取消
儲存