1 2 Previous Next 24 Replies Latest reply on Nov 13, 2014 12:30 PM by Terrence_

    How to get current time

    Terrence_

      <edit>

      I have verified that the Edison board has the correct time.

      Does anyone know why the year, month and day methods of Time.h return dates in the 1970's?

      I would think they would read the time from the board and return the current datetime.

      <endedit?

       

       

       

       

      I have verified by using putty to check the time on the board,  It is getting set by ntp and shows 11/08/2014.

       

      When I use time.h in my sketch this returns 1970

       

      char cTime[22];

      Serial.println("2");

      sprintf(cTime, "%04d.%02d.%02d.%02d:%02d:%02d", year(), month(), day(), hour(), minute(), second());

       

      19700101:000759

        • 1. Re: How to get current time
          Intel_Peter

          Hello Terrence_,

           

          Let me try it out on my Edison to see if I can replicate your error.

           

          Regards,

                        Peter

          • 2. Re: How to get current time
            deium

            Hope this may be helpful.  I am assuming a sketch is using c;

             

            #include <time.h>

             

            void main() {

            time_t t;

            struct tm *ptr_time;

            char buffer[22];

            // get the local time

            time(&t);

            // returns localtime to a tm structure

            ptr_time = localtime(&t);

            // converts time to a specific string

            strfttime(buffer, 22, "%Y.%m.%d %H:%M:%S ", ptr_time);

            printf("%s\n",buffer);

            }

            • 3. Re: How to get current time
              Terrence_

              deium, thank you for that suggestion.

               

              I will try it out tonight and get back with you tomorrow.

              • 4. Re: How to get current time
                Intel_Peter

                Hello Terrence_,

                 

                Could you attach your code here? In order to I can check out what's happening.

                 

                Regards,

                            Peter

                • 5. Re: How to get current time
                  UST_

                  Hi Terrence_ ,

                   

                  I'm trying to use the Time and DS1307 provided on Arduino's playground, but I believe both of these are not meshing well with the Edison or maybe the new IDE.

                   

                  Over the next couple days I was going to try and play around with these libraries and compare them to RTClib ( https://github.com/jcw/rtclib ) which does work and provide the current time if you have a DS1307 hooked up to the Arduino-Edison breakout board.

                   

                  My question to you is if you already have a functioning Time library that is compatible in the Arduino-Edison environment and wouldn't mind posting it, as I'm inexperienced and this would save me a lot of time.

                   

                  Any insight would be appreciated.

                   

                  Cheers,

                   

                  UST

                  • 6. Re: How to get current time
                    Terrence_

                    UST, thanks for your interest in my problem.

                     

                    I am real new to c and c++ and arduino so I won't be much help.

                     

                    I have been using Time.h and time.h for some time and they seem to work  some time and then on the next build it will crash and I can't get it to work anymore.  I don't know what the variable it that causes the issue.

                     

                    I have been trying for weeks to simply get a datetime string YYYYMMDD:HHmmSS and am still struggling with this simple request.  Funny no one else seems to need this functionality using Arduino and Edison.

                     

                    Let me know if I can help further.

                    • 7. Re: How to get current time
                      deium

                      Terrence_, the format you are looking for is a string being return by a function in the format YYYYMMDD:HHMMSS ?

                      Are you programming in C/C++ via the Eclipse IDE provided by the iotdk ?

                      • 8. Re: How to get current time
                        Terrence_

                        deium thank you for your reply.

                         

                        I am using Arduino's c/c++ and the Edison board.

                        >>strfttime(buffer, 22, "%Y.%m.%d %H:%M:%S ", ptr_time);

                        unfortunately strfttime is not available in Arduino c.

                         

                        This works some of the time, but does not compile at other times.

                        #include <time.h>

                        char cTime[22];

                        sprintf(cTime, "%04d%02d%02d:%02d%02d%02d", year(), month(), day(), hour(), minute(), second());

                        When it does compile it return 19700101:010100 not 20141111:0532

                         

                        Here are the errors:

                         

                         

                         

                        fcntl.h:In file included from

                         

                        fast_gpio_common.c:from

                         

                        stat.h:91:21: error: field 'st_atim' has incomplete type

                         

                        :struct timespec st_atim; \* Time of last access. *\

                         

                        :^

                         

                        stat.h:92:21: error: field 'st_mtim' has incomplete type

                         

                        :struct timespec st_mtim; \* Time of last modification. *\

                         

                        :^

                         

                        stat.h:93:21: error: field 'st_ctim' has incomplete type

                         

                        :struct timespec st_ctim; \* Time of last status change. *\

                         

                        :^

                         

                        fcntl.h:In file included from

                         

                        variant.cpp:from

                         

                        stat.h:91:21: error: field 'st_atim' has incomplete type

                         

                        :struct timespec st_atim; \* Time of last access. *\

                         

                        :^

                         

                        stat.h:92:21: error: field 'st_mtim' has incomplete type

                         

                        :struct timespec st_mtim; \* Time of last modification. *\

                         

                        :^

                         

                        stat.h:93:21: error: field 'st_ctim' has incomplete type

                         

                        :struct timespec st_ctim; \* Time of last status change. *\

                         

                        :^

                         

                        stat.h:152:21: error: field 'st_atim' has incomplete type

                         

                        :struct timespec st_atim; \* Time of last access. *\

                         

                        :^

                         

                        stat.h:153:21: error: field 'st_mtim' has incomplete type

                         

                        :struct timespec st_mtim; \* Time of last modification. *\

                         

                        :^

                         

                        stat.h:154:21: error: field 'st_ctim' has incomplete type

                         

                        :struct timespec st_ctim; \* Time of last status change. *\

                         

                        :^

                         

                        Error compiling core

                        • 9. Re: How to get current time
                          deium

                          ok, I'll take another stab at this for you.  It formats properly to your YYYYMMDD:HHMMSS on mine.

                          in your file at the top,

                           

                          #include <time.h>

                           

                          in your function try

                          time_t t = time(NULL);

                          struct tm tm = *localtime(&t);

                          when your ready to print it

                          printf("%04d%02d%02d:%02d%02d%02d",tm.tm_year+1900,tm.tm_mon+1,tm.tm_mday,tm.tm_hour,tm.tm_min,tm.tm_sec);

                          • 10. Re: How to get current time
                            Terrence_

                            Peter, here is the code that produces 1970 dates.

                             

                            #include <time.h>

                             

                            void setup()

                            char cTime[22];

                             

                            sprintf(cTime, "%04d.%02d.%02d.%02d:%02d:%02d", year(), month(), day(), hour(), minute(), second());

                             

                            Serial.println(__DATE__);

                             

                            Serial.println(cTime);

                            }

                             

                            Here is the output:

                             

                            Nov 11 2014

                            1970.01.01.00:00:02

                             

                             

                             

                            If you can tell me how to format __DATE__ to YYYYMMDD:HHmmSS then I will be happy and can move on.

                             

                            Thank you for your help.

                            • 11. Re: How to get current time
                              Terrence_

                              Deium, here is the source code.

                              Thank you for your help.

                               

                              #include <Time.h>

                               

                              #include <WiFi.h>

                               

                              #include <Ethernet.h>

                               

                              byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };

                               

                              char ssid[] = ""// your network SSID (name)

                               

                              char pass[] = ""// your network password

                               

                              int status = WL_IDLE_STATUS// the Wifi radio's status

                               

                              const char *server = "";

                               

                              const char *table_name = "sensor1";

                               

                              const char *ams_key = "";

                               

                              EthernetClient client;

                               

                              char buffer[100];

                               

                              int led = 13;

                               

                              int blinkdelay = 125;

                               

                              volatile bool sendData = false;

                               

                              void setup()

                               

                              pinMode(led, OUTPUT);

                               

                              Serial.println("In Setup()");

                                

                              // attempt to connect to Wifi network:

                               

                              while (status != WL_CONNECTED)

                               

                              Serial.print("Attempting to connect to WPA SSID: ");

                               

                              // Connect to WPA/WPA2 network: 

                               

                              // wait 10 seconds for connection:

                               

                              // you're connected now, so print out the data:

                               

                              Serial.print("You're connected to the network: ");

                               

                              while (!Serial) {

                               

                              ; // wait for serial port to connect.

                               

                              Serial.println("WiFi");

                               

                              if (Ethernet.begin(mac) == 0)

                               

                              Serial.println("ethernet failed");

                               

                              for (;;);

                               

                              // give the Ethernet shield a second to initialize:

                               

                              attachInterrupt(8, MotionDetected, RISING);

                               

                              Serial.println("Ready for Motion...");

                                

                              void loop()

                               

                              if (sendData == true)

                               

                              sendData = false;

                               

                              delay(1000);  // Wait 1 second

                               

                              void MotionDetected()

                               

                              sendData = true;

                                

                              void send_request()

                               

                              Serial.println("\nconnecting...");

                               

                              if (client.connect(server, 80)) {

                               

                              Serial.print("sending ");

                               

                              // POST URI

                               

                              Serial.println("post");

                               

                              sprintf(buffer, "POST /tables/%s HTTP/1.1", table_name);

                                

                              // Host header

                               

                              Serial.println("host");

                               

                              sprintf(buffer, "Host: %s", server);

                                

                              // Azure Mobile Services application key

                               

                              Serial.println("x-zumo");

                               

                              sprintf(buffer, "X-ZUMO-APPLICATION: %s", ams_key);

                                

                              // JSON content type

                               

                              client.println("Content-Type: application/json");

                               

                              char cTime[22];

                               

                              sprintf(cTime, "%04d%02d%02d:%02d%02d%02d", year(), month(), day(), hour(), minute(), second());

                               

                              //sprintf(buffer, "{\"location\":\"28 Oak\", \"sublocation\":\"bed3\", \"activitytime\":\"%s\"}", cTime);

                               

                              sprintf(buffer, "{\"location\":\"38 Oak\", \"sublocation\":\"bed1\", \"activitytime\":\"%s\"}", cTime);

                               

                              client.print("Content-Length: ");

                                

                              // End of headers

                               

                              // Request body

                               

                              else {

                               

                              Serial.println("connection failed");

                                

                              void read_response()

                               

                              Serial.println("begin: read resonse");

                               

                              bool print = true;

                               

                              char c = ' ';

                               

                              while (client.available())

                                

                              // Print only until the first carriage return

                               

                              if (c == '\n')

                               

                              print = false;

                               

                              // if (print)

                               

                              Serial.print("\n");

                               

                              Serial.println("end: read resonse");

                                 

                              void end_request()

                                

                              void wait_response()

                               

                              while (!client.available())

                               

                              if (!client.connected())

                               

                              return;

                                

                              void printWifiData() {

                               

                              // print your WiFi shield's IP address:

                               

                              IPAddress ip = WiFi.localIP();

                               

                              Serial.print("IP Address: ");

                                

                              // print your MAC address:

                               

                              byte mac[6];

                               

                              Serial.print("MAC address: ");

                               

                              Serial.print(mac[5], HEX);

                               

                              Serial.print(":");

                               

                              Serial.print(mac[4], HEX);

                               

                              Serial.print(":");

                               

                              Serial.print(mac[3], HEX);

                               

                              Serial.print(":");

                               

                              Serial.print(mac[2], HEX);

                               

                              Serial.print(":");

                               

                              Serial.print(mac[1], HEX);

                               

                              Serial.print(":");

                               

                              Serial.println(mac[0], HEX);

                                 

                              void printCurrentNet() {

                               

                              // print the SSID of the network you're attached to:

                               

                              Serial.print("SSID: ");

                                

                              // print the MAC address of the router you're attached to:

                               

                              byte bssid[6];

                               

                              Serial.print("BSSID: ");

                               

                              Serial.print(bssid[5], HEX);

                               

                              Serial.print(":");

                               

                              Serial.print(bssid[4], HEX);

                               

                              Serial.print(":");

                               

                              Serial.print(bssid[3], HEX);

                               

                              Serial.print(":");

                               

                              Serial.print(bssid[2], HEX);

                               

                              Serial.print(":");

                               

                              Serial.print(bssid[1], HEX);

                               

                              Serial.print(":");

                               

                              Serial.println(bssid[0], HEX);

                               

                              // print the received signal strength:

                               

                              long rssi = WiFi.RSSI();

                               

                              Serial.print("signal strength (RSSI):");

                                

                              // print the encryption type:

                               

                              byte encryption = WiFi.encryptionType();

                               

                              Serial.print("Encryption Type:");

                               

                              Serial.println(encryption, HEX);

                                

                              void blink(int blinkDelay, int led)

                               

                              digitalWrite(led, HIGH);

                               

                              delay(blinkDelay);

                               

                              digitalWrite(led, LOW);

                               

                              delay(blinkDelay);

                               

                              digitalWrite(led, HIGH);

                               

                              delay(blinkDelay);

                               

                              digitalWrite(led, LOW);

                               

                              delay(blinkDelay);

                               

                              digitalWrite(led, HIGH);

                               

                              delay(blinkDelay);

                               

                              digitalWrite(led, LOW);

                               

                              delay(blinkDelay);

                               

                              • 12. Re: How to get current time
                                UST_

                                Hello Terrence,

                                 

                                Well currently I consistenly get a error with my Time.h library (which is shown below). So I'm curious as to what time.h and Time.h files you are using. If you could post them that would be great, or if you can post the links that you downloaded them from.

                                 

                                ----------------------------------------------------------------------------------------- ERROR -----------------------------------------------------------------------------------------

                                 

                                In file included from C:\Arduino_Edison_IDE/hardware/tools/edison/sysroots/core2-32-poky-linux/usr/include/sys/types.h:132:0,

                                                from C:\Arduino_Edison_IDE/hardware/tools/edison/sysroots/core2-32-poky-linux/usr/include/stdlib.h:314,

                                                from C:\Arduino_Edison_IDE\hardware\arduino\edison\cores\arduino/WString.h:26,

                                                from C:\Arduino_Edison_IDE\hardware\arduino\edison\cores\arduino/Print.h:26,

                                                from C:\Arduino_Edison_IDE\hardware\arduino\edison\cores\arduino/Stream.h:26,

                                                from C:\Arduino_Edison_IDE\hardware\arduino\edison\libraries\Wire/Wire.h:24,

                                                from Impulse_Counter_Clock.ino:1:

                                C:\Arduino_Edison_IDE\hardware\arduino\edison\libraries\Time/time.h:83:22: error: declaration of C function 'int hour(time_t)' conflicts with

                                int    hour(time_t t);    // the hour for the given time

                                                      ^

                                C:\Arduino_Edison_IDE\hardware\arduino\edison\libraries\Time/time.h:82:9: error: previous declaration 'int hour()' here

                                int    hour();            // the hour now

                                        ^

                                C:\Arduino_Edison_IDE\hardware\arduino\edison\libraries\Time/time.h:85:30: error: declaration of C function 'int hourFormat12(time_t)' conflicts with

                                int    hourFormat12(time_t t); // the hour for the given time in 12 hour format

                                                              ^

                                C:\Arduino_Edison_IDE\hardware\arduino\edison\libraries\Time/time.h:84:9: error: previous declaration 'int hourFormat12()' here

                                int    hourFormat12();    // the hour now in 12 hour format

                                        ^

                                C:\Arduino_Edison_IDE\hardware\arduino\edison\libraries\Time/time.h:87:22: error: declaration of C function 'uint8_t isAM(time_t)' conflicts with

                                uint8_t isAM(time_t t);    // returns true the given time is AM

                                                      ^

                                C:\Arduino_Edison_IDE\hardware\arduino\edison\libraries\Time/time.h:86:9: error: previous declaration 'uint8_t isAM()' here

                                uint8_t isAM();            // returns true if time now is AM

                                        ^

                                C:\Arduino_Edison_IDE\hardware\arduino\edison\libraries\Time/time.h:89:22: error: declaration of C function 'uint8_t isPM(time_t)' conflicts with

                                uint8_t isPM(time_t t);    // returns true the given time is PM

                                                      ^

                                C:\Arduino_Edison_IDE\hardware\arduino\edison\libraries\Time/time.h:88:9: error: previous declaration 'uint8_t isPM()' here

                                uint8_t isPM();            // returns true if time now is PM

                                        ^

                                C:\Arduino_Edison_IDE\hardware\arduino\edison\libraries\Time/time.h:91:24: error: declaration of C function 'int minute(time_t)' conflicts with

                                int    minute(time_t t);  // the minute for the given time

                                                        ^

                                C:\Arduino_Edison_IDE\hardware\arduino\edison\libraries\Time/time.h:90:9: error: previous declaration 'int minute()' here

                                int    minute();          // the minute now

                                        ^

                                C:\Arduino_Edison_IDE\hardware\arduino\edison\libraries\Time/time.h:93:24: error: declaration of C function 'int second(time_t)' conflicts with

                                int    second(time_t t);  // the second for the given time

                                                        ^

                                C:\Arduino_Edison_IDE\hardware\arduino\edison\libraries\Time/time.h:92:9: error: previous declaration 'int second()' here

                                int    second();          // the second now

                                        ^

                                C:\Arduino_Edison_IDE\hardware\arduino\edison\libraries\Time/time.h:95:21: error: declaration of C function 'int day(time_t)' conflicts with

                                int    day(time_t t);    // the day for the given time

                                                    ^

                                C:\Arduino_Edison_IDE\hardware\arduino\edison\libraries\Time/time.h:94:9: error: previous declaration 'int day()' here

                                int    day();            // the day now

                                        ^

                                C:\Arduino_Edison_IDE\hardware\arduino\edison\libraries\Time/time.h:97:25: error: declaration of C function 'int weekday(time_t)' conflicts with

                                int    weekday(time_t t); // the weekday for the given time

                                                        ^

                                C:\Arduino_Edison_IDE\hardware\arduino\edison\libraries\Time/time.h:96:9: error: previous declaration 'int weekday()' here

                                int    weekday();        // the weekday now (Sunday is day 1)

                                        ^

                                C:\Arduino_Edison_IDE\hardware\arduino\edison\libraries\Time/time.h:99:23: error: declaration of C function 'int month(time_t)' conflicts with

                                int    month(time_t t);  // the month for the given time

                                                      ^

                                C:\Arduino_Edison_IDE\hardware\arduino\edison\libraries\Time/time.h:98:9: error: previous declaration 'int month()' here

                                int    month();          // the month now  (Jan is month 1)

                                        ^

                                C:\Arduino_Edison_IDE\hardware\arduino\edison\libraries\Time/time.h:101:22: error: declaration of C function 'int year(time_t)' conflicts with

                                int    year(time_t t);    // the year for the given time

                                                      ^

                                C:\Arduino_Edison_IDE\hardware\arduino\edison\libraries\Time/time.h:100:9: error: previous declaration 'int year()' here

                                int    year();            // the full four digit year: (2009, 2010 etc)

                                        ^

                                C:\Arduino_Edison_IDE\hardware\arduino\edison\libraries\Time/time.h:105:66: error: declaration of C function 'void setTime(int, int, int, int, int, int)' conflicts with

                                void    setTime(int hr,int min,int sec,int day, int month, int yr);

                                                                                                  ^

                                C:\Arduino_Edison_IDE\hardware\arduino\edison\libraries\Time/time.h:104:9: error: previous declaration 'void setTime(time_t)' here

                                void    setTime(time_t t);

                                 

                                ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

                                 

                                Cheers,

                                 

                                UST

                                • 13. Re: How to get current time
                                  deium

                                  Time.h is case sensitive as would ctime - ensure <time.h>

                                  although you declared your cTime varable as a char array of [22],

                                  cTime was not filled with a value,

                                  as was year() .. sec()  resulting in their default values giving you a 1970 datetime

                                  • 14. Re: How to get current time
                                    Terrence_

                                    Deium, thank you for you reply.

                                     

                                    char cTime[22];

                                     

                                    doesn't this sprint function fill the cTime array with the formatted data?

                                     

                                     

                                     

                                    sprintf(cTime, "%04d%02d%02d:%02d%02d%02d", year(), month(), day(), hour(), minute(), second());

                                     

                                     

                                     

                                     

                                     

                                    and I though year() would gather the current year.  How would I get year() to give me the current year?

                                     

                                     

                                    1 2 Previous Next