mirror of
https://github.com/GitJournal/GitJournal.git
synced 2025-06-24 15:53:45 +08:00
Update modified/created as unix timestamp if already in that format
Related to #349
This commit is contained in:
@ -33,6 +33,11 @@ abstract class NoteSerializerInterface {
|
|||||||
|
|
||||||
var emojiParser = EmojiParser();
|
var emojiParser = EmojiParser();
|
||||||
|
|
||||||
|
enum DateFormat {
|
||||||
|
Iso8601,
|
||||||
|
UnixTimeStamp,
|
||||||
|
}
|
||||||
|
|
||||||
class NoteSerializationSettings {
|
class NoteSerializationSettings {
|
||||||
String modifiedKey = "modified";
|
String modifiedKey = "modified";
|
||||||
String createdKey = "created";
|
String createdKey = "created";
|
||||||
@ -45,11 +50,16 @@ class NoteSerializationSettings {
|
|||||||
|
|
||||||
SettingsTitle titleSettings = SettingsTitle.Default;
|
SettingsTitle titleSettings = SettingsTitle.Default;
|
||||||
|
|
||||||
|
var modifiedFormat = DateFormat.Iso8601;
|
||||||
|
var createdFormat = DateFormat.Iso8601;
|
||||||
|
|
||||||
NoteSerializationSettings.fromConfig(NotesFolderConfig config) {
|
NoteSerializationSettings.fromConfig(NotesFolderConfig config) {
|
||||||
modifiedKey = config.yamlModifiedKey;
|
modifiedKey = config.yamlModifiedKey;
|
||||||
createdKey = config.yamlCreatedKey;
|
createdKey = config.yamlCreatedKey;
|
||||||
tagsKey = config.yamlTagsKey;
|
tagsKey = config.yamlTagsKey;
|
||||||
titleSettings = config.titleSettings;
|
titleSettings = config.titleSettings;
|
||||||
|
|
||||||
|
// FIXME: modified / created format!
|
||||||
}
|
}
|
||||||
NoteSerializationSettings();
|
NoteSerializationSettings();
|
||||||
}
|
}
|
||||||
@ -65,13 +75,27 @@ class NoteSerializer implements NoteSerializerInterface {
|
|||||||
data.body = emojiParser.unemojify(note.body);
|
data.body = emojiParser.unemojify(note.body);
|
||||||
|
|
||||||
if (note.created != null) {
|
if (note.created != null) {
|
||||||
data.props[settings.createdKey] = toIso8601WithTimezone(note.created!);
|
switch (settings.createdFormat) {
|
||||||
|
case DateFormat.Iso8601:
|
||||||
|
data.props[settings.createdKey] =
|
||||||
|
toIso8601WithTimezone(note.created!);
|
||||||
|
break;
|
||||||
|
case DateFormat.UnixTimeStamp:
|
||||||
|
data.props[settings.createdKey] = toUnixTimeStamp(note.created!);
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
data.props.remove(settings.createdKey);
|
data.props.remove(settings.createdKey);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (note.modified != null) {
|
if (note.modified != null) {
|
||||||
data.props[settings.modifiedKey] = toIso8601WithTimezone(note.modified!);
|
switch (settings.modifiedFormat) {
|
||||||
|
case DateFormat.Iso8601:
|
||||||
|
data.props[settings.modifiedKey] =
|
||||||
|
toIso8601WithTimezone(note.modified!);
|
||||||
|
break;
|
||||||
|
case DateFormat.UnixTimeStamp:
|
||||||
|
data.props[settings.modifiedKey] = toUnixTimeStamp(note.modified!);
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
data.props.remove(settings.modifiedKey);
|
data.props.remove(settings.modifiedKey);
|
||||||
}
|
}
|
||||||
@ -135,7 +159,13 @@ class NoteSerializer implements NoteSerializerInterface {
|
|||||||
for (var possibleKey in modifiedKeyOptions) {
|
for (var possibleKey in modifiedKeyOptions) {
|
||||||
var val = data.props[possibleKey];
|
var val = data.props[possibleKey];
|
||||||
if (val != null) {
|
if (val != null) {
|
||||||
note.modified = parseDateTime(val.toString());
|
if (val is int) {
|
||||||
|
note.modified = parseUnixTimeStamp(val);
|
||||||
|
settings.modifiedFormat = DateFormat.UnixTimeStamp;
|
||||||
|
} else {
|
||||||
|
note.modified = parseDateTime(val.toString());
|
||||||
|
settings.modifiedFormat = DateFormat.Iso8601;
|
||||||
|
}
|
||||||
settings.modifiedKey = possibleKey;
|
settings.modifiedKey = possibleKey;
|
||||||
|
|
||||||
propsUsed.add(possibleKey);
|
propsUsed.add(possibleKey);
|
||||||
@ -152,7 +182,13 @@ class NoteSerializer implements NoteSerializerInterface {
|
|||||||
for (var possibleKey in createdKeyOptions) {
|
for (var possibleKey in createdKeyOptions) {
|
||||||
var val = data.props[possibleKey];
|
var val = data.props[possibleKey];
|
||||||
if (val != null) {
|
if (val != null) {
|
||||||
note.created = parseDateTime(val.toString());
|
if (val is int) {
|
||||||
|
note.created = parseUnixTimeStamp(val);
|
||||||
|
settings.createdFormat = DateFormat.UnixTimeStamp;
|
||||||
|
} else {
|
||||||
|
note.created = parseDateTime(val.toString());
|
||||||
|
settings.createdFormat = DateFormat.Iso8601;
|
||||||
|
}
|
||||||
settings.createdKey = possibleKey;
|
settings.createdKey = possibleKey;
|
||||||
|
|
||||||
propsUsed.add(possibleKey);
|
propsUsed.add(possibleKey);
|
||||||
|
@ -80,3 +80,11 @@ DateTime? parseDateTime(String str) {
|
|||||||
|
|
||||||
return dt;
|
return dt;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
DateTime parseUnixTimeStamp(int val) {
|
||||||
|
return DateTime.fromMillisecondsSinceEpoch(val * 1000, isUtc: true);
|
||||||
|
}
|
||||||
|
|
||||||
|
int toUnixTimeStamp(DateTime dt) {
|
||||||
|
return dt.toUtc().millisecondsSinceEpoch ~/ 1000;
|
||||||
|
}
|
||||||
|
@ -253,12 +253,12 @@ Hello
|
|||||||
expect(note.created, DateTime.parse('2021-07-14T10:14:49Z'));
|
expect(note.created, DateTime.parse('2021-07-14T10:14:49Z'));
|
||||||
|
|
||||||
note.modified = DateTime.parse('2020-07-14T10:14:49Z');
|
note.modified = DateTime.parse('2020-07-14T10:14:49Z');
|
||||||
note.created = DateTime.parse('2020-06-14T10:14:49Z');
|
note.created = DateTime.parse('2020-06-13T10:14:49Z');
|
||||||
|
|
||||||
var expectedContent = """---
|
var expectedContent = """---
|
||||||
bar: Foo
|
bar: Foo
|
||||||
updated: 1626257689
|
updated: 1594721689
|
||||||
created: 1626257689
|
created: 1592043289
|
||||||
---
|
---
|
||||||
|
|
||||||
Hello
|
Hello
|
||||||
@ -268,6 +268,6 @@ Hello
|
|||||||
|
|
||||||
var actualContent = File(notePath).readAsStringSync();
|
var actualContent = File(notePath).readAsStringSync();
|
||||||
expect(actualContent, equals(expectedContent));
|
expect(actualContent, equals(expectedContent));
|
||||||
}, skip: true);
|
});
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user