package parser

import (
	"sync"
	"time"
)

const fallbackTimezoneName = "Europe/Moscow"

var (
	fallbackTimezoneOnce sync.Once
	fallbackTimezone     *time.Location
)

// DefaultArchiveLocation returns the timezone used for source timestamps
// that do not contain an explicit offset.
func DefaultArchiveLocation() *time.Location {
	fallbackTimezoneOnce.Do(func() {
		loc, err := time.LoadLocation(fallbackTimezoneName)
		if err != nil {
			fallbackTimezone = time.FixedZone("MSK", 3*60*60)
			return
		}
		fallbackTimezone = loc
	})
	return fallbackTimezone
}

// ParseInDefaultArchiveLocation parses timestamps without timezone information
// using Europe/Moscow as the assumed source timezone.
func ParseInDefaultArchiveLocation(layout, value string) (time.Time, error) {
	return time.ParseInLocation(layout, value, DefaultArchiveLocation())
}
