@rivascva/dt-idl 1.1.184 → 1.1.186

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,5 +1,7 @@
1
1
  package models
2
2
 
3
+ import "time"
4
+
3
5
  // ContextKey is a type used to define keys for a request context.
4
6
  type ContextKey string
5
7
 
@@ -34,4 +36,6 @@ const (
34
36
  // MarketCloseTime is the closing time of the market in a 24-hour format (HH:mm).
35
37
  // Based on the timezone specified in MarketTimezone.
36
38
  MarketCloseTime = "16:00"
39
+ // MarketCloseGracePeriod is a grace period added after the official close time to allow in-flight operations to finish.
40
+ MarketCloseGracePeriod = 45 * time.Second
37
41
  )
@@ -0,0 +1,78 @@
1
+ package utils
2
+
3
+ import (
4
+ "fmt"
5
+ "slices"
6
+ "strconv"
7
+ "strings"
8
+ "time"
9
+
10
+ "github.com/RivasCVA/dt-idl/go/models"
11
+ )
12
+
13
+ // IsMarketHours determines if the given time falls within the preset market hours.
14
+ func IsMarketHours(now time.Time) (bool, error) {
15
+ return isMarketHoursWithOpenAndCloseOffsets(now, 0, 0)
16
+ }
17
+
18
+ // IsMarketHoursWithCloseGracePeriod determines if the given time falls within the preset market hours with a close grace period added after the official close time.
19
+ // This is useful for in-flight operations to complete after the market officially closes.
20
+ func IsMarketHoursWithCloseGracePeriod(now time.Time) (bool, error) {
21
+ return isMarketHoursWithOpenAndCloseOffsets(now, 0, models.MarketCloseGracePeriod)
22
+ }
23
+
24
+ // isMarketHoursWithOpenAndCloseOffsets determines if the given time falls within the preset market hours.
25
+ // The given open and close offsets are used as buffer window on market open and close:
26
+ //
27
+ // - If the open offset is 5 seconds and the market opens at 9:30:00:
28
+ // - The function will only return true if the current time is 9:30:05 or later
29
+ //
30
+ // - If the close offset is 20 seconds and the market closes at 16:00:00:
31
+ // - The function will only return true if the current time is 16:00:20 or earlier
32
+ func isMarketHoursWithOpenAndCloseOffsets(now time.Time, openOffset time.Duration, closeOffset time.Duration) (bool, error) {
33
+ // load the market timezone
34
+ loc, err := time.LoadLocation(models.MarketTimezone)
35
+ if err != nil {
36
+ return false, fmt.Errorf("unable to load the market timezone: %w", err)
37
+ }
38
+
39
+ // convert the now time to the market timezone
40
+ now = now.In(loc)
41
+
42
+ // check if on weekend
43
+ if slices.Contains([]time.Weekday{time.Saturday, time.Sunday}, now.Weekday()) {
44
+ return false, nil
45
+ }
46
+
47
+ // parse the open time
48
+ openTimeParts := strings.Split(models.MarketOpenTime, ":")
49
+ openHour, err := strconv.Atoi(openTimeParts[0])
50
+ if err != nil {
51
+ return false, fmt.Errorf("unable to parse the open hour: %w", err)
52
+ }
53
+ openMinute, err := strconv.Atoi(openTimeParts[1])
54
+ if err != nil {
55
+ return false, fmt.Errorf("unable to parse the open minute: %w", err)
56
+ }
57
+
58
+ // parse the close time
59
+ closeTimeParts := strings.Split(models.MarketCloseTime, ":")
60
+ closeHour, err := strconv.Atoi(closeTimeParts[0])
61
+ if err != nil {
62
+ return false, fmt.Errorf("unable to parse the close hour: %w", err)
63
+ }
64
+ closeMinute, err := strconv.Atoi(closeTimeParts[1])
65
+ if err != nil {
66
+ return false, fmt.Errorf("unable to parse the close minute: %w", err)
67
+ }
68
+
69
+ // create the open time with the offset
70
+ openTime := time.Date(now.Year(), now.Month(), now.Day(), openHour, openMinute, 0, 0, loc)
71
+ openWithOffset := openTime.Add(openOffset)
72
+
73
+ // create the close time with the offset
74
+ closeTime := time.Date(now.Year(), now.Month(), now.Day(), closeHour, closeMinute, 0, 0, loc)
75
+ closeWithOffset := closeTime.Add(closeOffset)
76
+
77
+ return !now.Before(openWithOffset) && !now.After(closeWithOffset), nil
78
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@rivascva/dt-idl",
3
- "version": "1.1.184",
3
+ "version": "1.1.186",
4
4
  "description": "Dream Trade - Interface Definition Language",
5
5
  "main": "dist/index.cjs.js",
6
6
  "module": "dist/index.esm.js",