Browse Source

#65 fn: NOW and TODAY, and update dependencies

xuri 4 years ago
parent
commit
2350866d46
4 changed files with 44 additions and 9 deletions
  1. 29 0
      calc.go
  2. 6 0
      calc_test.go
  3. 3 3
      go.mod
  4. 6 6
      go.sum

+ 29 - 0
calc.go

@@ -320,6 +320,7 @@ var tokenPriority = map[string]int{
 //    MUNIT
 //    NA
 //    NOT
+//    NOW
 //    OCT2BIN
 //    OCT2DEC
 //    OCT2HEX
@@ -362,6 +363,7 @@ var tokenPriority = map[string]int{
 //    SUMSQ
 //    TAN
 //    TANH
+//    TODAY
 //    TRIM
 //    TRUE
 //    TRUNC
@@ -4647,6 +4649,33 @@ func (fn *formulaFuncs) DATE(argsList *list.List) formulaArg {
 	return newStringFormulaArg(timeFromExcelTime(daysBetween(excelMinTime1900.Unix(), d)+1, false).String())
 }
 
+// NOW function returns the current date and time. The function receives no arguments and therefore. The syntax of the function is:
+//
+//    NOW()
+//
+func (fn *formulaFuncs) NOW(argsList *list.List) formulaArg {
+	if argsList.Len() != 0 {
+		return newErrorFormulaArg(formulaErrorVALUE, "NOW accepts no arguments")
+	}
+	now := time.Now()
+	_, offset := now.Zone()
+	return newNumberFormulaArg(25569.0 + float64(now.Unix()+int64(offset))/86400)
+}
+
+// TODAY function returns the current date. The function has no arguments and
+// therefore. The syntax of the function is:
+//
+//    TODAY()
+//
+func (fn *formulaFuncs) TODAY(argsList *list.List) formulaArg {
+	if argsList.Len() != 0 {
+		return newErrorFormulaArg(formulaErrorVALUE, "TODAY accepts no arguments")
+	}
+	now := time.Now()
+	_, offset := now.Zone()
+	return newNumberFormulaArg(daysBetween(excelMinTime1900.Unix(), now.Unix()+int64(offset)) + 1)
+}
+
 // makeDate return date as a Unix time, the number of seconds elapsed since
 // January 1, 1970 UTC.
 func makeDate(y int, m time.Month, d int) int64 {

+ 6 - 0
calc_test.go

@@ -1436,6 +1436,10 @@ func TestCalcCellValue(t *testing.T) {
 		`=DATE("text",10,21)`:   "DATE requires 3 number arguments",
 		`=DATE(2020,"text",21)`: "DATE requires 3 number arguments",
 		`=DATE(2020,10,"text")`: "DATE requires 3 number arguments",
+		// NOW
+		"=NOW(A1)": "NOW accepts no arguments",
+		// TODAY
+		"=TODAY(A1)": "TODAY accepts no arguments",
 		// Text Functions
 		// CHAR
 		"=CHAR()":     "CHAR requires 1 argument",
@@ -1658,8 +1662,10 @@ func TestCalcCellValue(t *testing.T) {
 	}
 
 	volatileFuncs := []string{
+		"=NOW()",
 		"=RAND()",
 		"=RANDBETWEEN(1,2)",
+		"=TODAY()",
 	}
 	for _, formula := range volatileFuncs {
 		f := prepareCalcData(cellData)

+ 3 - 3
go.mod

@@ -6,9 +6,9 @@ require (
 	github.com/mohae/deepcopy v0.0.0-20170929034955-c48cc78d4826
 	github.com/richardlehane/mscfb v1.0.3
 	github.com/stretchr/testify v1.6.1
-	github.com/xuri/efp v0.0.0-20210128032744-13be4fd5dcb5
-	golang.org/x/crypto v0.0.0-20201221181555-eec23a3978ad
+	github.com/xuri/efp v0.0.0-20210311002341-9c6784cb2d17
+	golang.org/x/crypto v0.0.0-20210220033148-5ea612d1eb83
 	golang.org/x/image v0.0.0-20201208152932-35266b937fa6
-	golang.org/x/net v0.0.0-20210119194325-5f4716e94777
+	golang.org/x/net v0.0.0-20210226172049-e18ecbb05110
 	golang.org/x/text v0.3.5
 )

+ 6 - 6
go.sum

@@ -11,16 +11,16 @@ github.com/richardlehane/msoleps v1.0.1/go.mod h1:BWev5JBpU9Ko2WAgmZEuiz4/u3ZYTK
 github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
 github.com/stretchr/testify v1.6.1 h1:hDPOHmpOpP40lSULcqw7IrRb/u7w6RpDC9399XyoNd0=
 github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
-github.com/xuri/efp v0.0.0-20210128032744-13be4fd5dcb5 h1:hO7we8DcWAkmZX/Voqa04Tuo84SbeXIJbdgMj92hIpA=
-github.com/xuri/efp v0.0.0-20210128032744-13be4fd5dcb5/go.mod h1:uBiSUepVYMhGTfDeBKKasV4GpgBlzJ46gXUBAqV8qLk=
+github.com/xuri/efp v0.0.0-20210311002341-9c6784cb2d17 h1:Ou4I7pYPQBk/qE9K2y31rawl/ftLHbTJJAFYJPVSyQo=
+github.com/xuri/efp v0.0.0-20210311002341-9c6784cb2d17/go.mod h1:ybY/Jr0T0GTCnYjKqmdwxyxn2BQf2RcQIIvex5QldPI=
 golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
-golang.org/x/crypto v0.0.0-20201221181555-eec23a3978ad h1:DN0cp81fZ3njFcrLCytUHRSUkqBjfTo4Tx9RJTWs0EY=
-golang.org/x/crypto v0.0.0-20201221181555-eec23a3978ad/go.mod h1:jdWPYTVW3xRLrWPugEBEK3UY2ZEsg3UU495nc5E+M+I=
+golang.org/x/crypto v0.0.0-20210220033148-5ea612d1eb83 h1:/ZScEX8SfEmUGRHs0gxpqteO5nfNW6axyZbBdw9A12g=
+golang.org/x/crypto v0.0.0-20210220033148-5ea612d1eb83/go.mod h1:jdWPYTVW3xRLrWPugEBEK3UY2ZEsg3UU495nc5E+M+I=
 golang.org/x/image v0.0.0-20201208152932-35266b937fa6 h1:nfeHNc1nAqecKCy2FCy4HY+soOOe5sDLJ/gZLbx6GYI=
 golang.org/x/image v0.0.0-20201208152932-35266b937fa6/go.mod h1:FeLwcggjj3mMvU+oOTbSwawSJRM1uh48EjtB4UJZlP0=
 golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
-golang.org/x/net v0.0.0-20210119194325-5f4716e94777 h1:003p0dJM77cxMSyCPFphvZf/Y5/NXf5fzg6ufd1/Oew=
-golang.org/x/net v0.0.0-20210119194325-5f4716e94777/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg=
+golang.org/x/net v0.0.0-20210226172049-e18ecbb05110 h1:qWPm9rbaAMKs8Bq/9LRpbMqxWRVUAQwMI9fVrssnTfw=
+golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg=
 golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
 golang.org/x/sys v0.0.0-20191026070338-33540a1f6037/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
 golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=