Search code examples
.netdatetimeinformix

Informix: .NET driver: default datetime format


I am using Informix .NET driver in my winforms app and am populating a data grid with a stored procedure resultset. The weird thing is that the datetime year to second(format set by the stored proc and returned) column is being displayed as datetime year to minute with AM/PM. Does the .net driver change/apply formatting to the resultset's datetime column?


Solution

  • With .NET the date format is usually controlled by your localization settings (CultureInfo), it is not done by the provider.

    In the code below I'm using CultureInfo() to specify "en-US" and "en-GB" formats:

    using System;
    using System.IO;
    using System.Data;
    using System.Text;
    using System.Threading;
    using IBM.Data.Informix;
    
    
    class sample {
        static void Main(string[] args) {
    
         IfxConnection conn;
         try {
    
            conn = new IfxConnection("Server=ids1410;Database=sysmaster");
            conn.Open();    
            IfxCommand cmmd = conn.CreateCommand();
            cmmd.CommandText = "SELECT current::datetime year to second";
            IfxDataReader drdr;
            drdr = cmmd.ExecuteReader();
            drdr.Read();
            Console.WriteLine("GetIfxDateTime:\t\t"+drdr.GetIfxDateTime(0)); 
            Thread.CurrentThread.CurrentCulture = new System.Globalization.CultureInfo("en-US");
            Console.WriteLine("GetDateTime (en-US):\t"+drdr.GetDateTime(0)); 
            Thread.CurrentThread.CurrentCulture = new System.Globalization.CultureInfo("en-GB");
            Console.WriteLine("GetDateTime (en-GB):\t"+drdr.GetDateTime(0)); 
            
            conn.Close();
         }
         catch (Exception e) {
            Console.WriteLine(e.Message);
         }
       }
    }
    

    That will result in

    D:\Infx\cs>csc.exe /R:%INFORMIXDIR%\bin\netf40\IBM.Data.Informix.dll /nologo datetime.cs
    
    D:\Infx\cs>datetime
    GetIfxDateTime:         2020-10-20 09:30:32
    GetDateTime (en-US):    10/20/2020 9:30:32 AM
    GetDateTime (en-GB):    20/10/2020 09:30:32
    
    D:\Infx\cs>
    

    You may want to use GetIfxDateTime() instead.