Skip to content

Commit

Permalink
Add toCronString function (#50)
Browse files Browse the repository at this point in the history
  • Loading branch information
mbfakourii committed Dec 23, 2023
1 parent 9a738b8 commit 61fe62d
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 9 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Changelog

## 0.6.0

- Add `toCronString` function and improve package. [#50](https://github.com/agilord/cron/pull/50) by [mbfakourii](https://github.com/mbfakourii)

## 0.5.1

- Fixed scheduling issue. [#42](https://github.com/agilord/cron/issues/42) by [ccl0326](https://github.com/ccl0326)
Expand Down
20 changes: 18 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
# A cron-like time-based job scheduler for Dart
# Cron

Run tasks periodically at fixed times or intervals.

[![pub package](https://img.shields.io/pub/v/cron.svg)](https://pub.dev/packages/cron)

## Usage

A simple usage example:

```dart
import 'package:cron/cron.dart';
main() {
void main() {
final cron = Cron();
cron.schedule(Schedule.parse('*/3 * * * *'), () async {
Expand All @@ -22,6 +24,20 @@ main() {
}
```

## Cron parser
You can easily create and parse [cron format](https://en.wikipedia.org/wiki/Cron):

```dart
import 'package:cron/cron.dart';
void main() {
print(Schedule.parse('3-5 * * * *').minutes); // [3, 4, 5]
print(Schedule(hours: 12, minutes: 25, weekdays: [2, 3])
.toCronString()); // 25 12 * * 2,3
}
```

## Links

- [source code][source]
Expand Down
25 changes: 25 additions & 0 deletions lib/cron.dart
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,31 @@ class Schedule {
seconds != null &&
seconds!.isNotEmpty &&
(seconds!.length != 1 || !seconds!.contains(0));

/// Converts the schedule into a cron-formatted string.
String toCronString({bool hasSecond = false}) {
final minutesStr = _convertToCronString(minutes);
final hoursStr = _convertToCronString(hours);
final daysStr = _convertToCronString(days);
final monthsStr = _convertToCronString(months);
final weekdaysStr = _convertToCronString(weekdays);

if (hasSecond) {
final secondsStr = _convertToCronString(seconds);

return '$secondsStr $minutesStr $hoursStr $daysStr $monthsStr $weekdaysStr';
} else {
return '$minutesStr $hoursStr $daysStr $monthsStr $weekdaysStr';
}
}

String _convertToCronString(List<int>? list) {
if (list == null || list.isEmpty) {
return '*';
} else {
return list.join(',');
}
}
}

abstract class ScheduledTask {
Expand Down
21 changes: 14 additions & 7 deletions pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,15 +1,22 @@
name: cron
description: A time-based job scheduler similar to cron. Run tasks periodically at fixed times or intervals.
version: 0.5.1
homepage: https://github.com/agilord/cron
version: 0.6.0
repository: https://github.com/agilord/cron

topics:
- cron
- scheduler
- cron-parser
- schedule
- cron-time

environment:
sdk: '>=2.12.0 <3.0.0'
sdk: '>=2.12.0 <4.0.0'

dependencies:
clock: ^1.1.0
clock: ^1.1.1

dev_dependencies:
pedantic: ^1.0.0
test: ^1.0.0
fake_async: ^1.2.0
pedantic: ^1.11.1
test: ^1.25.0
fake_async: ^1.3.1
7 changes: 7 additions & 0 deletions test/cron_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -42,4 +42,11 @@ void main() {
expect(count, 1);
}, initialTime: DateTime(2000, 1, 1, 0, 0, 0, 0, 0));
});

test('should return correct cron format string.', () {
expect(
Schedule(hours: 13, minutes: 20, weekdays: [1, 2]).toCronString(),
'20 13 * * 1,2',
);
});
}

0 comments on commit 61fe62d

Please sign in to comment.