Skip to content

Commit

Permalink
Merge pull request #3 from mattstein/v2
Browse files Browse the repository at this point in the history
v2
  • Loading branch information
mattstein authored Sep 17, 2024
2 parents d8622ba + 321a4a8 commit 0355771
Show file tree
Hide file tree
Showing 11 changed files with 222 additions and 611 deletions.
2 changes: 2 additions & 0 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ jobs:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- name: Install Composer dependencies
uses: php-actions/composer@v6
# Store version number without `v`
- name: Write release version
run: |
Expand Down
27 changes: 27 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# Changelog

All notable changes to this project will be documented in this file.

The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [Unreleased]

## [2.0.0] - 2024-09-17

### Added

- Added GitHub Actions release flow.

### Changed

- Updated namespace.
- Renamed `master` branch to `main`.
- Swapped in `joetannenbaum/alfred-workflow` via Composer, with PHP 8.
- Updated icon.

## [1.0.0] - 2022-07-19

### Added

- Sort-of first version. (Never officially tagged or released.)
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

Get human-formatted date/time intervals with Alfred.

![Workflow screenshot](resources/screenshot.png)

Custom theme using [Berkeley Mono](https://berkeleygraphics.com/typefaces/berkeley-mono/).

## Installation

Download the `.alfredworkflow` file from the [latest release](https://github.com/mattstein/alfred-datespan-workflow/releases) and double-click to install.
Expand Down
6 changes: 6 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"require": {
"php": "^8.0",
"joetannenbaum/alfred-workflow": "^1.1"
}
}
68 changes: 68 additions & 0 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

184 changes: 68 additions & 116 deletions datespan.php
Original file line number Diff line number Diff line change
@@ -1,136 +1,103 @@
<?php

$timezone_abbreviation = isset($argv[2]) ? $argv[2] : 'PDT';
include 'vendor/autoload.php';

date_default_timezone_set(timezone_name_from_abbr($timezone_abbreviation));
use Alfred\Workflows\Workflow;

$icon = "icon.icns";
$workflow = new Workflow();
$timezoneAbbreviation = isset($argv[2]) ? $argv[2] : 'PDT';

require( 'workflows.php' ); // by David Ferguson
$wf = new Workflows();
date_default_timezone_set(timezone_name_from_abbr($timezoneAbbreviation));

$dates = explode(" to ", trim(@$argv[1])); // split arguments by " to "

for ($i=0; $i < sizeof($dates); $i++)
{
$dates[$i] = str_replace('-', '/', $dates[$i]);
}

array_map(static function($dateString) {
return str_replace('-', '/', $dateString);
}, $dates);

/*
* use the current date and time in lieu of a second argument
*/

$date2 = isset($dates[1]) ? new DateTime($dates[1]) : new DateTime();
$date1 = new DateTime($dates[0]);
try {
$date2 = isset($dates[1]) ? new DateTime($dates[1]) : new DateTime();
$date1 = new DateTime($dates[0]);
} catch (Exception $e) {
return;
}

$diff = $date1->diff($date2);

// http://www.php.net/manual/en/dateinterval.format.php

$minutes = intval($diff->format('%i'));
$hours = intval($diff->format('%h'));
$days = intval($diff->format('%d'));
$total_days = intval($diff->format('%a'));
$total_hours = $total_days*24;
$total_minutes = $total_hours*60;
$weeks = intval(intval($diff->format('%a'))/7);
$business_weeks = intval(intval($diff->format('%a'))/5);
$months = intval($diff->format('%m'));
$years = intval($diff->format('%y'));
$minutes = (int) $diff->format('%i');
$hours = (int) $diff->format('%h');
$days = (int) $diff->format('%d');
$total_days = (int) $diff->format('%a');
$total_hours = $total_days * 24;
$total_minutes = $total_hours * 60;
$weeks = (int) $diff->format('%a') / 7;
$business_weeks = (int) $diff->format('%a') / 5;
$months = (int) $diff->format('%m');
$years = (int) $diff->format('%y');
$sign = $diff->format('%R');

if ($total_days > 1)
{
if ($total_days > 1) {
$days++;
}


/*
* a single, complete string; may include years, months, days, hours, minutes,
* and "ago" if there's only one paramater and it's in the past
* and "ago" if there's only one parameter and it's in the past
*/

$complete = array();
$complete = [];

if ($years)
{
$complete[] = pluralize('year', $years);
}

if ($months)
{
$complete[] = pluralize('month', $months);
}

if ($days)
{
$complete[] = pluralize('day', $days);
}

if ($hours)
{
$complete[] = pluralize('hour', $hours);
}
if ($years) { $complete[] = pluralize('year', $years); }
if ($months) { $complete[] = pluralize('month', $months); }
if ($days) { $complete[] = pluralize('day', $days); }
if ($hours) { $complete[] = pluralize('hour', $hours); }
if ($minutes) { $complete[] = pluralize('minute', $minutes); }

if ($minutes)
{
$complete[] = pluralize('minute', $minutes);
}

if (sizeof($complete) > 1)
{
if (count($complete) > 1) {
$complete_string = implode(', ', array_slice($complete, 0, -1));
$complete_string .= " and ".$complete[sizeof($complete)-1];
}
else
{
$complete_string .= " and ".$complete[count($complete)-1];
} else {
$complete_string = implode(', ', $complete);
}

if ( ! isset($dates[1]) AND $sign === "+")
{
if ( ! isset($dates[1]) && $sign === "+") {
$complete_string .= " ago";
}

$wf->result(
"complete",
$complete_string,
$complete_string,
"copy to clipboard",
$icon
);
$workflow->item()
->title($complete_string)
->arg($complete_string)
->subtitle('Copy to clipboard');


/*
* include business weeks if we have them
*/

if ($business_weeks > 0)
{
$wf->result(
"business weeks",
pluralize('business week', $business_weeks),
pluralize('business week', $business_weeks),
"copy to clipboard",
$icon
);
if ($business_weeks > 0) {
$workflow->item()
->title(pluralize('business week', $business_weeks))
->arg(pluralize('business week', $business_weeks))
->subtitle('Copy to clipboard');
}


/*
* include weeks if we have them
*/

if ($weeks > 0)
{
$wf->result(
"weeks",
pluralize('week', $weeks),
pluralize('week', $weeks),
"copy to clipboard",
$icon
);
if ($weeks > 0) {
$workflow->item()
->title(pluralize('week', $weeks))
->arg(pluralize('week', $weeks))
->subtitle('Copy to clipboard');
}


Expand All @@ -139,60 +106,45 @@
* we should include it because it's probably interesting
*/

if ($total_days > $days)
{
$wf->result(
"days",
pluralize('day', $total_days),
pluralize('day', $total_days),
"copy to clipboard",
$icon
);
if ($total_days > $days) {
$workflow->item()
->title(pluralize('day', $total_days))
->arg(pluralize('day', $total_days))
->subtitle('Copy to clipboard');
}


/*
* include a total count of hours if we've got them
*/

if ($total_hours > 0)
{
$wf->result(
"hours",
pluralize('hour', $total_hours),
pluralize('hour', $total_hours),
"copy to clipboard",
$icon
);
if ($total_hours > 0) {
$workflow->item()
->title(pluralize('hour', $total_hours))
->arg(pluralize('hour', $total_hours))
->subtitle('Copy to clipboard');
}


/*
* include a total count of minutes if we've got them
*/

if ($total_minutes > 0)
{
$wf->result(
"minutes",
pluralize('minute', $total_minutes),
pluralize('minute', $total_minutes),
"copy to clipboard",
$icon
);
if ($total_minutes > 0) {
$workflow->item()
->title(pluralize('minute', $total_minutes))
->arg(pluralize('minute', $total_minutes))
->subtitle('Copy to clipboard');
}


echo $wf->toxml();
$workflow->output();


/*
* make pretty numbers, and add "s"'s if needed
*/

function pluralize($label, $value)
function pluralize($label, $value): string
{
return number_format($value) . " ". $label . ($value != 1 ? "s" : "");
return number_format($value) . " ". $label . ($value !== 1 ? "s" : "");
}

?>
Binary file removed icon.icns
Binary file not shown.
Binary file modified icon.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading

0 comments on commit 0355771

Please sign in to comment.