‘Bankers’ Algorith – SQL Function

CREATE FUNCTION dbo.BankersRound(@data DECIMAL(25,10), @digits INT)
RETURNS DECIMAL(25,10) AS
BEGIN
	SET @data = @data * POWER(10, @digits)
	RETURN
		CASE WHEN @data = FLOOR(@data) THEN @data
		ELSE
			CASE SIGN(CEILING(@data) - 2 * @data + FLOOR(@data))
				WHEN 1 THEN FLOOR(@data)
				WHEN -1 THEN CEILING(@data)
			ELSE 2 * ROUND(@data / 2, 0) END
		END / POWER(10, @digits) 
END
GO

Usage

SELECT 
     dbo.BankersRound(2.1, 2) AS r1
    ,dbo.BankersRound(2.11, 2) AS r2
    ,dbo.BankersRound(2.555, 2) AS r3
    ,dbo.BankersRound(2.565, 2) AS r4
    ,dbo.BankersRound(3.915, 2) AS r5

Backup all MySQL databases

#! /bin/bash

TIMESTAMP=`date +%Y-%m-%d_%H-%M-%S`
BACKUP_DIR="/backup"
BACKUP_TIMESTAMP_DIR="$BACKUP_DIR/$TIMESTAMP"
MYSQL=/usr/bin/mysql
MYSQL_USER="user"
MYSQL_PASSWORD="pass"
MYSQLDUMP=/usr/bin/mysqldump

mkdir -p "$BACKUP_TIMESTAMP_DIR"

echo "Starting MySQL Backup";
echo `date`;

databases=`$MYSQL --user=$MYSQL_USER -p$MYSQL_PASSWORD -e "SHOW DATABASES;" | grep -Ev "(Database|mysql|information_schema|performance_schema)"`

for db in $databases; do
  echo "Dumping database: $db"
  $MYSQLDUMP --force --opt --skip-comments --compact --user=$MYSQL_USER -p$MYSQL_PASSWORD --single-transaction --databases $db | gzip > "$BACKUP_TIMESTAMP_DIR/$db.sql.gz"
done

tar -zcf "$TIMESTAMP.tar.gz" -C "$BACKUP_TIMESTAMP_DIR" .
rm -rf "$BACKUP_TIMESTAMP_DIR"

find "$BACKUP_DIR" -name "*.tar.gz" -mtime 182 -exec rm -f {} \;

echo "Finished MySQL Backup";
echo `date`;

Swift UIColor extensions

extension UIColor {
    convenience init?(rgb: Int) {
        self.init(
            red:    CGFloat((rgb & 0xFF0000) >> 16) / 255.0,
            green:  CGFloat((rgb & 0xFF00) >> 8) / 255.0,
            blue:   CGFloat(rgb & 0xFF) / 255.0,
            alpha:  1.0)
    }

    convenience init?(rgba: Int) {
        self.init(
            red:    CGFloat((rgba & 0xFF000000) >> 32) / 255.0,
            green:  CGFloat((rgba & 0xFF0000) >> 16) / 255.0,
            blue:   CGFloat((rgba & 0xFF00) >> 8) / 255.0,
            alpha:  CGFloat(rgba & 0xFF) / 255.0)
    }
}

Usage

var color1 = UIColor(rgba: 0x435363FF)
var color2 = UIColor(rgb: 0x435363)