Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1438442
  • 博文数量: 704
  • 博客积分: 10140
  • 博客等级: 上将
  • 技术积分: 6230
  • 用 户 组: 普通用户
  • 注册时间: 2010-07-15 20:41
文章分类

全部博文(704)

文章存档

2013年(1)

2012年(16)

2011年(536)

2010年(151)

分类: C/C++

2011-05-04 20:16:52

#include
#include

unsigned long DayStr2Ulong(const char *TimeStr)
{
    struct tmcur; 
    char year[5]= "";
    char mon[3]= "";
    char day[3]= ""; 
   memset(year,0,sizeof(year));
   memcpy(year,TimeStr,4);    
   memset(mon,0,sizeof(mon));
   memcpy(mon,TimeStr+4,2);    
   memset(day,0,sizeof(day));
    memcpy(day,TimeStr+6,2);    
    cur.tm_sec =0;
    cur.tm_min =0;
    cur.tm_hour= 0; 
    cur.tm_mday= atoi(day);
    cur.tm_mon =atoi(mon) - 1;
    cur.tm_year= atoi(year) - 1900; 
    returnmktime(&cur);
}
unsigned long SecStr2Ulong(const char *TimeStr)
{
    struct tmcur; 
    char year[5]= "";
    char mon[3]= "";
    char day[3]= "";
    char hour[3] = "";
    char min[3] = "";
    char sec[3] ="";     
   memset(year,0,sizeof(year));
   memcpy(year,TimeStr,4);    
   memset(mon,0,sizeof(mon));
   memcpy(mon,TimeStr+4,2);    
   memset(day,0,sizeof(day));
    memcpy(day,TimeStr+6,2);   
   memset(hour,0,sizeof(day));
    memcpy(hour,TimeStr+8,2);   
   memset(min,0,sizeof(day));
    memcpy(min,TimeStr+10,2);   
   memset(sec,0,sizeof(day));
    memcpy(sec,TimeStr+12,2);    
    cur.tm_sec =atoi(sec);
    cur.tm_min =atoi(min);
    cur.tm_hour= atoi(hour); 
    cur.tm_mday= atoi(day);
    cur.tm_mon =atoi(mon) - 1;
    cur.tm_year= atoi(year) - 1900; 
    returnmktime(&cur);
}
 

unsigned long NSecStr2Ulong(const char *TimeStr)
{
    struct tmcur; 
    char year[5]= "";
    char mon[3]= "";
    char day[3]= "";
    char hour[3] = "";
    char min[3] = "";
    char sec[3] ="";     
   memset(year,0,sizeof(year));
   memcpy(year,TimeStr,4);    
   memset(mon,0,sizeof(mon));
   memcpy(mon,TimeStr+5,2);    
   memset(day,0,sizeof(day));
    memcpy(day,TimeStr+8,2);   
   memset(hour,0,sizeof(day));
    memcpy(hour,TimeStr+11,2);   
   memset(min,0,sizeof(day));
    memcpy(min,TimeStr+14,2);   
   memset(sec,0,sizeof(day));
    memcpy(sec,TimeStr+17,2);    
    cur.tm_sec =atoi(sec);
    cur.tm_min =atoi(min);
    cur.tm_hour= atoi(hour); 
    cur.tm_mday= atoi(day);
    cur.tm_mon =atoi(mon) - 1;
    cur.tm_year= atoi(year) - 1900; 
    returnmktime(&cur);
}
 

char * Ulong2DayStr(unsigned long time)
{
    struct tm*date;
    static charbuff[17];
   memset(buff,0,sizeof(buff));
    date =localtime(&time);   
   sprintf(buff,"%4d%02d%02d",date->tm_year + 1900, date->tm_mon+ 1,date->tm_mday);
    returnbuff;
}
 

char * Ulong2SecStr(unsigned long time)
{
    struct tm*date;
    static charbuff[17];
   memset(buff,0,sizeof(buff));
    date =localtime(&time);   
   sprintf(buff,"%4d%02d%02d%02d%02d%02d",date->tm_year + 1900,date->tm_mon +1,date->tm_mday,date->tm_hour,date->tm_min,date->tm_sec); 
    returnbuff;
}
 

char * Ulong2NSecStr(unsigned long time)
{
    struct tm*date;
    static charbuff[17];
   memset(buff,0,sizeof(buff));
    date =localtime(&time);   
   sprintf(buff,"%4d-%02d-%02d %02d:%02d:%02d",date->tm_year +1900, date->tm_mon +1,date->tm_mday,date->tm_hour,date->tm_min,date->tm_sec); 
    returnbuff;
}
 
int main()
{
   printf("20071007           => %lu\n",DayStr2Ulong("20071007"));
   printf("20071007000000     => %lu\n",SecStr2Ulong("20071007000000"));
   printf("2007-10-07 00:00:00 => %lu\n",NSecStr2Ulong("2007-10-0700:00:00"));
    
   printf("1191686400         => %s\n",Ulong2DayStr(1191686400));
   printf("1191686400         => %s\n",Ulong2SecStr(1191686400));
   printf("1191686400         => %s\n",Ulong2NSecStr(1191686400));
    
    return0;
    
阅读(1688) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~