TimeSubtract

TimeSubtract( base, offset, unit )
TimeSubtract( base, offset, unit )
TimeSubtract( base, offset, unit )
TimeSubtract( base, offset, unit )
time_subtract( base, offset, unit )
TimeSubtract( base, offset, unit )

Description

The TimeSubtract function decreases a base Timestamp or Date by subtracting the offset in units.

Parameters

Parameter Type Definition and Requirements

base

A Timestamp or Date to subtract from. base must match the type of unit.

offset

The integer number of units to subtract from base.

unit

Unit of the offset.

When base is a Date, unit must be one of day or days.

When base is a Timestamp, unit must be one of:

  • day, or days

  • half day, or half days

  • hour, or hours

  • minute, or minutes

  • second, or seconds

  • millisecond, or milliseconds

  • microsecond, or microseconds

  • nanosecond, or nanoseconds

Returns

When base is a Date, a new Date which represents base with offset units subtracted from it.

When base is a Timestamp, a new Timestamp which represents base with offset units subtracted from it.

Examples

The following query subtracts 1 second from 1 day after Unix epoch:

try
{
    Value result = await client.Query(
        TimeSubtract(Epoch(1, "day"), 1, "second")
    );

    Console.WriteLine(result);
}
catch (Exception e)
{
    Console.WriteLine($"ERROR: {e.Message}");
}
FaunaTime(1970-01-01T23:59:59Z)
result, err := client.Query(
	f.TimeSubtract(f.Epoch(1, "day"), 1, "second"))

if err != nil {
	fmt.Fprintln(os.Stderr, err)
} else {
	fmt.Println(result)
}
{0 62135683199 <nil>}
System.out.println(
    client.query(
        TimeSubtract(Epoch(1, "day"), 1, "second")
    ).get());
1970-01-01T23:59:59Z
client.query(
  q.TimeSubtract(q.Epoch(1, 'day'), 1, 'second')
)
.then((ret) => console.log(ret))
.catch((err) => console.error(
  'Error: [%s] %s: %s',
  err.name,
  err.message,
  err.errors()[0].description,
))
Time("1970-01-01T23:59:59Z")
result = client.query(
  q.time_subtract(q.epoch(1, "day"), 1, "second")
)
print(result)
FaunaTime('1970-01-01T23:59:59Z')
TimeSubtract(Epoch(1, 'day'), 1, 'second')
Time("1970-01-01T23:59:59Z")
Query metrics:
  •    bytesIn:  69

  •   bytesOut:  43

  • computeOps:   1

  •    readOps:   0

  •   writeOps:   0

  •  readBytes:   0

  • writeBytes:   0

  •  queryTime: 4ms

  •    retries:   0

The following query returns a Timestamp 5 seconds in the past before now:

try
{
    Value result = await client.Query(
        Arr(
            Now(),
            TimeSubtract(Now(), 5, "seconds")
        )
    );

    Console.WriteLine(result);
}
catch (Exception e)
{
    Console.WriteLine($"ERROR: {e.Message}");
}
Arr(FaunaTime(2019-10-22T16:45:13.988Z), FaunaTime(2019-10-22T16:45:08.988Z))
result, err := client.Query(
	f.Arr{
		f.Now(),
		f.TimeSubtract(f.Now(), 5, "seconds"),
	})

if err != nil {
	fmt.Fprintln(os.Stderr, err)
} else {
	fmt.Println(result)
}
[{879000000 63746603234 <nil>} {879000000 63746603229 <nil>}]
System.out.println(
    client.query(
        Arr(
            Time("2020-07-06T12:34:56.789Z"),
            TimeSubtract(Time("2020-07-06T12:34:56.789Z"), 5, "seconds")
        )
    ).get());
[2020-07-06T12:34:56.789Z, 2020-07-06T12:34:51.789Z]
client.query([
  q.Now(),
  q.TimeSubtract(q.Now(), 5, 'seconds'),
])
.then((ret) => console.log(ret))
.catch((err) => console.error(
  'Error: [%s] %s: %s',
  err.name,
  err.message,
  err.errors()[0].description,
))
[ Time("2019-10-22T16:45:19.755048Z"),
  Time("2019-10-22T16:45:14.755048Z") ]
result = client.query(
  [
    q.now(),
    q.time_subtract(q.now(), 5, "seconds"),
  ]
)
print(result)
[FaunaTime('2021-01-18T21:47:21.114Z'), FaunaTime('2021-01-18T21:47:16.114Z')]
[
  Now(),
  TimeSubtract(Now(), 5, 'seconds')
]
[ Time("2021-06-23T12:24:16.798Z"), Time("2021-06-23T12:24:11.798Z") ]
Query metrics:
  •    bytesIn:  73

  •   bytesOut:  84

  • computeOps:   1

  •    readOps:   0

  •   writeOps:   0

  •  readBytes:   0

  • writeBytes:   0

  •  queryTime: 5ms

  •    retries:   0

The following query returns a Date 1 day before 2019-11-26:

try
{
    Value result = await client.Query(
        TimeSubtract(Date("2019-11-26"), 1, "day")
    );

    Console.WriteLine(result);
}
catch (Exception e)
{
    Console.WriteLine($"ERROR: {e.Message}");
}
FaunaDate(2019-11-25 12:00:00 AM)
result, err := client.Query(
	f.TimeSubtract(f.Date("2019-11-26"), 1, "day"))

if err != nil {
	fmt.Fprintln(os.Stderr, err)
} else {
	fmt.Println(result)
}
{0 63710236800 <nil>}
System.out.println(
    client.query(
        TimeSubtract(Date("2019-11-26"), 1, "day")
    ).get());
2019-11-25
client.query(
  q.TimeSubtract(q.Date('2019-11-26'), 1, 'day')
)
.then((ret) => console.log(ret))
.catch((err) => console.error(
  'Error: [%s] %s: %s',
  err.name,
  err.message,
  err.errors()[0].description,
))
Date("2019-11-25")
result = client.query(
  q.time_subtract(q.date("2019-11-26"), 1, "day")
)
print(result)
2019-11-25
TimeSubtract(Date('2019-11-26'), 1, 'day')
Date("2019-11-25")
Query metrics:
  •    bytesIn:  63

  •   bytesOut:  35

  • computeOps:   1

  •    readOps:   0

  •   writeOps:   0

  •  readBytes:   0

  • writeBytes:   0

  •  queryTime: 4ms

  •    retries:   0

Is this article helpful? 

Tell Fauna how the article can be improved:
Visit Fauna's forums or email docs@fauna.com

Thank you for your feedback!