fixes imported post times

changes the client side to round the unix time to avoid floats

alters the time to match the client time zone on the server side
This commit is contained in:
Rob Loranger 2020-01-14 10:44:56 -08:00
parent 75e2b60328
commit 0766e6cb36
No known key found for this signature in database
GPG key ID: D6F1633A4F0903B8
2 changed files with 12 additions and 1 deletions

View file

@ -9,6 +9,7 @@ import (
"net/http"
"os"
"path/filepath"
"strconv"
"strings"
"time"
@ -85,6 +86,7 @@ func handleImport(app *App, u *User, w http.ResponseWriter, r *http.Request) err
log.Error("invalid form data for file dates: %v", err)
return impart.HTTPError{http.StatusBadRequest, "form data for file dates was invalid"}
}
fileTZ := r.FormValue("tz")
files := r.MultipartForm.File["files"]
var fileErrs []error
filesSubmitted := len(files)
@ -147,6 +149,12 @@ func handleImport(app *App, u *User, w http.ResponseWriter, r *http.Request) err
post.Collection = collAlias
}
dateTime := time.Unix(fileDates[formFile.Filename], 0)
offset, err := strconv.Atoi(fileTZ)
if err != nil {
log.Error("form time zone offset not a valid integer: %v", err)
continue
}
dateTime = dateTime.Add(time.Minute * time.Duration(offset))
post.Created = &dateTime
created := post.Created.Format("2006-01-02T15:04:05Z")
submittedPost := SubmittedPost{

View file

@ -32,6 +32,7 @@
<input id="fileInput" class="fileInput" name="files" type="file" multiple accept="text/markdown, text/plain"/>
</label>
<input id="fileDates" name="fileDates" hidden/>
<input id="tzInput" name="tz" hidden/>
<label>
Import these posts to:
<select name="collection">
@ -42,13 +43,15 @@
</select>
</label>
<script>
const tzInput = document.getElementById('tzInput');
tzInput.value = new Date().getTimezoneOffset();
const fileInput = document.getElementById('fileInput');
const fileDates = document.getElementById('fileDates');
fileInput.addEventListener('change', (e) => {
const files = e.target.files;
let dateMap = {};
for (let file of files) {
dateMap[file.name] = file.lastModified / 1000;
dateMap[file.name] = Math.round(file.lastModified / 1000);
}
fileDates.value = JSON.stringify(dateMap);
})