Diferencia entre revisiones de «DateTime»
De ArduWiki
(→Descripcion) |
(→Ejemplo) |
||
Línea 24: | Línea 24: | ||
== Ejemplo == | == Ejemplo == | ||
<syntaxhighlight lang="c++"> | <syntaxhighlight lang="c++"> | ||
+ | #include <Wire.h> | ||
+ | #include <RTClib.h> | ||
+ | |||
+ | RTC_Millis rtc; | ||
+ | char daysOfTheWeek[7][12] = {"Domingo", "Lunes", "Martes", "Miercoles", "Jueves", "Viernes", "Sabado"}; | ||
+ | |||
+ | void setup () { | ||
+ | Serial.begin(57600); | ||
+ | //Para setear RTC con fecha y hora cuando se compila | ||
+ | rtc.adjust(DateTime(F(__DATE__), F(__TIME__))); | ||
+ | //Para setear RTC a 21-MAR-2019 8:00:00 | ||
+ | //rtc.adjust(DateTime(2019, 3, 21, 8, 0, 0)); | ||
+ | } | ||
+ | |||
+ | void loop () { | ||
+ | DateTime now = rtc.now(); | ||
+ | |||
+ | Serial.print(now.year()); | ||
+ | Serial.print('/'); | ||
+ | Serial.print(now.month()); | ||
+ | Serial.print('/'); | ||
+ | Serial.print(now.day()); | ||
+ | Serial.print(" ("); | ||
+ | Serial.print(daysOfTheWeek[now.dayOfTheWeek()]); | ||
+ | Serial.print(") "); | ||
+ | Serial.print(now.hour()); | ||
+ | Serial.print(':'); | ||
+ | Serial.print(now.minute()); | ||
+ | Serial.print(':'); | ||
+ | Serial.println(now.second()); | ||
+ | |||
+ | Serial.print("Desde 1-ENE-1970 = "); | ||
+ | |||
+ | Serial.print("Segundos = "); | ||
+ | Serial.println(now.unixtime()); | ||
+ | |||
+ | Serial.print("Dias = "); | ||
+ | Serial.println(now.unixtime() / 86400L); | ||
+ | |||
+ | //calculate a date which is 7 days and 30 seconds into the future | ||
+ | DateTime future (now + TimeSpan(7,12,30,6)); | ||
+ | |||
+ | Serial.print(" now + 7d + 30s: "); | ||
+ | Serial.print(future.year()); | ||
+ | Serial.print('-'); | ||
+ | Serial.print(future.month()); | ||
+ | Serial.print('-'); | ||
+ | Serial.print(future.day()); | ||
+ | Serial.print(' '); | ||
+ | Serial.print(future.hour()); | ||
+ | Serial.print(':'); | ||
+ | Serial.print(future.minute()); | ||
+ | Serial.print(':'); | ||
+ | Serial.print(future.second()); | ||
+ | Serial.println(); | ||
+ | |||
+ | Serial.println(); | ||
+ | delay(3000); | ||
+ | } | ||
</syntaxhighlight> | </syntaxhighlight> | ||
Revisión del 00:35 23 mar 2019
Contenido
Descripción
La libreria RTClib.h agrega la funcionanlidad de reloj al Arduino, con o sin hardware externo. Permite que un boceto obtenga la fecha y hora actual. También proporciona tiempo estandar DateTime.
Sintaxis
DateTime variable (año,mes,dia,hora,min,seg);
Parametros
- variable
- año
- 4 digitos del año.
- mes
- 2 digitos del mes entre 1~12.
- dia
- 2 digitos del dia entre 1~31.
- hora
- 2 digitos de la hora entre 0~23.
- min
- 2 digitos de los minutos entre 0~59.
- seg
- 2 digitos de los segundos entre 0~59.
Retorna
Nada.
Advertencias
Nada.
Ejemplo
#include <Wire.h>
#include <RTClib.h>
RTC_Millis rtc;
char daysOfTheWeek[7][12] = {"Domingo", "Lunes", "Martes", "Miercoles", "Jueves", "Viernes", "Sabado"};
void setup () {
Serial.begin(57600);
//Para setear RTC con fecha y hora cuando se compila
rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
//Para setear RTC a 21-MAR-2019 8:00:00
//rtc.adjust(DateTime(2019, 3, 21, 8, 0, 0));
}
void loop () {
DateTime now = rtc.now();
Serial.print(now.year());
Serial.print('/');
Serial.print(now.month());
Serial.print('/');
Serial.print(now.day());
Serial.print(" (");
Serial.print(daysOfTheWeek[now.dayOfTheWeek()]);
Serial.print(") ");
Serial.print(now.hour());
Serial.print(':');
Serial.print(now.minute());
Serial.print(':');
Serial.println(now.second());
Serial.print("Desde 1-ENE-1970 = ");
Serial.print("Segundos = ");
Serial.println(now.unixtime());
Serial.print("Dias = ");
Serial.println(now.unixtime() / 86400L);
//calculate a date which is 7 days and 30 seconds into the future
DateTime future (now + TimeSpan(7,12,30,6));
Serial.print(" now + 7d + 30s: ");
Serial.print(future.year());
Serial.print('-');
Serial.print(future.month());
Serial.print('-');
Serial.print(future.day());
Serial.print(' ');
Serial.print(future.hour());
Serial.print(':');
Serial.print(future.minute());
Serial.print(':');
Serial.print(future.second());
Serial.println();
Serial.println();
delay(3000);
}
Vea también
- Fecha y hora
- time_t - TimeLib.h
- tmElements_t - TimeLib.h