<commit_before>package time

import (
	"github.com/Shopify/go-lua"
	"time"
)

func Open(l *lua.State) {
	timeOpen := func(l *lua.State) int {
		lua.NewLibrary(l, timeLibrary)
		return 1
	}
	lua.Require(l, "goluago/time", timeOpen, false)
	l.Pop(1)
}

var timeLibrary = []lua.RegistryFunction{
	{"now", now},
	{"nowUTC", nowUTC},
	{"sleep", sleep},
	{"since", since},
	{"addTime", addTime}
}

func nowUTC(l *State) int {
	l.PushString(time.Now().UTC().Format(time.RFC3339))
	return 1
}},

{"addHourUTC", func(l *State) int{
	start := lua.CheckInteger(l, 1)
	hourToAdd := lua.CheckInteger(l, 2)
	diff := start.Add(time.Hour * time.Duration(hourToAdd))
	l.PushString(diff.Format(time.RFC3339))
	return 1
}},

func sleep(l *lua.State) int {
	ns := lua.CheckInteger(l, 1)
	time.Sleep(time.Nanosecond * time.Duration(ns))
	return 1
}

func now(l *lua.State) int {
	l.PushNumber(float64(time.Now().UnixNano()))
	return 1
}

func since(l *lua.State) int {
	start := lua.CheckNumber(l, 1)
	diff := float64(time.Now().UnixNano()) - start
	l.PushNumber(diff)
	return 1
}<commit_msg>Updated the methods to format by taking the layout and zone. Also adding time by duration.<commit_after>package time

import (
	"github.com/Shopify/go-lua"
	"time"
)

func Open(l *lua.State) {
	timeOpen := func(l *lua.State) int {
		lua.NewLibrary(l, timeLibrary)
		return 1
	}
	lua.Require(l, "goluago/time", timeOpen, false)
	l.Pop(1)
}

var timeLibrary = []lua.RegistryFunction{
	{"now", now},
	{"format", format},
	{"sleep", sleep},
	{"since", since},
	{"add", add},
}

func format(l *lua.State) int {
	epochNanoToFormat := int64(lua.CheckNumber(l, 1))
	layout := lua.CheckString(l, 2)
	zone := lua.CheckString(l, 3)

	loc, err := time.LoadLocation(zone)
	if err != nil {
		lua.Errorf(l, err.Error())
	}

	unixTime := time.Unix(0, epochNanoToFormat)
	timeInTimeZone := unixTime.In(loc)
	l.PushString(timeInTimeZone.Format(layout))

	return 1
}

func add(l *lua.State) int {
	start := int64(lua.CheckNumber(l, 1))
	startUnix := time.Unix(0, start)
	valueToAdd := lua.CheckMap(l, 2)

	inc := startUnix.Add((time.Hour * time.Duration(valueToAdd["hour"])) + (time.Minute * time.Duration(valueToAdd["minute"])) + (time.Second * time.Duration(valueToAdd["second"])))
	l.PushNumber(float64(inc.UnixNano()))
	return 1
}

func sleep(l *lua.State) int {
	ns := lua.CheckInteger(l, 1)
	time.Sleep(time.Nanosecond * time.Duration(ns))
	return 1
}

func now(l *lua.State) int {
	l.PushNumber(float64(time.Now().UnixNano()))
	return 1
}

func since(l *lua.State) int {
	start := lua.CheckNumber(l, 1)
	diff := float64(time.Now().UnixNano()) - start
	l.PushNumber(diff)
	return 1
}
{'fraction_non_alphanumeric': 0.18137069291934874, 'fraction_numerical': 0.018553578190079516, 'mean_word_length': 4.5974576271186445, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '182101', 'n_tokens_mistral': 1113, 'n_tokens_neox': 1054, 'n_words': 220}