// Code generated by modernc.org/undup from the per-target sqlite_*.go files; DO NOT EDIT.

//go:build (freebsd && arm) || (linux && arm) || (linux && arm64) || (linux && ppc64le) || (linux && riscv64) || (linux && s390x) || (openbsd && arm64)

package sqlite3

import (
	"unsafe"

	"modernc.org/libc"
)

// C documentation
//
//	/*
//	** If the DateTime p is raw number, try to figure out if it is
//	** a julian day number of a unix timestamp.  Set the p value
//	** appropriately.
//	*/
func _autoAdjustDate(tls *libc.TLS, p uintptr) {
	var r float64
	_ = r
	if !(int32(uint32(*(*uint8)(unsafe.Pointer(p + 44))&0x1>>0)) != 0) || (*TDateTime)(unsafe.Pointer(p)).FvalidJD != 0 {
		libc.SetBitFieldPtr8Uint32(p+44, libc.Uint32FromInt32(0), 0, 0x1)
	} else {
		if (*TDateTime)(unsafe.Pointer(p)).Fs >= float64(int64(-libc.Int32FromInt32(21086676))*libc.Int64FromInt32(10000)) && (*TDateTime)(unsafe.Pointer(p)).Fs <= float64(libc.Int64FromInt32(25340230)*libc.Int64FromInt32(10000)+libc.Int64FromInt32(799)) {
			r = float64((*TDateTime)(unsafe.Pointer(p)).Fs*float64(1000)) + float64(2.1086676e+14)
			_clearYMD_HMS_TZ(tls, p)
			(*TDateTime)(unsafe.Pointer(p)).FiJD = int64(r + libc.Float64FromFloat64(0.5))
			(*TDateTime)(unsafe.Pointer(p)).FvalidJD = uint8(1)
			libc.SetBitFieldPtr8Uint32(p+44, libc.Uint32FromInt32(0), 0, 0x1)
		}
	}
}

// C documentation
//
//	/*
//	** Convert from YYYY-MM-DD HH:MM:SS to julian day.  We always assume
//	** that the YYYY-MM-DD is according to the Gregorian calendar.
//	**
//	** Reference:  Meeus page 61
//	*/
func _computeJD(tls *libc.TLS, p uintptr) {
	var A, B, D, M, X1, X2, Y int32
	_, _, _, _, _, _, _ = A, B, D, M, X1, X2, Y
	if (*TDateTime)(unsafe.Pointer(p)).FvalidJD != 0 {
		return
	}
	if (*TDateTime)(unsafe.Pointer(p)).FvalidYMD != 0 {
		Y = (*TDateTime)(unsafe.Pointer(p)).FY
		M = (*TDateTime)(unsafe.Pointer(p)).FM
		D = (*TDateTime)(unsafe.Pointer(p)).FD
	} else {
		Y = int32(2000) /* If no YMD specified, assume 2000-Jan-01 */
		M = int32(1)
		D = int32(1)
	}
	if Y < -int32(4713) || Y > int32(9999) || int32(uint32(*(*uint8)(unsafe.Pointer(p + 44))&0x1>>0)) != 0 {
		_datetimeError(tls, p)
		return
	}
	if M <= int32(2) {
		Y = Y - 1
		M = M + int32(12)
	}
	A = (Y + int32(4800)) / int32(100)
	B = int32(38) - A + A/int32(4)
	X1 = int32(36525) * (Y + int32(4716)) / int32(100)
	X2 = int32(306001) * (M + int32(1)) / int32(10000)
	(*TDateTime)(unsafe.Pointer(p)).FiJD = int64(float64((float64(X1+X2+D+B) - libc.Float64FromFloat64(1524.5)) * libc.Float64FromInt32(86400000)))
	(*TDateTime)(unsafe.Pointer(p)).FvalidJD = uint8(1)
	if (*TDateTime)(unsafe.Pointer(p)).FvalidHMS != 0 {
		**(**Tsqlite3_int64)(__ccgo_up(p)) += int64((*TDateTime)(unsafe.Pointer(p)).Fh*int32(3600000)+(*TDateTime)(unsafe.Pointer(p)).Fm*int32(60000)) + int64(float64((*TDateTime)(unsafe.Pointer(p)).Fs*libc.Float64FromInt32(1000))+libc.Float64FromFloat64(0.5))
		if (*TDateTime)(unsafe.Pointer(p)).Ftz != 0 {
			**(**Tsqlite3_int64)(__ccgo_up(p)) -= int64((*TDateTime)(unsafe.Pointer(p)).Ftz * int32(60000))
			(*TDateTime)(unsafe.Pointer(p)).FvalidYMD = uint8(0)
			(*TDateTime)(unsafe.Pointer(p)).FvalidHMS = uint8(0)
			(*TDateTime)(unsafe.Pointer(p)).Ftz = 0
			libc.SetBitFieldPtr8Uint32(p+44, libc.Uint32FromInt32(1), 3, 0x8)
			libc.SetBitFieldPtr8Uint32(p+44, libc.Uint32FromInt32(0), 4, 0x10)
		}
	}
}

// C documentation
//
//	/*
//	** Compute the Year, Month, and Day from the julian day number.
//	*/
func _computeYMD(tls *libc.TLS, p uintptr) {
	var A, B, C, D, E, X1, Z, alpha, v1 int32
	_, _, _, _, _, _, _, _, _ = A, B, C, D, E, X1, Z, alpha, v1
	if (*TDateTime)(unsafe.Pointer(p)).FvalidYMD != 0 {
		return
	}
	if !((*TDateTime)(unsafe.Pointer(p)).FvalidJD != 0) {
		(*TDateTime)(unsafe.Pointer(p)).FY = int32(2000)
		(*TDateTime)(unsafe.Pointer(p)).FM = int32(1)
		(*TDateTime)(unsafe.Pointer(p)).FD = int32(1)
	} else {
		if !(_validJulianDay(tls, (*TDateTime)(unsafe.Pointer(p)).FiJD) != 0) {
			_datetimeError(tls, p)
			return
		} else {
			Z = int32(((*TDateTime)(unsafe.Pointer(p)).FiJD + libc.Int64FromInt32(43200000)) / libc.Int64FromInt32(86400000))
			alpha = int32((float64(Z)+libc.Float64FromFloat64(32044.75))/libc.Float64FromFloat64(36524.25)) - int32(52)
			A = Z + int32(1) + alpha - (alpha+int32(100))/int32(4) + int32(25)
			B = A + int32(1524)
			C = int32((float64(B) - libc.Float64FromFloat64(122.1)) / libc.Float64FromFloat64(365.25))
			D = int32(36525) * (C & int32(32767)) / int32(100)
			E = int32(float64(B-D) / libc.Float64FromFloat64(30.6001))
			X1 = int32(float64(libc.Float64FromFloat64(30.6001) * float64(E)))
			(*TDateTime)(unsafe.Pointer(p)).FD = B - D - X1
			if E < int32(14) {
				v1 = E - int32(1)
			} else {
				v1 = E - int32(13)
			}
			(*TDateTime)(unsafe.Pointer(p)).FM = v1
			if (*TDateTime)(unsafe.Pointer(p)).FM > int32(2) {
				v1 = C - int32(4716)
			} else {
				v1 = C - int32(4715)
			}
			(*TDateTime)(unsafe.Pointer(p)).FY = v1
		}
	}
	(*TDateTime)(unsafe.Pointer(p)).FvalidYMD = uint8(1)
}

// C documentation
//
//	/*
//	**    datetime( TIMESTRING, MOD, MOD, ...)
//	**
//	** Return YYYY-MM-DD HH:MM:SS
//	*/
func _datetimeFunc(tls *libc.TLS, context uintptr, argc int32, argv uintptr) {
	bp := tls.Alloc(80)
	defer tls.Free(80)
	var Y, n, s int32
	var _ /* x at bp+0 */ TDateTime
	var _ /* zBuf at bp+48 */ [32]uint8
	_, _, _ = Y, n, s
	if _isDate(tls, context, argc, argv, bp) == 0 {
		_computeYMD_HMS(tls, bp)
		Y = (**(**TDateTime)(__ccgo_up(bp))).FY
		if Y < 0 {
			Y = -Y
		}
		(**(**[32]uint8)(__ccgo_up(bp + 48)))[int32(1)] = libc.Uint8FromInt32(int32('0') + Y/int32(1000)%int32(10))
		(**(**[32]uint8)(__ccgo_up(bp + 48)))[int32(2)] = libc.Uint8FromInt32(int32('0') + Y/int32(100)%int32(10))
		(**(**[32]uint8)(__ccgo_up(bp + 48)))[int32(3)] = libc.Uint8FromInt32(int32('0') + Y/int32(10)%int32(10))
		(**(**[32]uint8)(__ccgo_up(bp + 48)))[int32(4)] = libc.Uint8FromInt32(int32('0') + Y%int32(10))
		(**(**[32]uint8)(__ccgo_up(bp + 48)))[int32(5)] = uint8('-')
		(**(**[32]uint8)(__ccgo_up(bp + 48)))[int32(6)] = libc.Uint8FromInt32(int32('0') + (**(**TDateTime)(__ccgo_up(bp))).FM/int32(10)%int32(10))
		(**(**[32]uint8)(__ccgo_up(bp + 48)))[int32(7)] = libc.Uint8FromInt32(int32('0') + (**(**TDateTime)(__ccgo_up(bp))).FM%int32(10))
		(**(**[32]uint8)(__ccgo_up(bp + 48)))[int32(8)] = uint8('-')
		(**(**[32]uint8)(__ccgo_up(bp + 48)))[int32(9)] = libc.Uint8FromInt32(int32('0') + (**(**TDateTime)(__ccgo_up(bp))).FD/int32(10)%int32(10))
		(**(**[32]uint8)(__ccgo_up(bp + 48)))[int32(10)] = libc.Uint8FromInt32(int32('0') + (**(**TDateTime)(__ccgo_up(bp))).FD%int32(10))
		(**(**[32]uint8)(__ccgo_up(bp + 48)))[int32(11)] = uint8(' ')
		(**(**[32]uint8)(__ccgo_up(bp + 48)))[int32(12)] = libc.Uint8FromInt32(int32('0') + (**(**TDateTime)(__ccgo_up(bp))).Fh/int32(10)%int32(10))
		(**(**[32]uint8)(__ccgo_up(bp + 48)))[int32(13)] = libc.Uint8FromInt32(int32('0') + (**(**TDateTime)(__ccgo_up(bp))).Fh%int32(10))
		(**(**[32]uint8)(__ccgo_up(bp + 48)))[int32(14)] = uint8(':')
		(**(**[32]uint8)(__ccgo_up(bp + 48)))[int32(15)] = libc.Uint8FromInt32(int32('0') + (**(**TDateTime)(__ccgo_up(bp))).Fm/int32(10)%int32(10))
		(**(**[32]uint8)(__ccgo_up(bp + 48)))[int32(16)] = libc.Uint8FromInt32(int32('0') + (**(**TDateTime)(__ccgo_up(bp))).Fm%int32(10))
		(**(**[32]uint8)(__ccgo_up(bp + 48)))[int32(17)] = uint8(':')
		if int32(uint32(*(*uint8)(unsafe.Pointer(bp + 44))&0x4>>2)) != 0 {
			s = int32(float64(libc.Float64FromFloat64(1000)*(**(**TDateTime)(__ccgo_up(bp))).Fs) + libc.Float64FromFloat64(0.5))
			(**(**[32]uint8)(__ccgo_up(bp + 48)))[int32(18)] = libc.Uint8FromInt32(int32('0') + s/int32(10000)%int32(10))
			(**(**[32]uint8)(__ccgo_up(bp + 48)))[int32(19)] = libc.Uint8FromInt32(int32('0') + s/int32(1000)%int32(10))
			(**(**[32]uint8)(__ccgo_up(bp + 48)))[int32(20)] = uint8('.')
			(**(**[32]uint8)(__ccgo_up(bp + 48)))[int32(21)] = libc.Uint8FromInt32(int32('0') + s/int32(100)%int32(10))
			(**(**[32]uint8)(__ccgo_up(bp + 48)))[int32(22)] = libc.Uint8FromInt32(int32('0') + s/int32(10)%int32(10))
			(**(**[32]uint8)(__ccgo_up(bp + 48)))[int32(23)] = libc.Uint8FromInt32(int32('0') + s%int32(10))
			(**(**[32]uint8)(__ccgo_up(bp + 48)))[int32(24)] = uint8(0)
			n = int32(24)
		} else {
			s = int32((**(**TDateTime)(__ccgo_up(bp))).Fs)
			(**(**[32]uint8)(__ccgo_up(bp + 48)))[int32(18)] = libc.Uint8FromInt32(int32('0') + s/int32(10)%int32(10))
			(**(**[32]uint8)(__ccgo_up(bp + 48)))[int32(19)] = libc.Uint8FromInt32(int32('0') + s%int32(10))
			(**(**[32]uint8)(__ccgo_up(bp + 48)))[int32(20)] = uint8(0)
			n = int32(20)
		}
		if (**(**TDateTime)(__ccgo_up(bp))).FY < 0 {
			(**(**[32]uint8)(__ccgo_up(bp + 48)))[0] = uint8('-')
			Xsqlite3_result_text(tls, context, bp+48, n, uintptr(-libc.Int32FromInt32(1)))
		} else {
			Xsqlite3_result_text(tls, context, bp+48+1, n-int32(1), uintptr(-libc.Int32FromInt32(1)))
		}
	}
}

// C documentation
//
//	/*
//	** Input "r" is a numeric quantity which might be a julian day number,
//	** or the number of seconds since 1970.  If the value if r is within
//	** range of a julian day number, install it as such and set validJD.
//	** If the value is a valid unix timestamp, put it in p->s and set p->rawS.
//	*/
func _setRawDateNumber(tls *libc.TLS, p uintptr, r float64) {
	(*TDateTime)(unsafe.Pointer(p)).Fs = r
	libc.SetBitFieldPtr8Uint32(p+44, libc.Uint32FromInt32(1), 0, 0x1)
	if r >= float64(0) && r < float64(5.3734845e+06) {
		(*TDateTime)(unsafe.Pointer(p)).FiJD = int64(float64(r*libc.Float64FromFloat64(8.64e+07)) + libc.Float64FromFloat64(0.5))
		(*TDateTime)(unsafe.Pointer(p)).FvalidJD = uint8(1)
	}
}

// C documentation
//
//	/*
//	**    time( TIMESTRING, MOD, MOD, ...)
//	**
//	** Return HH:MM:SS
//	*/
func _timeFunc(tls *libc.TLS, context uintptr, argc int32, argv uintptr) {
	bp := tls.Alloc(64)
	defer tls.Free(64)
	var n, s int32
	var _ /* x at bp+0 */ TDateTime
	var _ /* zBuf at bp+48 */ [16]uint8
	_, _ = n, s
	if _isDate(tls, context, argc, argv, bp) == 0 {
		_computeHMS(tls, bp)
		(**(**[16]uint8)(__ccgo_up(bp + 48)))[0] = libc.Uint8FromInt32(int32('0') + (**(**TDateTime)(__ccgo_up(bp))).Fh/int32(10)%int32(10))
		(**(**[16]uint8)(__ccgo_up(bp + 48)))[int32(1)] = libc.Uint8FromInt32(int32('0') + (**(**TDateTime)(__ccgo_up(bp))).Fh%int32(10))
		(**(**[16]uint8)(__ccgo_up(bp + 48)))[int32(2)] = uint8(':')
		(**(**[16]uint8)(__ccgo_up(bp + 48)))[int32(3)] = libc.Uint8FromInt32(int32('0') + (**(**TDateTime)(__ccgo_up(bp))).Fm/int32(10)%int32(10))
		(**(**[16]uint8)(__ccgo_up(bp + 48)))[int32(4)] = libc.Uint8FromInt32(int32('0') + (**(**TDateTime)(__ccgo_up(bp))).Fm%int32(10))
		(**(**[16]uint8)(__ccgo_up(bp + 48)))[int32(5)] = uint8(':')
		if int32(uint32(*(*uint8)(unsafe.Pointer(bp + 44))&0x4>>2)) != 0 {
			s = int32(float64(libc.Float64FromFloat64(1000)*(**(**TDateTime)(__ccgo_up(bp))).Fs) + libc.Float64FromFloat64(0.5))
			(**(**[16]uint8)(__ccgo_up(bp + 48)))[int32(6)] = libc.Uint8FromInt32(int32('0') + s/int32(10000)%int32(10))
			(**(**[16]uint8)(__ccgo_up(bp + 48)))[int32(7)] = libc.Uint8FromInt32(int32('0') + s/int32(1000)%int32(10))
			(**(**[16]uint8)(__ccgo_up(bp + 48)))[int32(8)] = uint8('.')
			(**(**[16]uint8)(__ccgo_up(bp + 48)))[int32(9)] = libc.Uint8FromInt32(int32('0') + s/int32(100)%int32(10))
			(**(**[16]uint8)(__ccgo_up(bp + 48)))[int32(10)] = libc.Uint8FromInt32(int32('0') + s/int32(10)%int32(10))
			(**(**[16]uint8)(__ccgo_up(bp + 48)))[int32(11)] = libc.Uint8FromInt32(int32('0') + s%int32(10))
			(**(**[16]uint8)(__ccgo_up(bp + 48)))[int32(12)] = uint8(0)
			n = int32(12)
		} else {
			s = int32((**(**TDateTime)(__ccgo_up(bp))).Fs)
			(**(**[16]uint8)(__ccgo_up(bp + 48)))[int32(6)] = libc.Uint8FromInt32(int32('0') + s/int32(10)%int32(10))
			(**(**[16]uint8)(__ccgo_up(bp + 48)))[int32(7)] = libc.Uint8FromInt32(int32('0') + s%int32(10))
			(**(**[16]uint8)(__ccgo_up(bp + 48)))[int32(8)] = uint8(0)
			n = int32(8)
		}
		Xsqlite3_result_text(tls, context, bp+48, n, uintptr(-libc.Int32FromInt32(1)))
	}
}
