从VC6迁移到VS2005 - LocalTime
来源:互联网 发布:sha256代码java 编辑:程序博客网 时间:2024/04/28 10:16
我们在编程时,会使用比较多的日期、时间等值;其中time、localtime是经常使用的!
time函数声明:
time_t time( time_t *timer );locatime结构声明:
struct tm *localtime( const time_t *timer );大部分我们需要输出一个像:xxxx-xx-xx格式的日期
下面的程序在VC6中调试通过:
/* * Gets the local system time * * Created on: 2011-12-24 * Author: xiaobin */char *getLocalMachineTime(){struct tm *myTime;time_t long_time;time(&long_time);myTime = localtime(&long_time);char *result = NULL;result = (char *)malloc(1);int iYear = myTime->tm_year;iYear += 1900;char *myYear = (char *)malloc(5);myYear = _itoa(iYear, myYear, 10);strcpy(result, myYear);strncat(result, "-", 1);int iMonth = myTime->tm_mon;iMonth += 1;char *myMonth = (char *)malloc(3);if(iMonth < 10) {strcpy(myMonth, "0");} _itoa(iMonth, myMonth, 10);strncat(result, myMonth, sizeof(myMonth));strncat(result, "-", 1);int iDay = myTime->tm_mday;char *myDay = (char *)malloc(3);if(iDay < 10) {strcpy(myDay, "0");} _itoa(iDay, myDay, 10);strncat(result, myDay, sizeof(myDay));return result;}
输出:
2013-09-09
需要更改的函数主要为:localtime->localtime_s、_itoa->_itoa_s、strcpy->strcpy_s、strncat->strncat_s
localtime_s函数声明:
errno_t localtime_s( struct tm* _tm, const time_t *time );_itoa_s函数声明:
errno_t _itoa_s( int value, char *buffer, size_t sizeInCharacters, int radix );其中的radix参数为进制数:2,8,10,16进制等等
strcpy_s函数声明:
errno_t strcpy_s( char *strDestination, size_t numberOfElements, const char *strSource );strncat_s函数声明:
errno_t strncat_s( char *strDest, size_t numberOfElements, const char *strSource, size_t count);
以下程序在VS2005以上版本编译通过:
/* * Gets the local system time * * Created on: 2011-12-24 * Author: xiaobin */char *getLocalMachineTime(){struct tm myTime;time_t long_time;time(&long_time);localtime_s(&myTime, &long_time);char result[12];int iYear = myTime.tm_year;iYear += 1900;char chYear[5];_itoa_s(iYear, chYear, 5, 10);strcpy_s(result, chYear);strncat_s(result, "-", 1);int iMonth = myTime.tm_mon;iMonth += 1;char chZero[] = "0";char chMonth[3];_itoa_s(iMonth, chMonth, 3, 10);if(iMonth < 10) {strncat_s(result, chZero, 2);} strncat_s(result, chMonth, 2);strncat_s(result, "-", 1);int iDay = myTime.tm_mday;char chDay[3];_itoa_s(iDay, chDay, 3, 10);if(iDay < 10) {strncat_s(result, chZero, 2);}strncat_s(result, chDay, 3);return result;}
程序测试:
// prjLocalDateTime.cpp : Defines the entry point for the console application.///* * prjLocalDateTime.cpp * * Created on: 2011-12-24 * Author: xiaobin */#include "stdafx.h"#include <comdef.h>#include "time.h"#include <stdlib.h>char *getLocalMachineTime();int _tmain(int argc, _TCHAR* argv[]){char dateChar[12];char *temp = getLocalMachineTime();strcpy(dateChar, temp);for(int i=0; i<12; i++){printf("%c", dateChar[i]);}return 0;}
0 0
- 从VC6迁移到VS2005 - LocalTime
- 从Vc6迁移到Vs2005
- 再谈从vc6迁移到vs2005
- 再谈从vc6迁移到vs2005
- 从VC6迁移到VS2005 - MESSAGE
- 从VC6迁移到VS2005 - METHOD
- 从VC6迁移到VS2005 - ATL
- 从VC6迁移至VS2005
- 从VC6迁移至VS2005
- 从VC6迁移至VS2005
- 从VC6迁移至VS2005 ,VS2008
- 从VC6迁移至VS2005 ,VS2008
- VC6.0迁移到VS2005:error C2440
- VC6.0迁移到VS2005:error C2440
- VC6.0迁移到VS2005:error C2440
- VC6.0迁移到VS2005:error C2440
- VC6.0迁移到VS2005:error C2440
- 从vc6迁移到vs2005过程中的问题(学习过程中逐渐添加)
- 黑马程序员_基本数据类型对象包装类新特性
- 【精心推荐】20款优秀 jQuery Accordion(手风琴)特效插件
- soapUI测试webService服务
- 动态规划-有向无回路图中两点间最远距离
- jsp 通过 Ajax 和servlet之间的连接及数据传递示例
- 从VC6迁移到VS2005 - LocalTime
- 网路编程中常用的线程(进程)模型以及封闭原则
- Backbone的写类方式
- ubuntu 12.10中MyEclipse 10.6+下载+安装+破解
- 网站上的页面元素 Meta标签-关键字,描述,机器人和辅助功能标签页标题内容-定位整个副本的关键字的内容和支付链接-连接结构,ALT标签,文本链接的话和标题,页面上的各主要部分的关键字密度,HTML有
- TCP的状态变迁及RST报文段产生与处理
- 两个整型大数相减
- Java中@Override的作用
- 关于导航条navigationBar消失的问题