timestamp.c

来自「PostgreSQL7.4.6 for Linux」· C语言 代码 · 共 841 行 · 第 1/2 页

C
841
字号
					replace_val.uint_val = tm->tm_mday;					replace_type = PGTYPES_TYPE_UINT_2_LZ;					break;				case 'D':					/*					 * ts, dDate, dow, tm is information about the					 * timestamp					 *					 * q is the start of the current output buffer					 *					 * pstr_len is a pointer to the remaining size of output,					 * i.e. the size of q					 */					i = dttofmtasc_replace(ts, dDate, dow, tm,										   q, pstr_len,										   "%m/%d/%y");					if (i)						return i;					break;				case 'e':					replace_val.uint_val = tm->tm_mday;					replace_type = PGTYPES_TYPE_UINT_2_LS;					break;				case 'E':					{						char		tmp[4] = "%Ex";						p++;						if (*p == '\0')							return -1;						tmp[2] = *p;						/* XXX: fall back to strftime */						/*						 * strftime's month is 0 based, ours is 1 based						 */						tm->tm_mon -= 1;						i = strftime(q, *pstr_len, tmp, tm);						if (i == 0)							return -1;						while (*q)						{							q++;							(*pstr_len)--;						}						tm->tm_mon += 1;						replace_type = PGTYPES_TYPE_NOTHING;						break;					}				case 'G':					/* XXX: fall back to strftime */					tm->tm_mon -= 1;					i = strftime(q, *pstr_len, "%G", tm);					if (i == 0)						return -1;					while (*q)					{						q++;						(*pstr_len)--;					}					tm->tm_mon += 1;					replace_type = PGTYPES_TYPE_NOTHING;					break;				case 'g':					/* XXX: fall back to strftime */					{						char	   *fmt = "%g"; /* Keep compiler quiet												 * about 2-digit year */						tm->tm_mon -= 1;						i = strftime(q, *pstr_len, fmt, tm);						if (i == 0)							return -1;						while (*q)						{							q++;							(*pstr_len)--;						}						tm->tm_mon += 1;						replace_type = PGTYPES_TYPE_NOTHING;					}					break;				case 'H':					replace_val.uint_val = tm->tm_hour;					replace_type = PGTYPES_TYPE_UINT_2_LZ;					break;				case 'I':					replace_val.uint_val = tm->tm_hour % 12;					replace_type = PGTYPES_TYPE_UINT_2_LZ;					break;				case 'j':					replace_val.uint_val = tm->tm_yday;					replace_type = PGTYPES_TYPE_UINT_3_LZ;					break;				case 'k':					replace_val.uint_val = tm->tm_hour;					replace_type = PGTYPES_TYPE_UINT_2_LS;					break;				case 'l':					replace_val.uint_val = tm->tm_hour % 12;					replace_type = PGTYPES_TYPE_UINT_2_LS;					break;				case 'm':					replace_val.uint_val = tm->tm_mon;					replace_type = PGTYPES_TYPE_UINT_2_LZ;					break;				case 'M':					replace_val.uint_val = tm->tm_min;					replace_type = PGTYPES_TYPE_UINT_2_LZ;					break;				case 'n':					replace_val.char_val = '\n';					replace_type = PGTYPES_TYPE_CHAR;					break;				case 'p':					if (tm->tm_hour < 12)						replace_val.str_val = "AM";					else						replace_val.str_val = "PM";					replace_type = PGTYPES_TYPE_STRING_CONSTANT;					break;				case 'P':					if (tm->tm_hour < 12)						replace_val.str_val = "am";					else						replace_val.str_val = "pm";					replace_type = PGTYPES_TYPE_STRING_CONSTANT;					break;				case 'r':					i = dttofmtasc_replace(ts, dDate, dow, tm,										   q, pstr_len,										   "%I:%M:%S %p");					if (i)						return i;					break;				case 'R':					i = dttofmtasc_replace(ts, dDate, dow, tm,										   q, pstr_len,										   "%H:%M");					if (i)						return i;					break;				case 's':#ifdef HAVE_INT64_TIMESTAMP					replace_val.int64_val = ((*ts - SetEpochTimestamp()) / 1000000e0);					replace_type = PGTYPES_TYPE_INT64;#else					replace_val.double_val = *ts - SetEpochTimestamp();					replace_type = PGTYPES_TYPE_DOUBLE_NF;#endif					break;				case 'S':					replace_val.uint_val = tm->tm_sec;					replace_type = PGTYPES_TYPE_UINT_2_LZ;					break;				case 't':					replace_val.char_val = '\t';					replace_type = PGTYPES_TYPE_CHAR;					break;				case 'T':					i = dttofmtasc_replace(ts, dDate, dow, tm,										   q, pstr_len,										   "%H:%M:%S");					if (i)						return i;					break;				case 'u':					if (dow == 0)						dow = 7;					replace_val.uint_val = dow;					replace_type = PGTYPES_TYPE_UINT;					break;				case 'U':					/* XXX: fall back to strftime */					tm->tm_mon -= 1;					i = strftime(q, *pstr_len, "%U", tm);					if (i == 0)						return -1;					while (*q)					{						q++;						(*pstr_len)--;					}					tm->tm_mon += 1;					replace_type = PGTYPES_TYPE_NOTHING;					break;				case 'V':					/* XXX: fall back to strftime */					i = strftime(q, *pstr_len, "%V", tm);					if (i == 0)						return -1;					while (*q)					{						q++;						(*pstr_len)--;					}					replace_type = PGTYPES_TYPE_NOTHING;					break;				case 'w':					replace_val.uint_val = dow;					replace_type = PGTYPES_TYPE_UINT;					break;				case 'W':					/* XXX: fall back to strftime */					tm->tm_mon -= 1;					i = strftime(q, *pstr_len, "%U", tm);					if (i == 0)						return -1;					while (*q)					{						q++;						(*pstr_len)--;					}					tm->tm_mon += 1;					replace_type = PGTYPES_TYPE_NOTHING;					break;				case 'x':					/* XXX: fall back to strftime */					{						char	   *fmt = "%x"; /* Keep compiler quiet												 * about 2-digit year */						tm->tm_mon -= 1;						i = strftime(q, *pstr_len, fmt, tm);						if (i == 0)							return -1;						while (*q)						{							q++;							(*pstr_len)--;						}						tm->tm_mon += 1;						replace_type = PGTYPES_TYPE_NOTHING;					}					break;				case 'X':					/* XXX: fall back to strftime */					tm->tm_mon -= 1;					i = strftime(q, *pstr_len, "%X", tm);					if (i == 0)						return -1;					while (*q)					{						q++;						(*pstr_len)--;					}					tm->tm_mon += 1;					replace_type = PGTYPES_TYPE_NOTHING;					break;				case 'y':					replace_val.uint_val = tm->tm_year % 100;					replace_type = PGTYPES_TYPE_UINT_2_LZ;					break;				case 'Y':					replace_val.uint_val = tm->tm_year;					replace_type = PGTYPES_TYPE_UINT;					break;				case 'z':					/* XXX: fall back to strftime */					tm->tm_mon -= 1;					i = strftime(q, *pstr_len, "%z", tm);					if (i == 0)						return -1;					while (*q)					{						q++;						(*pstr_len)--;					}					tm->tm_mon += 1;					replace_type = PGTYPES_TYPE_NOTHING;					break;				case 'Z':					/* XXX: fall back to strftime */					tm->tm_mon -= 1;					i = strftime(q, *pstr_len, "%Z", tm);					if (i == 0)						return -1;					while (*q)					{						q++;						(*pstr_len)--;					}					tm->tm_mon += 1;					replace_type = PGTYPES_TYPE_NOTHING;					break;				case '%':					replace_val.char_val = '%';					replace_type = PGTYPES_TYPE_CHAR;					break;				case '\0':					/* fmtstr: blabla%' */					/*					 * this is not compliant to the specification					 */					return -1;				default:					/*					 * if we don't know the pattern, we just copy it					 */					if (*pstr_len > 1)					{						*q = '%';						q++;						(*pstr_len)--;						if (*pstr_len > 1)						{							*q = *p;							q++;							(*pstr_len)--;						}						else						{							*q = '\0';							return -1;						}						*q = '\0';					}					else						return -1;					break;			}			i = pgtypes_fmt_replace(replace_val, replace_type, &q, pstr_len);			if (i)				return i;		}		else		{			if (*pstr_len > 1)			{				*q = *p;				(*pstr_len)--;				q++;				*q = '\0';			}			else				return -1;		}		p++;	}	return 0;}intPGTYPEStimestamp_fmt_asc(timestamp *ts, char *output, int str_len, char *fmtstr){	struct tm	tm;	fsec_t		fsec;	date		dDate;	int			dow;	dDate = PGTYPESdate_from_timestamp(*ts);	dow = PGTYPESdate_dayofweek(dDate);	timestamp2tm(*ts, NULL, &tm, &fsec, NULL);	return dttofmtasc_replace(ts, dDate, dow, &tm, output, &str_len, fmtstr);}intPGTYPEStimestamp_sub(timestamp *ts1, timestamp *ts2, interval *iv){	if (TIMESTAMP_NOT_FINITE(*ts1) || TIMESTAMP_NOT_FINITE(*ts2))		return PGTYPES_TS_ERR_EINFTIME;	else#ifdef HAVE_INT64_TIMESTAMP		iv->time = (ts1 - ts2);#else		iv->time = JROUND(ts1 - ts2);#endif	iv->month = 0;	return 0;}intPGTYPEStimestamp_defmt_asc(char *str, char *fmt, timestamp *d){	int			year,				month,				day;	int			hour,				minute,				second;	int			tz;	int			i;	char	   *mstr;	char	   *mfmt;	if (!fmt)		fmt = "%Y-%m-%d %H:%M:%S";	if (!fmt[0])		return 1;	mstr = pgtypes_strdup(str);	mfmt = pgtypes_strdup(fmt);	/*	 * initialize with impossible values so that we can see if the fields	 * where specified at all	 */	/* XXX ambiguity with 1 BC for year? */	year = -1;	month = -1;	day = -1;	hour = 0;	minute = -1;	second = -1;	tz = 0;	i = PGTYPEStimestamp_defmt_scan(&mstr, mfmt, d, &year, &month, &day, &hour, &minute, &second, &tz);	free(mstr);	free(mfmt);	return i;}

⌨️ 快捷键说明

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