Files
GitJournal/lib/utils/datetime.dart
Vishesh Handa 14e7f344d1 Add an explicit serializer from Note -> NoteData
This is so that we can start configuring what the yaml metadata keys
should be called, and not have them hardcoded.

This also allows the note.created to be null. Earlier we were always
setting it to some value. That's doesn't seem right.
2020-01-01 19:29:57 +01:00

61 lines
1.3 KiB
Dart

import 'dart:core';
import 'package:intl/intl.dart';
String toIso8601(DateTime dt) {
return DateFormat("yyyy-MM-ddTHH:mm:ss").format(dt);
}
String toIso8601WithTimezone(DateTime dt, [Duration offset]) {
var result = DateFormat("yyyy-MM-ddTHH:mm:ss").format(dt);
offset = offset ?? dt.timeZoneOffset;
int minutes = (offset.inMinutes % 60);
int hours = offset.inHours.toInt();
String sign = '+';
if (hours < 0) {
hours = hours < 0 ? hours * -1 : hours;
minutes = minutes < 0 ? minutes * -1 : minutes;
sign = '-';
}
String hourStr;
if (hours < 10) {
hourStr = '0' + hours.toString();
} else {
hourStr = hours.toString();
}
String minutesStr;
if (minutes < 10) {
minutesStr = '0' + minutes.toString();
} else {
minutesStr = minutes.toString();
}
return result + sign + hourStr + ':' + minutesStr;
}
DateTime parseDateTime(String str) {
if (str == null) return null;
DateTime dt;
try {
dt = DateTime.parse(str).toLocal();
} catch (ex) {
// Ignore it
}
if (dt == null) {
var regex = RegExp(
r"(\d{4})-(\d{2})-(\d{2})T(\d{2})\:(\d{2})\:(\d{2})\+(\d{2})\:(\d{2})");
if (regex.hasMatch(str)) {
// FIXME: Handle the timezone!
str = str.substring(0, 19);
dt = DateTime.parse(str);
}
}
return dt;
}