博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Unix时间戳与C# DateTime时间类型、C语言互换 1970-01-01 00:00:00
阅读量:5245 次
发布时间:2019-06-14

本文共 4886 字,大约阅读时间需要 16 分钟。

和同事的下位机交互的时候,需要使用到时间转换,

刚好找到这篇文章,用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     ///  8     /// 时间相关函数 9     /// 10     public static class Function11     {12         /// 13         /// 将Unix时间戳转换为DateTime类型时间14         /// 15         /// double 型数字16         /// 
DateTime
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 /// 26 /// 将c# DateTime时间格式转换为Unix时间戳格式27 /// 28 /// 时间29 ///
double
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 = 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 = 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 }

 

转载于:https://www.cnblogs.com/craigtao/p/3973056.html

你可能感兴趣的文章
20162304 2017-2018-1 《程序设计与数据结构》第二周学习总结
查看>>
九.python面向对象(双下方法内置方法)
查看>>
2018-09-12
查看>>
go:channel(未完)
查看>>
[JS]递归对象或数组
查看>>
CSS与Theme的作用——Asp.Net
查看>>
LeetCode(17) - Letter Combinations of a Phone Number
查看>>
20165115 2017-2018-2 《Java程序设计》第四周学习总结
查看>>
Linux查找命令对比(find、locate、whereis、which、type、grep)
查看>>
WPF自定义集合控件概述与遇到的问题
查看>>
路由器外接硬盘做nas可行吗?
查看>>
python:从迭代器,到生成器,再到协程的示例代码
查看>>
pytest的参数化测试
查看>>
Java多线程系列——原子类的实现(CAS算法)
查看>>
docker运行环境安装-centos(一)
查看>>
安装Pygame和pip的艰辛之路
查看>>
Hibernate的实体类为什么需要实现 java.io.Serializable 接口
查看>>
在Ubuntu下配置Apache多域名服务器
查看>>
多线程《三》进程与线程的区别
查看>>
Min Stack
查看>>