utils

package module
v0.0.5 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jan 7, 2026 License: MIT Imports: 17 Imported by: 9

README

Station Manager: utils package

This module contains various stand-alone utils used by various modules/pacakges in the Station Manager application suite.

Documentation

Index

Constants

View Source
const (
	// EnvSmWorkingDir is the internal constant name for the environment variable name used to specify
	// the working directory.
	EnvSmWorkingDir = "SM_WORKING_DIR"
)

Variables

View Source
var (
	ErrFrequencyTooShort = errors.New("frequency string too short (minimum 9 characters)")
	ErrFrequencyTooLong  = errors.New("frequency string too long (maximum 10 characters)")
	ErrFrequencySyntax   = errors.New("invalid frequency string (must have 2 periods)")
	ErrFrequencyInvalid  = errors.New("invalid frequency string (must have 9 characters)")
)
View Source
var BandNames = map[string]string{
	"54.": "6m",
	"53.": "6m",
	"52.": "6m",
	"51.": "6m",
	"50.": "6m",
	"29.": "10m",
	"28.": "10m",
	"24.": "12m",
	"21.": "15m",
	"18.": "17m",
	"14.": "20m",
	"10.": "30m",
	"7.":  "40m",
	"5.":  "60m",
	"3.":  "80m",
	"2.":  "160m",
	"1.":  "160m",
}
View Source
var FrequencyRanges = map[string][2]float64{
	"54.": {50.000000, 54.000000},
	"53.": {50.000000, 54.000000},
	"52.": {50.000000, 54.000000},
	"51.": {50.000000, 54.000000},
	"50.": {50.000000, 54.000000},
	"29.": {28.000000, 29.700000},
	"28.": {28.000000, 29.700000},
	"24.": {24.890000, 24.990000},
	"21.": {21.000000, 21.450000},
	"18.": {18.068000, 18.168000},
	"14.": {14.000000, 14.350000},
	"10.": {10.100000, 10.150000},
	"7.":  {7.000000, 7.200000},
	"5.":  {5.351500, 5.366500},
	"3.":  {3.500000, 3.800000},
	"2.":  {1.810000, 2.000000},
	"1.":  {1.810000, 2.000000},
}

FrequencyRanges holds the mapping of frequency prefixes to their min and max ranges.

Functions

func AbsDirPathForExecutable

func AbsDirPathForExecutable() (string, error)

AbsDirPathForExecutable returns the absolute directory path of the currently running executable.

func ConvertToXDDDMMM

func ConvertToXDDDMMM(input string) (string, error)

ConvertToXDDDMMM converts a latitude or longitude string to the XDDD°MMM.MMM'N/S/E/W' format and returns the result. It parses the input, calculates degrees and minutes, determines the direction, and formats it accordingly. Returns an error if the input cannot be parsed as a valid floating-point number.

func DXCCFromISO2

func DXCCFromISO2(cc string) (dxcc string, ok bool)

DXCCFromISO2 returns the ADIF DXCC entity code (as a string) for a given two-character ISO 3166-1 alpha-2 country code (case-insensitive).

Notes and caveats:

  • ADIF/ARRL DXCC entities do not always map 1:1 to ISO country codes (e.g., the United Kingdom has multiple DXCC entities). Consequently, this function only provides direct, unambiguous mappings for commonly used ISO codes. If a code is not present in the table, the function returns "" and false.
  • The mapping can be extended over time as additional needs arise.

Returned values: - dxcc: ADIF DXCC entity code as a string. - ok: true if a mapping was found; otherwise false.

func DateNowAsYYYYMMDD

func DateNowAsYYYYMMDD() string

func DecodeStringToUTF8

func DecodeStringToUTF8(input string) (string, error)

DecodeStringToUTF8 converts a given string to UTF-8 encoding, resolving any encoding issues if possible. Returns the UTF-8 decoded string or an error if the conversion fails.

func DeepCopy

func DeepCopy(in interface{}, out interface{}) error

DeepCopy performs a deep copy of any object using JSON serialization and deserialization. The input `in` is the object to copy, and the output `out` should be a pointer to the desired type.

func ExecName

func ExecName(stripExt bool) (string, error)

ExecName returns the name of the current executable, with an option to strip its file extension (if it exists). It resolves symlinks to determine the actual path of the executable when possible. The parameter stripExt specifies whether to remove the file extension from the executable name. It returns the extracted name as a string and an error if retrieval fails. All path components are stripped from the returned name.

func FormatDate

func FormatDate(rawDate string) string

FormatDate converts a raw date string in YYYYMMDD format into a formatted date string in YYYY-MM-DD format. Returns "YYYY-MM-DD" if the input does not have exactly 8 characters.

func FormatFrequencyToKhz

func FormatFrequencyToKhz(rawFreq string) (string, error)

FormatFrequencyToKhz converts a 9-character raw frequency string into a formatted frequency string in kHz format. Returns an error if the input string length is invalid.

func FormatFrequencyToMhz

func FormatFrequencyToMhz(rawFreq string) (string, error)

FormatFrequencyToMhz formats a raw frequency string (e.g., "014.074.000" or "14.074") into MHz format "14.074". It is lenient about length and focuses on dot-separated parts; returns an error if structure is clearly invalid.

func FormatTime

func FormatTime(rawTime string) string

FormatTime converts a 4-digit string representing time in HHMM format to a string in HH:MM format. Returns "HH:MM" on error.

func FrequencyToBand

func FrequencyToBand(freq string) string

FrequencyToBand determines the band corresponding to a given frequency string using predefined mappings. It returns the band name if a match is found or an empty string if no match exists.

func GenerateDateYYYYMMDD

func GenerateDateYYYYMMDD(t time.Time) string

func GetFrequencyRange

func GetFrequencyRange(freq string) (float64, float64)

GetFrequencyRange retrieves the min and max frequency range for a given frequency prefix. It returns the minimum and maximum frequency values if a match is found, or 0, 0 if no match exists.

func IsNetworkError

func IsNetworkError(err error) bool

IsNetworkError determines if an error represents a network-related failure using type assertions and error checking without string matching. This includes timeouts, connection failures, DNS errors, TLS errors, and various syscall network errors.

func IsValidDateYYYYMMDD

func IsValidDateYYYYMMDD(s string) bool

IsValidDateYYYYMMDD validates a date string strictly in the format YYYYMMDD. Rules: - exactly 8 digits (no separators) - represents a real calendar date (UTC) including leap years - disallow all-zero date like 00000000

func IsValidFrequencyMHz

func IsValidFrequencyMHz(s string) bool

func IsValidTimeADIF

func IsValidTimeADIF(s string) bool

IsValidTimeADIF validates an ADIF time string. Accepts: - HHMM (4 digits) - HHMMSS (6 digits) Where HH is 00-23, MM is 00-59, SS is 00-59. Spaces and separators are not allowed.

func IsXDDDMMM

func IsXDDDMMM(s string) bool

IsXDDDMMM returns true if s matches the XDDD MMM.MMM latitude/longitude format. Acceptable directions: N, S, E, W. Degrees must be zero-padded to 3 digits (000–180), minutes must be zero-padded with exactly two digits before the decimal point and exactly three digits after (00.000–59.999). Note: When degrees = 180, minutes must be 00.000 to be a valid coordinate; this function enforces that.

func NewHTTPClient

func NewHTTPClient(httpTimeout time.Duration) *http.Client

func PathExists

func PathExists(path string) (bool, error)

PathExists checks if a specified file or directory exists at the given path. It resolves symlinks because it uses os.Stat. It returns true if the path exists, false if it does not exist, and an error for permission-related issues or unexpected file system errors.

func SanitizeDateToYYYYMMDD

func SanitizeDateToYYYYMMDD(s string) string

SanitizeDateToYYYYMMDD converts date strings in formats YYYY-MM-DD or YYYY/MM/DD to YYYYMMDD. If the input is already in YYYYMMDD, it is returned unchanged. Leading/trailing spaces are ignored. Returns empty string if the input cannot be sanitized into a valid YYYYMMDD date.

func SanitizeTimeToADIF

func SanitizeTimeToADIF(s string) string

SanitizeTimeToADIF converts a string time into compact ADIF formats HHMM or HHMMSS. Accepted inputs include: - HH:MM, H:MM, HH:MM:SS, H:MM:SS - Plain digits HHMM or HHMMSS - Separators ':', '-', '.', ' ' between parts will be ignored Returns empty string if it cannot be sanitized to a valid time.

func SetStructStringField

func SetStructStringField(v interface{}, fieldName string, value string) error

SetStructStringField sets the value of a string field in a struct by its field name dynamically using reflection. Requires a pointer to a struct; returns an error if v is not a pointer to a struct, the field does not exist, or cannot be set.

func WorkingDir

func WorkingDir(workingDir ...string) (string, error)

WorkingDir determines the working directory path, prioritizing function argument, environment variable, or executable location. It validates the directory exists and returns an absolute path or an error if validation fails.

Types

type FIFOList

type FIFOList[T any] struct {
	// contains filtered or unexported fields
}

func NewFIFOList

func NewFIFOList[T any]() *FIFOList[T]

NewFIFOList creates a new FIFO list

func (*FIFOList[T]) Clear

func (f *FIFOList[T]) Clear()

Clear removes all items from the list

func (*FIFOList[T]) IsEmpty

func (f *FIFOList[T]) IsEmpty() bool

IsEmpty returns true if the list is empty

func (*FIFOList[T]) Len

func (f *FIFOList[T]) Len() int

Len returns the number of items in the list

func (*FIFOList[T]) Peek

func (f *FIFOList[T]) Peek() (T, bool)

Peek returns the first item without removing it Returns the zero value of T and false if the list is empty

func (*FIFOList[T]) Pop

func (f *FIFOList[T]) Pop() (T, bool)

Pop removes and returns the first item from the list Returns the zero value of T and false if the list is empty

func (*FIFOList[T]) Push

func (f *FIFOList[T]) Push(item T)

Push adds an item to the end of the list

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL