1. typedef struct t_xtime {
  2. int year; int month; int day;
  3. int hour; int minute; int second;
  4. } _xtime ;
  5.  
  6. #define xMINUTE (60 ) //1分的秒数
  7. #define xHOUR (60*xMINUTE) //1小时的秒数
  8. #define xDAY (24*xHOUR ) //1天的秒数
  9. #define xYEAR (365*xDAY ) //1年的秒数

可以通过在线转换工具,对程序结果进行验证:http://tool.chinaz.com/Tools/unixtime.aspx

将localtime(UTC+8北京时间)转为UNIX TIME,以1970年1月1日为起点

  1. unsigned int xDate2Seconds(_xtime *time)
  2. {
  3. static unsigned int month[12]={
  4. /*01月*/xDAY*(0),
  5. /*02月*/xDAY*(31),
  6. /*03月*/xDAY*(31+28),
  7. /*04月*/xDAY*(31+28+31),
  8. /*05月*/xDAY*(31+28+31+30),
  9. /*06月*/xDAY*(31+28+31+30+31),
  10. /*07月*/xDAY*(31+28+31+30+31+30),
  11. /*08月*/xDAY*(31+28+31+30+31+30+31),
  12. /*09月*/xDAY*(31+28+31+30+31+30+31+31),
  13. /*10月*/xDAY*(31+28+31+30+31+30+31+31+30),
  14. /*11月*/xDAY*(31+28+31+30+31+30+31+31+30+31),
  15. /*12月*/xDAY*(31+28+31+30+31+30+31+31+30+31+30)
  16. };
  17. unsigned int seconds = 0;
  18. unsigned int year = 0;
  19. year = time->year-1970; //不考虑2100年千年虫问题
  20. seconds = xYEAR*year + xDAY*((year+1)/4); //前几年过去的秒数
  21. seconds += month[time->month-1]; //加上今年本月过去的秒数
  22. if( (time->month > 2) && (((year+2)%4)==0) )//2008年为闰年
  23. seconds += xDAY; //闰年加1天秒数
  24. seconds += xDAY*(time->day-1); //加上本天过去的秒数
  25. seconds += xHOUR*time->hour; //加上本小时过去的秒数
  26. seconds += xMINUTE*time->minute; //加上本分钟过去的秒数
  27. seconds += time->second; //加上当前秒数
     seconds -= 8 * xHOUR;
  28. return seconds;
  29. }

  

将UNIX时间转为UTC+8 即北京时间

  1. //UNIX转为UTC 已进行时区转换 北京时间UTC+8
  2. void xSeconds2Date(unsigned long seconds,_xtime *time )
  3. {
  4. static unsigned int month[12]={
  5. /*01月*/31,
  6. /*02月*/28,
  7. /*03月*/31,
  8. /*04月*/30,
  9. /*05月*/31,
  10. /*06月*/30,
  11. /*07月*/31,
  12. /*08月*/31,
  13. /*09月*/30,
  14. /*10月*/31,
  15. /*11月*/30,
  16. /*12月*/31
  17. };
  18. unsigned int days;
  19. unsigned short leap_y_count;
  20. time->second = seconds % 60;//获得秒
  21. seconds /= 60;
  22. time->minute = seconds % 60;//获得分
  23. seconds += 8 * 60 ; //时区矫正 转为UTC+8 bylzs
  24. seconds /= 60;
  25. time->hour = seconds % 24;//获得时
  26. days = seconds / 24;//获得总天数
  27. leap_y_count = (days + 365) / 1461;//过去了多少个闰年(4年一闰)
  28. if( ((days + 366) % 1461) == 0)
  29. {//闰年的最后1天
  30. time->year = 1970 + (days / 366);//获得年
  31. time->month = 12; //调整月
  32. time->day = 31;
  33. return;
  34. }
  35. days -= leap_y_count;
  36. time->year = 1970 + (days / 365); //获得年
  37. days %= 365; //今年的第几天
  38. days = 01 + days; //1日开始
  39. if( (time->year % 4) == 0 )
  40. {
  41. if(days > 60)--days; //闰年调整
  42. else
  43. {
  44. if(days == 60)
  45. {
  46. time->month = 2;
  47. time->day = 29;
  48. return;
  49. }
  50. }
  51. }
  52. for(time->month = 0;month[time->month] < days;time->month++)
  53. {
  54. days -= month[time->month];
  55. }
  56. ++time->month; //调整月
  57. time->day = days; //获得日
  58. }

  

UNIX Time 时间戳 与 北京时间 相互转换的更多相关文章

  1. python(6)时间戳和北京时间互转,输出当前的时间和推到七天前的日期

    项目发展的需要:(包含时间函数)time datetime 时间戳和北京时间互转 import time import datetime s = '2015-04-17 11:25:30' d = d ...

  2. 练手——用Python写的时间戳转换为北京时间的小工具

    #北京时间需加上8小时bj = 8*3600 def time_stamp(times):    #一天总秒数    nonDaySeconds = 24*3600    leapmonths = [ ...

  3. 关于Unix时间戳转北京时间的问题

    工具在这里:http://tool.chinaz.com/Tools/unixtime.aspx?qq-pf-to=pcqq.group 今天踩了坑,无论参数是多少,年份总是1970.才发现原来参数必 ...

  4. C# 时间戳和普通时间相互转换

    // 时间戳转为C#格式时间 private DateTime StampToDateTime(string timeStamp) { DateTime dateTimeStart = TimeZon ...

  5. Delphi中获取Unix时间戳及注意事项(c语言中time()是按格林威治时间计算的,比北京时间多了8小时)

    uses DateUtils;DateTimeToUnix(Now) 可以转换到unix时间,但是注意的是,它得到的时间比c语言中time()得到的时间大了8*60*60这是因为Now是当前时区的时间 ...

  6. unix时间戳(unix timestamp)与北京时间的互转方法

    1.在linux bash下北京时间与unix时间戳互转: 获取unix timestamp: 命令:date "+%s" 输出:1372654714 获取北京时间: 命令:dat ...

  7. 如何将北京时间批量转为Unix时间?用Excel!

    前面我们说过Unix时间戳转换怎样在Excel批量修改,有些人就想如果有特殊需求,那能不能批量将北京时间批量转成unix时间呢?能!用Excel就可以实现!跟ytkah一起试试吧. 将unix时间戳转 ...

  8. PHP时间戳与时间相互转换(精确到毫秒)

    原文:PHP时间戳与时间相互转换(精确到毫秒) /** 获取当前时间戳,精确到毫秒 */ function microtime_float(){   list($usec, $sec) = explo ...

  9. Unix时间戳转日期时间格式,C#、Java、Python各语言实现!

    之前有个Q上好友没事问我,怎么自己写Unix时间戳转日期时间?于是我就顺手写了个C#版本给他!最近想起来,就萌发多写几个语言的版本分享,权当练习思路外加熟悉另外两种语言. 先说转换步骤 先处理年份,从 ...

随机推荐

  1. css编写规范

    一.注释规范 1.文件顶部注释(推荐使用) /* * @description: 中文说明 * @author: name * @update: name (2013-04-13 18:32) */ ...

  2. 【项目】'NSRangeException', reason: '*** -[__NSArrayM removeObjectAtIndex:]: index 2 beyond bounds [0 .. 1]'

    问题代码: [self.assetsArray objectAtIndex:indexPath.row] 问题解决思路:这里

  3. 机器学习实战------利用logistics回归预测病马死亡率

    大家好久不见,实战部分一直托更,很不好意思.本文实验数据与代码来自机器学习实战这本书,倾删. 一:前期代码准备 1.1数据预处理 还是一样,设置两个数组,前两个作为特征值,后一个作为标签.当然这是简单 ...

  4. JavaWeb学习笔记——SAX解析

    import org.xml.sax.Attributes; import org.xml.sax.SAXException; import org.xml.sax.helpers.DefaultHa ...

  5. JavaScript学习笔记——BOM_window对象

    javascript浏览器对象模型-windwo对象 BOM Browser Object Model window对象 是BOM中所有对象的核心. 一.属性 1.(位置类型-获得浏览器的位置) IE ...

  6. JavaWeb学习笔记——开发动态WEB资源(七)bookapp

    该工程的功能是实现一个bookapp 1.开发注册页面,注册使用properties文件,存储在classpath跟路径 2.注册成功跳转到登录页面 3.输入用户名密码登录,登录成功跳转到book显示 ...

  7. Codeforces Round #371 (Div. 2)B. Filya and Homework

    题目链接:http://codeforces.com/problemset/problem/714/B 题目大意: 第一行输入一个n,第二行输入n个数,求是否能找出一个数x,使得n个数中的部分数加上x ...

  8. [ngRepeat:dupes] Duplicates in a repeater are not allowed. Use 'track by' expression to specify uniq

    angularjs 使用ng-repeat报错 <div ng-init="words = ['高校','高校','高校']" ng-repeat="word in ...

  9. sql FOR XML PATH

    FOR XML PATH 有的人可能知道有的人可能不知道,其实它就是将查询结果集以XML形式展现,有了它我们可以简化我们的查询语句实现一些以前可能需要借助函数活存储过程来完成的工作.那么以一个实例为主 ...

  10. Auto generating Entity classes with xsd.exe for XML Serialization and De-Serialization

    More info here: http://blogs.msdn.com/b/yojoshi/archive/2011/05/14/xml-serialization-and-deserializa ...