About Time

As we prepare to “fall back” this weekend in the US I’ve been thinking about writing code that deals with dates and times. This is easy to get wrong and is a common cause of bugs.

Dates

The first issue is how to write dates. For example, if I write 11/03/2021 that means November Third in the US, but in other countries it might be March Eleventh. Oops.

With that in mind, I always use ISO 8601 format for dates: 2021-11-03. To me this just makes sense. The year is the biggest so it comes first, followed by the month and day. In a Rails console you can try something like this:

Date.new(2021, 11, 3)

Date.parse(“2021-11-03”)

Easy. No confusion. The order is the same in both cases.

Times

Times are easy, right? Everyone does hours, colon, minutes. Well, not exactly. Some folks like 24-hour time and some like AM / PM.

In casual conversation I use AM / PM, but in code I try to always use 24-hour time. That way you immediately know if the time is before or after noon. For example, 1:00 could be AM or PM, but 01:00 and 13:00 are clear. Back to Rails, it looks like this:

Time.parse(“2021-11-03 13:00”)

But there’s still one piece missing.

Time Zones

The bane of programmers everywhere. The dreaded time zone.

In my experience things usually default to UTC time. I’ve never seen a database that stored times in a different time zone. In Rails you can configure the default time zone:

# config/application.rb

config.time_zone = “UTC”

You can parse times in other time zones. You can also change the time zone on existing objects. This is useful when you want to display a time in the user’s time zone.

t = Time.parse(“2021-11-03 13:00 CDT”)
=> “2021-11-03 13:00:00 -05:00”


t.in_time_zone(“America/New_York”)
=> “Wed, 03 Nov 2021 14:00:00 EDT -04:00”

Notice how the format is different after I changed the time zone? That’s because it also changed the class of the object.

t = Time.parse(“2021-11-03 13:00 CDT”)
=> “2021-11-03 13:00:00 -05:00”

t.class
=> Time


t.in_time_zone(“America/New_York”).class
=> ActiveSupport::TimeWithZone

You can format the output however you like, these are just the defaults. Note that setting the time zone in Rails only changes the way the time is displayed. The actual value remains the same.

t = Time.parse(“2021-11-03 13:00 CDT”)
=> “2021-11-03 13:00:00 -05:00”

t.to_i
=> 1635962400


t.in_time_zone(“America/New_York”).to_i
=> 1635962400

Comparisons between times in different time zones will work as expected. 13:00 central time is equal to 14:00 eastern time.

I’m on call for work this weekend. Look for a follow-up post on Monday talking about everything that broke on Saturday night.

The MacBook Pro

Apple has really put the “Pro” back in MacBook Pro. Ars Technica says “Yep, it’s what you’ve been waiting for”. That looks to be true.

I guess some might say I’m an Apple fanboy. I have an iPhone, iPad, Apple Watch, Apple TV, etc. Now that I’m so deep in the ecosystem it just makes sense to stick with it.

I bought 13-inch M1 MacBook Pro a year ago when they were announced. I was immediately impressed. It’s the fastest computer I’ve ever owned, the battery lasts all day, and it seems like the fan never even turns on.

There are a few things I’m not crazy about. I never use the touch bar. I don’t look at the keyboard while I type, so I don’t even think about it. I would like a little more screen real estate. The M1 is still plenty fast, but faster is always better.

The lack of ports doesn’t bother me. I rarely plug in anything other than the charging cable. On my old MacBook Pro I did occasionally use the SD Card reader, and I’ve even used HDMI to connect to TV.

So of course I ordered a 14-inch MacBook Pro with the M1 Pro. The good news is I can get $880 for my 1-year-old 13-inch M1. Combine that with 3% cash back on the Apple Card (of course) and the price tag doesn’t look so bad.

I won’t receive it until end of November, early December. Once it arrives I’ll post more details about how it compares to the M1. And decide if it was really worth it or I just like having the latest and greatest.

Foundation

I’m really enjoying the new Foundation series on Apple TV+. Here’s the trailer if you haven’t seen it.

I read the books years ago and listened to the audiobook of Foundation earlier this year. As I started watching the series I found myself trying to compare it to the book which was a mistake.

The series is based on the books, but differs in some pretty significant ways. For one thing, the future that Asimov’s books describe seems to be made up entirely of white males. In the series both Gaal Dornick and Salvor Hardin are played by young black women. A much needed change.

The special effects are very well done. I don’t remember especially vivid descriptions of space travel in the books, but the series does a great job. I also like the fact that space travel takes time. The ships look amazing, but they aren’t magical.

Readers of the books know that just as you get comfortable with a set of characters the story jumps forward and introduces a new cast. This happens between episodes 2 and 3 of the new series and it was a little jarring.

Some are already calling this the next Game of Thrones, but I don’t think that will be the case. I hope it makes it to eight seasons, but it seems like things come and go quickly in the streaming world.

It’s a smart series, very well done, and updated for modern times. I’m looking forward to future episodes.

iPadOS 15

Of course I updated to iPadOS 15 as soon as I could. I don’t run beta versions anymore, but since I don’t use my iPad for anything critical, I’m not afraid of release day updates.

The way multitasking works now is great. Discovering and remembering the multitasking gestures before was next to impossible. I can’t believe it took this long to add a tiny menu to the top of every app.

Reading and Writing

It’s nice having the App Library on iPad now. I only use a handful of apps on a daily basis. It’s nice having all of the infrequently used apps available, but out of the way. This should also help make room for the new widgets.

Speaking of widgets, I’m not currently using any. I’m not sure I really see the point. Maybe they just don’t fit my iPad usage patterns. I spend most of my time catching up on news, reading books, writing a bit, and playing casual games. I’m not sure where widgets fit into that.

I’m still getting used to the changes to Safari. The new extensions UI is great. I was able to install 1Password with just a few taps. The new rounded tabs are growing on me, but I still think they look more like buttons than tabs.

The best new features, in my opinion, are additional privacy protections in Safari and Mail. It seems like Apple is continuing to focus on online privacy. Anything that keeps advertisers from tracking me across the web makes me happy. I understand that content creators want to get paid, but you can still show ads without tracking.

Crafting Interpreters

I’ve been working my way through Crafting Interpreters by Robert Nystrom for the past few days. It’s an excellent book about creating your own programming language.

I was already familiar with Nystrom’s previous book Game Programming Patterns. A great book on design patterns, even if you’re not a game programmer.

Crafting Interpreters covers a new object-oriented scripting language called Lox. You’ll first implement a tree-walk interpreter in Java and then create a bytecode virtual machine in C.

I probably won’t ever directly use these skills in my day job, but gaining a deeper understanding of interpreters and virtual machines can’t hurt. Especially considering I split my time between the Ruby interpreter and Java VM.

Almost as interesting as the book itself is the way he wrote it using a combination of Markdown for prose and hand-drawn diagrams. More details are in his blog post Crafting “Crafting Interpreters”.

If the book sounds interesting to you, you can read it online for free at the link above. After reading the first few chapters onscreen, I bought a print copy. I like having the book open on my desk while writing code.

The Matrix Resurrections

Oh my god. I realize this is two Matrix posts in a row, but this trailer looks amazing!

In theaters and HBO Max on December 22. That makes me even happier. I really want to see this, but I’m still not ready to step back into a theater. I honestly may never be.

Matrix Time

When I was a younger there was a phone number you could call where a recording would tell you the current time and temperature. I never really thought too much about that. It was obviously an early computer generated voice filling in the time and temperature.

Now, there’s a website for the upcoming Matrix movie that incorporates your local time the right way.

The Choice Is Yours

Thanks to a post on Reddit, you can see how it was done. There are 1,440 copies of each trailer, one for each minute of the day. A bit of clever code picks the correct video to play based on the local time in your browser.

Not rocket science, but it did get people talking.

Bringing Back WebRings?

If you can make it past the over abundance of ads (Reader View is your friend), this is an interesting article about the Internet days of old.

What these rebellious programmers are building goes by many names—indieweb, yesterweb, folk internet—and relies on simple design choices, often borrowing elements from the 1980s and 1990s.

Tech Workers Rebel Against A Lame-Ass Internet By Bringing Back ‘Geocities-Style’ WebRings

I don’t think this is exactly the modern definition of indieweb. See IndieWeb.org for more info. But I guess this is close. I like the terms yesterweb and folk Internet for this style of website.

I had a webring on one of my old web pages, and I had a Geocities site back in the day, but I don’t see myself jumping on Neocities any time soon. I understand the nostalgia, for sure. I guess I could create a page with a few of those “under construction” gifs and call it done.

Remember the time before content was served via algorithms designed to exchange attention for dollars? At the end of the day, this is just another example of a group of old folks like me longing for the web that was.

The Internet Is Dead

This is probably the least-crazy conspiracy theory I’ve read about lately. In some ways i guess it’s true. The “real” Internet is definitely not what it once was.

Let me explain. Dead-internet theory suggests that the internet has been almost entirely taken over by artificial intelligence. Like lots of other online conspiracy theories, the audience for this one is growing because of discussion led by a mix of true believers, sarcastic trolls, and idly curious lovers of chitchat.

Maybe You Missed It, but the Internet ‘Died’ Five Years Ago

Most people think of social networks when they think of “The Internet.” It’s well known that much of what ones sees there is created by only a few people and served to the masses via a clever algorithm.

Want everyone to see your content? Trick the algorithm into thinking your latest work is very engaging. All it takes is one “influencer,” a few hundred bots, or a paid promotion and the world will know your name.

Of course this conspiracy theory goes one step further. (Don’t they always?) It claims that most of the content you see online is actually created by an artificial intelligence before being propagated by bots.

More great writing from The Atlantic.