Skip to content

Commit

Permalink
feat: factory function 'fromMilliseconds' added (#24)
Browse files Browse the repository at this point in the history
* feat: factory function 'fromMilliseconds' added

* fix: timezone problem failing tests
  • Loading branch information
ali77gh authored Aug 4, 2024
1 parent 85ff4ba commit 528a63e
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 0 deletions.
6 changes: 6 additions & 0 deletions lib/src/gregorian/gregorian_date.dart
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,12 @@ class Gregorian extends Date {
);
}

/// Create a Gregorian date from milliseconds since epoch
factory Gregorian.fromMillisecondsSinceEpoch(int milliseconds) {
return Gregorian.fromDateTime(
DateTime.fromMillisecondsSinceEpoch(milliseconds));
}

/// Get Gregorian date for now
factory Gregorian.now() {
return Gregorian.fromDateTime(DateTime.now());
Expand Down
6 changes: 6 additions & 0 deletions lib/src/jalali/jalali_date.dart
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,12 @@ class Jalali extends Date {
);
}

/// Create a Jalali date from milliseconds since epoch
factory Jalali.fromMillisecondsSinceEpoch(int milliseconds) {
return Jalali.fromDateTime(
DateTime.fromMillisecondsSinceEpoch(milliseconds));
}

/// Get Jalali date for now
factory Jalali.now() {
return Gregorian.now().toJalali();
Expand Down
28 changes: 28 additions & 0 deletions test/shamsi_date_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2449,6 +2449,34 @@ void main() {
expect(Gregorian(2119, 3, 21), Jalali(1498));
expect(true, Jalali(1498).isLeapYear());
});

test('Gregorian FromMilliseconds Test Cases', () {
const millis = 1722319497219;
final timezone = DateTime.now().timeZoneOffset;
final gdt = Gregorian.fromMillisecondsSinceEpoch(
millis - timezone.inMilliseconds); // defusing timezone
expect(gdt.year, 2024);
expect(gdt.month, 7);
expect(gdt.day, 30);
expect(gdt.hour, 6);
expect(gdt.minute, 4);
expect(gdt.second, 57);
expect(gdt.millisecond, 219);
});

test('Jalali FromMilliseconds Test Cases', () {
const millis = 1722319497219;
final timezone = DateTime.now().timeZoneOffset;
final jdt = Jalali.fromMillisecondsSinceEpoch(
millis - timezone.inMilliseconds); // defusing timezone
expect(jdt.year, 1403);
expect(jdt.month, 5);
expect(jdt.day, 9);
expect(jdt.hour, 6);
expect(jdt.minute, 4);
expect(jdt.second, 57);
expect(jdt.millisecond, 219);
});
}

/// Mock Gregorian
Expand Down

0 comments on commit 528a63e

Please sign in to comment.