Weird signs written to SD Card

I made easy temp dataloger with RTC3231, LC Sdcard module and DS18B20 temp sensor.

results are saved to sd card as csv file every 1 minute. For 2 hours results are valid, then for 5,5 h are invalid and then again valid for 1 hour (test period end).

sdram is used in about 67% after compilation.

here is output in the file:

2014-12-07 00:43:11,25.75
2014-12-07 00:44:12,25.75
2014-12-07 00:45:13,25.75
2014-12-07 00:46:14,25.75
2014-12-07Ű:47:15,25.75
2014-12-07Ű:48:16,25.75
2014-12-07Ű:49:17,25.75
2014-12-07Ű:50:18,25.75
2014-12-07Ű:51:19,25.75
...
2014-12-07Ű:32:11,25.75
2014-12 7Ű:33L12,25.75
2014-12-07Ű:34:13,25.75
2014-12-07Ű:35:13,25.75
2014-12-07Ű:36:14,25.75
2014-12-07Ű:37:15,25.75
2014-12-07Ű:38:16,25.75
2014-12-07Ű:39:17,25.75
2014-12-07Ű:40:18,25.50
2014-12-07Ű:41:19,25.50
2014-12-07Ű:42:19,25.7G
2014-12-07Ű:43:20,2 75
20C4-12-07Ű:44:21,25.75
2014-12-07Ű:45:22,25.75
2014-12-07Ű:47:24,25.50
2014-12-07Ű:49:26,25.75
2014-12-07Ű:50:26,25.75
2014-12-07Ű:53:29,25.75
2014-12-07Ű:54:30,25.75
2014-12-07Ľ:56:32,25.75
2014-12-07Ľ:58:33,25.75
2014-12-07ť:03:38,25.75
2014-12-07ť:05:39,25.75
2014-12-07ť:06:40,25.50
2014-12-07ť:07:41,25.50
2014-12-07Ż:08:42,25.75
2014-12-07ť:09:43,25.50
2014-12-07~:11:45,0.00
2014-12-07 07:12:46,25.50
2014-12-07 07:13:46,25.50
2014-12-07 07:14:46,25.50

my sketch:

/* WIRING
RTC:
SCL - A5
SCA - A4
VCC - +5V
GND - GND

SDCARD:
MISO  - 12
SCK   - 13
MOSI  - 11
CS    - 4
+5V   - +5V
+3.3V - +3.3V
GND   - GND
*/

// RTC
#include <Wire.h>
#include <DS3231.h>
// SDCARD
#include <SPI.h>
#include <SD.h>
// DALLAS TEMP SENSOR
#include <OneWire.h>
#include <DallasTemperature.h>

#define ONE_WIRE_BUS 2
OneWire oneWire(ONE_WIRE_BUS);

// Pass our oneWire reference to Dallas Temperature. 
DallasTemperature sensors(&oneWire);

// Assign the addresses of your 1-Wire temp sensors.
// See the tutorial on how to obtain these addresses:
// http://www.hacktronics.com/Tutorials/arduino-1-wire-address-finder.html

DeviceAddress insideThermometer = { 0x28, 0x1C, 0x98, 0xF3, 0x04, 0x00, 0x00, 0xD6 };

DS3231 clock;
RTCDateTime dt;

const int chipSelect = 4;
Sd2Card card;

File myFile;
char filename[9] = "test.csv";

void setup()
{
  Serial.begin(9600);

  // Initialize DS3231
  Serial.println(F("Initialize DS3231"));;
  
  clock.begin();
  Serial.println(F("Initializing SD card..."));
  pinMode(10, OUTPUT);

  if (!SD.begin(chipSelect)) {
    Serial.println(F("initialization failed!"));
    return;
  }
  
  
  Serial.println(F("initialization done."));
  Serial.println(F("Start sensor."));
  sensors.begin();
  // set the resolution to 10 bit (good enough?)
  sensors.setResolution(insideThermometer, 10);
  // Set sketch compiling time
  //clock.setDateTime(__DATE__, __TIME__);

  // Set from UNIX timestamp
  // clock.setDateTime(1397408400);

  // Manual (YYYY, MM, DD, HH, II, SS
  // clock.setDateTime(2014, 4, 13, 19, 21, 00);
}

void loop()
{
  dt = clock.getDateTime();
  char* time = clock.dateFormat("Y-m-d H:i:s", dt);
  
//  Serial.print("Long number format:          ");
  Serial.print(time);


//  Serial.println();
  Serial.println(F(" Getting temperatures..."));
  sensors.requestTemperatures();
  Serial.print(time);
    Serial.print(F(" Inside temperature is: "));
//  printTemperature(insideThermometer);
  Serial.println(F(""));

  openFile(time);
  writeFile(time);
  closeFile(time);
  
  delay(60000);
}

void openFile(char* time){
  
  //Check file name exist?
  if (SD.exists(filename)) {
    Serial.print(time);
    Serial.println(F(" File exists."));
    myFile = SD.open(filename, FILE_WRITE);
  }
  else {
    Serial.print(time);
    Serial.print(F(" File doesn't exist. "));
    Serial.println(F("Creating new file"));
    myFile = SD.open(filename, FILE_WRITE); 
  }
}

void writeFile(char* time){

  Serial.print(time);
  Serial.println(F(" writing temperature to file"));
  myFile.print(time);
  myFile.print(F(","));
  printTemperature(insideThermometer);
}

void closeFile(char* time){
    Serial.print(time);
    Serial.println(F(" ended writing temperature to file"));
    myFile.close(); 
}

void printTemperature(DeviceAddress deviceAddress)
{
  float tempC = sensors.getTempC(deviceAddress);
  if (tempC == -127.00) {
    Serial.print(F("Error getting temperature"));
  } else {
    Serial.print(F("C: "));
    Serial.print(tempC);
    Serial.print(F(" F: "));
    Serial.println(DallasTemperature::toFahrenheit(tempC));
    myFile.println(tempC);
  }
}

SD Card is FAT 2GB
genuine arduino uno
arduino ide 1.5.8

Looks like dateFormat() has a problem.

char* DS3231::dateFormat(const char* dateFormat, RTCDateTime dt)
{
    char buffer[255];

// Lots of code to format the data/time here

    return buffer;
}

You can't expect the buffer array to be maintained since it is on the stack. Amazing it works as well as it does. Guess it sort of works because the array is 255 bytes long and the stack grows down.

I suspect there could be other problems.

I suppose that I am to newbie to understand this answer ;/ It means that if formated date is longer than 255 chars it will make this weird signs or buffer is not cleared properly between date formating?

#include <DS3231.h>

There are several libraries with this name. Which one are you using?

It would appear that there is in error in the function dateFormat() which is a part of this library.

Rather than debug and fix the library, you can just use the individual components of the date and time and store them individually. This can be an advantage if you transfer your data to a spread sheet or data base and want to sort.

Or, if you really want the long format datetime as one item, you could use sprintf to format the components and reinitialize its buffer each time you assemble the data.

I use this library: https://github.com/jarzebski/Arduino-DS3231

By default, regular arrays of local scope (for example, those declared within a function) are left uninitialized. This means that none of its elements are set to any particular value; their contents are undetermined at the point the array is declared.

If the problem is in array initialization you can patch the library as follows

char* DS3231::dateFormat(const char* dateFormat, RTCDateTime dt)
{
    //char buffer[255];
    char buffer[255] = {};

   // buffer[0] = 0; redundant after explicit initialization

    //char helper[11];
    char helper[11] = {};

//lots of other stuff

You can test the system with these changes, but fat16lib is the guru here, and if he suspects other problems in the function or issues relating to stack mangement beyond initialization you may not be able to use this dateFormat function.