From 40636246df7d4d2605b1169f03f60e31b766b590 Mon Sep 17 00:00:00 2001 From: krawieck Date: Fri, 12 Feb 2021 17:53:51 +0100 Subject: [PATCH] Add extension for relative time and fix a bug the bug was that time from server is in UTC format, and our time is in local format, so it needed to be shifted back to UTC. oddly enough .toUtc() method didn't work --- lib/util/extensions/datetime.dart | 12 ++++++++++++ lib/widgets/comment.dart | 4 ++-- lib/widgets/post.dart | 4 ++-- lib/widgets/user_profile.dart | 4 ++-- 4 files changed, 18 insertions(+), 6 deletions(-) create mode 100644 lib/util/extensions/datetime.dart diff --git a/lib/util/extensions/datetime.dart b/lib/util/extensions/datetime.dart new file mode 100644 index 0000000..3628e5a --- /dev/null +++ b/lib/util/extensions/datetime.dart @@ -0,0 +1,12 @@ +import 'package:timeago/timeago.dart' as timeago; + +extension FancyTime on DateTime { + /// returns [this] time as a relative, human-readable string. In short format + String get fancyShort => timeago.format(this, + locale: 'en_short', + clock: DateTime.now().subtract(DateTime.now().timeZoneOffset)); + + /// returns [this] time as a relative, human-readable string + String get fancy => timeago.format(this, + clock: DateTime.now().subtract(DateTime.now().timeZoneOffset)); +} diff --git a/lib/widgets/comment.dart b/lib/widgets/comment.dart index 12910c0..3b7df7d 100644 --- a/lib/widgets/comment.dart +++ b/lib/widgets/comment.dart @@ -5,7 +5,6 @@ import 'package:flutter/material.dart'; import 'package:flutter/services.dart'; import 'package:flutter_hooks/flutter_hooks.dart'; import 'package:lemmy_api_client/v2.dart'; -import 'package:timeago/timeago.dart' as timeago; import 'package:url_launcher/url_launcher.dart' as ul; import '../comment_tree.dart'; @@ -13,6 +12,7 @@ import '../hooks/delayed_loading.dart'; import '../hooks/logged_in_action.dart'; import '../hooks/stores.dart'; import '../util/extensions/api.dart'; +import '../util/extensions/datetime.dart'; import '../util/goto.dart'; import '../util/intl.dart'; import '../util/text_color.dart'; @@ -385,7 +385,7 @@ class CommentWidget extends HookWidget { Text(compactNumber(comment.counts.score + (wasVoted ? 0 : myVote.value.value))), const Text(' · '), - Text(timeago.format(comment.comment.published)), + Text(comment.comment.published.fancy), ], ), ) diff --git a/lib/widgets/post.dart b/lib/widgets/post.dart index 8293973..9f417c9 100644 --- a/lib/widgets/post.dart +++ b/lib/widgets/post.dart @@ -6,7 +6,6 @@ import 'package:flutter/material.dart'; import 'package:flutter_hooks/flutter_hooks.dart'; import 'package:intl/intl.dart'; import 'package:lemmy_api_client/v2.dart'; -import 'package:timeago/timeago.dart' as timeago; import 'package:url_launcher/url_launcher.dart' as ul; import '../hooks/delayed_loading.dart'; @@ -15,6 +14,7 @@ import '../pages/full_post.dart'; import '../url_launcher.dart'; import '../util/cleanup_url.dart'; import '../util/extensions/api.dart'; +import '../util/extensions/datetime.dart'; import '../util/goto.dart'; import '../util/more_icon.dart'; import 'bottom_modal.dart'; @@ -216,7 +216,7 @@ class PostWidget extends HookWidget { ), TextSpan( text: - ' · ${timeago.format(post.post.published, locale: 'en_short')}'), + ' · ${post.post.published.fancyShort}'), if (post.post.locked) const TextSpan(text: ' · 🔒'), if (post.post.stickied) diff --git a/lib/widgets/user_profile.dart b/lib/widgets/user_profile.dart index 3ffdbf6..dd80b52 100644 --- a/lib/widgets/user_profile.dart +++ b/lib/widgets/user_profile.dart @@ -3,12 +3,12 @@ import 'package:flutter/material.dart'; import 'package:flutter_hooks/flutter_hooks.dart'; import 'package:intl/intl.dart'; import 'package:lemmy_api_client/v2.dart'; -import 'package:timeago/timeago.dart' as timeago; import '../hooks/memo_future.dart'; import '../hooks/stores.dart'; import '../pages/manage_account.dart'; import '../util/extensions/api.dart'; +import '../util/extensions/datetime.dart'; import '../util/goto.dart'; import '../util/intl.dart'; import '../util/text_color.dart'; @@ -282,7 +282,7 @@ class _UserOverview extends HookWidget { ), const SizedBox(height: 15), Text( - 'Joined ${timeago.format(userView.user.published)}', + 'Joined ${userView.user.published.fancy}', style: theme.textTheme.bodyText1, ), Row(