Documentation
¶
Overview ¶
Funciones de conversión de fechas, precios, intervalos, números, uuids, lógicas y opciones a textos y viceversa
Index ¶
- func AddInterval(t pgtype.Timestamp, i pgtype.Interval) (result pgtype.Timestamp)
- func MustParseFechaHora(s string) time.Time
- func MustParseHora(s string) time.Time
- func MustParseUUID(uuid string) pgtype.UUID
- func ParseDate(s string, ff FormatoFecha) (result pgtype.Date, err error)
- func ParseDuracion(s string) (result time.Duration, err error)
- func ParseFechaHora(s string, ff FormatoFecha) (result time.Time, err error)
- func ParseHora(s string) (result time.Time, err error)
- func ParseIntervalAMSD(s string) (result pgtype.Interval, err error)
- func ParseIntervalHMS(s string) (result pgtype.Interval, err error)
- func ParseLogica(s string) (bool, error)
- func ParseNumero(v, sepDecimal string) (result float64, err error)
- func ParseOpcion(opcion string, admitidas ...string) (string, error)
- func ParsePrecio(p string, fp FormatoPrecio) (result float64, err error)
- func ParseTime(s string) (result pgtype.Time, err error)
- func ParseTimestamp(s string, ff FormatoFecha) (result pgtype.Timestamp, err error)
- func ParseUUID(uuid string) (result pgtype.UUID, err error)
- func PrintDate(fh pgtype.Date, ff FormatoFecha) string
- func PrintDuracion(z time.Duration) string
- func PrintDuracionIso(z time.Duration) string
- func PrintFecha(fh time.Time, ff FormatoFecha) string
- func PrintFechaHora(fh time.Time, ff FormatoFecha) string
- func PrintHora(fh time.Time, secs bool) string
- func PrintInterval(z pgtype.Interval) string
- func PrintIntervalIso(z pgtype.Interval) string
- func PrintNumero(v float64, decimales int, sepDecimal, sepMiles string) string
- func PrintPrecio(v float64, fp FormatoPrecio) string
- func PrintTime(fh pgtype.Time, secs bool) string
- func PrintTimestamp(fh pgtype.Timestamp, ff FormatoFecha) string
- func PrintUUID(uuid pgtype.UUID) string
- func RedondeaPrecio(v float64, fp FormatoPrecio) float64
- func SubInterval(t pgtype.Timestamp, i pgtype.Interval) (result pgtype.Timestamp)
- type FormatoFecha
- type FormatoPrecio
Examples ¶
- AddInterval
- MustParseFechaHora
- MustParseHora
- MustParseUUID
- ParseDate
- ParseDuracion
- ParseFechaHora
- ParseHora
- ParseIntervalAMSD
- ParseIntervalHMS
- ParseLogica
- ParseNumero
- ParseOpcion
- ParsePrecio
- ParseTime
- ParseTimestamp
- ParseUUID
- PrintDate
- PrintDuracion
- PrintDuracionIso
- PrintFecha
- PrintFechaHora
- PrintHora
- PrintInterval
- PrintIntervalIso
- PrintNumero
- PrintPrecio
- PrintTime
- PrintTimestamp
- PrintUUID
- RedondeaPrecio
- SubInterval
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func AddInterval ¶
Añade un pgtype.Interval a un pgtype.Timestamp
Example ¶
unDia := pgtype.Interval{Days: 1, Status: pgtype.Present} fechaInicial, err := ParseTimestamp("4/7/2023 12:00", DMA) errores.PanicIfError(err) fechaSiguiente := AddInterval(fechaInicial, unDia) fmt.Println(PrintTimestamp(fechaSiguiente, DMA))
Output: 05/07/2023 12:00
func MustParseFechaHora ¶
Parsea una fecha con hora opcional en formato ISO
Example ¶
fh := MustParseFechaHora("2023-10-12T12:45:00") fmt.Println(PrintFechaHora(fh, DMA))
Output: 12/10/2023 12:45
func MustParseHora ¶
Parsea una hora
Example ¶
h := MustParseHora("15:16:17") fmt.Println(PrintHora(h, true))
Output: 15:16:17
func MustParseUUID ¶
Parsea un objeto a UUID, los vacíos se consideran NULL, panic si error
Example ¶
uuid := MustParseUUID("7e2a8034-c319-4dfa-a846-e2c176aba2e4") fmt.Println(PrintUUID(uuid))
Output: 7e2a8034-c319-4dfa-a846-e2c176aba2e4
func ParseDate ¶
func ParseDate(s string, ff FormatoFecha) (result pgtype.Date, err error)
Parsea una fecha a pgtype.Date. Mismas consideraciones que ParseFechaHora. Los vacios se consideran null.
Example ¶
d, err := ParseDate("25/4/1974", DMA) errores.PanicIfError(err) fmt.Println(PrintDate(d, AMD))
Output: 1974-04-25
func ParseDuracion ¶
Parsea una duración a time.Duration. Soporta H horas, M minutos o S segundos.
Example ¶
d, err := ParseDuracion("48h") errores.PanicIfError(err) fmt.Println(PrintDuracion(d))
Output: 48h
func ParseFechaHora ¶
func ParseFechaHora(s string, ff FormatoFecha) (result time.Time, err error)
Parsea una fecha con hora opcional Admite varios tipos de fecha: ff=ISO (ISO A-M-D), ff=MDA (USA M/D/A), ff=AMD (internacional A-M-D), ff=DMA (resto D/M/A) Para MDA, AMD y DMA también se admiten . y - como separador de fecha, el separador de hora siempre es :
Example ¶
fh, err := ParseFechaHora("12/10/2023 12:45", DMA) errores.PanicIfError(err) fmt.Println(PrintFechaHora(fh, ISO))
Output: 2023-10-12T12:45:00
func ParseHora ¶
Parsea una hora
Example ¶
h, err := ParseHora("12:45") errores.PanicIfError(err) fmt.Println(PrintHora(h, false))
Output: 12:45
func ParseIntervalAMSD ¶
Parsea una duración a pgtype.Interval. Soporta A años, M meses, S semanas o D dias.
Example ¶
i, err := ParseIntervalAMSD("30d") errores.PanicIfError(err) fmt.Println(PrintInterval(i))
Output: 30d
func ParseIntervalHMS ¶
Parsea una duración a pgtype.Interval. Soporta H horas, M minutos o S segundos.
Example ¶
i, err := ParseIntervalHMS("48h") errores.PanicIfError(err) fmt.Println(PrintInterval(i))
Output: 48h
func ParseLogica ¶
Parsea una variable lógica, soporta: true, false, yes, si, no, 1 y 0
Example ¶
f, err := ParseLogica("Si") errores.PanicIfError(err) fmt.Println(f) f, err = ParseLogica("F") errores.PanicIfError(err) fmt.Println(f) _, err = ParseLogica("ZERO") fmt.Println(err)
Output: true false valor "ZERO" no reconocido
func ParseNumero ¶
Parsea un numero, considerando que puede incluir separadores
Example ¶
fmt.Println(PrintNumero(12345.6789, 2, ",", ".")) fmt.Println(PrintNumero(12345.6789, 0, ",", ".")) fmt.Println(PrintNumero(12345.6789, -2, ",", "."))
Output: 12.345,68 12.346 12.300
func ParseOpcion ¶
Se asegura de que la opción sea alguna de las admitidas, en mayúsculas o minúsculas. Opcionalmente se puede hacer una conversion si admitido tiene el formato "opcion->convertido" La opción por defecto se puede indicar con el formato "->opcion"
Example ¶
opt, err := ParseOpcion("tres", "Uno", "Dos", "Tres") errores.PanicIfError(err) fmt.Println(opt) opt, err = ParseOpcion("tres", "Uno->1", "Dos->2", "Tres->3") errores.PanicIfError(err) fmt.Println(opt) _, err = ParseOpcion("cuatro", "Uno", "Dos", "Tres") fmt.Println(err)
Output: Tres 3 opción "cuatro" no reconocida
func ParsePrecio ¶
func ParsePrecio(p string, fp FormatoPrecio) (result float64, err error)
Parsea un precio
Example ¶
p, err := ParsePrecio(" 1 234,56 € ", EUR) errores.PanicIfError(err) fmt.Println(p)
Output: 1234.56
func ParseTime ¶
Parsea una hora a pgtype.Time. Mismas consideraciones que ParseFechaHora. Los vacios se consideran null.
Example ¶
t, err := ParseTime("22:55") errores.PanicIfError(err) fmt.Println(PrintTime(t, false))
Output: 22:55
func ParseTimestamp ¶
func ParseTimestamp(s string, ff FormatoFecha) (result pgtype.Timestamp, err error)
Parsea una fecha+hora a pgtype.Timestamp. Mismas consideraciones que ParseFechaHora. Los vacios se consideran null.
Example ¶
ts, err := ParseTimestamp("25/4/1974 22:55", DMA) errores.PanicIfError(err) fmt.Println(PrintTimestamp(ts, AMD))
Output: 1974-04-25 22:55
func ParseUUID ¶
Parsea un objeto a UUID, los vacíos se consideran NULL
Example ¶
uuid, err := ParseUUID("b55e7cec-7126-4a19-8ab1-86481ead2803") errores.PanicIfError(err) fmt.Println(PrintUUID(uuid))
Output: b55e7cec-7126-4a19-8ab1-86481ead2803
func PrintDate ¶
func PrintDate(fh pgtype.Date, ff FormatoFecha) string
Imprime un pgtype.Date
Example ¶
d, err := ParseDate("25/4/1974", DMA) errores.PanicIfError(err) fmt.Println(PrintDate(d, AMD))
Output: 1974-04-25
func PrintDuracion ¶
Imprime una duración en formato X horas, X minutos o X segundos.
Example ¶
d, err := ParseDuracion("48h") errores.PanicIfError(err) fmt.Println(PrintDuracion(d))
Output: 48h
func PrintDuracionIso ¶
Imprime una duración en formato ISO8601, util para órdenes SQL
Example ¶
d, err := ParseDuracion("48h") errores.PanicIfError(err) fmt.Println(PrintDuracionIso(d))
Output: PT172800S
func PrintFecha ¶
func PrintFecha(fh time.Time, ff FormatoFecha) string
Imprime una fecha
Example ¶
fh, err := ParseFechaHora("12/10/2023 12:45", DMA) errores.PanicIfError(err) fmt.Println(PrintFecha(fh, DMA))
Output: 12/10/2023
func PrintFechaHora ¶
func PrintFechaHora(fh time.Time, ff FormatoFecha) string
Imprime una fecha+hora
Example ¶
fh, err := ParseFechaHora("12/10/2023 12:45", DMA) errores.PanicIfError(err) fmt.Println(PrintFechaHora(fh, ISO))
Output: 2023-10-12T12:45:00
func PrintHora ¶
Imprime una hora
Example ¶
h, err := ParseHora("12:34:56") errores.PanicIfError(err) fmt.Println(PrintHora(h, false)) fmt.Println(PrintHora(h, true))
Output: 12:34 12:34:56
func PrintInterval ¶
Imprime una duración en formato A años, M meses, S semanas, D dias, H horas, M minutos, S segundos.
Example ¶
i, err := ParseIntervalAMSD("30d") errores.PanicIfError(err) fmt.Println(PrintInterval(i))
Output: 30d
func PrintIntervalIso ¶
Imprime una duración en formato ISO8601, util en órdenes SQL
Example ¶
i, err := ParseIntervalAMSD("30d") errores.PanicIfError(err) fmt.Println(PrintIntervalIso(i))
Output: P30D
func PrintNumero ¶
Imprime un número con decimales, usando los separadores especificados. Si los decimales son negativos, se ponen a 0 las cifras menos significativas. PrintNumero(12345.6789,2,","," ") = "12 345,68" PrintNumero(-12345.6789,-2,","," ") = "-12 300" Ver https://en.wikipedia.org/wiki/Decimal_separator.
Example ¶
fmt.Println(PrintNumero(12345.6789, 2, ",", ".")) fmt.Println(PrintNumero(12345.6789, 0, ",", ".")) fmt.Println(PrintNumero(12345.6789, -2, ",", "."))
Output: 12.345,68 12.346 12.300
func PrintPrecio ¶
func PrintPrecio(v float64, fp FormatoPrecio) string
Imprime un precio
Example ¶
fmt.Println(PrintPrecio(12345.6789, EUR))
Output: 12.345,68€
func PrintTime ¶
Imprime un pgtype.Time
Example ¶
t, err := ParseTime("22:55") errores.PanicIfError(err) fmt.Println(PrintTime(t, false))
Output: 22:55
func PrintTimestamp ¶
func PrintTimestamp(fh pgtype.Timestamp, ff FormatoFecha) string
Imprime un pgtype.Timestamp
Example ¶
ts, err := ParseTimestamp("25/4/1974 22:55", DMA) errores.PanicIfError(err) fmt.Println(PrintTimestamp(ts, AMD))
Output: 1974-04-25 22:55
func PrintUUID ¶
Imprime un UUID, los NULL se imprimen como vacíos
Example ¶
uuid, err := ParseUUID("7ca4d9eb-9b81-4aae-86d9-682d73f4138f") errores.PanicIfError(err) fmt.Println(PrintUUID(uuid))
Output: 7ca4d9eb-9b81-4aae-86d9-682d73f4138f
func RedondeaPrecio ¶
func RedondeaPrecio(v float64, fp FormatoPrecio) float64
Redondea un precio con el número de decimales de la moneda
Example ¶
fmt.Println(RedondeaPrecio(12345.6789, EUR))
Output: 12345.68
func SubInterval ¶
Sustrae un pgtype.Interval de un pgtype.Timestamp
Example ¶
unDia := pgtype.Interval{Days: 1, Status: pgtype.Present} fechaInicial, err := ParseTimestamp("4/7/2023 12:00", DMA) errores.PanicIfError(err) fechaAnterior := SubInterval(fechaInicial, unDia) fmt.Println(PrintTimestamp(fechaAnterior, DMA))
Output: 03/07/2023 12:00
Types ¶
type FormatoFecha ¶
type FormatoFecha string
https://en.wikipedia.org/wiki/Date_format_by_country
const ( ISO FormatoFecha = "ISO" // RFC3339 DMA FormatoFecha = "DMA" // Dia/Mes/Año MDA FormatoFecha = "MDA" // Mes/Dia/Año AMD FormatoFecha = "AMD" // Año-Mes-Dia )
type FormatoPrecio ¶
type FormatoPrecio string
https://en.wikipedia.org/wiki/ISO_4217
const ( EUR FormatoPrecio = "EUR" // Euros USD FormatoPrecio = "USD" // Dólares USA COP FormatoPrecio = "COP" // Pesos colombianos )