Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Allow a timezone configuration for partial date handling

See original GitHub issue

Currently, if I set a field to the date type and enter a partial date, e.g., 2021-10-21 I’m given a date object initialized with a value in UTC, like this 2021-10-21T00:00:00.000Z. Ideally I’d be able to have that partial date interpreted correctly for my timezone — I’m in America/New_York so I’d like to see 2021-10-21T04:00:00.000Z.

A possible, straightforward solution would be the date-fns-tz libraries zonedTimeToUtc function and an optional IANA timezone in the contentlayer config. E.g.

zonedTimeToUtc(doc.createdAt, opts.timezone)

If you’re interested in a contribution I could work on a PR.

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Reactions:1
  • Comments:6 (2 by maintainers)

github_iconTop GitHub Comments

mshickcommented, Nov 26, 2021

Apologies for the long delay in testing this, but I was trying out the feature today and noticed a couple things:

Minor issue

The makeSource fn is being exported without the correct types for this feature. It looks like the PartialArgs type in core was updated, but makeSource is exported with a custom Args type passed to the generic.

Larger issue

The utility of this option doesn’t work as I would have imagined. In my custom solution for this issue I treated my field publishedAt as a string, and passed that string to zonedTimeToUtc. This has the result of interpreting the string value in the specific timezone, and then doing the timezone offset. So, the partial date string is converted from 2020-10-20 to 2021-10-20T04:00:00.000Z.

The option not working as expected is due to this line:

If that date object were created only if we don’t need to use zonedTimeToUtc then things should work correctly assuming I specify the frontmatter field in my markdown as an explicit string.

  publishedAt: '2020-10-20'

If I add the field without the quotes though, I think gray-matter is turning it into a Date object before you touch it. I’m not sure if it’s even an option, but since you apply typing explicitly in contentlayer it might make sense to have gray-matter treat the field as a string no matter what, so that you can control when / how it is turned into a Date, and can offer a consistent user experience for the new timezone option — meaning, they can get the TZ applied whether they specify the partial date as a quoted string or an unquoted date-string.

schicklingcommented, Aug 5, 2022

This should be addressed with the 0.2.7 release. 🎉

Read more comments on GitHub >

github_iconTop Results From Across the Web

Working with Time Zones - W3C
Date and time values based on incremental time are time-zone-independent, since at any given moment it is the same time in UTC everywhere:...
Read more >
How to format DateComponents as a partial date string?
I've tried DateComponentsFormatter but none of the properties get anywhere near what I'd like to see. let components = DateComponents(calendar: ...
Read more >
Settings — DateParser 1.1.2 documentation - Read the Docs
RELATIVE_BASE : allows setting the base datetime to use for interpreting partial or relative date strings. Defaults to the current date and time....
Read more >
Partial Date |
This module provides date and time fields that allows any individual component to be left blank, while still providing a sortable listing of...
Read more >
Dates and times | Google developer documentation style guide
Partial dates and abbreviations; Dates in the middle of a sentence ... If a time zone is necessary, use the timestamp format as...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Post

No results found

github_iconTop Related Hashnode Post

No results found