zoukankan      html  css  js  c++  java
  • Datetime interoperation between c plus plus and .net

    Here is the senario in my requirements:

      I need store datetime value in a serialization file(binary file). the file will be transfer to server side by network. the server-side is used c plus plus and it is responsible for parse the file tranfer from network. the file content contains different type data. of course,datetime value just be contained. Unfortunally, I write data to file use .net.

        How to store datetime value without use string format like this yyyy-MM-dd HH:mm:ss?

    I got an idea, windows use 64-bit value to present datetime format value. I also can convert datetime to 64-bit value.

    Let me to this:

       1:  private static DateTime origin = System.TimeZone.CurrentTimeZone.ToLocalTime(new DateTime(1970, 1, 1, 0, 0, 0));
       2:   
       3:   public static System.DateTime Convert(UInt64 t)
       4:   {
       5:        DateTime convertedValue = origin + new TimeSpan((long)t * TimeSpan.TicksPerDay);
       6:        if (System.TimeZone.CurrentTimeZone.IsDaylightSavingTime(convertedValue) == true)
       7:        {
       8:            System.Globalization.DaylightTime daylightTime = System.TimeZone.CurrentTimeZone.GetDaylightChanges(convertedValue.Year);
       9:            convertedValue = convertedValue + daylightTime.Delta;
      10:        }
      11:        return convertedValue;
      12:    }
      13:   public static UInt64 ConvertBack(System.DateTime value)
      14:   {
      15:        DateTime convertedValue = value;
      16:        if (System.TimeZone.CurrentTimeZone.IsDaylightSavingTime(convertedValue) == true)
      17:        {
      18:            System.Globalization.DaylightTime daylightTime = System.TimeZone.CurrentTimeZone.GetDaylightChanges(convertedValue.Year);
      19:            convertedValue = convertedValue - daylightTime.Delta;
      20:       }
      21:       long diff = convertedValue.Ticks - origin.Ticks;
      22:       return (UInt64)(diff / TimeSpan.TicksPerDay);
      23:    }

    I store total seconds in file. and I will parse the datetime easily in the server side .

    the following is the c plus plus code:

       1:  struct tm gettime (long value)
       2:  {
       3:      time_t time = time_t(vlaue); 
       4:      struct tm t;
       5:      localtime_s (&t, &time);
       6:      //struct tm *tm2 = localtime(&ltime);
       7:      t.tm_year += 1900; 
       8:      t.tm_mon += 1;
       9:   
      10:      return t;
      11:  }

     

    Great,you only need use localtime() function to do this. Is it so easy?

    time_t is defined as a 64-bit int number type. in crtdefs.h header file, you will find this:

       1:  typedef __int64 __time64_t;     /* 64-bit time value */
       2:  typedef __time64_t time_t;      /* time value */

    tm as struct defined in wchar.c

       1:  struct tm {
       2:          int tm_sec;     /* seconds after the minute - [0,59] */
       3:          int tm_min;     /* minutes after the hour - [0,59] */
       4:          int tm_hour;    /* hours since midnight - [0,23] */
       5:          int tm_mday;    /* day of the month - [1,31] */
       6:          int tm_mon;     /* months since January - [0,11] */
       7:          int tm_year;    /* years since 1900 */
       8:          int tm_wday;    /* days since Sunday - [0,6] */
       9:          int tm_yday;    /* days since January 1 - [0,365] */
      10:          int tm_isdst;   /* daylight savings time flag */
      11:          };

    ok, util now. I solve the interoperation problem. It seems very easy.um…

    Author:repository
    From:  http://repository.cnblogs.com
    本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。
  • 相关阅读:
    SGU 271 Book Pile (双端队列)
    POJ 3110 Jenny's First Exam (贪心)
    HDU 4310 Hero (贪心)
    ZOJ 2132 The Most Frequent Number (贪心)
    POJ 3388 Japanese Puzzle (二分)
    UVaLive 4628 Jack's socks (贪心)
    POJ 2433 Landscaping (贪心)
    CodeForces 946D Timetable (DP)
    Android Studio教程从入门到精通
    Android Tips – 填坑手册
  • 原文地址:https://www.cnblogs.com/repository/p/2231824.html
Copyright © 2011-2022 走看看