首页 > 代码库 > Unix时间戳与C# DateTime时间类型、C语言互换 1970-01-01 00:00:00
Unix时间戳与C# DateTime时间类型、C语言互换 1970-01-01 00:00:00
和同事的下位机交互的时候,需要使用到时间转换,
刚好找到这篇文章,用C语言实现的话,还挺麻烦的
下面给出C#和C的源码
注:C# 转自 http://www.cnblogs.com/hanhualangzi/archive/2012/02/10/2345952.html
C源码是同事给我的;
C#:
1 dangranusing System; 2 using System.Collections.Generic; 3 using System.Text; 4 5 namespace WWFramework.DateTimes 6 { 7 /// <summary> 8 /// 时间相关函数 9 /// </summary>10 public static class Function11 {12 /// <summary>13 /// 将Unix时间戳转换为DateTime类型时间14 /// </summary>15 /// <param name="d">double 型数字</param>16 /// <returns>DateTime</returns>17 public static System.DateTime ConvertIntDateTime(double d)18 {19 System.DateTime time = System.DateTime.MinValue;20 System.DateTime startTime = TimeZone.CurrentTimeZone.ToLocalTime(new System.DateTime(1970, 1, 1));21 time = startTime.AddSeconds(d);22 return time;23 }24 25 /// <summary>26 /// 将c# DateTime时间格式转换为Unix时间戳格式27 /// </summary>28 /// <param name="time">时间</param>29 /// <returns>double</returns>30 public static double ConvertDateTimeInt(System.DateTime time)31 {32 double intResult = 0;33 System.DateTime startTime = TimeZone.CurrentTimeZone.ToLocalTime(new System.DateTime(1970, 1, 1));34 intResult = (time - startTime).TotalSeconds;35 return intResult;36 }37 }38 }
C源码:
1 struct tm 2 { 3 uint32 tm_sec; /* seconds after the minute - [0, 59] */ 4 uint32 tm_min; /* minutes after the hour - [0, 59] */ 5 uint32 tm_hour; /* hours since midnight - [0, 23] */ 6 uint32 tm_mday; /* day of the month - [1, 31] */ 7 uint32 tm_mon; /* months since January - [0, 11] */ 8 uint32 tm_year; /* year since 1900 */ 9 uint32 tm_wday; /* days since Sunday - [0, 6] */ 10 uint32 tm_yday; /* days since January 1 - [0, 365] */ 11 uint32 tm_isdst; /* Daylight Saving Time flag */ 12 }; 13 tm time; 14 void localtime_h(uint32 time, struct tm *ret_time) 15 { 16 static const char month_days[12] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; 17 static const char leap_year[4] = {0, 0, 1, 0}; 18 uint32 j = 0; 19 uint32 day_count = 0; 20 uint32 i = 0; 21 uint32 leave_for_fouryear = 0; 22 uint32 four_year_count = 0; 23 uint32 temp_value = http://www.mamicode.com/0; 24 uint32 leave_for_year_days; 25 uint32 leave_for_month_days; 26 //time=time-28800; 27 ret_time->tm_sec = 0; 28 ret_time->tm_min = 0; 29 ret_time->tm_hour = 0; 30 ret_time->tm_wday = 0; 31 ret_time->tm_year = 0; 32 ret_time->tm_mday = 0; 33 ret_time->tm_mon = 0; 34 ret_time->tm_sec = time % 60; 35 temp_value = http://www.mamicode.com/time / 60; 36 ret_time->tm_min = temp_value % 60; 37 temp_value /= 60; 38 temp_value += 8; 39 ret_time->tm_hour = temp_value % 24; 40 temp_value /= 24; 41 ret_time->tm_wday = (temp_value + 4) % 7; 42 four_year_count = temp_value / (365 * 4 + 1); 43 leave_for_fouryear = temp_value % (365 * 4 + 1); 44 leave_for_year_days = leave_for_fouryear; 45 for (i = 0; i < 4; i++) 46 { 47 day_count = leap_year[i] ? 366 : 365; 48 49 if (leave_for_year_days <= day_count) 50 { 51 break; 52 } 53 else 54 { 55 leave_for_year_days -= day_count; 56 } 57 } 58 ret_time->tm_year = four_year_count * 4 + i ; 59 ret_time->tm_yday = leave_for_year_days; 60 leave_for_month_days = leave_for_year_days; 61 for (j = 0; j < 12; j++) 62 { 63 if (((leap_year[i])) && (j == 1)) 64 { 65 if (leave_for_month_days <= 29) 66 { 67 break; 68 } 69 else if (leave_for_month_days == 29) 70 { 71 i++; 72 leave_for_month_days = 0; 73 break; 74 } 75 else 76 { 77 leave_for_month_days -= 29; 78 } 79 80 continue; 81 } 82 83 if (leave_for_month_days < month_days[j]) 84 { 85 break; 86 } 87 else if(leave_for_month_days == month_days[j]){ 88 i++; 89 leave_for_month_days = 0; 90 break; 91 } 92 else 93 { 94 leave_for_month_days -= month_days[j]; 95 } 96 } 97 ret_time->tm_mday = leave_for_month_days + 1; 98 ret_time->tm_mon = j; 99 }100 void ConvSecTimeToCalendar(struct tm *t_tm,uint32 t)101 {102 t=(t>=28800)?(t-28800):t;103 localtime_h(t,t_tm);104 t_tm->tm_year +=1970;105 t_tm->tm_mon +=1;106 }
Unix时间戳与C# DateTime时间类型、C语言互换 1970-01-01 00:00:00
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。