Skip to content

Instantly share code, notes, and snippets.

@lukas-h
Last active April 23, 2024 05:47
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lukas-h/6e9be42689fd1b8c008432ff33d8836a to your computer and use it in GitHub Desktop.
Save lukas-h/6e9be42689fd1b8c008432ff33d8836a to your computer and use it in GitHub Desktop.
Dart DateTime DateFormat to RFC-822 String
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