Skip to content

Commit 3b6061c

Browse files
Fix timestamp parsing with momentjs
1 parent 17c3ff1 commit 3b6061c

File tree

6 files changed

+53
-23
lines changed

6 files changed

+53
-23
lines changed

bun.lockb

333 Bytes
Binary file not shown.

cynthia_websites_mini_server/test/cynthia_websites_mini_server_test.gleam

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import gleam/io
2+
import gleam/string
13
import gleeunit
24
import gleeunit/should
35

@@ -16,9 +18,16 @@ pub fn hello_world_test() {
1618
}
1719

1820
// Test timestamp conversion
19-
pub fn timestamp_to_test() {
20-
let time = "2025-01-22T12:12:07+0000"
21+
pub fn timestamp_to_timestamp_test() {
22+
let time = "2025-01-22T12:12:07.001+01:00"
2123
let parsed = timestamps.parse(time)
2224
timestamps.create(parsed)
2325
|> should.equal(time)
2426
}
27+
28+
// Test timestamp conversion
29+
pub fn string_to_timestamp_test() {
30+
timestamps.parse("2025-01-24 20:51:03 GMT+0100")
31+
|> timestamps.create()
32+
|> should.equal("2025-01-24T20:51:03.000+01:00")
33+
}
Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,27 @@
1-
import plinth/javascript/date
1+
/// We use the moment.js library to parse and create timestamps.
2+
/// This is because the JavaScript `Date` object is not capable of parsing timestamps in the ISO 8601 format reliably enough.
3+
/// This means we also work with `Moment` objects!
4+
pub type Moment
25

36
/// Converts a timestamp from the ISO 8601 date format (EG: 2025-01-22T12:12:07+0000)
47
/// To a JavaScript 'date' object, accessible through Plinth.
5-
@external(javascript, "./timestamps_ffi.mjs", "parse")
6-
pub fn parse(timestamp: String) -> date.Date
8+
@external(javascript, "./timestamps_ffi.ts", "parse")
9+
pub fn parse(timestamp: String) -> Moment
710

811
/// Converts a JavaScript 'date' object, accessible through Plinth
912
/// To a timestamp in the ISO 8601 date format (EG: 2025-01-22T12:12:07+0000)
1013
/// This is the inverse of the `parse` function.
11-
pub fn create(date_object: date.Date) -> String {
12-
date.to_iso_string(date_object)
13-
}
14+
@external(javascript, "./timestamps_ffi.ts", "create")
15+
pub fn create(date_object: Moment) -> String
16+
17+
/// Convert a moment object to an epoch timestamp in minutes.
18+
@external(javascript, "./timestamps_ffi.ts", "to_minutes_since_epoch")
19+
pub fn to_minutes_since_epoch(moment: Moment) -> Int
20+
21+
/// Convert minutes since epoch to a moment object.
22+
@external(javascript, "./timestamps_ffi.ts", "from_minutes_since_epoch")
23+
pub fn from_minutes_since_epoch(minutes: Int) -> Moment
24+
25+
/// Get the current time as a moment object.
26+
@external(javascript, "./timestamps_ffi.ts", "rn")
27+
pub fn rn() -> Moment

cynthia_websites_mini_shared/src/cynthia_websites_mini_shared/timestamps_ffi.mjs

Lines changed: 0 additions & 15 deletions
This file was deleted.
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import moment, { type Moment } from "moment";
2+
3+
export function parse(datestr: string) {
4+
return moment(datestr, false);
5+
}
6+
7+
export function create(timestamp: Moment) {
8+
return timestamp.toISOString(true);
9+
}
10+
11+
export function to_minutes_since_epoch(timestamp: Moment): number {
12+
return timestamp.unix() / 60;
13+
}
14+
15+
export function from_minutes_since_epoch(minutes: number): Moment {
16+
return moment.unix(minutes * 60);
17+
}
18+
19+
export function rn(): Moment {
20+
return moment();
21+
}

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
"dependencies": {
2626
"autoprefixer": "^10.4.20",
2727
"daisyui": "^4.12.23",
28+
"moment": "^2.30.1",
2829
"postcss": "^8.5.1",
2930
"smol-toml": "^1.3.1",
3031
"sql.js": "^1.12.0",

0 commit comments

Comments
 (0)