Skip to content

Instantly share code, notes, and snippets.

@Rudde
Rudde / firstDayofFirstWeekinCurrentMonth.php
Last active April 8, 2016 16:11
Gives first day of the first week of the current month
<?php
/**
* Created by PhpStorm.
* User: Rudde
* Date: 01.04.2016
* Time: 00.25
*/
function firstDayofFirstWeekinCurrentMonth($date = NULL) {
$date = is_null ($date) ? new DateTime('first day of this month') : $date->modify('first day of this month');
@Rudde
Rudde / pcp.sh
Created April 28, 2016 15:52
Bash copy tool giving speed and progress bar, working for files and directories built of pv
#!/bin/bash
hash pv
if [ $? -ne 0 ]; then
echo "ERROR: Command pv is not available but required, please run: sudo apt-get install pv"
exit 1
fi
WORKDIR=$(pwd)
@Rudde
Rudde / sec2ts.sh
Created May 10, 2016 00:36
Takes int or float seconds as input will output in format hh:mm:ss.ms where ms is rounded to 3 decimals
#!/bin/bash
function padnum {
NUM=$1
if [ $(echo "$NUM < 10" | bc) -ne 0 ]
then
echo 0$NUM
else
echo $NUM
fi
@Rudde
Rudde / numSuffix.lib.php
Created May 12, 2016 18:58
Take a number input output it with proper suffix like 1st, 2nd, 3rd, and so on
<?php
/* Written by Barand from phpfreaks.com */
function numSuffix($n) {
$str = "$n";
$t = $n > 9 ? substr($str,-2,1) : 0;
$u = substr($str,-1);
if ($t==1) return $str . 'th';
else switch ($u) {
case 1: return $str . 'st';
@Rudde
Rudde / array_filter_key.php
Created May 14, 2016 00:27 — forked from h4cc/array_filter_key.php
Filtering a PHP array by key instead of value.
<?php
/**
* Filtering a array by its keys using a callback.
*
* @param $array array The array to filter
* @param $callback Callback The filter callback, that will get the key as first argument.
*
* @return array The remaining key => value combinations from $array.
*/
@Rudde
Rudde / HTTP Cookie header parser.php
Last active January 20, 2019 19:51 — forked from pokeb/HTTP Cookie header parser
Quick and dirty HTTP cookie header parser in PHP
@Rudde
Rudde / IncrementLetters.cs
Last active August 29, 2017 06:13
Increment letters
using System;
namespace ConsoleApp1
{
class Program
{
static void Main(string[] args)
{
while (true)
{
Function Format-FileSize() {
Param ([long]$size)
If ($size -gt 1TB) {[string]::Format("{0:0.00} TB", $size / 1TB)}
ElseIf ($size -gt 1GB) {[string]::Format("{0:0.00} GB", $size / 1GB)}
ElseIf ($size -gt 1MB) {[string]::Format("{0:0.00} MB", $size / 1MB)}
ElseIf ($size -gt 1KB) {[string]::Format("{0:0.00} kB", $size / 1KB)}
ElseIf ($size -gt 0) {[string]::Format("{0:0.00} B", $size)}
Else {"0 byte"}
}
Function Format-Time() {
Param ([int]$ms)
If ($ms -gt 3600000) {[string]::Format("{0:0.00} hours", $ms / 3600000)}
ElseIf ($ms -gt 60000) {[string]::Format("{0:0.00} minutes", $ms / 60000)}
ElseIf ($ms -gt 1000) {[string]::Format("{0:0.00} seconds", $ms / 1000)}
ElseIf ($ms -gt 0) {[string]::Format("{0:0.00} milliseconds", $ms)}
Else {"0 milliseconds"}
}
@Rudde
Rudde / ConsoleSpinner.cs
Created October 7, 2020 09:51
Will display a pleasant spinner in your C# console apps.
public class ConsoleSpinner : IDisposable
{
private const string Sequence = @"/-\|";
private int counter = 0;
private readonly int? left;
private readonly int? top;
private readonly int delay;
private bool active;
private readonly Thread thread;
private readonly ConsoleColor? color;