DerekErb

This could go in the networking forum as it involves the WiFi shield or the storage forum as it involves the SD card... but I thought the fact that it should set the info on the actual Arduino puts it here.

I have the WiFi shield with the built-in SD card unit. Whenever I create a file on the SD card, through the Arduino and the WiFi shield, the date and time of the file creation is set to the standard January 1, 2000 00:00. I have found how to connect to an NTP server and read the current date and time. But how can I set the current date and time on the Arduino for it to use in it's other operations such as file I/O?

PeterH

The Arduino documentation for the SD library covers access to the directory and file content, but not to attributes such as the creation/modification times. As far as I can see, these are supported by the underlying SdFat library so could be read at the SD library interface, but there's no obvious way to set them. I see some code in SdBaseFile.cpp which suggests that the creation and modification times default to 1 Jan 2000 but could be modified if a user-defined function was implemented to look up the local date/time:

I haven't noticed any documentation describing how to use this (but then, I haven't looked very hard) but I would guess that if you implement a function with the correct signature and then update dateTime_ to point to that function, it would be invoked when you created/updated a file. This leaves you with the problem of how to find the current date/time, and I expect the usual responses would point you towards an RTC or a network interface and (S)NTP.

NickPyner

Whenever I create a file on the SD card, through the Arduino and the WiFi shield, the date and time of the file creation is set to the standard January 1, 2000 00:00. I have found how to connect to an NTP server and read the current date and time. But how can I set the current date and time on the Arduino for it to use in it's other operations such as file I/O?

I think the simplest approach is to do your own stamping using a DS1307 clock module. You can use it to name the file and/or include date/time in the data. In my case, the clock is used to create a new file at midnight - 20130102,csv - and thus only the time need be recorded therein.

DerekErb

For most of my uses I'm not worried about the date/time surviving a board reset as each of my current and foreseen boards is either Ethernet or WiFi based. I will get the current time, via NTP, in setup() every time the board is reset.

The date and time of the file creation, and modification, are not primordial for me. I just wanted to know where and how to be able to control that for future operations and, most importantly, if it is possible. The date and time recordings I need will be in fields inside my files, mostly CSVs, and that's not a problem as I control that inside the files. But when transferring those files from the SD card to my computers I would prefer it is the last modification date and time were correct.

My next project, as soon as I get some more time, is to start working on moving my SD routines from the SD library to the SdFat library as I already need that to correctly return the amount of space available on the SD card. At that time I'll look in to how to possibly set both the file creation date/time and the file modification date/time ... if it's possible.

hi there can i ask a question,the DS1307 RTC have battery backup so it doesnt lose the time...but what about hour change?(when clock turn fwd and bckwd) do i have to change the arduino values allways?or is there any way to do it buy it self..thankyou

DerekErb

hi there can i ask a question,the DS1307 RTC have battery backup so it doesnt lose the time...but what about hour change?(when clock turn fwd and bckwd) do i have to change the arduino values allways?or is there any way to do it buy it self..thankyou

For my network (NTP) version I always use a timer which reverifies and resets the time. Once every 24 hours, for example, it gets the time from the NTP server and sets the internal clock. The frequency of your timer reset would be based on how much, or little, overhead you want to add to your loop()

For a version which isn't using the network you would have to build-in DST calculations, with additional location-based and timezone-based modifications, in to your code...

PeterH

what about hour change?(when clock turn fwd and bckwd) do i have to change the arduino values allways?

DST is usually only relevant when the time value is rendered for display, and if you're going to support DST you need to provide a way to choose which timezone you're going to render the time for (perhaps hard-coded) and to apply the DST adjustments applicable to that TZ. This is not changing the value of the underlying timestamp (nor the clock that it came from) but just how the date/time value is displayed.

I usually set the RTC to UTC then use this library:http://arduino.cc/forum/index.php/topic,96891.0.html

github has changed their download process slightly, I'll update the instructions shortly, but it's simple enough, click the ZIP button, then after downloading and un-zipping, rename the folder to just "Timezone".

DerekErb

I've come back here as it seems logical to continue this discussion rather than starting a new thread as I think my current problem is related.

As you can see below I got everything to work as I wanted with my Ethernet Shield and then with my Ethernet Arduino.

Now I'm trying to apply the same principles to a much simpler project: a WiFi smoke detector and thermometer.

Where I am blocking is setting the time for the Arduino. It appears as though my original approach, which worked via Ethernet and Ethernet UDP, isn't going to work with WiFi. I am hoping that someone here is going to tell me that the following thread is dated and this (somehow) works now:

//////////////////////////////////////////////////////////// GetNTPTime//// Get the time via NTP, adjust for Time Zone and return for synchronisation//unsigned long GetNTPTime() { // Time Zone (Difference from GMT in seconds) const long lTZ = 3600;

// Set an NTP packet to the NTP time server sendNTPpacket(ipTimeServer); delay(1000);

if (Udp.parsePacket()) { // We've received a packet, read the data from it Udp.read(byPacketBuffer, iNTP_PACKET_SIZE);

//the timestamp starts at byte 40 of the received packet and is four bytes, // or two words, long. First, extract the two words: unsigned long highWord = word(byPacketBuffer[40], byPacketBuffer[41]); unsigned long lowWord = word(byPacketBuffer[42], byPacketBuffer[43]); // combine the four bytes (two words) into a long integer // this is NTP time (seconds since Jan 1 1900): unsigned long secsSince1900 = highWord << 16 | lowWord;

// all NTP fields have been given values, now // you can send a packet requesting a timestamp: Udp.beginPacket(address, 123); //NTP requests are to port 123 Udp.write(byPacketBuffer, iNTP_PACKET_SIZE); Udp.endPacket();}