Problem with dates in C # and MariaDB XAMPP

0

I am developing an application with Xamarin.Forms that communicates with my REST API made in Node.js and it communicates with my database in phpmyadmin. (All the development is in local).

The problem I have is that when I send the JSON data and I see with a console.log() what is sent I see that the date is correct.

2018-05-16T00: 00: 00 + 02: 00

Then when checking from phpymyadmin that the data has been saved well I see that it is a day less. (The field is a DATE).

Also when I want to receive the data from my database before sending it to me I have checked with a console.log() and it returns this date format with one day less:

2018-05-15T22: 00: 00.000Z

When I deserialize the JSON and see it with Debug.WriteLine (date) it shows me this other type of date:

5/15/2018 10:00:00 PM +00: 00

  • I have checked from Visual Studio with Debug.WriteLine (date.Kind) when sending and receiving to know if they are UTC or Local and in both cases they are Local.
  • I have configured the timezone with process.env.TZ = 'Europe / Madrid' in Node.js.
  • I have changed the timezone of php.ini just in case and also MySQL from console I have tried these sentences:
MariaDB [(none)]> SELECT NOW();
+---------------------+
| NOW()               |
+---------------------+
| 2018-05-16 22:40:27 |
+---------------------+
1 row in set (0.00 sec)
MariaDB [(none)]> SELECT @@global.time_zone, @@session.time_zone;
+--------------------+---------------------+
| @@global.time_zone | @@session.time_zone |
+--------------------+---------------------+
| SYSTEM             | SYSTEM              |
+--------------------+---------------------+
1 row in set (0.00 sec)
    
asked by Levi Arista 16.05.2018 в 22:46
source

1 answer

1

Explanation

There is a +2 that defines the time zone.

2018-05-16T00:00:00+02:00

Here it does not have that +2 but it has a "Z" at the end.

2018-05-15T22:00:00.000Z

That "Z" means that it has "Zero Offset", in other words, it is universal time. If the time is in UTC, saying "09:30 UTC" is the same as "09: 30Z" or "0930Z". "14:45:15 UTC" would be "14: 45: 15Z" or "144515Z".

In C #, when executing:

Console.WriteLine(DateTime.UtcNow.ToString("o"));  
Console.WriteLine(DateTime.Now.ToString("o"));

It gives us as a result:

2018-05-16T21:06:58.4259227Z
2018-05-16T14:06:58.4269282-07:00

That 7Z or -07 indicates the time slot where this code is running, in this case it indicates:

Time in US Mountain Standard Time.

Once this is explained, let's go through your case:

2018-05-16T00:00:00+02:00 

We have 2 hours in advance, which means that the registration time in "universal" mode was in:

2018-05-15T22:00:00.000Z

And at the time of deserializar is still the universal date, only in another format. (22 hours == 10pm) & (Oz == +00)

5/15/2018 10:00:00 PM +00:00

Suggestion

Work in universal hours and the conversions are made on the client.

Reference and recommended reading

ISO 8601: link

    
answered by 16.05.2018 в 23:16