Skip to content
Advertisement

Error when saving data on a specific date and time

I have a server with Debian 10,Apache/2.4.38, MySQLi, mongodb and Php. The server is running a custom PHP program which allows users to upload their measurements (in .csv files) which are then stored in mongodb.

The uploaded .csv files must comply with a predetermined format in order to be uploaded, like so:

Timestamp,Measurement 1,Measurement 2,(etc)
05/06/20 00:15,201,1
05/06/20 00:30,305,4
05/06/20 00:45,400,65
.
.
(continues for the entire duration of the day/week/year)

The timestamp must be set every 15 minutes exactly. The code responsible for this check is:

//check time interval
if (isset($document['timestamp']) && $time_step==false && $fileLine>1){
    if ($interval>900){
        $time_step=true;
        $condition_violated=True;
        $conditions_errors.="line ".$fileLine. " warning: The ".$key[$i]." condition is violated. rn";
        file_put_contents($file_name,"line ".$fileLine. " error: One or more time instants are missing in the file you try to upload. Please check the consistency of the measurements in the file. rn",FILE_APPEND);
    }
}

This works fine except one very specific date and time every year. When trying to upload a file starting on 22/02/19 00:15, and ending on 31/12/19 23:45, i get an error:

line 23724 error: One or more time instants are missing in the file you try to upload. Please check the consistency of the measurements in the file. 

The above line is the date: 27/10/19 02:45 If i split the file in two different .csv files, the first one with lines 1-23723, and the second one with lines 23725-end, essentially skipping line 23724-date 27/10/19 02:45, the file uploads just fine.

If i try to upload a file with just the three following timesteps:

Timestamp,Measurement 1,Measurement 2,(etc)
27/10/19 02:30,0,0
27/10/19 02:45,0,0
27/10/19 03:00,0,0

I get the same error. Same issue appears for 25/10/20 02:45 in the entire 2020 year. Tried many .csv files, same error.

I am trying to understand whether there is something wrong with the OS, the PHP code or the database (mongodb). Any suggestions?

1st EDIT

@Buzz The code that checks the interval should be here:

for($i=0; $i < $numofcols; $i++){
    $key[$i] =remove_utf8_bom($key[$i]) ;
    $min_max_violated=False;
    if ($key[$i] == "Timestamp"){
        if(strlen($value[$i]) != 14){
            $date_time_error=true;
            $dt=0;
            file_put_contents($file_name,"line ".$fileLine. " error: Date-Time format error.rn",FILE_APPEND);
        }else{
            $document['timestamp'] = $value[$i];
            if ($fileLine == 1){
            $timestamp_to_cmp=DateTime::createFromFormat('d/m/y H:i',$document['timestamp'])->getTimestamp();
            $dt=$timestamp_to_cmp;
            }
            else{
                $dt = DateTime::createFromFormat('d/m/y H:i',$document['timestamp'])->getTimestamp();
                $interval=$dt-$timestamp_to_cmp;
/*                  echo json_encode(["dd"=>$interval]);
                exit(); */
            }
        }

Mongodb comes in in a different .php file, i believe that the error is in this file.

Advertisement

Answer

The date 27/10/19 02:45 was the date when the timechange occurs. At that time instant PHP adds +3600 seconds to the measurement. Apparently, this only happens to debian installations, it worked without the change on Arch linux. I fixed it by adding:

date_default_timezone_set('UTC');

at the start of the file where the PHP checks the time interval.

User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement