Last active
April 23, 2024 05:47
-
-
Save lukas-h/6e9be42689fd1b8c008432ff33d8836a to your computer and use it in GitHub Desktop.
Dart DateTime DateFormat to RFC-822 String
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
import 'package:intl/intl.dart'; | |
import 'package:test/test.dart'; | |
formatRfc822(DateTime d) { | |
var template = "EEE, dd MMM yyyy HH:mm:ss"; | |
var out = DateFormat(template).format(d); | |
if (d.isUtc) { | |
out += ' GMT'; | |
} else { | |
var offset = d.toLocal().timeZoneOffset.inHours * 100; | |
out += ' +${offset < 1000 ? '0' : ''}$offset'; | |
} | |
return out; | |
} | |
main(){ | |
test('rfc 822', () { | |
var d = DateTime(2020, 4, 21, 20); | |
expect(formatRfc822(d.toUtc()), 'Tue, 21 Apr 2020 18:00:00 GMT'); | |
expect(formatRfc822(d.toLocal()), 'Tue, 21 Apr 2020 20:00:00 +0200'); | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment