@@ -1843,6 +1843,10 @@ class DateText extends StatelessWidget {
1843
1843
Widget build (BuildContext context) {
1844
1844
final messageListTheme = MessageListTheme .of (context);
1845
1845
final zulipLocalizations = ZulipLocalizations .of (context);
1846
+ final formattedTimestamp = MessageTimestampStyle .dateOnlyRelative.format (
1847
+ timestamp,
1848
+ now: ZulipBinding .instance.utcNow ().toLocal (),
1849
+ zulipLocalizations: zulipLocalizations)! ;
1846
1850
return Text (
1847
1851
style: TextStyle (
1848
1852
color: messageListTheme.labelTime,
@@ -1852,46 +1856,7 @@ class DateText extends StatelessWidget {
1852
1856
// https://developer.mozilla.org/en-US/docs/Web/CSS/font-variant-caps#all-small-caps
1853
1857
fontFeatures: const [FontFeature .enable ('c2sc' ), FontFeature .enable ('smcp' )],
1854
1858
),
1855
- formatHeaderDate (
1856
- zulipLocalizations,
1857
- DateTime .fromMillisecondsSinceEpoch (timestamp * 1000 ),
1858
- now: ZulipBinding .instance.utcNow ().toLocal ()));
1859
- }
1860
- }
1861
-
1862
- @visibleForTesting
1863
- String formatHeaderDate (
1864
- ZulipLocalizations zulipLocalizations,
1865
- DateTime dateTime, {
1866
- required DateTime now,
1867
- }) {
1868
- assert (! dateTime.isUtc && ! now.isUtc,
1869
- '`dateTime` and `now` need to be in local time.' );
1870
-
1871
- if (dateTime.year == now.year &&
1872
- dateTime.month == now.month &&
1873
- dateTime.day == now.day) {
1874
- return zulipLocalizations.today;
1875
- }
1876
-
1877
- final yesterday = now
1878
- .copyWith (hour: 12 , minute: 0 , second: 0 , millisecond: 0 , microsecond: 0 )
1879
- .add (const Duration (days: - 1 ));
1880
- if (dateTime.year == yesterday.year &&
1881
- dateTime.month == yesterday.month &&
1882
- dateTime.day == yesterday.day) {
1883
- return zulipLocalizations.yesterday;
1884
- }
1885
-
1886
- // If it is Dec 1 and you see a label that says `Dec 2`
1887
- // it could be misinterpreted as Dec 2 of the previous
1888
- // year. For times in the future, those still on the
1889
- // current day will show as today (handled above) and
1890
- // any dates beyond that show up with the year.
1891
- if (dateTime.year == now.year && dateTime.isBefore (now)) {
1892
- return DateFormat .MMMd ().format (dateTime);
1893
- } else {
1894
- return DateFormat .yMMMd ().format (dateTime);
1859
+ formattedTimestamp);
1895
1860
}
1896
1861
}
1897
1862
@@ -1985,9 +1950,9 @@ class SenderRow extends StatelessWidget {
1985
1950
}
1986
1951
}
1987
1952
1988
- // TODO centralize on this for wherever we show message timestamps
1989
1953
enum MessageTimestampStyle {
1990
1954
none,
1955
+ dateOnlyRelative,
1991
1956
timeOnly,
1992
1957
1993
1958
// TODO(#45): E.g. "Yesterday at 4:47 PM"; see details in #45
@@ -2007,6 +1972,40 @@ enum MessageTimestampStyle {
2007
1972
full,
2008
1973
;
2009
1974
1975
+ static String _formatDateOnlyRelative (
1976
+ DateTime dateTime, {
1977
+ required DateTime now,
1978
+ required ZulipLocalizations zulipLocalizations,
1979
+ }) {
1980
+ assert (! dateTime.isUtc && ! now.isUtc,
1981
+ '`dateTime` and `now` need to be in local time.' );
1982
+
1983
+ if (dateTime.year == now.year &&
1984
+ dateTime.month == now.month &&
1985
+ dateTime.day == now.day) {
1986
+ return zulipLocalizations.today;
1987
+ }
1988
+
1989
+ final yesterday = now
1990
+ .copyWith (hour: 12 , minute: 0 , second: 0 , millisecond: 0 , microsecond: 0 )
1991
+ .add (const Duration (days: - 1 ));
1992
+ if (dateTime.year == yesterday.year &&
1993
+ dateTime.month == yesterday.month &&
1994
+ dateTime.day == yesterday.day) {
1995
+ return zulipLocalizations.yesterday;
1996
+ }
1997
+
1998
+ // If it is Dec 1 and you see a label that says `Dec 2`
1999
+ // it could be misinterpreted as Dec 2 of the previous
2000
+ // year. For times in the future, those still on the
2001
+ // current day will show as today (handled above) and
2002
+ // any dates beyond that show up with the year.
2003
+ if (dateTime.year == now.year && dateTime.isBefore (now)) {
2004
+ return DateFormat .MMMd ().format (dateTime);
2005
+ } else {
2006
+ return DateFormat .yMMMd ().format (dateTime);
2007
+ }
2008
+ }
2010
2009
static final _timeOnlyFormat = DateFormat ('h:mm aa' , 'en_US' );
2011
2010
static final _lightboxFormat = DateFormat .yMMMd ().add_Hms ();
2012
2011
static final _fullFormat = DateFormat .yMMMd ().add_jm ();
@@ -2023,6 +2022,9 @@ enum MessageTimestampStyle {
2023
2022
2024
2023
switch (this ) {
2025
2024
case none: return null ;
2025
+ case dateOnlyRelative:
2026
+ return _formatDateOnlyRelative (asDateTime,
2027
+ now: now, zulipLocalizations: zulipLocalizations);
2026
2028
case timeOnly: return _timeOnlyFormat.format (asDateTime);
2027
2029
case lightbox: return _lightboxFormat.format (asDateTime);
2028
2030
case full: return _fullFormat.format (asDateTime);
0 commit comments