Skip to content

Commit

Permalink
Better timezone handling; use file_put_contents
Browse files Browse the repository at this point in the history
I run my PHP in a container, which defaults to UTC time; but I want US
Eastern time used by the `date()` function when building date-based
paths.  The way this previously worked was that I set the default
timezone **after** I'd used a `date()` function in `config.php`, which
resulted in a UTC-based date that was "tomorrow" from my local time
zone's perspective.

Explicitly setting the timezone in `config.php` first, rather than
making this a variable in the `$config` hash does what I want: sets the
default timezone as early as possible, and allows all subsequent
`date()` invocations to render US Eastern times.

This also streamlines the creation of the Markdown files by using
`file_put_contents`, which states:
```
This function is identical to calling fopen(), fwrite() and fclose()
successively to write data to a file.
```
  • Loading branch information
Scott Merrill committed May 17, 2018
1 parent f67682d commit f6a1ea6
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 15 deletions.
12 changes: 4 additions & 8 deletions config.php.sample
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
<?php

/**
this is the configuration file for my micropub implementation. Rather than
jump through a bunch of hoops trying to parse the host name, and figure out
the current working directory, just capture those values as needed.
**/
# We set the default timezone here so that you can safely use the PHP
# date() function inside the config elements below, should you desire.
date_default_timezone_set('America/New_York');

$config = array(
# the URL of our site, with trailing slash.
'base_url' => 'https://' . $_SERVER['HTTP_HOST'] .'/',
Expand Down Expand Up @@ -47,9 +46,6 @@ $config = array(
# the IndieAuth token endpoint to use
'token_endpoint' => 'https://tokens.indieauth.com/token',

# the timezone to use for all times
'tz' => 'America/New_York',

# the command used to build the site
'command' => '/var/www/bin/hugo --quiet --config /var/www/skippy/config.yaml -s /var/www/skippy/ -d /var/www/html/',
);
Expand Down
8 changes: 2 additions & 6 deletions inc/content.php
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ function reply_or_repost($properties, $content) {
# given an array of front matter and body content, return a full post
function build_post( $front_matter, $content) {
ksort($front_matter);
return "---\n" . Yaml::dump($front_matter) . "---\n" . $content . "\n";
return "---\n" . Yaml::dump($front_matter) . "---\n" . $content . "\n";
}

function write_file($file, $content, $overwrite = false) {
Expand All @@ -130,13 +130,9 @@ function write_file($file, $content, $overwrite = false) {
if (file_exists($file) && ($overwrite == false) ) {
quit(400, 'file_conflict', 'The specified file exists');
}
if ( ! $fh = fopen( $file, 'w' ) ) {
if ( FALSE === file_put_contents( $file, $content ) ) {
quit(400, 'file_error', 'Unable to open Markdown file');
}
if ( fwrite($fh, $content ) === FALSE ) {
quit(400, 'write_error', 'Unable to write to Markdown file');
}
fclose($fh);
}

function delete($request) {
Expand Down
1 change: 0 additions & 1 deletion index.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
die;
}
$config = include_once './config.php';
date_default_timezone_set($config['tz']);

# invoke the composer autoloader for our dependencies
require_once __DIR__.'/vendor/autoload.php';
Expand Down

0 comments on commit f6a1ea6

Please sign in to comment.