github.com/treeverse/lakefs@v1.24.1-0.20240520134607-95648127bfb0/pkg/actions/lua/time/time.go (about)

     1  package time
     2  
     3  import (
     4  	"time"
     5  
     6  	"github.com/Shopify/go-lua"
     7  )
     8  
     9  const Iso8601Format = `2006-01-02T15:04:05-07:00`
    10  
    11  func Open(l *lua.State) {
    12  	timeOpen := func(l *lua.State) int {
    13  		lua.NewLibrary(l, timeLibrary)
    14  		return 1
    15  	}
    16  	lua.Require(l, "time", timeOpen, false)
    17  	l.Pop(1)
    18  }
    19  
    20  var timeLibrary = []lua.RegistryFunction{
    21  	{Name: "now", Function: now},
    22  	{Name: "format", Function: format},
    23  	{Name: "format_iso", Function: formatISO},
    24  	{Name: "sleep", Function: sleep},
    25  	{Name: "since", Function: since},
    26  	{Name: "add", Function: add},
    27  	{Name: "parse", Function: parse},
    28  	{Name: "parse_iso", Function: parseISO},
    29  }
    30  
    31  func format(l *lua.State) int {
    32  	epochNanoToFormat := int64(lua.CheckNumber(l, 1))
    33  	layout := lua.CheckString(l, 2)
    34  	zone := lua.CheckString(l, 3)
    35  
    36  	loc, err := time.LoadLocation(zone)
    37  	if err != nil {
    38  		lua.Errorf(l, "%s", err.Error())
    39  	}
    40  
    41  	unixTime := time.Unix(epochNanoToFormat/1e9, 0)
    42  	timeInTimeZone := unixTime.In(loc)
    43  	l.PushString(timeInTimeZone.Format(layout))
    44  
    45  	return 1
    46  }
    47  
    48  func formatISO(l *lua.State) int {
    49  	epochNanoToFormat := int64(lua.CheckNumber(l, 1))
    50  	zone := lua.OptString(l, 2, "")
    51  
    52  	loc, err := time.LoadLocation(zone)
    53  	if err != nil {
    54  		lua.Errorf(l, "%s", err.Error())
    55  	}
    56  
    57  	unixTime := time.Unix(epochNanoToFormat/1e9, 0)
    58  	timeInTimeZone := unixTime.In(loc)
    59  	l.PushString(timeInTimeZone.Format(Iso8601Format))
    60  
    61  	return 1
    62  }
    63  
    64  func add(l *lua.State) int {
    65  	start := int64(lua.CheckNumber(l, 1))
    66  	startUnix := time.Unix(0, start)
    67  
    68  	lua.CheckType(l, 2, lua.TypeTable)
    69  	l.Field(2, "hour")
    70  	l.Field(2, "minute")
    71  	l.Field(2, "second")
    72  	second := lua.OptInteger(l, -1, 0)
    73  	minute := lua.OptInteger(l, -2, 0)
    74  	hour := lua.OptInteger(l, -3, 0)
    75  	l.Pop(3)
    76  
    77  	inc := startUnix.Add(time.Hour*time.Duration(hour) + time.Minute*time.Duration(minute) + time.Second*time.Duration(second))
    78  	l.PushNumber(float64(inc.UnixNano()))
    79  	return 1
    80  }
    81  
    82  func sleep(l *lua.State) int {
    83  	ns := lua.CheckInteger(l, 1)
    84  	time.Sleep(time.Nanosecond * time.Duration(ns))
    85  	return 1
    86  }
    87  
    88  func now(l *lua.State) int {
    89  	l.PushNumber(float64(time.Now().UnixNano()))
    90  	return 1
    91  }
    92  
    93  func since(l *lua.State) int {
    94  	start := lua.CheckNumber(l, 1)
    95  	diff := float64(time.Now().UnixNano()) - start
    96  	l.PushNumber(diff)
    97  	return 1
    98  }
    99  
   100  func parse(l *lua.State) int {
   101  	format := lua.CheckString(l, 1)
   102  	value := lua.CheckString(l, 2)
   103  
   104  	return parseTime(l, format, value)
   105  }
   106  
   107  func parseISO(l *lua.State) int {
   108  	value := lua.CheckString(l, 1)
   109  
   110  	return parseTime(l, Iso8601Format, value)
   111  }
   112  
   113  func parseTime(l *lua.State, format, value string) int {
   114  	t, err := time.Parse(format, value)
   115  	if err != nil {
   116  		lua.Errorf(l, "%s", err.Error())
   117  	}
   118  
   119  	l.PushNumber(float64(t.UnixNano()))
   120  
   121  	return 1
   122  }