#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <time.h>
#include <sys/timeb.h>
#define TONG_LIB_INTERNAL
#include "tong_time.h"
/**********************************************************
* ¸ñʽ»¯Ê±¼äÐÅÏ¢£¬·µ»ØÊ±¼ä´®: YYYYMMDD hh:mm:ss
* ¾«È·µ½Ãë
**********************************************************/
static int DayOfMonth[] = {31,28,31,30,31,30,31,31,30,31,30,31};
/*
Function name : TimeStructToTime
Description : ½«Ê±¼ä½á¹¹×ª»»ÎªÊ±¼äÖµ
Return type : >=0: ת»»ºóµÄʱ¼äÖµ
-1: ʧ°Ü
Argument : int Year
Argument : int Month
Argument : int Day
Argument : int Hour
Argument : int Minute
Argument : int Second
*/
time_t WINAPI TimeStructToTime(int Year, int Month, int Day, int Hour, int Minute, int Second)
{
struct tm tf;
if (Year < 1900)
return -1;
if (Month<1 || Month>12)
return -1;
if (Day < 1)
return -1;
else if (Year%4==0 && Month==2) {
if (Day > (DayOfMonth[Month-1]+1))
return -1;
}
else if (Day > DayOfMonth[Month-1])
return -1;
if (Hour<0 || Hour>=24)
return -1;
if (Minute<0 || Minute >= 60)
return -1;
if (Second<0 || Second >= 60)
return -1;
tf.tm_year = Year - 1900;
tf.tm_mon = Month - 1;
tf.tm_mday = Day;
tf.tm_hour = Hour;
tf.tm_min = Minute;
tf.tm_sec = Second;
tf.tm_isdst = -1;
return mktime(&tf);
}
#define DATEFMT_YEAR 'Y' /* ʱ¼ä¸ñʽÖÐÄêµÄ±êʶ×Ö·û */
#define DATEFMT_MONTH 'M' /* ʱ¼ä¸ñʽÖÐÔµıêʶ×Ö·û */
#define DATEFMT_DAY 'D' /* ʱ¼ä¸ñʽÖÐÈյıêʶ×Ö·û */
#define DATEFMT_HOUR 'h' /* ʱ¼ä¸ñʽÖÐСʱµÄ±êʶ×Ö·û */
#define DATEFMT_MINUTE 'm' /* ʱ¼ä¸ñʽÖзÖÖӵıêʶ×Ö·û */
#define DATEFMT_SECOND 's' /* ʱ¼ä¸ñʽÖÐÃëÊýµÄ±êʶ×Ö·û */
static char *T_TIMEFORMAT = "YYYYMMDDhhmmss";
/*
Function name : DateStrToTimeByFmt
Description : ¸ù¾Ý¸ñʽ½«Ê±¼ä×Ö·û´®×ª»»ÎªÊ±¼äÖµ
Return type : >=0: ת»»ºóµÄʱ¼äÖµ
-1: ʧ°Ü
Argument : char *TBuf ʱ¼ä×Ö·û´®Ö¸Õë
Argument : char *Fmt ¸ñʽÃèÊöÖ¸Õë
*/
time_t WINAPI DateStrToTimeByFmt(char *TBuf, char *Fmt)
{
int Year = 0;
int Month = 0;
int Day = 0;
int Hour = 0;
int Minute = 0;
int Second = 0;
int ValueLen;
int Loc;
int ErrorFlag;
char *Ptr;
if (TBuf == NULL)
return -1;
if (Fmt == NULL)
Fmt = T_TIMEFORMAT;
ValueLen = strlen(TBuf);
Loc = 0;
ErrorFlag = 0;
Ptr = TBuf;
while (*Fmt && Loc<ValueLen) {
switch (*Fmt) {
case DATEFMT_YEAR:
if (isdigit(*Ptr)) {
Year = Year*10 + (*Ptr) - '0';
Ptr ++;
Loc ++;
}
break;
case DATEFMT_MONTH:
if (isdigit(*Ptr)) {
Month = Month*10 + (*Ptr) - '0';
Ptr ++;
Loc ++;
}
break;
case DATEFMT_DAY:
if (isdigit(*Ptr)) {
Day = Day*10 + (*Ptr) - '0';
Ptr ++;
Loc ++;
}
break;
case DATEFMT_HOUR:
if (isdigit(*Ptr)) {
Hour = Hour*10 + (*Ptr) - '0';
Ptr ++;
Loc ++;
}
break;
case DATEFMT_MINUTE:
if (isdigit(*Ptr)) {
Minute = Minute*10 + (*Ptr) - '0';
Ptr ++;
Loc ++;
}
break;
case DATEFMT_SECOND:
if (isdigit(*Ptr)) {
Second = Second*10 + (*Ptr) - '0';
Ptr ++;
Loc ++;
}
break;
default: /* ÊÇ·Ö¸ô·ûºÅ */
if (*Fmt == *Ptr) {
Ptr ++;
Loc ++;
}
else { /* ÓëÄ£°æ¸ñʽ²»Æ¥Åä */
Loc = ValueLen;
ErrorFlag = 1;
}
break;
}
Fmt ++;
}
if (ErrorFlag)
return -1;
if (*Fmt==0 && Loc<ValueLen) /* Ä£°æ½áÊøµ«Êý¾Ýδ½áÊø */
return -1;
return TimeStructToTime(Year, Month, Day, Hour, Minute, Second);
}
/*
Function name : DateStrToTime
Description : ½«YYYYMMHH¸ñʽµÄÈÕÆÚ×Ö·û´®×ª»»ÎªÊ±¼äÖµ
Return type : >=0: ת»»ºóµÄʱ¼äÖµ
-1: ʧ°Ü
Argument : char *TBuf ÈÕÆÚ×Ö·û´®Ö¸Õë
*/
time_t WINAPI DateStrToTime(char *TBuf)
{
/* DateStr format is YYYYMMHH */
struct tm tf;
char buf[10];
if (TBuf == NULL)
return -1;
if (strlen(TBuf) != 8)
return -1;
memset(&tf, 0, sizeof(tf));
memcpy(buf, TBuf, 4);
buf[4] = 0;
tf.tm_year = atoi(buf) - 1900;
memcpy(buf, TBuf+4, 2);
buf[2] = 0;
tf.tm_mon = atoi(buf) - 1;
memcpy(buf, TBuf+6, 2);
buf[2] = 0;
tf.tm_mday = atoi(buf);
tf.tm_hour = 0;
tf.tm_min = 0;
tf.tm_sec = 0;
tf.tm_isdst = -1;
return mktime(&tf);
}
char * WINAPI DateToString(time_t t, char *TimeBuf)
{
struct tm *tf;
if (TimeBuf) {
tf = localtime(&t);
sprintf(TimeBuf, "%4d%02d%02d",
tf->tm_year+1900, tf->tm_mon+1, tf->tm_mday);
}
return TimeBuf;
}
char * WINAPI TimeToString(time_t t, char *TimeBuf)
{
struct tm *tf;
if (TimeBuf) {
tf = localtime(&t);
sprintf(TimeBuf,"%4d%02d%02d%02d%02d%02d",
tf->tm_year+1900, tf->tm_mon+1, tf->tm_mday,
tf->tm_hour, tf->tm_min, tf->tm_sec);
}
return TimeBuf;
}
/*
Function name : *FmtTime
Description : ½«Ê±¼äֵת»»Îª×Ö·û´®¸ñʽ£¬Êä³ö¸ñʽΪYYYYMMDD hh:mm:ss
Return type : ×Ö·û´®¸ñʽʱ¼äµÄÖ¸Õë
Argument : t ʱ¼äÖµ
Argument : TimeBuf ´æ·Å×Ö·û´®¸ñʽʱ¼äµÄ»º³åÇøÖ¸Õë
*/
char * WINAPI FmtTime(time_t t, char *TimeBuf)
{
struct tm *tf;
if (TimeBuf) {
tf=localtime(&t);
if (tf)
sprintf(TimeBuf,"%4d%02d%02d %02d:%02d:%02d",
tf->tm_year+1900,tf->tm_mon+1,tf->tm_mday,
tf->tm_hour,tf->tm_min,tf->tm_sec);
else
TimeBuf[0] = 0;
}
return TimeBuf;
}
static char TimeBuf[30];
char * WINAPI GetCurDate(char *TimeBuf)
{
time_t t;
time(&t);
return DateToString(t, TimeBuf);
}
/*
Function name : *GetCurTime
Description : »ñÈ¡µ±Ç°Ê±¼ä£¬²¢ÒÔ×Ö·û´®ÐÎʽÊä³ö£¬Êä³ö¸ñʽΪYYYYMMDD hh:mm:ss
Return type : ×Ö·û´®¸ñʽʱ¼äµÄÖ¸Õë
*/
char * WINAPI GetCurTime()
{ time_t t;
time(&t);
return FmtTime(t,TimeBuf);
}
/**********************************************************
* ¸ù¾Ýµ±Ç°Ê±¼ä ,·µ»ØÊ±¼ä´®: MMDD hh:mm:ss.ms
* ¾«È·µ½ºÁÃë
**********************************************************/
static char STIME[31];
/*
Function name : *GetCurMTime
Description : »ñÈ¡µ±Ç°ºÁÃ뼶ʱ¼ä£¬²¢ÒÔ×Ö·û´®ÐÎʽÊä³ö£¬Êä³ö¸ñʽΪMMDD hh:mm:ss.ms
Return type : ×Ö·û´®¸ñʽʱ¼äµÄÖ¸Õë
*/
char * WINAPI GetCurMTime()
{
struct tm *t;
struct timeb mtp;
ftime(&mtp);
memset(STIME,0,31);
t = localtime(&mtp.time);
sprintf(STIME, "%02d%02d %02d:%02d:%02d.%03d",
t->tm_mon+1, t->tm_mday, t->tm_hour, t->tm_min, t->tm_sec, mtp.millitm);
return STIME;
}
/**********************************************************
* ÓÐЩϵͳ²»Ö§³Öftime
* ʹÓÃÌõ¼þ±àÒëT_FTIMEÀ´Ê¹Óøö¨Òå
**********************************************************/
#ifdef T_FTIME
/* timeb.h file in /usr/coff/usr/include/bcs/sys */
int ftime(tb)
struct timeb *tb;
{
time(&tb->time);
tb->millitm=0;
return 0;
}
#endif /* T_FTIME */
民主不同于专制,不需要强加于人。--- 賴斯
大道之行也,天下为公,选贤与能,讲信修睦。故人不独亲其亲,不独子其子,使老有所终,壮有所用,幼有所长,矜、寡、孤、独、废疾者,皆有所养。男有分,女有归。货恶其弃於地也,不必藏於己;力恶其不出於身也,不必为己。是故,谋闭而不兴,盗窃乱贼而不作,故外户而不闭,是谓大同。--《礼运·大同篇》