lumeland / lume

🔥 Static site generator for Deno 🦕
https://lume.land
MIT License
1.79k stars 77 forks source link

Date format does not behave as expected (off by one day) #58

Closed kevinlang closed 3 years ago

kevinlang commented 3 years ago

I have a template with the date specified as e.g., 2021-04-08.

I would expect this formatting:

post.data.date | date('PPP')

To return:

April 8th, 2021

However, it returns:

April 7th, 2021

If I format with the ATOM format, I can see that the issue seems to be related to stripping off the timezone, or something.

post.data.date | date(`ATOM')
=> "2021-04-07T20:00:00-04:00"

(I'm in GMT-4)

oscarotero commented 3 years ago

Mmm, interesting. Maybe we have to add an option to define a default timezone, or something like that.

kevinlang commented 3 years ago

https://github.com/date-fns/date-fns/issues/489#issuecomment-570635228

Yeah, looks like this is a known issue. There are some workarounds in that thread, I linked to one above.

oscarotero commented 3 years ago

Intead in the plugin, I've fixed when the date is detected: https://github.com/lumeland/lume/commit/be274c7ecc7877a18dcc2a8158009168a9f99968

It's not published yet, but could you test it and confirm if it works fine to you?

kevinlang commented 3 years ago

Unfortunately it does not seem to have fixed the issue. It is still off by a day.

Here is how I ran the latest changes, in case that is somehow the issue (I'm not too familiar with deno):

  1. Cloned this repo to an adjacent folder
  2. Ensured that the commit above was present in the clone
  3. From the adjacent folder that had my site, I ran deno run --unstable -A ../lume/cli.js --serv
oscarotero commented 3 years ago

The way to run the latest changes is editing your _config.js file to import the last version of lume. You don't need to clone the repo, just import directly using the raw github url:

import lume from "https://raw.githubusercontent.com/lumeland/lume/be274c7ecc7877a18dcc2a8158009168a9f99968/mod.js";

// Here the rest of your code

You can continue using lume --serve (or lume -s) command because the cli has not changed.

kevinlang commented 3 years ago

Got it. I did that just now and the issue looks fixed. Thanks again for your assistance! :smile:

oscarotero commented 3 years ago

Great. You can undo this change in your _config.js and use the latest stable version after releasing this.

valtlai commented 3 years ago

Mmm, interesting. Maybe we have to add an option to define a default timezone, or something like that.

The following seems to work:

Deno.env.set("TZ", "Europe/Helsinki");

We could add an option to abstract that:

const site = lume({
  timeZone: "Europe/Helsinki",
});

There should also be a note in the docs to not use a time zone offset in date values. The following won’t work correctly after the commit be274c7ecc7877a18dcc2a8158009168a9f99968:

---
date: 2021-04-04 03:02:01 +03:00
templateEngine: njk,md
---

{{ date }}

On my machine (where the time zone is Europe/Helsinki and offset UTC+03:00 in DST), it outputs:

Sun Apr 04 2021 00:02:01 GMT+0300 (Eastern European Summer Time)

But without +03:00 in the date field, it works correctly. Before the commit, it behaved on the contrary: correctly with a time zone offset and incorrectly without it.

That’s because the time zone offset defaults to UTC (Z = +00:00).

oscarotero commented 3 years ago

Thanks @valtlai You're absolutelly right, my commit was only a hack for this specific case, but it doesn't solve the problem.

I didn't know about TZ environment variable, and it looks a better solution because it will be applied to all Date instances created, not only to the date value.

oscarotero commented 3 years ago

Looks like Deno.env.set("TZ", "Z"); does the job.

oscarotero commented 3 years ago

@kevinlang Finally I've reverted this change because it's not the best way to solve this problem.

For now, you can add Deno.env.set("TZ", "Z") to your _config.js file.

CM-IV commented 1 year ago

This is an old issue, but I'm basically having the same problem. The date is behind by 1 day for me even after I set the TZ inside of the _config.ts file (America/Chicago).

image image

In order to offset this somewhat, I change my TZ to Australia/Canberra and I then get the correct date.

image image

oscarotero commented 1 year ago

@CM-IV If it helps, I'm using environment variables in the Lume tests to avoid different results in my computer and in GitHub actions: https://github.com/lumeland/lume/blob/master/deno.json#L3 I think it's safer to provide the environment in the cli command.