选择显示字体大小

unix环境下的日期程序

前段时间做了一个计费程序,其中涉及到有关日期与时间的计算,如求某日某时的前(或后)一段时间是什么时候,unix c系统本身并未提供此类函数,笔者经摸索,设计了一个求时间的函数,现介绍给大家。

功能介绍与参数说明 
该函数的主要功能是根据给定的日期时间及时长求出此前或后(bill_long为负)的日期时间及其星期。 

  参数说明如下: 

  s:为给定的日期时间,如2001年6月2日18点30分04秒为“20010602183004”;

  bill_long:给定的时间长度,单位为秒;

  d_str:求出的日期,如2001年6月2日为“20010602”;

  t_str:求出的时间,如18点28分06秒为“182806”;

  函数返回值为星期,如星期日为0,星期一至六分别对应1~6。

实现代码
  该函数的具体实现代码如下:

  int getdatetime( char s,long int bill_long,chard_str, char t_str) {

   time_t timer,tim ;

   struct tm tb, tb1 ;

   int year_off = 1900 ;

   int mon_off = 1 ;

   char s1[20] ;

   if ( strlen( s )!=14 )

   return -1;

   strncpy( s1, s, 4 );

   s1[4] = '\0' ;

   tb.tm_year = atoi( s1 );

   strncpy( s1, s+4, 2 );

   s1[2] = '\0' ;

   tb.tm_mon = atoi( s1 );

   strncpy( s1, s+6, 2 );

   s1[2] = '\0' ;

   tb.tm_mday = atoi( s1 );

   if ( tb.tm_year==0 tb.tm_mon==0 tb.tm_mday==0 )

   return -1;

   strncpy( s1, s+8, 2 );

   s1[2] = '\0' ;

   tb.tm_hour = atoi( s1 );

   strncpy( s1, s+10, 2 );

   s1[2] = '\0' ;

   tb.tm_min = atoi( s1 );

   strncpy( s1, s+12, 2 );

   s1[2] = '\0' ;

   tb.tm_sec = atoi( s1 );

   tb.tm_year -= year_off ;

   tb.tm_mon -= mon_off ;

   tb.tm_isdst = 0 ;

   tim=mktime( &tb ) ;

   tim=tim-bill_long;

   tb1=localtime(&tim);

   sprintf(d_str, "%#04d%#02d%#02d",1900+tb1->tm_year,tb1->tm_mon+1,tb1->tm_mday);

   sprintf(t_str, "%#02d%#02d%#02d",tb1->tm_hour, tb1->tm_min,tb1->tm_sec);

   return (tb1->tm_wday);

  } / end of getdatetime /


  该函数不仅可求出某个时间前(后)一段时长的日期与时间,而且可得出这个日期是星期几,给程序设计带来不少便利,也方便了费用的计算与核实,读者可直接调用该函数。


 


关键字 本文所属关键字

相关 与本文相关文章

分类 所有文章关键字导航

源码编程相关

Java   Asp   PHP   .Net   XML   C/C++   CGI   VB   Jsp   J2ee   J2se   J2me   EJB   Servlet   Tomcat   Resin   Struts   Weblogic   Eclipse   ANT   GUI   JMS   Web servise   IDEA   Webphere   Hibernate   Spring   Jboss   Applet   Swing   Socket   Javamail   Perl   Ajax   P2P   安全   模式   框架   测试   开源   游戏

SQL数据库相关

My-SQL   Ms-SQL   Access   DB2   Oracle   Sybase   SQLserver   索引   存储过程   加密   数据库   分页   视图  

手机无线相关

3G   Wap   CDMA   GRPS   GSM   IVR   彩信   短信   无线   增值业务

网页设计制作相关

HTML   CSS   网页配色   网页特效   Javascript   VBscript   Dreamweaver   Frontpage   JS   Web   网站设计

网站建设推广相关

建站经验   网站优化   网站排名   推广   Alexa

操作系统/服务器相关

Windows XP   Windows 2000   Windows 2003   Windows Me   Windows 9.x   Linux   UNIX   注册表   操作系统   服务器   应用服务器

图形图像多媒体相关

Photoshop   Fireworks   Flash   Coreldraw   Illustrator   Freehand   Photoimpact   多媒体   图形图像

标准 网站致力的规范

Valid CSS!

无不良内容,无不良广告,无恶意代码

Valid XHTML 1.0 Transitional

creativecommons