⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 mytime.c

📁 API
💻 C
字号:
/*
Name:	mytime.c
Func:	Obtain the string of current date/time , option with "/" and ":".
	( 获取字符串格式的系统日期/时间, 可选带分隔符 "/", ":" )
By:	Lin Sijian, Fujian STAR CO.
Time:	1996/04/19 
*/



#include	<sys/types.h>
#include	<sys/timeb.h>
#include	<time.h>
#include	"mytools.h"



datetime ( str, dort, fmts1, fmts2, fmts3 )

char	* str;
int	dort;
char	* fmts1,	* fmts2,	* fmts3;

{

	char	fmt[ 40 ];
	long	clock;
	struct	tm	* tm;




	time (  & clock  );
	tm = localtime (  & clock  );



	switch ( dort ) {

	case	DATE_ONLY:
			sprintf ( fmt, "%%04d%s%%02d%s%%02d%s",
					fmts1, fmts2, fmts3 );
			sprintf ( str, fmt, 
					(tm -> tm_year ) + 1900,
					(tm -> tm_mon  ) + 1,
					(tm -> tm_mday ) );
			break;


	case	TIME_ONLY:
			sprintf ( fmt, "%%02d%s%%02d%s%%02d%s",
					fmts1, fmts2, fmts3 );
			sprintf ( str, fmt, 
					(tm -> tm_hour ) ,
					(tm -> tm_min  ) ,
					(tm -> tm_sec  ) );
			break;


	default	:
			return ( -1 );
	}



	return ( 0 );
}

milltime( str, fmt1 )
char	*str;
char	*fmt1;
{
struct	timeb	tp;
struct	tm	*tm;
char	fmt[20];

	ftime( &tp );

	tm = localtime (  &(tp.time)  );
	sprintf ( fmt, "%%02d%s%%02d%s%%02d%s%%03d", fmt1, fmt1, fmt1 );
	sprintf ( str, fmt, (tm -> tm_hour ) , (tm -> tm_min  ) ,
		(tm -> tm_sec  ),	( tp.millitm ) );
}

#ifndef zgx010421_added

static int day_tab[2][13] = {
	{0,31,28,31,30,31,30,31,31,30,31,30,31},
	{0,31,29,31,30,31,30,31,31,30,31,30,31}
};

/*
 *	计算两个日期之间的天数.
 *	若date1>date2, 则为负数;
 *	否则, 为正.
 */
have_days(date1, date2)
int	date1, date2;
{
	char	flag;
	int	yy, mm, dd;
	int	mon1, mon2;
	int	days = 0;
	int	i, leap = 0;
	int	year;

	if (date1 > date2) {
		yy = date2;
		date2 = date1;
		date1 = yy;
		flag = 1;
	} else
		flag = 0;

	mon1 = date1/100;
	mon2 = date2/100;

	for (i = mon1; i < mon2; ) {
		year = i / 100;		/* 199605=>1996 */
		leap = (year % 4 == 0) && (year % 100 != 0) || (year % 400 == 0);
		days += day_tab[leap][i%100];
		if (i % 100 == 12)
			i = (i/100+1)*100+1;
		else
			i++;
	}

	days += date2%100-date1%100;

	if (flag)
		return(-days);
	return(days);
}

/*
 *	计算同一日期两个时间之间的秒数.
 *	若time1>time2, 则为负数;
 *	否则, 为正.
 */
have_secs(time1, time2)
int	time1, time2;
{
	long	sec1, sec2;

	sec1 = (time1 / 10000 % 100) * 3600 + time1 / 100 % 100 + time1 % 100;
	sec2 = (time2 / 10000 % 100) * 3600 + time2 / 100 % 100 + time2 % 100;

	return(sec2 - sec1);
}

#endif	/* zgx010421_added */

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -