Skip to content

Instantly share code, notes, and snippets.

View spatialtime's full-sized avatar
🎨
Focusing

Matt Savage spatialtime

🎨
Focusing
View GitHub Profile
@spatialtime
spatialtime / iso8601_ordinaldate.py
Last active May 12, 2020 15:57
Pythonic formatting and parsing of ISO 8601 ordinal dates.
# This snippet demonstrates Pythonic formatting and parsing of
# ISO 8601 ordinal dates (4-digit year + "-" + ordinal day).
# Note: an ordinal day in this context is the nth day of
# the year, with Jan 1 being ordinal day 1 and Dec 31
# of non-leap year (a "common year") being day 365.
from datetime import datetime
d = datetime.strptime("2020-355", "%Y-%j")
@spatialtime
spatialtime / iso8601_week.py
Last active May 12, 2020 16:01
Pythonic formatting and parsing of ISO 8601 weeks.
# This snippet demonstrates Pythonic formatting and parsing of
# ISO 8601 weeks.
# The first ISO week of the year is the first week containing
# a Thursday.
# ISO weeks begin on Monday, with Monday being day 1.
from datetime import datetime
d = datetime.strptime("2020-W17-7", "%G-W%V-%u")
@spatialtime
spatialtime / iso8601_duration.py
Last active May 14, 2024 18:55
Python and ISO 8601 durations
# This snippet demonstrates Pythonic formatting and parsing of
# ISO 8601 durations.
# A little work is required to navigate between Python's
# timedelta syntax and ISO 8601 duration syntax.
import re
from datetime import datetime
from datetime import timedelta
@spatialtime
spatialtime / iso8601_datetime.py
Created May 12, 2020 16:18
Pythonic formatting and parsing of ISO 8601 dates, times, and time zones.
# This snippet demonstrates Pythonic formatting and parsing of ISO 8601 dates.
from datetime import datetime
# datetime.strptime() is used to parse iso 8601 strings to
# Python datetime.datetime instances.
# datetime.strftime() is used to format Python datetime.datetime instances
# to ISO 8601 strings.
@spatialtime
spatialtime / iso8601_datetime.go
Created May 12, 2020 16:33
Golang formatting and parsing of ISO 8601 dates, times, and time zones.
// This snippet demonstrates Golang formatting and parsing of
// ISO 8601 dates, times, time zones
import(
"fmt"
"time"
)
loc, _ := time.LoadLocation("America/Los_Angeles")
@spatialtime
spatialtime / iso8601_extract_datetimes.js
Created May 12, 2020 22:33
JavaScript code for extracting ISO 8601 dates/time fields from a JavaScript Date object.
// Passing a string to new Date() is cross-implementation-safe only if the
// format of the string conforms to ECMAScript's specified format.
// From the specification (https://tc39.es/ecma262/#sec-date.parse):
// " If the String does not conform to that format the function may fall
// back to any implementation-specific heuristics or implementation-specific date formats. "
// As such, we are good to go here with passing it a correct ISO 8601 date/time string.
let d = new Date("2020-04-02T13:00:00.000Z")
// The beauty of a fixed-width ISO 8601 output string.
@spatialtime
spatialtime / iso8601_parse_datetimes.js
Created May 21, 2020 19:31
JavaScript code for parsing an ISO 8601 string to a JavaScript Date instance.
/*
* dateFromISODatetime creates a JavaScript date from a string that adheres to
* ECMAScript's specified syntax:
* Zulu/UTC time:
* YYYY-MM-DDTHH:mm:ss.sssZ
* Specified time zone offset:
* YYYY-MM-DDTHH:mm:ss.sss±HH:mm
* Local time (no time zone specification):
* YYYY-MM-DDTHH:mm:ss.sss
*/
@spatialtime
spatialtime / iso8601_ordinal_dates.js
Last active May 23, 2020 01:58
JavaScript and ISO 8601 ordinal dates
//------------------------------------------------------------------------------
// Constants
//------------------------------------------------------------------------------
const MIN_YEAR =1;
const MAX_YEAR = 9999;
const MIN_MONTH = 1;
const MAX_MONTH = 12;
const MIN_DAY = 1;
const MIN_HOUR = 0;
const MAX_HOUR = 24;
@spatialtime
spatialtime / iso8601_weeks.js
Created May 21, 2020 19:45
JavaScript code for parsing and formatting ISO 8601 weeks
//------------------------------------------------------------------------------
// Constants
//------------------------------------------------------------------------------
const MIN_YEAR =1;
const MAX_YEAR = 9999;
const MIN_MONTH = 1;
const MAX_MONTH = 12;
const MIN_DAY = 1;
const MIN_HOUR = 0;
const MAX_HOUR = 24;
@spatialtime
spatialtime / iso8601_duration.go
Last active May 12, 2024 02:23
Golang and ISO 8601 durations
// This snippet demonstrates Golang formatting and parsing of ISO 8601 durations.
// A little work is required to navigate between Golang's
// duration syntax and ISO 8601 duration syntax.
import (
"errors"
"fmt"
"regexp"
"strings"
"time"