Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

works on past and future, and custom suffix #3

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 12 additions & 6 deletions lib/time_formatter.dart
Original file line number Diff line number Diff line change
@@ -1,17 +1,14 @@
/*
Time formatting library for Dart programs.
Copyright (C) 2019 Jeremi Gendron <[email protected]>

This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License along
with this program; if not, write to the Free Software Foundation, Inc.,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
Expand All @@ -20,10 +17,19 @@
/// Formats a given UNIX millisecond timestamp into a human-readable string.
///
/// Progresses from smallest unit (second), to largest (years)
String formatTime(int timestamp) {
String formatTime(int timestamp, {String pastsuffix = '', String futuresuffix = ''}) {
/// The number of milliseconds that have passed since the timestamp
int difference = DateTime.now().millisecondsSinceEpoch - timestamp;
int differenceReal = DateTime.now().millisecondsSinceEpoch - timestamp;
int difference = differenceReal.abs();
String result;

String suffix;

if (differenceReal > 0) {
suffix = pastsuffix;
} else {
suffix = futuresuffix;
}

if (difference < 60000) {
result = countSeconds(difference);
Expand All @@ -40,7 +46,7 @@ String formatTime(int timestamp) {
} else
result = countYears(difference);

return !result.startsWith("J") ? result + ' ago' : result;
return !result.startsWith("J") ? result + ' ' + suffix : result;
}

/// Converts the time difference to a number of seconds.
Expand Down