-
Notifications
You must be signed in to change notification settings - Fork 26
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(nextcloud): add date time utils
Signed-off-by: Nikolas Rimikis <leptopoda@users.noreply.github.com>
- Loading branch information
Showing
3 changed files
with
48 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
/// Common methods to work with [DateTime] data. | ||
extension DateTimeUtils on DateTime { | ||
/// Constructs a new [DateTime] instance | ||
/// with the given [secondsSinceEpoch]. | ||
/// | ||
/// If [isUtc] is false then the date is in the local time zone. | ||
/// | ||
/// The constructed [DateTime] represents | ||
/// 1970-01-01T00:00:[secondsSinceEpoch]Z in the given | ||
/// time zone (local or UTC). | ||
/// ```dart | ||
/// final newYearsDay = | ||
/// DateTimeUtils.fromSecondsSinceEpoch(1641031200, isUtc:true); | ||
/// print(newYearsDay); // 2022-01-01 10:00:00.000Z | ||
/// ``` | ||
static DateTime fromSecondsSinceEpoch(int secondsSinceEpoch, {bool isUtc = false}) { | ||
final millisecondsSinceEpoch = secondsSinceEpoch * Duration.millisecondsPerSecond; | ||
|
||
return DateTime.fromMillisecondsSinceEpoch(millisecondsSinceEpoch, isUtc: isUtc); | ||
} | ||
|
||
/// The number of seconds since the "Unix epoch" 1970-01-01T00:00:00Z (UTC). | ||
/// | ||
/// This value is independent of the time zone. | ||
/// | ||
/// This value is at most | ||
/// 8,640,000,000,000s (100,000,000 days) from the Unix epoch. | ||
/// In other words: `secondsSinceEpoch.abs() <= 8640000000000`. | ||
int get secondsSinceEpoch => millisecondsSinceEpoch ~/ Duration.millisecondsPerSecond; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
/// This library contains various utility methods for working with nextcloud APIs. | ||
library; | ||
|
||
export 'src/utils/date_time.dart'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import 'package:nextcloud/src/utils/date_time.dart'; | ||
import 'package:test/test.dart'; | ||
|
||
void main() { | ||
test('DateTimeUtils', () { | ||
final date = DateTime.utc(2016, 6, 2, 23, 53, 45, 243, 234); | ||
|
||
expect(date.secondsSinceEpoch, 1464911625); | ||
expect(date.microsecondsSinceEpoch, 1464911625243234); | ||
|
||
expect(DateTimeUtils.fromSecondsSinceEpoch(1464911625, isUtc: true), date.copyWith(millisecond: 0, microsecond: 0)); | ||
expect(DateTimeUtils.fromSecondsSinceEpoch(1464911625, isUtc: true).microsecondsSinceEpoch, 1464911625000000); | ||
}); | ||
} |