# Put int variables into a time\_t type

**URL:** <https://forum.arduino.cc/t/put-int-variables-into-a-time_t-type/385755>\
**Category:** Programming\
**Created:** [May 13, 2016, 8:39pm UTC](https://forum.arduino.cc/t/put-int-variables-into-a-time_t-type/385755 "2016-05-13T20:39:07Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![RunThisCode](https://avatars.discourse-cdn.com/v4/letter/r/e56c9b/32.png) [@RunThisCode](https://forum.arduino.cc/u/RunThisCode)\
**Post date:** [May 13, 2016, 8:39pm UTC](https://forum.arduino.cc/t/put-int-variables-into-a-time_t-type/385755/1 "2016-05-13T20:39:07Z")

</div>

Hi! I need help! I've got a problem: I need to compare hours and minutes setted by a user whit ordinary hours and minutes, for an alarm. I've tried with many libraries and methods like \<Time.h\> and \<TimeLib.h\> but I don''t understand many things. For example, a "time\_t" type, need all fields defined? And, is possible take ints from a form for convert them in "time\_t" and compare it whit the current time of an RTC?

---

<div class="post-metadata">

**Author:** ![RunThisCode](https://avatars.discourse-cdn.com/v4/letter/r/e56c9b/32.png) [@RunThisCode](https://forum.arduino.cc/u/RunThisCode)\
**Post date:** [May 13, 2016, 9:05pm UTC](https://forum.arduino.cc/t/put-int-variables-into-a-time_t-type/385755/3 "2016-05-13T21:05:04Z")

</div>

Pratically, I need to convert variables "ore" and "minuti" (both ints) into a certain time type (respectively, "hours" and "minutes" of the type) to compare it with the incoming time. This is the part of code that take inputs from two components of an app called Blink which allows an "interface" between Android and Arduino:

```cpp
BLYNK_WRITE(V0){
  int i;
  for(i=1; i<=24; i++){
    if(param.asInt() == i){
      if(param.asInt() < 10){
      Serial.println("Ore: 0");
      Serial.println(i-1);
      ore = i-1;
      Serial.println(ore);
      }else{
        Serial.println("Ore: ");
        Serial.println(i-1);
        ore = i-1;
        Serial.println(ore);
      }
    }
  }
}

BLYNK_WRITE(V1){
  int i;
  for(i=1; i<=60; i++){
    if(param.asInt() == i){
      if(param.asInt() < 10){
      Serial.print("Minuti: 0");
      Serial.println(i-1);
      minuti = i-1;
      }else{
        Serial.println("Minuti: ");
        Serial.println(i-1);
        minuti = i-1;
      }
    }
  }
}

```

Into the app, I take hours and minutes that I need to compare to the real time for ring an alarm (sorry for my bad english...)

---

<div class="post-metadata">

**Author:** ![BulldogLowell](https://avatars.discourse-cdn.com/v4/letter/b/22d042/32.png) [@BulldogLowell](https://forum.arduino.cc/u/BulldogLowell)\
**Post date:** [May 13, 2016, 9:13pm UTC](https://forum.arduino.cc/t/put-int-variables-into-a-time_t-type/385755/4 "2016-05-13T21:13:55Z")

</div>

If you have some experience, I wrote a library that can accommodate what you are trying to do, and it may be easier for you to use.

Take a look at my Git Repo [DailyTimer library](https://github.com/BulldogLowell/DailyTimer), there is an example that you can try.

Anyone who wants to make a contribution is welcome; I've been using it for a while and it seems pretty stable.

example sketch:

```cpp
#include "DailyTimer.h"
#include <Time.h>

DailyTimer timer1( 14, 37, 14, 39, EVERY_DAY, FIXED, customSeedGenerator);  // optional callback function for random number generation, see below example
DailyTimer timer2( 14, 38, 20, 30, SATURDAY, RANDOM_START);                  // default is FIXED, this will randomize the start time only
DailyTimer timer3( 14, 45,  1, 30, WEEKENDS);                                // creates with a fixed start time and end time
bool timer1_LastState = false;

void setup() 
{
  setTime(1461422271);                        // set your sync provider!!
  Serial.begin(9600);
  pinMode(13, OUTPUT);
  timer1.setDaysActive(WEEKENDS);            // Set the timer to be active on weekends
  //timer1.setDaysActive(B10101010);          // or define a custom day mask... SMTWTFS0
  //timer1.setRandomDays(4);                  // or four random days per week
  //timer2.setRandomOffset(30, RANDOM_START); // Change to random start time, +/- 30 minutes... max 59 mins
  Serial.println(timer1.getDays(), BIN);      // getDays() returns active days as a byte in the format above
  Serial.print(F("Today is "));
  Serial.println(dayStr(weekday()));
}

uint32_t lastUpdateTime = 0;

void loop() 
{
  bool timerState = timer1.isActive();
  if(timerState != timer1_LastState)
  {
    if(timerState)
    {
      digitalWrite(13, HIGH);
      Serial.println(F("Timer 1 is ON"));
    }
    else
    {
      digitalWrite(13, LOW);
      Serial.println(F("Timer 1 is OFF"));
    }
    timer1_LastState = timerState;
  }
  if(timer2.startTrigger()) 
  {
    Serial.println(F("Timer 2 FIRED!"));
  }
  if(millis() - lastUpdateTime > 1000UL)
  {
    char timeBuffer[32] = "";
    sprintf(timeBuffer, "%02d/%02d/%d %02d:%02d:%02d" , month(), day(), year(), hour(), minute(), second());
    Serial.println(timeBuffer);
    lastUpdateTime += 1000UL;
  }
  if(timer3.startTrigger())
  {
    Serial.println(F("Timer 3 FIRED!"));
  }
}

uint32_t customSeedGenerator()
{
  uint32_t seed = analogRead(A5);
  delay(100);
  seed << 8;
  return  seed |= analogRead(A5);
}

```

---

<div class="post-metadata">

**Author:** ![RunThisCode](https://avatars.discourse-cdn.com/v4/letter/r/e56c9b/32.png) [@RunThisCode](https://forum.arduino.cc/u/RunThisCode)\
**Post date:** [May 13, 2016, 9:18pm UTC](https://forum.arduino.cc/t/put-int-variables-into-a-time_t-type/385755/5 "2016-05-13T21:18:41Z")

</div>

> BulldogLowell:  
> If you have some experience, I wrote a library that can accommodate what you are trying to do, and it may be easier for you to use.
> 
> Take a look at my Git Repo [DailyTimer library](https://github.com/BulldogLowell/DailyTimer), there is an example that you can try.
> 
> Anyone who wants to make a contribution is welcome; I've been using it for a while and it seems pretty stable.
> 
> example sketch:
> 
> ```arduino
> #include "DailyTimer.h"
> 
> ```

#include \<Time.h\>

DailyTimer timer1( 14, 37, 14, 39, EVERY\_DAY, FIXED, customSeedGenerator);&nbsp; // optional callback function for random number generation, see below example  
DailyTimer timer2( 14, 38, 20, 30, SATURDAY, RANDOM\_START);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // default is FIXED, this will randomize the start time only  
DailyTimer timer3( 14, 45,&nbsp; 1, 30, WEEKENDS);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // creates with a fixed start time and end time  
bool timer1\_LastState = false;

void setup()  
{  
&nbsp; setTime(1461422271);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // set your sync provider!!  
&nbsp; Serial.begin(9600);  
&nbsp; pinMode(13, OUTPUT);  
&nbsp; timer1.setDaysActive(WEEKENDS);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // Set the timer to be active on weekends  
&nbsp; [//timer1.setDaysActive](https://timer1.setDaysActive)(B10101010);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // or define a custom day mask... SMTWTFS0  
&nbsp; [//timer1.setRandomDays](https://timer1.setRandomDays)(4);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // or four random days per week  
&nbsp; [//timer2.setRandomOffset](https://timer2.setRandomOffset)(30, RANDOM\_START); // Change to random start time, +/- 30 minutes... max 59 mins  
&nbsp; Serial.println(timer1.getDays(), BIN);&nbsp; &nbsp; &nbsp; // getDays() returns active days as a byte in the format above  
&nbsp; Serial.print(F("Today is "));  
&nbsp; Serial.println(dayStr(weekday()));  
}

uint32\_t lastUpdateTime = 0;

void loop()  
{  
&nbsp; bool timerState = timer1.isActive();  
&nbsp; if(timerState != timer1\_LastState)  
&nbsp; {  
&nbsp; &nbsp; if(timerState)  
&nbsp; &nbsp; {  
&nbsp; &nbsp; &nbsp; digitalWrite(13, HIGH);  
&nbsp; &nbsp; &nbsp; Serial.println(F("Timer 1 is ON"));  
&nbsp; &nbsp; }  
&nbsp; &nbsp; else  
&nbsp; &nbsp; {  
&nbsp; &nbsp; &nbsp; digitalWrite(13, LOW);  
&nbsp; &nbsp; &nbsp; Serial.println(F("Timer 1 is OFF"));  
&nbsp; &nbsp; }  
&nbsp; &nbsp; timer1\_LastState = timerState;  
&nbsp; }  
&nbsp; if(timer2.startTrigger())  
&nbsp; {  
&nbsp; &nbsp; Serial.println(F("Timer 2 FIRED!"));  
&nbsp; }  
&nbsp; if(millis() - lastUpdateTime \> 1000UL)  
&nbsp; {  
&nbsp; &nbsp; char timeBuffer[32] = "";  
&nbsp; &nbsp; sprintf(timeBuffer, "%02d/%02d/%d %02d:%02d:%02d" , month(), day(), year(), hour(), minute(), second());  
&nbsp; &nbsp; Serial.println(timeBuffer);  
&nbsp; &nbsp; lastUpdateTime += 1000UL;  
&nbsp; }  
&nbsp; if(timer3.startTrigger())  
&nbsp; {  
&nbsp; &nbsp; Serial.println(F("Timer 3 FIRED!"));  
&nbsp; }  
}

uint32\_t customSeedGenerator()  
{  
&nbsp; uint32\_t seed = analogRead(A5);  
&nbsp; delay(100);  
&nbsp; seed \<\< 8;  
&nbsp; return&nbsp; seed |= analogRead(A5);  
}

> ```arduino
> 
> ```

Wow! Awesome! I'll try it and let you know!

---

<div class="post-metadata">

**Author:** ![RunThisCode](https://avatars.discourse-cdn.com/v4/letter/r/e56c9b/32.png) [@RunThisCode](https://forum.arduino.cc/u/RunThisCode)\
**Post date:** [May 13, 2016, 9:54pm UTC](https://forum.arduino.cc/t/put-int-variables-into-a-time_t-type/385755/7 "2016-05-13T21:54:22Z")

</div>

> Delta\_G:  
> Use the Time library. Make a tmElements and use makeTime to put that into a time\_t.

Could you, please, let me see? I'm in big troubles! I've tried to make it like this:

```arduino
if(ore == NULL){
  t.tm_year = year();
  t.tm_mon = month();
  t.tm_mday = day();
  t.tm_hour = ore;  
  t.tm_min = minuti;   
  t.tm_sec = second();   
  t.tm_isdst = 0;

  sveglia = mktime(&t);
  }

```

Where "ore" and "minuti" are ints taken by Blink application

---

<div class="post-metadata">

**Author:** ![KeithRB](https://avatars.discourse-cdn.com/v4/letter/k/74df32/32.png) [@KeithRB](https://forum.arduino.cc/u/KeithRB)\
**Post date:** [May 13, 2016, 10:04pm UTC](https://forum.arduino.cc/t/put-int-variables-into-a-time_t-type/385755/8 "2016-05-13T22:04:23Z")

</div>

if ore is an int why are you comparing it to a NULL pointer?  
And surely you mean !=

---

<div class="post-metadata">

**Author:** ![RunThisCode](https://avatars.discourse-cdn.com/v4/letter/r/e56c9b/32.png) [@RunThisCode](https://forum.arduino.cc/u/RunThisCode)\
**Post date:** [May 13, 2016, 10:19pm UTC](https://forum.arduino.cc/t/put-int-variables-into-a-time_t-type/385755/9 "2016-05-13T22:19:44Z")

</div>

> KeithRB:  
> if ore is an int why are you comparing it to a NULL pointer?  
> And surely you mean !=

Because in globals I set them NULL and so, if they were NULL because the Blink app doesn't put nothing into that ints, I'll make a control (if != NULL) which doesn't assign nothing.

Maybe, all the code, help to understand what I mean:

```arduino
/*-------------------LIBRERIE----------------------------------*/
#define SENSIBILITA 200
#define BLYNK_PRINT Serial    // Comment this out to disable prints and save space
#include <SPI.h>
#include <WiFi.h>
#include <BlynkSimpleWifi.h>
#include <SimpleTimer.h>
#include <TimeLib.h>
#include <Time.h>
#include <DailyTimer.h>
#include <WidgetRTC.h>
#include <TimeAlarms.h>
#include "pitches.h"

/*--------------------GLOBALI---------------------------------*/
char auth[] = " ********";

char ssid[] = " *******";
char pass[] = " *******";          // Set to "" for open networks
IPAddress ip (45,55,195,102);

int SHIR = A5;

//SimpleTimer timer;

WidgetRTC rtc;
BLYNK_ATTACH_WIDGET(rtc, V4);

int ore = NULL;
int minuti = NULL;

bool hosuonato = false;
int dist;

BLYNK_WRITE(V0){
  int i;
  for(i=1; i<=24; i++){
    if(param.asInt() == i){
      if(param.asInt() < 10){
      Serial.println("Ore: 0");
      Serial.println(i-1);
      ore = i-1;
      Serial.println(ore);
      }else{
        Serial.println("Ore: ");
        Serial.println(i-1);
        ore = i-1;
        Serial.println(ore);
      }
    }
  }
}

BLYNK_WRITE(V1){
  int i;
  for(i=1; i<=60; i++){
    if(param.asInt() == i){
      if(param.asInt() < 10){
      Serial.print("Minuti: 0");
      Serial.println(i-1);
      minuti = i-1;
      }else{
        Serial.println("Minuti: ");
        Serial.println(i-1);
        minuti = i-1;
      }
    }
  }
}

int melody[] = {
  NOTE_C4, NOTE_G3, NOTE_G3, NOTE_A3, NOTE_G3, 0, NOTE_B3, NOTE_C4
};

int noteDurations[] = {
  4, 8, 8, 4, 4, 4, 4, 4
};

void printDigits(int digits)
{
  Serial.print(":");
  if(digits < 10) {
    Serial.print('0');
  }
  Serial.print(digits);
}

void clockDisplay()
{
  // You can call hour(), minute(), ... at any time
  // Please see Time library examples for details
  Serial.print("Current time: ");
  Serial.print(hour());
  printDigits(minute());
  printDigits(second());
  Serial.print(" ");
  Serial.print(day());
  Serial.print(" ");
  Serial.print(month());
  Serial.print(" ");
  Serial.print(year());
  Serial.println();
}

void suona()
{
  delay(500);
        for (int thisNote = 0; thisNote < 8; thisNote++) {
        int noteDuration = 1000 / noteDurations[thisNote];
        tone(8, melody[thisNote], noteDuration);
        int pauseBetweenNotes = noteDuration * 1.30;
        delay(pauseBetweenNotes);
        noTone(8);
      }
}

void sveglia()
{
  int dist = analogRead(SHIR);
  if(hosuonato == false){
    Serial.println(dist);
    suona();
    if(dist > SENSIBILITA){
      Serial.println(dist);
      hosuonato = true;
    }
  }
}
/*------------------SETUP-------------------------------------*/
void setup()
{
  Serial.begin(9600);
  Blynk.begin(auth, ssid, pass, ip, 8442);
  pinMode( SHIR,INPUT );

  while (Blynk.connect() == false) {
    // Wait until connected
  }
  rtc.begin();
  //Alarm.alarmRepeat(ore,minuti,0, suona);
}
  
/*---------------------LOOP--------------------------------------*/
void loop()
{ 
  time_t sveglia;
  struct tm t;
  
  if(ore == NULL){
  t.tm_year = year();
  t.tm_mon = month();
  t.tm_mday = day();
  t.tm_hour = ore;  
  t.tm_min = minuti;   
  t.tm_sec = second();   
  t.tm_isdst = 0;

  sveglia = mktime(&t);
  }
  
  Blynk.run();
  /*clockDisplay();
  Alarm.delay(1000);*/
  sveglia();
  
  //timer.run();
}

```

---

<div class="post-metadata">

**Author:** ![anon57585045](https://avatars.discourse-cdn.com/v4/letter/a/ed8c4c/32.png) [@anon57585045](https://forum.arduino.cc/u/anon57585045)\
**Post date:** [May 14, 2016, 3:12am UTC](https://forum.arduino.cc/t/put-int-variables-into-a-time_t-type/385755/10 "2016-05-14T03:12:30Z")

</div>

That isn't your real Wifi login, is it? 🙂

---

<div class="post-metadata">

**Author:** ![RunThisCode](https://avatars.discourse-cdn.com/v4/letter/r/e56c9b/32.png) [@RunThisCode](https://forum.arduino.cc/u/RunThisCode)\
**Post date:** [May 14, 2016, 5:19am UTC](https://forum.arduino.cc/t/put-int-variables-into-a-time_t-type/385755/11 "2016-05-14T05:19:20Z")

</div>

> aarg:  
> That isn't your real Wifi login, is it? 🙂

Aaaah... Damn, I forgot. But I stay in south Italy! If someone come here and want to use it, is welcome! 😃

---

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/system/32/1140315_2.png) [@system](https://forum.arduino.cc/u/system)\
**Post date:** [May 5, 2021, 9:53pm UTC](https://forum.arduino.cc/t/put-int-variables-into-a-time_t-type/385755/12 "2021-05-05T21:53:42Z")

</div>


