Skip to content

Commit

Permalink
feat: sample article (#54)
Browse files Browse the repository at this point in the history
* chore: cleaning old posts

* chore: adding scrap folder to gitignore

* feat: article

---------

Co-authored-by: Andrey <[email protected]>
  • Loading branch information
codenomnom and codenomnom authored Oct 12, 2024
1 parent 5d47ffa commit d180a86
Show file tree
Hide file tree
Showing 11 changed files with 56 additions and 129 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,5 @@ pnpm-debug.log*

# jetbrains setting folder
.idea/

scrap/
3 changes: 1 addition & 2 deletions src/content/quirks/01-image-bottom-margin.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
---
title: Image <code>bottom</code> margin
# slug: image-bottom-margin
date: 2023-09-18
tags: [html, js]
tags: [html]
---

Okay, this one got me good. It's one of the things I'm constantly amazed by, primarily because I often use libraries and CSS normalize/reset.
Expand Down
7 changes: 0 additions & 7 deletions src/content/quirks/02-collapsing-margins.md

This file was deleted.

7 changes: 0 additions & 7 deletions src/content/quirks/03-lobotomized-owl-selector.md

This file was deleted.

53 changes: 53 additions & 0 deletions src/content/quirks/03-timezoned-unix-timestamps.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
---
title: Timezoned unix timestamps
date: 2024-10-11
tags: [js, date]
---

```ts
// TL;DR
const time = Math.floor(now.getTime() / 1000) + now.getTimezoneOffset() * 60;
```

---

A JavaScript's [Date](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date) love/hate letter.

**Use case**: I received a date from an external source (backend) in a UNIX timestamp format
(number of seconds since January 1st, 1970, shortly named `UTC`).
I wanted to know if that timestamp is in the future or in the past
(so that I could validate a [JWT](https://jwt.io) token).

But the server's time was in different timezone than mine. Which caused issues even though
I was using [epoch](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date#the_epoch_timestamps_and_invalid_date) time:

```ts
const date: number = payload.date; // server's date
console.log(date); // 172863765
console.log(Date.now()); // 1728648454925
```

---

Long story short - regardless of what you do with your current date, you will still get UTC time **including**
your timezone difference. Making both timestamps work withouth a timezone (GMT) required some manipulation:

```ts
const now = new Date();

// time elapsed since January 1st, 1970 in MILLISECONDS
const localTimeSinceEpoch = now.getTime(); // 1728648454925

// timezone difference IN MINUTES 🤯
const timezoneOffset = now.getTimezoneOffset(); // -180

// let's talk the same language - SECONDS
const timezonedTimestamp = Math.floor(localTimeSinceEpoch / 1000);
const offsetInSeconds = timezoneOffset * 60;
const timeSinceGMTEpoch = timezonedTimestamp + offsetInSeconds;

// everything's in seconds, let's compare
if (tokenExpiry < timeSinceGMTEpoch) {
// not yet expired!
}
```
6 changes: 0 additions & 6 deletions src/content/quirks/fifthpost.md

This file was deleted.

27 changes: 0 additions & 27 deletions src/content/quirks/firstpost.md

This file was deleted.

8 changes: 0 additions & 8 deletions src/content/quirks/line-height-causes-margin-collapse.md

This file was deleted.

16 changes: 0 additions & 16 deletions src/content/quirks/secondpost.md

This file was deleted.

7 changes: 0 additions & 7 deletions src/content/quirks/the-sass-ampersand.md

This file was deleted.

49 changes: 0 additions & 49 deletions src/content/quirks/thirdpost.md

This file was deleted.

0 comments on commit d180a86

Please sign in to comment.