dateblock 2.1.0__tar.gz → 2.2.0__tar.gz

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,6 +1,12 @@
1
1
  # Change Log
2
2
  All notable changes to this project will be documented in this file.
3
3
 
4
+ ## [2.2.0] - 2026-11-10
5
+ ### Added
6
+ - A drift can be negative, moving each matched time back that many
7
+ seconds, ie: `dateblock -c '0 0 9 1 * * -259200'` is a reminder at 9:00
8
+ three days before each 1st of the month.
9
+
4
10
  ## [2.1.0] - 2026-10-31
5
11
  ### Added
6
12
  - PHP bindings (PHP 8.0 and newer): `dateblock('0 0 9', ref: $tokyo)`
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: dateblock
3
- Version: 2.1.0
3
+ Version: 2.2.0
4
4
  Summary: Block (sleep) until a cron-style moment in time
5
5
  Home-page: https://github.com/caronc/dateblock
6
6
  Author: Chris Caron
@@ -62,8 +62,8 @@ The difference is very subtle but can prove to be very useful.
62
62
 
63
63
  It offers a few new features that the Internet Systems Consortium (ICS) version
64
64
  does not (refering to /etc/crontab style). Dateblock specifically offers the ability
65
- to wake/unblock on a _specific second_ and/or _drift_ for certain period afterwards
66
- your calculated time is met (kind of like combining _sleep_ and _ICS Cron_ into one).
65
+ to wake/unblock on a _specific second_ and/or _drift_ a number of seconds after
66
+ (or before) your calculated time is met (kind of like combining _sleep_ and _ICS Cron_ into one).
67
67
 
68
68
  Python, PHP and Node.js bindings are additionally included, supporting the
69
69
  standard date and time objects of each language!
@@ -118,7 +118,8 @@ __Allowed options__:
118
118
  -m [ --month ] arg Month (1-12 or Jan-Dec)
119
119
  -w [ --dow ] arg Day of Week (0-6 or Sun-Sat; Sun=0 or 7)
120
120
  -c [ --cron ] arg Cron string formatting
121
- -x [ --drift ] arg Additional drift time (in seconds).
121
+ -x [ --drift ] arg Additional drift time (in seconds); a negative
122
+ drift wakes up that much earlier.
122
123
  -z [ --timezone ] arg Timezone to read the schedule in (ie:
123
124
  America/Toronto); defaults to this computer's.
124
125
  -C [ --count ] arg List the next N times (1-1000) instead of
@@ -140,7 +141,7 @@ A variety of syntax is accepted by this tool such as:
140
141
  too, so Fri-7 is Friday to Sunday.
141
142
  ```
142
143
 
143
- __Note__: With the exception of the drift option (+), all variations of the syntax
144
+ __Note__: With the exception of the drift option (+ or -), all variations of the syntax
144
145
  mentioned above can be mixed if separated using the 'comma' (Separator)
145
146
  operator. ie: */a,b,c-d,/e is valid.
146
147
  However: x-y-z is not valid, nor is /x/y, /x-y or a '*' mixed with
@@ -166,7 +167,7 @@ interested in. Asterisks are automatically placed in strings missing all 6
166
167
  arguments (separated by white space).
167
168
 
168
169
  The crontab shortcuts work too (with or without `--isc`). Without `--isc`
169
- one can be followed by a drift (ie: `@daily +30`):
170
+ one can be followed by a drift (ie: `@daily +30` or `@daily -30`):
170
171
  ```
171
172
  @hourly the start of every hour
172
173
  @daily (or @midnight) every day at 00:00
@@ -259,12 +260,13 @@ every minute. You could however achieve the previous example using a drift
259
260
  value of '60' (seconds) and a cron (minute) entry of '*/10'.
260
261
 
261
262
  In a way, drifting is a little like executing a cron and then sleeping for
262
- an additional period right afterwards. However this is a much more elegant
263
+ an additional period right afterwards (a negative drift wakes up that much
264
+ sooner instead). However this is a much more elegant
263
265
  solution with a more precise blocking period.
264
266
 
265
267
  Some things to consider about drifting:
266
268
 
267
- - Drifting is always calculated 'after' a specified cron.
269
+ - Drifting is always calculated from the time the cron matches.
268
270
  - You can not specify a drifting value on it's own without a cron entry to go
269
271
  with it.
270
272
  - A drift value is always specified in seconds.
@@ -296,6 +298,16 @@ Some things to consider about drifting:
296
298
  ```dateblock -c '* */5 +60'``` is the same as writing ```dateblock -c '* */5 * * * * 60'```
297
299
  Once you invoke the __+__ character however, any entries found afterwards
298
300
  will be treated as an error.
301
+ - A drift can be negative; each matched time then moves _back_ that many
302
+ seconds instead of forward.
303
+ - A reminder 3 days (259200 seconds) before rent is due on the 1st:
304
+ ```dateblock -c '0 0 9 1 * * -259200'``` wakes at 9:00 on the 28th, 29th
305
+ or 26th, however long the month is.
306
+ - ```dateblock -c '0 0 9 25 12 * -518400'``` (or ```-x -518400```) wakes at
307
+ 9:00 on December 19th (6 days before December 25th).
308
+ - Like _+_, an entry starting with _-_ jumps to the drift.
309
+ - An early time that has already passed is skipped, like any past match.
310
+ - Drifts go from -2147483647 to 2147483647.
299
311
 
300
312
  ### Examples
301
313
  The following would block until a minute divisible by 10 was reached. Minutes
@@ -343,7 +355,7 @@ dateblock -c "* 5 * * * * 20"
343
355
  dateblock -n 5 -x 20
344
356
 
345
357
  # You can skip to the drift portion by just denoting a plus (+) as the last
346
- # entry specified. This is interpreted as a drift time.
358
+ # entry specified (or a minus (-) for a negative drift). This is interpreted as a drift time.
347
359
  dateblock -c "* 5 +20"
348
360
  ```
349
361
  The expression is considered invalid if more entries are found after the drift
@@ -352,6 +364,12 @@ period. However this is still valid:
352
364
  dateblock -c "* 5 * * * * +20"
353
365
  ```
354
366
 
367
+ A negative drift wakes up early; this blocks until 4:40 past each hour (20
368
+ seconds before the 5 minute mark):
369
+ ```bash
370
+ dateblock -n 5 -x -20
371
+ ```
372
+
355
373
  ### DateBlock Python Bindings
356
374
  The DateBlock python bindings are really easy to use too:
357
375
  ```python
@@ -375,6 +393,9 @@ result = dateblock("* /20", ref=relative, block=False)
375
393
  # had you not set the isc flag)
376
394
  result = dateblock("/1", isc=True)
377
395
 
396
+ # A negative drift wakes up early: 6 days before 9:00 on December 25th
397
+ result = dateblock("0 0 9 25 12 * -518400", block=False)
398
+
378
399
  # Dateblock supports date, time and epoch time too!
379
400
  #
380
401
  # Date: (yyyy, mm, dd)
@@ -469,6 +490,9 @@ $result = dateblock('/5', false, 13424236);
469
490
  // A regular crontab entry: the start of each minute
470
491
  $result = dateblock('* * * * *', isc: true);
471
492
 
493
+ // A negative drift wakes up early: 6 days before 9:00 on December 25th
494
+ $result = dateblock('0 0 9 25 12 * -518400', block: false);
495
+
472
496
  // Timezones: the cron is read in the timezone of the reference, and the
473
497
  // result comes back in that same timezone. This waits for 9:00 in Tokyo.
474
498
  $tokyo = new DateTimeImmutable('now', new DateTimeZone('Asia/Tokyo'));
@@ -517,6 +541,9 @@ next('* /20', { ref: new Date(Date.now() - 4 * 86400000) });
517
541
  // A regular crontab entry: the start of each minute
518
542
  next('* * * * *', { isc: true });
519
543
 
544
+ // A negative drift wakes up early: 6 days before 9:00 on December 25th
545
+ next('0 0 9 25 12 * -518400');
546
+
520
547
  // Timezones: 9:00 in Tokyo, whatever timezone this computer is in
521
548
  await dateblock('0 0 9', { timeZone: 'Asia/Tokyo' });
522
549
 
@@ -14,8 +14,8 @@ The difference is very subtle but can prove to be very useful.
14
14
 
15
15
  It offers a few new features that the Internet Systems Consortium (ICS) version
16
16
  does not (refering to /etc/crontab style). Dateblock specifically offers the ability
17
- to wake/unblock on a _specific second_ and/or _drift_ for certain period afterwards
18
- your calculated time is met (kind of like combining _sleep_ and _ICS Cron_ into one).
17
+ to wake/unblock on a _specific second_ and/or _drift_ a number of seconds after
18
+ (or before) your calculated time is met (kind of like combining _sleep_ and _ICS Cron_ into one).
19
19
 
20
20
  Python, PHP and Node.js bindings are additionally included, supporting the
21
21
  standard date and time objects of each language!
@@ -70,7 +70,8 @@ __Allowed options__:
70
70
  -m [ --month ] arg Month (1-12 or Jan-Dec)
71
71
  -w [ --dow ] arg Day of Week (0-6 or Sun-Sat; Sun=0 or 7)
72
72
  -c [ --cron ] arg Cron string formatting
73
- -x [ --drift ] arg Additional drift time (in seconds).
73
+ -x [ --drift ] arg Additional drift time (in seconds); a negative
74
+ drift wakes up that much earlier.
74
75
  -z [ --timezone ] arg Timezone to read the schedule in (ie:
75
76
  America/Toronto); defaults to this computer's.
76
77
  -C [ --count ] arg List the next N times (1-1000) instead of
@@ -92,7 +93,7 @@ A variety of syntax is accepted by this tool such as:
92
93
  too, so Fri-7 is Friday to Sunday.
93
94
  ```
94
95
 
95
- __Note__: With the exception of the drift option (+), all variations of the syntax
96
+ __Note__: With the exception of the drift option (+ or -), all variations of the syntax
96
97
  mentioned above can be mixed if separated using the 'comma' (Separator)
97
98
  operator. ie: */a,b,c-d,/e is valid.
98
99
  However: x-y-z is not valid, nor is /x/y, /x-y or a '*' mixed with
@@ -118,7 +119,7 @@ interested in. Asterisks are automatically placed in strings missing all 6
118
119
  arguments (separated by white space).
119
120
 
120
121
  The crontab shortcuts work too (with or without `--isc`). Without `--isc`
121
- one can be followed by a drift (ie: `@daily +30`):
122
+ one can be followed by a drift (ie: `@daily +30` or `@daily -30`):
122
123
  ```
123
124
  @hourly the start of every hour
124
125
  @daily (or @midnight) every day at 00:00
@@ -211,12 +212,13 @@ every minute. You could however achieve the previous example using a drift
211
212
  value of '60' (seconds) and a cron (minute) entry of '*/10'.
212
213
 
213
214
  In a way, drifting is a little like executing a cron and then sleeping for
214
- an additional period right afterwards. However this is a much more elegant
215
+ an additional period right afterwards (a negative drift wakes up that much
216
+ sooner instead). However this is a much more elegant
215
217
  solution with a more precise blocking period.
216
218
 
217
219
  Some things to consider about drifting:
218
220
 
219
- - Drifting is always calculated 'after' a specified cron.
221
+ - Drifting is always calculated from the time the cron matches.
220
222
  - You can not specify a drifting value on it's own without a cron entry to go
221
223
  with it.
222
224
  - A drift value is always specified in seconds.
@@ -248,6 +250,16 @@ Some things to consider about drifting:
248
250
  ```dateblock -c '* */5 +60'``` is the same as writing ```dateblock -c '* */5 * * * * 60'```
249
251
  Once you invoke the __+__ character however, any entries found afterwards
250
252
  will be treated as an error.
253
+ - A drift can be negative; each matched time then moves _back_ that many
254
+ seconds instead of forward.
255
+ - A reminder 3 days (259200 seconds) before rent is due on the 1st:
256
+ ```dateblock -c '0 0 9 1 * * -259200'``` wakes at 9:00 on the 28th, 29th
257
+ or 26th, however long the month is.
258
+ - ```dateblock -c '0 0 9 25 12 * -518400'``` (or ```-x -518400```) wakes at
259
+ 9:00 on December 19th (6 days before December 25th).
260
+ - Like _+_, an entry starting with _-_ jumps to the drift.
261
+ - An early time that has already passed is skipped, like any past match.
262
+ - Drifts go from -2147483647 to 2147483647.
251
263
 
252
264
  ### Examples
253
265
  The following would block until a minute divisible by 10 was reached. Minutes
@@ -295,7 +307,7 @@ dateblock -c "* 5 * * * * 20"
295
307
  dateblock -n 5 -x 20
296
308
 
297
309
  # You can skip to the drift portion by just denoting a plus (+) as the last
298
- # entry specified. This is interpreted as a drift time.
310
+ # entry specified (or a minus (-) for a negative drift). This is interpreted as a drift time.
299
311
  dateblock -c "* 5 +20"
300
312
  ```
301
313
  The expression is considered invalid if more entries are found after the drift
@@ -304,6 +316,12 @@ period. However this is still valid:
304
316
  dateblock -c "* 5 * * * * +20"
305
317
  ```
306
318
 
319
+ A negative drift wakes up early; this blocks until 4:40 past each hour (20
320
+ seconds before the 5 minute mark):
321
+ ```bash
322
+ dateblock -n 5 -x -20
323
+ ```
324
+
307
325
  ### DateBlock Python Bindings
308
326
  The DateBlock python bindings are really easy to use too:
309
327
  ```python
@@ -327,6 +345,9 @@ result = dateblock("* /20", ref=relative, block=False)
327
345
  # had you not set the isc flag)
328
346
  result = dateblock("/1", isc=True)
329
347
 
348
+ # A negative drift wakes up early: 6 days before 9:00 on December 25th
349
+ result = dateblock("0 0 9 25 12 * -518400", block=False)
350
+
330
351
  # Dateblock supports date, time and epoch time too!
331
352
  #
332
353
  # Date: (yyyy, mm, dd)
@@ -421,6 +442,9 @@ $result = dateblock('/5', false, 13424236);
421
442
  // A regular crontab entry: the start of each minute
422
443
  $result = dateblock('* * * * *', isc: true);
423
444
 
445
+ // A negative drift wakes up early: 6 days before 9:00 on December 25th
446
+ $result = dateblock('0 0 9 25 12 * -518400', block: false);
447
+
424
448
  // Timezones: the cron is read in the timezone of the reference, and the
425
449
  // result comes back in that same timezone. This waits for 9:00 in Tokyo.
426
450
  $tokyo = new DateTimeImmutable('now', new DateTimeZone('Asia/Tokyo'));
@@ -469,6 +493,9 @@ next('* /20', { ref: new Date(Date.now() - 4 * 86400000) });
469
493
  // A regular crontab entry: the start of each minute
470
494
  next('* * * * *', { isc: true });
471
495
 
496
+ // A negative drift wakes up early: 6 days before 9:00 on December 25th
497
+ next('0 0 9 25 12 * -518400');
498
+
472
499
  // Timezones: 9:00 in Tokyo, whatever timezone this computer is in
473
500
  await dateblock('0 0 9', { timeZone: 'Asia/Tokyo' });
474
501
 
@@ -32,7 +32,7 @@ LT_INIT([disable-static])
32
32
 
33
33
  # Shared library version (current:revision:age); see the libtool manual
34
34
  # before changing this. It produces libdateblock.so.1
35
- AC_SUBST([DATEBLOCK_LT_VERSION], [2:0:1])
35
+ AC_SUBST([DATEBLOCK_LT_VERSION], [2:1:1])
36
36
 
37
37
  AC_CONFIG_FILES([Makefile src/Makefile tests/Makefile dateblock.pc])
38
38
  AC_OUTPUT
@@ -32,7 +32,7 @@ Run ``help(dateblock)``, ``help(dateblock.wait)`` and
32
32
  # The package details.
33
33
  __title__ = "dateblock"
34
34
  __description__ = "Block (sleep) until a cron-style moment in time"
35
- __version__ = "2.1.0"
35
+ __version__ = "2.2.0"
36
36
  __author__ = "Chris Caron"
37
37
  __email__ = "lead2gold@gmail.com"
38
38
  __license__ = "GPL-2.0-or-later"
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: dateblock
3
- Version: 2.1.0
3
+ Version: 2.2.0
4
4
  Summary: Block (sleep) until a cron-style moment in time
5
5
  Home-page: https://github.com/caronc/dateblock
6
6
  Author: Chris Caron
@@ -62,8 +62,8 @@ The difference is very subtle but can prove to be very useful.
62
62
 
63
63
  It offers a few new features that the Internet Systems Consortium (ICS) version
64
64
  does not (refering to /etc/crontab style). Dateblock specifically offers the ability
65
- to wake/unblock on a _specific second_ and/or _drift_ for certain period afterwards
66
- your calculated time is met (kind of like combining _sleep_ and _ICS Cron_ into one).
65
+ to wake/unblock on a _specific second_ and/or _drift_ a number of seconds after
66
+ (or before) your calculated time is met (kind of like combining _sleep_ and _ICS Cron_ into one).
67
67
 
68
68
  Python, PHP and Node.js bindings are additionally included, supporting the
69
69
  standard date and time objects of each language!
@@ -118,7 +118,8 @@ __Allowed options__:
118
118
  -m [ --month ] arg Month (1-12 or Jan-Dec)
119
119
  -w [ --dow ] arg Day of Week (0-6 or Sun-Sat; Sun=0 or 7)
120
120
  -c [ --cron ] arg Cron string formatting
121
- -x [ --drift ] arg Additional drift time (in seconds).
121
+ -x [ --drift ] arg Additional drift time (in seconds); a negative
122
+ drift wakes up that much earlier.
122
123
  -z [ --timezone ] arg Timezone to read the schedule in (ie:
123
124
  America/Toronto); defaults to this computer's.
124
125
  -C [ --count ] arg List the next N times (1-1000) instead of
@@ -140,7 +141,7 @@ A variety of syntax is accepted by this tool such as:
140
141
  too, so Fri-7 is Friday to Sunday.
141
142
  ```
142
143
 
143
- __Note__: With the exception of the drift option (+), all variations of the syntax
144
+ __Note__: With the exception of the drift option (+ or -), all variations of the syntax
144
145
  mentioned above can be mixed if separated using the 'comma' (Separator)
145
146
  operator. ie: */a,b,c-d,/e is valid.
146
147
  However: x-y-z is not valid, nor is /x/y, /x-y or a '*' mixed with
@@ -166,7 +167,7 @@ interested in. Asterisks are automatically placed in strings missing all 6
166
167
  arguments (separated by white space).
167
168
 
168
169
  The crontab shortcuts work too (with or without `--isc`). Without `--isc`
169
- one can be followed by a drift (ie: `@daily +30`):
170
+ one can be followed by a drift (ie: `@daily +30` or `@daily -30`):
170
171
  ```
171
172
  @hourly the start of every hour
172
173
  @daily (or @midnight) every day at 00:00
@@ -259,12 +260,13 @@ every minute. You could however achieve the previous example using a drift
259
260
  value of '60' (seconds) and a cron (minute) entry of '*/10'.
260
261
 
261
262
  In a way, drifting is a little like executing a cron and then sleeping for
262
- an additional period right afterwards. However this is a much more elegant
263
+ an additional period right afterwards (a negative drift wakes up that much
264
+ sooner instead). However this is a much more elegant
263
265
  solution with a more precise blocking period.
264
266
 
265
267
  Some things to consider about drifting:
266
268
 
267
- - Drifting is always calculated 'after' a specified cron.
269
+ - Drifting is always calculated from the time the cron matches.
268
270
  - You can not specify a drifting value on it's own without a cron entry to go
269
271
  with it.
270
272
  - A drift value is always specified in seconds.
@@ -296,6 +298,16 @@ Some things to consider about drifting:
296
298
  ```dateblock -c '* */5 +60'``` is the same as writing ```dateblock -c '* */5 * * * * 60'```
297
299
  Once you invoke the __+__ character however, any entries found afterwards
298
300
  will be treated as an error.
301
+ - A drift can be negative; each matched time then moves _back_ that many
302
+ seconds instead of forward.
303
+ - A reminder 3 days (259200 seconds) before rent is due on the 1st:
304
+ ```dateblock -c '0 0 9 1 * * -259200'``` wakes at 9:00 on the 28th, 29th
305
+ or 26th, however long the month is.
306
+ - ```dateblock -c '0 0 9 25 12 * -518400'``` (or ```-x -518400```) wakes at
307
+ 9:00 on December 19th (6 days before December 25th).
308
+ - Like _+_, an entry starting with _-_ jumps to the drift.
309
+ - An early time that has already passed is skipped, like any past match.
310
+ - Drifts go from -2147483647 to 2147483647.
299
311
 
300
312
  ### Examples
301
313
  The following would block until a minute divisible by 10 was reached. Minutes
@@ -343,7 +355,7 @@ dateblock -c "* 5 * * * * 20"
343
355
  dateblock -n 5 -x 20
344
356
 
345
357
  # You can skip to the drift portion by just denoting a plus (+) as the last
346
- # entry specified. This is interpreted as a drift time.
358
+ # entry specified (or a minus (-) for a negative drift). This is interpreted as a drift time.
347
359
  dateblock -c "* 5 +20"
348
360
  ```
349
361
  The expression is considered invalid if more entries are found after the drift
@@ -352,6 +364,12 @@ period. However this is still valid:
352
364
  dateblock -c "* 5 * * * * +20"
353
365
  ```
354
366
 
367
+ A negative drift wakes up early; this blocks until 4:40 past each hour (20
368
+ seconds before the 5 minute mark):
369
+ ```bash
370
+ dateblock -n 5 -x -20
371
+ ```
372
+
355
373
  ### DateBlock Python Bindings
356
374
  The DateBlock python bindings are really easy to use too:
357
375
  ```python
@@ -375,6 +393,9 @@ result = dateblock("* /20", ref=relative, block=False)
375
393
  # had you not set the isc flag)
376
394
  result = dateblock("/1", isc=True)
377
395
 
396
+ # A negative drift wakes up early: 6 days before 9:00 on December 25th
397
+ result = dateblock("0 0 9 25 12 * -518400", block=False)
398
+
378
399
  # Dateblock supports date, time and epoch time too!
379
400
  #
380
401
  # Date: (yyyy, mm, dd)
@@ -469,6 +490,9 @@ $result = dateblock('/5', false, 13424236);
469
490
  // A regular crontab entry: the start of each minute
470
491
  $result = dateblock('* * * * *', isc: true);
471
492
 
493
+ // A negative drift wakes up early: 6 days before 9:00 on December 25th
494
+ $result = dateblock('0 0 9 25 12 * -518400', block: false);
495
+
472
496
  // Timezones: the cron is read in the timezone of the reference, and the
473
497
  // result comes back in that same timezone. This waits for 9:00 in Tokyo.
474
498
  $tokyo = new DateTimeImmutable('now', new DateTimeZone('Asia/Tokyo'));
@@ -517,6 +541,9 @@ next('* /20', { ref: new Date(Date.now() - 4 * 86400000) });
517
541
  // A regular crontab entry: the start of each minute
518
542
  next('* * * * *', { isc: true });
519
543
 
544
+ // A negative drift wakes up early: 6 days before 9:00 on December 25th
545
+ next('0 0 9 25 12 * -518400');
546
+
520
547
  // Timezones: 9:00 in Tokyo, whatever timezone this computer is in
521
548
  await dateblock('0 0 9', { timeZone: 'Asia/Tokyo' });
522
549
 
@@ -257,6 +257,44 @@ class DateblockTests(unittest.TestCase):
257
257
  self.assertEqual(db("* 10 +5,8", ref=date(2000, 3, 1), block=False),
258
258
  datetime(2000, 3, 1, 0, 10, 5))
259
259
 
260
+ def test_negative_drift_wakes_up_early(self):
261
+ # 6 days (518400 seconds) before 9:00 on December 25th
262
+ ref = date(2026, 11, 10)
263
+ for cron in ("0 0 9 25 12 * -518400", "0 0 9 25 12 -518400",
264
+ "0 0 9 25 12 +-518400"):
265
+ self.assertEqual(db(cron, ref=ref, block=False),
266
+ datetime(2026, 12, 19, 9, 0, 0), cron)
267
+
268
+ def test_negative_drift_skips_early_times_already_passed(self):
269
+ # Christmas is still to come, but its early time is not: next year
270
+ for ref in (datetime(2026, 12, 19, 9, 0, 0), date(2026, 12, 22)):
271
+ self.assertEqual(db("0 0 9 25 12 * -518400", ref=ref,
272
+ block=False),
273
+ datetime(2027, 12, 19, 9, 0, 0), ref)
274
+ self.assertEqual(db("@yearly -1", ref=date(2026, 6, 1), block=False),
275
+ datetime(2026, 12, 31, 23, 59, 59))
276
+
277
+ def test_negative_and_positive_drifts_mixed(self):
278
+ cron = "0 0 10 20 12 * -3600,3600"
279
+ self.assertEqual(db(cron, ref=datetime(2026, 12, 20, 8), block=False),
280
+ datetime(2026, 12, 20, 9, 0, 0))
281
+ self.assertEqual(db(cron, ref=datetime(2026, 12, 20, 9, 30),
282
+ block=False),
283
+ datetime(2026, 12, 20, 11, 0, 0))
284
+
285
+ def test_negative_drift_with_a_timezone(self):
286
+ plus9 = timezone(timedelta(hours=9))
287
+ ref = datetime(2026, 11, 10, tzinfo=plus9)
288
+ self.assertEqual(db("0 0 9 25 12 -518400", ref=ref, block=False),
289
+ datetime(2026, 12, 19, 9, 0, 0, tzinfo=plus9))
290
+
291
+ def test_drift_limits(self):
292
+ self.assertEqual(dateblock.DRIFT_MIN, -dateblock.DRIFT_MAX)
293
+ self.assertEqual(
294
+ db("* * * * * * %d" % dateblock.DRIFT_MIN, ref=date(2000, 1, 1),
295
+ block=False),
296
+ datetime(2000, 1, 1, 0, 0, 53))
297
+
260
298
  def test_month_and_weekday_names(self):
261
299
  # 2000-01-01 is a Saturday
262
300
  ref = date(2000, 1, 1)
@@ -285,7 +323,10 @@ class DateblockTests(unittest.TestCase):
285
323
  ("0 0 0 * * Sat-Sun", "'Sat-Sun' goes backwards"),
286
324
  ("0 Mon", "'Mon' is not a number"),
287
325
  ("@reboot", "'@reboot' is not a time"),
288
- ("@daily 5", "only a drift (ie: '+20') may follow")):
326
+ ("@daily 5", "only a drift (ie: '+20' or '-20') may follow"),
327
+ ("0 -5 0", "nothing may follow the drift"),
328
+ ("* * * * * * -2147483648",
329
+ "-2147483648 is outside of -2147483647 to 2147483647")):
289
330
  with self.assertRaises(SyntaxError) as raised:
290
331
  db(cron, block=False)
291
332
  self.assertIn(says, str(raised.exception))
@@ -781,6 +822,12 @@ class UpcomingTests(unittest.TestCase):
781
822
  with self.assertRaises(TypeError):
782
823
  dateblock.upcoming("* 5", count)
783
824
 
825
+ def test_negative_drift_lists_each_early_time(self):
826
+ self.assertEqual(
827
+ dateblock.upcoming("@daily -3600", 3, ref=datetime(2026, 1, 1, 12)),
828
+ (datetime(2026, 1, 1, 23), datetime(2026, 1, 2, 23),
829
+ datetime(2026, 1, 3, 23)))
830
+
784
831
  def test_each_time_follows_the_one_before(self):
785
832
  cron = "*/20 5 1-3 * * * +7"
786
833
  at = datetime(2026, 1, 1)
@@ -353,12 +353,13 @@ namespace {
353
353
 
354
354
  // Splits a whitespace separated cron string into its fields.
355
355
  // - Missing fields are filled in with '*'.
356
- // - A '+N' entry (dateblock format only) jumps straight to the drift
357
- // field; nothing may follow it.
356
+ // - A '+N' or '-N' entry (dateblock format only) jumps straight to the
357
+ // drift field; nothing may follow it. No other field can be below
358
+ // zero, so a '-' at the start of an entry always means a drift.
358
359
  // - A shortcut such as '@daily' must be the first entry; only a '+N'
359
- // drift may follow it (dateblock format).
360
+ // or '-N' drift may follow it (dateblock format).
360
361
  // - Returns false (with 'why' set) if the string has more fields than
361
- // allowed, or a '+' without a drift.
362
+ // allowed, or a '+' or '-' without a drift.
362
363
  bool SplitCronString(const string& strIn, bool isISC,
363
364
  vector<string>& tokens, string& why)
364
365
  {
@@ -385,15 +386,17 @@ namespace {
385
386
 
386
387
  if (drifted)
387
388
  {
388
- why = "nothing may follow the drift (the '+' entry)";
389
+ why = "nothing may follow the drift (the '+' or '-' entry)";
389
390
  ok = false;
390
391
  break;
391
392
  }
392
- if (!shortcut.empty() && (isISC || token[0] != '+'))
393
+ bool drift = !isISC && (token[0] == '+' || token[0] == '-');
394
+ if (!shortcut.empty() && !drift)
393
395
  {
394
396
  why = isISC ?
395
397
  "nothing may follow '" + shortcut + "'" :
396
- "only a drift (ie: '+20') may follow '" + shortcut + "'";
398
+ "only a drift (ie: '+20' or '-20') may follow '" + shortcut +
399
+ "'";
397
400
  ok = false;
398
401
  break;
399
402
  }
@@ -425,19 +428,22 @@ namespace {
425
428
  break;
426
429
  }
427
430
 
428
- if (!isISC && token[0] == '+')
431
+ if (drift)
429
432
  {
430
- // drift entry; fill in our blanks first
433
+ // drift entry; fill in our blanks first. A '-' stays (it is
434
+ // part of the number), a '+' is dropped.
431
435
  while (tokens.size() < (token_max-1))
432
436
  tokens.push_back("*");
433
- token.erase(0, 1);
434
437
  drifted = true;
435
- if (token.empty())
438
+ if (token == "+" || token == "-")
436
439
  {
437
- why = "a '+' needs the drift after it (ie: '+20')";
440
+ why = "a '" + token + "' needs the drift after it (ie: '" +
441
+ token + "20')";
438
442
  ok = false;
439
443
  break;
440
444
  }
445
+ if (token[0] == '+')
446
+ token.erase(0, 1);
441
447
  }
442
448
  tokens.push_back(token);
443
449
  }
@@ -449,9 +455,17 @@ namespace {
449
455
  return ok;
450
456
  }
451
457
 
458
+ // A field's range as shown in a message: '0-59', or '-10 to 10' when
459
+ // it goes below zero (so the '-' isn't read as a range)
460
+ string RangeText(int lo, int hi)
461
+ {
462
+ return to_string(lo) + (lo < 0 ? " to " : "-") + to_string(hi);
463
+ }
464
+
452
465
  // Reads the number starting at 'pos' (moving 'pos' past it), stopping
453
466
  // at 'end'. Fails (with 'why' set) if there is no number there or it
454
467
  // is outside of 'lo' to 'hi'.
468
+ // - When 'lo' is below zero, the number may start with a '-'.
455
469
  // - 'names' (if not NULL) holds a name for each value from 'lo' on
456
470
  // (ie: "Jan" for 1), ending with NULL; a name may be used instead of
457
471
  // the number, and upper and lower case don't matter.
@@ -480,30 +494,38 @@ namespace {
480
494
  why = "'" + Shown(word) + "' is not a name (use " + list + ")";
481
495
  return false;
482
496
  }
497
+ // A field that allows values below zero (the drift) takes a '-'
498
+ // in front of the number
499
+ bool negative = (lo < 0 && pos < end && str[pos] == '-');
500
+ if (negative)
501
+ pos++;
502
+ size_t digits = pos;
503
+ long long limit = negative ? -(long long)lo : (long long)hi;
483
504
  while (pos < end && IsDigit((unsigned char)str[pos]))
484
505
  {
485
506
  out = out * 10 + (str[pos] - '0');
486
- if (out > hi)
507
+ if (out > limit)
487
508
  {
488
509
  while (pos < end && IsDigit((unsigned char)str[pos]))
489
510
  pos++;
490
511
  why = Shown(str, start, pos - start) + " is outside of " +
491
- to_string(lo) + "-" + to_string(hi);
512
+ RangeText(lo, hi);
492
513
  return false;
493
514
  }
494
515
  pos++;
495
516
  }
496
- if (pos == start)
517
+ if (pos == digits)
497
518
  {
498
- why = (pos < end) ?
499
- "'" + Shown(str, pos, end - pos) + "' is not a number" :
519
+ why = (start < end && (pos < end || negative)) ?
520
+ "'" + Shown(str, start, end - start) + "' is not a number" :
500
521
  "a number is missing at the end";
501
522
  return false;
502
523
  }
524
+ if (negative)
525
+ out = -out;
503
526
  if (out < lo)
504
527
  {
505
- why = to_string(out) + " is outside of " + to_string(lo) + "-" +
506
- to_string(hi);
528
+ why = to_string(out) + " is outside of " + RangeText(lo, hi);
507
529
  return false;
508
530
  }
509
531
  return true;
@@ -837,6 +859,8 @@ namespace {
837
859
  // every x'th value from the lowest one (crontab)
838
860
  // Anything else (ie: '5-', 'x-y-z', '/x/y', or an empty entry such as
839
861
  // '5,,6') is invalid.
862
+ // - In a field that goes below zero (the drift), x and y may be
863
+ // negative (ie: '-60', '-120--60' or '-60-60'); a step never is.
840
864
  // - Each entry is handed to 'add(first, last, step)', which may refuse
841
865
  // it by returning false.
842
866
  // - 'given' is false when the field is '*' (or empty).
@@ -890,7 +914,7 @@ namespace {
890
914
  {
891
915
  // Modulus
892
916
  pos += (str[start] == '/') ? 1 : 2;
893
- if (!ReadNumber(str, pos, end, lo, hi, step, why))
917
+ if (!ReadNumber(str, pos, end, lo < 0 ? 0 : lo, hi, step, why))
894
918
  {
895
919
  why = "the step after '/': " + why;
896
920
  return false;
@@ -1200,6 +1224,10 @@ namespace {
1200
1224
  // A drift is added after the match. Matching from 'now - drift' keeps
1201
1225
  // only the part of the drift that fits inside the schedule's interval,
1202
1226
  // so no match is ever skipped. With several drifts the earliest wins.
1227
+ // - A negative drift works the same way: the search starts after
1228
+ // 'now + |drift|', so the time it gives (the match less the
1229
+ // drift) is still after now. Matches whose early time has
1230
+ // already passed are skipped.
1203
1231
  // - Drifts are handled largest first. One search from 'now -
1204
1232
  // largest' finds a match that every drift reaching past it shares,
1205
1233
  // so those need no search of their own; only the smallest of them
@@ -1218,14 +1246,17 @@ namespace {
1218
1246
  while (end != drifts.begin())
1219
1247
  {
1220
1248
  time_t largest = *(end - 1);
1221
- if (after < lowest + largest)
1249
+ if (largest > 0 ? after < lowest + largest
1250
+ : after > highest + largest)
1222
1251
  throw runtime_error("The reference time is out of range.");
1223
1252
  time_t match = NextMatch(s, after - largest, zone);
1224
1253
 
1225
- // The smallest drift that still lands after now
1254
+ // The smallest drift that still lands after now. (A negative one
1255
+ // lands between now and the match, so only a positive one can
1256
+ // overflow.)
1226
1257
  vector<time_t>::iterator use =
1227
1258
  upper_bound(drifts.begin(), end, after - match);
1228
- if (match > highest - *use)
1259
+ if (*use > 0 && match > highest - *use)
1229
1260
  throw runtime_error("The matching time is out of range.");
1230
1261
  time_t when = match + *use;
1231
1262
  if (when < best)
@@ -1267,14 +1298,11 @@ const int Date::DBL_CRON_FIELD_COUNT = 7;
1267
1298
  // Drift Defaults
1268
1299
  const int Date::T_NO_ENTRY=-1;
1269
1300
 
1270
- // Presently our AddSec() function only handles integers as input and therefore
1271
- // we need to set the max to the maximum values of a 'signed integer' AddSec()
1272
- // will always take a signed value since we allow the decrementing of time by
1273
- // passing in a negative number
1301
+ // A drift is a number of seconds that fits in an int. It may be negative
1302
+ // (wake up that long before the matched time); the lowest is -INT_MAX so
1303
+ // that every drift can be turned around without overflowing.
1274
1304
  const int Date::T_DRIFT_MAX=INT_MAX;
1275
-
1276
- // Minimum Drift Time
1277
- const int Date::T_DRIFT_MIN=0;
1305
+ const int Date::T_DRIFT_MIN=-INT_MAX;
1278
1306
 
1279
1307
  //------------------------------------------------
1280
1308
  //
@@ -1883,7 +1911,9 @@ const Date Date::Cron(int lSecOffset,
1883
1911
  f.given[i] = (values[i] >= Field(i).lo && values[i] <= Field(i).hi);
1884
1912
  f.mask[i] = f.given[i] ? (uint64_t)1 << values[i] : 0;
1885
1913
  }
1886
- if (lDriftOffset >= T_DRIFT_MIN && lDriftOffset <= T_DRIFT_MAX)
1914
+ // T_NO_ENTRY (-1) still means no drift here; a drift of -1 second needs
1915
+ // one of the string versions
1916
+ if (lDriftOffset != T_NO_ENTRY && lDriftOffset >= T_DRIFT_MIN)
1887
1917
  f.drifts.push_back(lDriftOffset);
1888
1918
  f.isISC = false;
1889
1919
  f.domStar = f.dowStar = true;
@@ -55,6 +55,7 @@ class Date
55
55
  static const int T_DOM_MAX;
56
56
  static const int T_DOW_MIN;
57
57
  static const int T_DOW_MAX;
58
+ // A drift may be negative (-INT_MAX to INT_MAX seconds)
58
59
  static const int T_DRIFT_MIN;
59
60
  static const int T_DRIFT_MAX;
60
61
  static const int T_NO_ENTRY;
@@ -201,7 +202,12 @@ class Date
201
202
  // The string versions also take names (Jan-Dec, Sun-Sat) and, for
202
203
  // a whole cron string, shortcuts such as '@daily'.
203
204
  // Drift Offset identifies how many seconds to adjust
204
- // the final results by.
205
+ // the final results by; a negative drift wakes up that
206
+ // many seconds before the matched time (ie: -86400 is a
207
+ // day early). The result is always after this date: a
208
+ // match whose early time has already passed is skipped.
209
+ // In the int version below, -1 (T_NO_ENTRY) still means
210
+ // no drift.
205
211
  // Returns a Date Object of the next moment in time
206
212
  // that matches specified cron entry
207
213
  // By default lSecoffset is always set to 0 if 'nothing'
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes