none
mktime equivalent in C# RRS feed

  • Question

  • Hi

    I have c++ code i want to convert to C#; 

    long  MakeTime( short month, short day, short year, short hour, short minute )
    {
      struct tm tmpTm;
    
      memset( &tmpTm, 0, sizeof( tmpTm ) );
      tmpTm.tm_isdst = -1;
      tmpTm.tm_mon = month - 1;
      tmpTm.tm_mday = day;
      tmpTm.tm_year = year - 1900;
      tmpTm.tm_hour = hour;
      tmpTm.tm_min = minute;
    
      return mktime( &tmpTm );
    }
    

    this function return the number 1371550800 when I provide current date info :2013.6,18

    why am I providing return value?

    well, in the c++ project I am dealing with I have settings in the database that store date as long valued field in a table ; and the value of end_date I get from database is very close to this number : something like 1329155012 

    Now this number i get from database I want to convert to datetime  and get :day:month:year   to plug into text boxes; I have tried this

     void loadSchedData()
              {
                long strDate = CSchedule._Events[0].StartDate;// what i get from database
                      
                DateTime date = new DateTime(strtDate);
    
                textBoxFrom.Text = date.ToShortDateString();
            
            }

    of course i get very old date in the year 0001; i tried to multiply strtDate  by factors of tens

    but could not get any date close to current date; 

    so I am wondering How mktime of C++ is manipulating  it and  returning this number which does not work with c# code? I read about mktime in msdn learn but there was no information about the return value

    Tuesday, June 18, 2013 6:35 PM

Answers

  • Oh - to go the other way, you could use:

    DateTime MakeDateTime(int secondsSince1970)
    {
        DateTime reference = new DateTime (1970, 1, 1);
    
        return reference.AddSeconds(secondsSince1970);
    }


    Reed Copsey, Jr. - http://reedcopsey.com
    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".

    • Marked as answer by kobosh Tuesday, June 18, 2013 7:36 PM
    Tuesday, June 18, 2013 7:22 PM
    Moderator

All replies

  • In C#, you'd just use DateTime directly, and not rely on "time_t" and related timings.

    If you need to convert from DateTime to the integer value returned from mktime, you could use:

    int MakeTime(int month, int day, int year, int hour, int minute)
    {
       // Build the date for your specified values
       DateTime date = new DateTime(year, month, day, hour, minute, 0);
    
       // Epoch date for time_t is 1970
       DateTime reference = new DateTime (1970, 1, 1);
    
       // Return number of seconds since epoch date
       return (date - reference).TotalSeconds;
    }


    Reed Copsey, Jr. - http://reedcopsey.com
    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".

    Tuesday, June 18, 2013 6:40 PM
    Moderator
  • sorry i havent made my question clear: I get this number from database and i want to convert it to datetime object this number I am getting is 1371550800;

    i tried to plug it into       mydate=new datetime(number i get from database)

    but this number will give very very old date nothing close to 2013   ;

    how can I manipulate this number to get a date close to 2013

    Tuesday, June 18, 2013 7:12 PM
  • Oh - to go the other way, you could use:

    DateTime MakeDateTime(int secondsSince1970)
    {
        DateTime reference = new DateTime (1970, 1, 1);
    
        return reference.AddSeconds(secondsSince1970);
    }


    Reed Copsey, Jr. - http://reedcopsey.com
    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".

    • Marked as answer by kobosh Tuesday, June 18, 2013 7:36 PM
    Tuesday, June 18, 2013 7:22 PM
    Moderator