mirror of https://github.com/tasks/tasks
Use timestamp from backup filename
parent
ee46c74994
commit
5ed4d89e43
@ -1,47 +0,0 @@
|
||||
/*
|
||||
* Copyright (c) 2012 Todoroo Inc
|
||||
*
|
||||
* See the file "LICENSE" for the full license governing this code.
|
||||
*/
|
||||
|
||||
package com.todoroo.astrid.backup;
|
||||
|
||||
/**
|
||||
* Constants for backup XML attributes and nodes.
|
||||
*
|
||||
* @author Tim Su <tim@todoroo.com>
|
||||
*/
|
||||
public class BackupConstants {
|
||||
|
||||
// Do NOT edit the constants in this file! You will break compatibility with old backups
|
||||
|
||||
// --- general xml
|
||||
|
||||
/** Tag containing Astrid backup data */
|
||||
public static final String ASTRID_TAG = "astrid";
|
||||
|
||||
/** Attribute indicating backup file format */
|
||||
public static final String ASTRID_ATTR_FORMAT = "format";
|
||||
|
||||
// --- format 2
|
||||
|
||||
/** Tag containing a task */
|
||||
public static final String TASK_TAG = "task";
|
||||
|
||||
/** Tag containing a comment item */
|
||||
public static final String COMMENT_TAG = "comment";
|
||||
|
||||
/** Tag containing a metadata item */
|
||||
public static final String METADATA_TAG = "metadata";
|
||||
|
||||
/** Tag containing a tagdata item */
|
||||
public static final String TAGDATA_TAG = "tagdata";
|
||||
|
||||
// --- general
|
||||
|
||||
public static final String INTERNAL_BACKUP = "backup.json";
|
||||
|
||||
public static final String EXPORT_FILE_NAME = "user.%s.json";
|
||||
|
||||
public static final String BACKUP_FILE_NAME = "auto.%s.json";
|
||||
}
|
@ -0,0 +1,69 @@
|
||||
/*
|
||||
* Copyright (c) 2012 Todoroo Inc
|
||||
*
|
||||
* See the file "LICENSE" for the full license governing this code.
|
||||
*/
|
||||
package com.todoroo.astrid.backup
|
||||
|
||||
import com.google.api.services.drive.model.File
|
||||
import org.tasks.time.DateTime
|
||||
import java.util.regex.Pattern
|
||||
|
||||
/**
|
||||
* Constants for backup XML attributes and nodes.
|
||||
*
|
||||
* @author Tim Su <tim></tim>@todoroo.com>
|
||||
*/
|
||||
object BackupConstants {
|
||||
// Do NOT edit the constants in this file! You will break compatibility with old backups
|
||||
// --- general xml
|
||||
/** Tag containing Astrid backup data */
|
||||
const val ASTRID_TAG = "astrid"
|
||||
|
||||
/** Attribute indicating backup file format */
|
||||
const val ASTRID_ATTR_FORMAT = "format"
|
||||
// --- format 2
|
||||
/** Tag containing a task */
|
||||
const val TASK_TAG = "task"
|
||||
|
||||
/** Tag containing a comment item */
|
||||
const val COMMENT_TAG = "comment"
|
||||
|
||||
/** Tag containing a metadata item */
|
||||
const val METADATA_TAG = "metadata"
|
||||
|
||||
/** Tag containing a tagdata item */
|
||||
const val TAGDATA_TAG = "tagdata"
|
||||
|
||||
// --- general
|
||||
const val INTERNAL_BACKUP = "backup.json"
|
||||
const val EXPORT_FILE_NAME = "user.%s.json"
|
||||
const val BACKUP_FILE_NAME = "auto.%s.json"
|
||||
|
||||
private val MATCHER = Pattern.compile("(auto|user)\\.(\\d{2})(\\d{2})(\\d{2})-(\\d{2})(\\d{2})\\.json")
|
||||
|
||||
fun isBackupFile(name: String) = MATCHER.matcher(name).matches()
|
||||
|
||||
fun getTimestamp(file: java.io.File): Long {
|
||||
return getTimestampFromFilename(file.name) ?: file.lastModified()
|
||||
}
|
||||
|
||||
fun getTimestamp(file: File): Long {
|
||||
return getTimestampFromFilename(file.name) ?: file.modifiedTime.value
|
||||
}
|
||||
|
||||
private fun getTimestampFromFilename(name: String): Long? {
|
||||
return MATCHER
|
||||
.matcher(name)
|
||||
.takeIf { it.matches() }
|
||||
?.let {
|
||||
DateTime(
|
||||
2000 + it.group(2)!!.toInt(),
|
||||
it.group(3)!!.toInt(),
|
||||
it.group(4)!!.toInt(),
|
||||
it.group(5)!!.toInt(),
|
||||
it.group(6)!!.toInt())
|
||||
.millis
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,38 @@
|
||||
package com.todoroo.astrid.backup
|
||||
|
||||
import org.junit.Assert.*
|
||||
import org.junit.Test
|
||||
import org.tasks.time.DateTime
|
||||
|
||||
class BackupConstantsTest {
|
||||
@Test
|
||||
fun autoBackupMatchesFilename() {
|
||||
assertTrue(BackupConstants.isBackupFile("auto.200909-0003.json"))
|
||||
}
|
||||
|
||||
@Test
|
||||
fun userBackupMatchesFilename() {
|
||||
assertTrue(BackupConstants.isBackupFile("user.200909-1503.json"))
|
||||
}
|
||||
|
||||
@Test
|
||||
fun ignoreCopiedFile() {
|
||||
assertFalse(BackupConstants.isBackupFile("user.200909-1503 (1).json"))
|
||||
}
|
||||
|
||||
@Test
|
||||
fun getTimestampFromAutoBackup() {
|
||||
assertEquals(
|
||||
DateTime(2020, 9, 10, 15, 3).millis,
|
||||
BackupConstants.getTimestampFromFilename("auto.200910-1503.json")
|
||||
)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun getTimestampFromUserBackup() {
|
||||
assertEquals(
|
||||
DateTime(2020, 9, 10, 15, 3).millis,
|
||||
BackupConstants.getTimestampFromFilename("user.200910-1503.json")
|
||||
)
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue