Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/brave-sloths-smoke.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'orga': patch
---

Fixed a bug with parsing org days. Added `active` on `planning` to indicate the activeness of a given timestamp.
126 changes: 57 additions & 69 deletions packages/orga/src/timestamp.ts
Original file line number Diff line number Diff line change
@@ -1,70 +1,58 @@
import { zonedTimeToUtc } from 'date-fns-tz'
import { read } from 'text-kit'
import { Timestamp } from './types'

export const parse = (
input: string,
{ timezone = Intl.DateTimeFormat().resolvedOptions().timeZone } = {}
): Timestamp | undefined => {
const { match, eat, getChar, jump } = read(input)

eat('whitespaces')
const timestamp = () => {
// opening
const opening = eat(/[<[]/g)
if (!opening) return
const active = opening.value === '<'

// date
const { value: _date } = eat(/\d{4}-\d{2}-\d{2}/)
let date = _date

eat('whitespaces')

let end: string | undefined

// day
const { value: _day } = eat(/[a-zA-Z]+/)
eat('whitespaces')

// time
const time = match(/(\d{2}:\d{2})(?:-(\d{2}:\d{2}))?/)
if (time) {
date = `${_date} ${time.result[1]}`
if (time.result[2]) {
end = `${_date} ${time.result[2]}`
}
jump(time.position.end)
import { zonedTimeToUtc } from 'date-fns-tz';
import { read } from 'text-kit';
export const parse = (input, { timezone = Intl.DateTimeFormat().resolvedOptions().timeZone } = {}) => {
const { match, eat, getChar, jump } = read(input);
eat('whitespaces');
const timestamp = () => {
// opening
const opening = eat(/[<[]/g);
if (!opening)
return;
const active = opening.value === '<';
// date
const { value: _date } = eat(/\d{4}-\d{2}-\d{2}/);
let date = _date;
eat('whitespaces');
let end;

let _day;
if (/[A-Z]/.test(getChar())) {
const { value: _day } = eat(/[a-zA-Z]+/) ;
}

eat('whitespaces');
const time = match(/(\d{2}:\d{2})(?:-(\d{2}:\d{2}))?/);
if (time) {
date = `${_date} ${time.result[1]}`;
if (time.result[2]) {
end = `${_date} ${time.result[2]}`;
}
jump(time.position.end);
}
// closing
const closing = getChar();
if ((opening.value === '[' && closing === ']') ||
(opening.value === '<' && closing === '>')) {
eat('char');
return {
date: zonedTimeToUtc(date, timezone),
end: end ? zonedTimeToUtc(end, timezone) : undefined,
active: active
};
}
// opening closing does not match
};
const ts = timestamp();
if (!ts)
return;
if (!ts.end) {
const doubleDash = eat(/--/);
if (doubleDash) {
const end = timestamp();
if (end) {
ts.end = end.date;
}
}
}

// closing
const closing = getChar()
if (
(opening.value === '[' && closing === ']') ||
(opening.value === '<' && closing === '>')
) {
eat('char')
return {
date: zonedTimeToUtc(date, timezone),
end: end ? zonedTimeToUtc(end, timezone) : undefined,
}
}

// opening closing does not match
}

const ts = timestamp()
if (!ts) return

if (!ts.end) {
const doubleDash = eat(/--/)
if (doubleDash) {
const end = timestamp()
if (end) {
ts.end = end.date
}
}
}

return ts
}
return ts;
};