Skip to content

Instantly share code, notes, and snippets.

View adamjforster's full-sized avatar

Adam J. Forster adamjforster

View GitHub Profile
@adamjforster
adamjforster / sql_update_with_join.sql
Created July 29, 2010 15:24
Example SQL update statements using a join for PostgreSQL and MySQL.
/* PostgreSQL */
UPDATE a
SET a.b_id = b.id
FROM b
WHERE a.b_name LIKE b.name;
/* MySQL */
UPDATE a, b
SET a.b_id = b.id
WHERE a.b_name LIKE b.name;
@adamjforster
adamjforster / add_working_days.asp
Created October 21, 2010 10:31
Add a number of working days to a given date.
<%
Function add_working_days(start_date, to_add)
For days = 0 To to_add - 1
start_date = DateAdd("d", 1, start_date)
If DatePart("w", start_date) = 1 Then
start_date = DateAdd("d", 1, start_date)
ElseIf DatePart("w", start_date) = 7 Then
start_date = DateAdd("d", 2, start_date)
End If
Next
@adamjforster
adamjforster / subtract_years.py
Created December 13, 2010 15:46
A simple fuction to subtract a number of years from a date.
import calendar
import datetime
def subtract_years(date, years):
"""Subtract a number of years from a given date.
>>> d = datetime.date(year=2012, month=2, day=29)
>>> subtract_years(d, 1)
datetime.date(2011, 2, 28)
>>> subtract_years(d, 4)
@adamjforster
adamjforster / truncate_on.py
Created January 26, 2011 15:07
Truncate a string at the nearest delimiter to a certain length.
def truncate_on(string, length, delimiter=' '):
"""Truncate a string at the nearest delimiter to a certain length."""
length = int(length)
if len(string) > length:
index = string.rfind(delimiter, 0, length)
if index == -1:
string = string[:50]
else:
string = string[:index]
return string
@adamjforster
adamjforster / is_valid_time.js
Created August 1, 2011 11:06
Tests if a string is a valid time for a 24hr clock.
function is_valid_time(value) {
/* Validates that a string is a valid time in either of the following
* formats:
*
* HH:MM
* HH:MM:SS
*/
regex = /^([01]\d|2[0-3])(:[0-5]\d){1,2}$/;
return regex.test(value);
}
@adamjforster
adamjforster / slugify.js
Created March 6, 2012 18:16
A function to slugify a string in JavaScript.
function slugify(value) {
/**
* Convert to lowercase, remove non-alphanumeric characters and replace
* whitespace with hyphens.
*
* Inspired by Django's slugify function.
*/
value = value.toLowerCase().trim();
value = value.replace(/[^\w\s\-]/g, '');
@adamjforster
adamjforster / job_software_developer_feb_2016.md
Last active February 18, 2016 12:01
Job advert for Software Developer (Feb 2016)

================== Software Developer

Jewellery Quarter Bullion are seeking promising candidates looking for an opportunity to fulfil their potential as great software developers.

Salary: £25k - £35k + bonus (up to 30%)

Key responsibilities

  • Developing and maintaining our bespoke integrated e-commerce, trading, and stock management platform.
  • Configuring, monitoring, and maintaining our server infrastructure.
@adamjforster
adamjforster / job_front_end_developer_designer_feb_2016.md
Last active February 18, 2016 12:04
Job advert Front End Developer / Designer (Feb 2016)

Front End Developer / Designer

Jewellery Quarter Bullion are seeking promising candidates looking for an opportunity to fulfil their potential as great front end developer / designers.

Salary: £22k - £32k + bonus (up to 30%)

Key responsibilities

  • Designing and building responsive front ends for our existing and upcoming e-commerce platforms.
  • Steering the branding and usability of our websites.
  • Conducting user acceptance testing to ensure a positive user experience.
  • Working closely with other team members to produce high-quality products, on time and on budget.
@adamjforster
adamjforster / replication_delay.sql
Created April 25, 2018 14:24
SQL queries to show Postgresql streaming replication delay
/***
* Secondary server commands.
*/
// Show the number of seconds between now and the last replayed transaction.
SELECT EXTRACT(EPOCH FROM (now() - pg_last_xact_replay_timestamp()))::INT;
// Show the number of minutes between now and the last replayed transaction.
SELECT EXTRACT(EPOCH FROM (now() - pg_last_xact_replay_timestamp()))::INT / 60;
// Get last transaction log location received and synced to disk by streaming replication.