Setting up my website
How did we get here?
This website was built using a static site generator called Jekyll, and it is using a theme called Chirpy. As you may have noticed from the URL (.github.io
), this site is built and hosted on Github.
My posts on this site are written as code using the Markdown language. Markdown is a markup language used to create and format text and is widely used as a more simple, human-readable alternative to HTML when it comes to creating websites. When a user browses to a page written in Markdown, the format is translated into HTML so the page renders properly.
Any time I want to add content to my website, I create a new .md
(Markdown) file in a _posts
folder that is found in a local directory tree on my system. After I am done making changes, I save the file and I “push” the changes made to the directory tree on my system to my repository in the Github servers.
Getting things to work properly
In order for my posts to display correctly, the beginning of the empty .md
file must have the following header information inserted:
1
2
3
4
5
6
---
title: TITLE
date: XXXX-XX-XX XX:XX:XX -XXX
categories: [category1,category2,...]
tags: [tag1,tag2]
---
If any of the information in that header is incorrect, the post will not display on my site. I learned this from experience of course. Further, I learned that inputting the time correctly is crucial.
I knew that it was probably going to be a struggle to find and copy the current time in the exact format I needed from some random time zone website, so I opted to create a Bash script to output this information for me. I knew a script would give me the EXACT time and would be a better long-term solution than inputting time data manually into the date:
field like I was trying to do initially.
So without further ado, check out this bash script I ChatGPT created to format the time for my posts.
Bash time script
1
2
3
4
current_utc_seconds=$(date -u +%s)
cst_seconds=$((current_utc_seconds - 21600))
cst_time=$(date -u -d "@$cst_seconds" +"date: %Y-%m-%d %H:%M:%S -600")
echo "$cst_time"
As a self-respecting, soon-to-be Linux+ certified IT professional, it would be unbecoming of me to blindly use a script generated by ChatGPT without having a granular understanding of it. So let’s break it down.
Script output
First, let’s examine the output of the script at my current time of 01:14 AM
1
2
clay@clay-Dreambox:~$ UTCtime.sh
date: 2023-09-26 00:14:45 -600
Here we can see that in order for my posts to display correctly, my local time needs to be shifted back an hour and also include the six-hour UTC (Coordinated Universal Time) time zone offset for Central Standard Time where -600
in the output represents UTC-6.
Line 1
1
current_utc_seconds=$(date -u +%s)
The first line of the script creates the variable current_utc_seconds
and sets it equal to the output of the date -u +%s
command.
The date
command displays time/date information depending on the format specifers that were input along with the command. In this case, the +%s
format specifier represents the number of seconds since the Unix epoch (00:00:00 UTC on January 1, 1970). The -u
option gives us the time in seconds since the Unix epoch in the UTC time zone, rather than my local time zone: UTC-6.
1
2
3
4
5
6
7
8
clay@clay-Dreambox:~$ date
Tue Sep 26 01:44:45 AM CDT 2023
clay@clay-Dreambox:~$ date -u
Tue Sep 26 06:44:49 AM UTC 2023
clay@clay-Dreambox:~$ date +%s
1695710693
clay@clay-Dreambox:~$ date -u +%s
1695710699
In the example above, we can see that there is no functional difference between the last two commands. The +%s
format specifier will output the same amount of seconds regardless of the timezone specified. The amount of time since the Unix epoch does not change across time zones; the -u
option merely alters how the time is reported.
Line 2
1
cst_seconds=$((current_utc_seconds - 21600))
In this line, we take the value of the current_utc_seconds
variable and apply my local time zone offset. A UTC-6 time zone offset of -6 hours translates into 21600 based on the math: 6 hours * 60 minutes * 60 seconds. This line directly applies the time offset that the date -u +%s
command did not apply.
Line 3
1
cst_time=$(date -u -d "@$cst_seconds" +"date: %Y-%m-%d %H:%M:%S -600")
This is where it all comes together. A new variable cst_time
is derived from the date
command and cst_seconds
variable that was created in the previous line of the script.
The date
command in this line can be broken up into two sections.
Line 3, pt 1
The first half date -u -d "@$cst_seconds"
gives us our time and date The -u
option displays the date in the UTC time zone. The -d
option provides a date string for the command to work with.
Running echo
using just the first half of this command displays the following:
1
2
3
4
5
current_utc_seconds=$(date -u +%s)
cst_seconds=$((current_utc_seconds - 21600))
cst_time=$(date -u -d "@$cst_seconds")
echo "$cst_time"
Tue Sep 26 01:27:34 AM UTC 2023
Line 3, pt 2
Recall, the date needs to be formatted as such in order for my website posts to display correctly:
date: XXXX-XX-XX XX:XX:XX -XXX
The second half of the command: +"date: %Y-%m-%d %H:%M:%S -600"
takes the time/date string generated by date -u -d "@$cst_seconds"
and displays it in the format required by our website generator.
The +
before the double quotes helps to create a more detailed format specifier. In this case, the text date:
and -600
do not actually represent any other values which means this text will be displayed the same in the script output. The placeholders %Y
, %m
, %d
, %H
, %M
, and %S
represent the year, month, day, hour, minute, and second respectively.
Line 4
1
echo "$cst_time"
Everything came together in the previous, penultimate step of this script, but the last line is where the magic really happens. The echo
command allows us to output the value of the cst-time
variable.
Adding the script to PATH environment variable
If I wanted to run UTCtime.sh
I would either have to specify or navigate to the directory it is contained in, and then type use the sh
command to run the script.
1
2
3
clay@clay-Dreambox:~$ cd ~/Documents/"Custom Scripts"
clay@clay-Dreambox:~/Documents/Custom Scripts$ sh UTCtime.sh
date: 2023-09-26 02:00:53 -600
This method works, but I am lazy and would prefer to minimize the amount of time I use to perform a simple function. A start would be using the chmod
command to add the execute permission to the UTCtime.sh
script file. This removes the requirement to use the sh
command to run the script.
1
2
3
clay@clay-Dreambox:~/Documents/Custom Scripts$ chmod +x UTCtime.sh
clay@clay-Dreambox:~/Documents/Custom Scripts$ UTCtime.sh
date: 2023-09-26 02:04:29 -600
This is all well and good, but I still need to specify or navigate to the directory containing the script in order to run it. I want to be able to run the script from any directory.
Editing the ~/.bashrc file
The ~/.bashrc
file is used to configure user-specific bash shell features. In this case, we want to add a line at the end of this file that will point to the directory containing my custom scripts and include it as a part of my PATH
variable.
When a directory containing scripts or binaries is added to the PATH
variable, you can run the scripts or binaries from within any directory while working in the shell as if the files were located in whatever directory you are currently in.
1
export PATH="$HOME/Documents/Custom Scripts:$PATH"
Now we can see after making the UTCtime.sh
executable and adding the above line to the ~/.bashrc
file that I can now run UTCtime.sh
as a command from within any directory.
1
2
3
clay@clay-Dreambox:~/claylamothe.github.io$ cd ~
clay@clay-Dreambox:~$ UTCtime.sh
date: 2023-09-26 02:20:42 -600
Conclusion
Now while I work on my website, I can simply open a terminal from within VS Code and run my script as a command to quickly input times for my website posts, including this one!
I give credit to Techno Tim for publishing a tutorial on how to create a website using Jekyll that helped me to get started.