{"id":24784,"date":"2021-11-16T08:09:19","date_gmt":"2021-11-16T16:09:19","guid":{"rendered":"https:\/\/www.podfeet.com\/blog\/?p=24784"},"modified":"2021-11-16T10:50:18","modified_gmt":"2021-11-16T18:50:18","slug":"time-shifter-clock-dst-fix","status":"publish","type":"post","link":"https:\/\/www.podfeet.com\/blog\/2021\/11\/time-shifter-clock-dst-fix\/","title":{"rendered":"Time Shifter Clock and DST Leads to Fun with Virtual Machines"},"content":{"rendered":"<p>You\u2019ve probably heard me talk about my amazing web app, <a href=\"http:\/\/timeshifterclock.com\" target=\"_blank\" rel=\"noopener\">Time Shifter Clock<\/a> that allows you to figure out what time it <em>will<\/em> be in multiple cities at once in order to schedule times to talk to people around the globe.<\/p>\n<h2>The Problem to be Solved<\/h2>\n<p>Last week I had a chance to see how it dealt with the US leaving Daylight Saving Time. Most of the year, Los Angeles and Dublin are 8 hours apart, but because the US is idiotic, we get on and off Daylight Saving Time at different times than the rest of the globe.  That means Bart and I are 8 hours apart most of the year, then briefly for a week or two we\u2019re 7 hours apart, then we go back to 8 hours apart, and then back to 7 hours apart for a couple of weeks. It\u2019s a hot mess.<\/p>\n<p>The night before the US got off Daylight Saving Time (November 6th), Dublin and LA were 7 hours apart according to my clock, which was correct. To test that it was working properly, I used the slider to shift time forward to November 7th when it <em>should<\/em> have showed us 8 hours apart, but sadly it did not.  I was very disappointed that there was a bug I&#8217;d have to fix.<\/p>\n<p>I spent some quality time with my code and figured out my mistake. I take the current time in the first city (which defaults to LA because it\u2019s my app) and I convert that to universal time, aka UTC.  It\u2019s a bit arrogant that we think we\u2019re in charge of declaring what the time is for the universe, but I digress.<\/p>\n<p>After I change it to UTC, I round it down to the nearest hour, and then I convert any cities chosen to their offset from UTC, and then I add the time chosen with the slider to move each city forward in time.  My mistake is that I should add the slider value to the UTC time <em>before<\/em> I convert it to the different cities&#8217; times.<\/p>\n<h2>GitHub Copilot<\/h2>\n<p>Once I realized my error, it was time to code the fix.  I use VSCode as my code editor, and I\u2019ve been experimenting with a plugin called GitHub Copilot that makes suggestions for what it thinks you might want to write. It\u2019s pretty helpful to me because I often know what I want to do, but I\u2019m unsure of the syntax. If I get a running start at a function, GitHub Copilot often will write the rest of it for me.<\/p>\n<p>In this case, GitHub Copilot came through like a champ.  It wrote, all by itself (after I had set up the previous line):<\/p>\n<p><code>let UTCrdtShifted = moment(UTCrdt).add(timeShiftedVal, \"h\");<\/code><\/p>\n<p>That may look and sound like gibberish to you but it was exactly what I needed. It says to take my rounded down time in UTC and add the value from the time shifter to create a new reference point in UTC.<\/p>\n<h2>Testing the Fix<\/h2>\n<p>While I was fairly certain that I had found the root cause of the problem, I wasn\u2019t certain that I had implemented the fix correctly.  I needed to test my fix, but there was a big problem.  Time Shifter Clock only lets you shift forward 7 days, so how can I test this solution without waiting till the next Daylight Saving Time change?<\/p>\n<p>I told  Bart that I was pretty sure I\u2019d fixed the bug but I was sad I couldn\u2019t test it.  He suggested that I change the date and time on my Mac to right before the previous (or next) Daylight Saving Time change and then test it.<\/p>\n<p>Two things about that idea. First of all, I had no idea that Time Shifter Clock was using the time of the user&#8217;s computer to do its calculations! I thought my code was reaching out to some Time Lord to know what time it was!  I thought the moment.js library I was using was in charge of the actual time.<\/p>\n<p>I\u2019m really glad I had this bug because learning you have a fundamental misunderstanding in how your code functions is a very good thing.  I mean, of course it would be better if you <em>didn\u2019t<\/em> have a fundamental misunderstanding, but if you do have one, you really want to learn about it!<\/p>\n<p>Now to Bart\u2019s idea of changing the date\/time on my Mac. I was trepidatious about this suggestion because bugs in a system can be easily introduced by an incorrect system clock. In my case I know for sure that \u201cspooky action at a distance\u201d would occur, because I have a lot of Hazel rules that are time-based. For example, when my recorded audio files are over 2 weeks old, Hazel whisks them over to my Synology NAS for cold storage and deletes them from my internal drive.<\/p>\n<figure style=\"float: center; margin: 10px\"><img decoding=\"async\" src=\"https:\/\/www.podfeet.com\/blog\/wp-content\/uploads\/2021\/11\/Hazel-Rule-to-Move-Files-15-Days-to-Synology.png\" alt=\"Hazel Rule to Move Files >15 Days to Synology&#8221; title=&#8221;#title#&#8221; width=&#8221;600 &#8221; height=&#8221;284&#8243;><figcaption style=\"text-align:center\">Hazel Rule to Move Files >15 Days to Synology<\/figcaption><\/figure>\n<p>Even if I turned off Hazel for the duration of my testing, I was still worried about what else I might have going on that was time-based that I just didn\u2019t remember.  I induce enough weirdness on my computers all by myself, I don\u2019t need to invite spooky action at a distance into my house!<\/p>\n<p>When I explained my trepidation, Bart made an even better suggestion. Why not do it in a virtual machine? I thought that sounded like great fun. I could install any flavor of Linux, change the system date within the OS, and do my experiments all without breaking anything on my Mac.<\/p>\n<h2>VM Installation<\/h2>\n<p>Virtual machine software such as Parallels and VMware Fusion are highly-capable and awesome pieces of software for people who need to consistently run another operating system.  They&#8217;re not inexpensive, so for a tinkerer like me with needs a couple times a year, they\u2019re not the right answer.<\/p>\n<h3>VirtualBox<\/h3>\n<p>A hundred or so years ago Sun Microsystems came out with an open source virtual machine app called VirtualBox and when Oracle bought out Sun (and the stock I owned went to zero), they kept VirtualBox going.  I have used it in the past so I downloaded it on my M1 Mac. While it was downloading, I also downloaded the Arm version of the Linux distribution CentOS Stream.<\/p>\n<p>I expected that Ubuntu would work and would be easier, but what fun would that be? I wanted to try something more challenging.  When you download a disk image for an operating system like Linux, it\u2019s called an ISO, which makes it look like an old-fashioned CD to the installer software.<\/p>\n<figure style=\"float: center; margin: 10px\"><img decoding=\"async\" src=\"https:\/\/www.podfeet.com\/blog\/wp-content\/uploads\/2021\/11\/VirtualBox-Fails-Installation.png\" alt=\"VirtualBox Fails Installation\" title=\"#title#\" width=\"600 \" height=\"430\"><figcaption style=\"text-align:center\">VirtualBox Fails Installation<\/figcaption><\/figure>\n<p>Unfortunately, it looks like Oracle is no longer a good steward for VirtualBox, because macOS Monterey refused to install it, saying<\/p>\n<blockquote><p>\n  The Installer encountered an error that caused the installation to fail. Contact the software manufacturer for assistance.\n<\/p><\/blockquote>\n<h3>VMs on the Synology<\/h3>\n<h4>Docker Container<\/h4>\n<p>My next idea, since I didn\u2019t want to pay for the commercial virtual machine apps, was to see if I could install Linux into a Docker container on my Synology NAS.  I\u2019m always looking for excuses to justify the cost of the Synology so this was a perfect opportunity.<\/p>\n<p>I\u2019m not an expert on Docker containers, but they\u2019re essentially wholly-encapsulated environments where if you break anything, you just throw away the Docker container and start over. This is one of the reasons I love playing with software \u2013 if you break it you can start over.<\/p>\n<figure style=\"float: center; margin: 10px\"><img decoding=\"async\" src=\"https:\/\/www.podfeet.com\/blog\/wp-content\/uploads\/2021\/11\/Ubuntu-Docker-Container.png\" alt=\"Ubuntu Docker Container\" title=\"#title#\" width=\"600 \" height=\"284\"><figcaption style=\"text-align:center\">Ubuntu Docker Container<\/figcaption><\/figure>\n<p>On the Synology, I already have Docker installed, so I just needed to find a Docker container for Linux. I easily found one for Ubuntu and installed it.  I had no problems at this point but I could not figure out how to make the Ubuntu container \u201cgo\u201d.  There was no launch, no start, no go button at all.  No way to point to a downloaded ISO image of Ubuntu or anything like that that I could find.<\/p>\n<p>I\u2019m sure there is a way to run it from within Docker, or there wouldn\u2019t have been a container. If someone knows, let me know.<\/p>\n<h4>Virtual Machine Manager<\/h4>\n<p>When I went to the googles to try to figure out how to make my virtual machine inside a Docker container go, I discovered something delightful.  Synology provides a tool called Virtual Machine Manager specifically for installing and running virtual machines.<\/p>\n<p>I found a fantastic <a href=\"https:\/\/www.youtube.com\/watch?v=LZ62gFzIRZA\" target=\"_blank\" rel=\"noopener\">video by an Australian named Emilio Aguero<\/a> explaining exactly how to configure the Virtual Machine Manager on a Synology NAS with Ubuntu server.  I know at this point I should have just followed his instructions, but I already had the ISO for the ARM version of CentOS downloaded, so I used that instead.<\/p>\n<p>I followed his instructions to upload it from my Mac to the Synology.  If you\u2019ve been paying close attention, you\u2019ll understand why that failed.  While the ARM version made sense when I was going to install it in Virtual Box on my ARM-based Mac, of course it won\u2019t work on the X86-based Synology.<\/p>\n<p>Still wanting to test out CentOS stream, I downloaded the X86 version. it started to install, but then it asked me a whole bunch of questions for which I did not know the answer.  Time to bail on CentOS Stream and switch to Ubuntu. Maybe there\u2019s a reason why so many people like it.<\/p>\n<p>Once I went down the Ubuntu path using the Virtual Machine Manager on Synology, it was relatively clean sailing.  I got it installed and spent a little time figuring out how to set the screen resolution in Linux so I could see what I was doing.  Technically it all worked but for some reason it thought I had caps lock on when I didn\u2019t, and I never did figure out how to fix that particular problem. I also must say that it is really, really slow. It was usable but I definitely wouldn\u2019t want to use the Synology for any length of time as a host for my virtual machines.<\/p>\n<h2>Finally, Testing Time Shifter Clock<\/h2>\n<p>Now what was the point of this whole experiment? Other than getting a chance to play with my Synology and learn about new tools, I needed to test my Time Shifter Clock in an environment where I could mess with time on the local machine.<\/p>\n<p>In order to test Time Shifter Clock, I wanted to set the clock back to Saturday November 6th, the day before the US came off of Daylight Saving Time. On that day, Dublin and Los Angeles should have been 7 hours apart.  But if I slide the time shifter into Sunday morning, it should, at some point, start to show us as 8 hours apart once Daylight Saving Time has been abandoned in the US.<\/p>\n<p>My testing needed to be on the unreleased version of my code, which is in GitHub, which in turn <a href=\"https:\/\/nosillacast.github.io\/time-shifter-clock\/\" target=\"_blank\" rel=\"noopener\">creates a web page for me automatically<\/a>.<\/p>\n<figure style=\"float: right; margin-left: 10px\"><img decoding=\"async\" src=\"https:\/\/www.podfeet.com\/blog\/wp-content\/uploads\/2021\/11\/Set-Date-and-Time-in-Linux.png\" alt=\"Set Date and Time in Linux\" title=\"#title#\" width=\"400 \" height=\"227\"><figcaption style=\"text-align:center\">Set Date and Time in Linux<\/figcaption><\/figure>\n<p>I set the clock on the virtual machine to 10pm on November  6th and then tried to open the GitHub web page for Time Shifter Clock. Boy howdy, GitHub did it not like that at all.  GitHub knew the time on my system, and yet I was asking for something from the future. I thought that was really interesting. I suspect it had to do with their very tight authentication requirements, including using a time-based authentication code.<\/p>\n<p>I figured out that if I went to the GitHub web page first (with the time set correctly on the virtual machine), I could then change the time and the web page would continue to function.<\/p>\n<p>Now for the big test. The Daylight Saving Time change happens either at 1am or 2am depending on how you look at it. I moved the time shifter forward until it said 1am in Los Angeles and confirmed that Dublin was still 7 hours ahead, showing 8am. I nudged it forward a half hour and saw 1:30am and 8:30am. When I nudged it forward a half hour more, Dublin moved to 9am, but Los Angeles jumped back to 1am again, finally moving us to 8 hours apart.  You can imagine my squeal of glee when I was able to verify that my web app was functioning perfectly now!<\/p>\n<div class=\"group\" style=\"\">\n<figure style=\"float: right; margin-left: 10px\"><img decoding=\"async\" src=\"https:\/\/www.podfeet.com\/blog\/wp-content\/uploads\/2021\/11\/Time-Shifter-Clock-1am-LA-8am-Dublin.png\" alt=\"Time Shifter Clock 1am LA 8am Dublin\" title=\"#title#\" width=\"300 \" height=\"375\"><figcaption style=\"text-align:center\">1am LA 8am Dublin<\/figcaption><\/figure>\n<figure style=\"float: right; margin-left: 10px\"><img decoding=\"async\" src=\"https:\/\/www.podfeet.com\/blog\/wp-content\/uploads\/2021\/11\/Time-Shifter-Clock-130am-LA-830am-Dublin.png\" alt=\"Time Shifter Clock 130am LA 830am Dublin\" title=\"#title#\" width=\"300 \" height=\"374\"><figcaption style=\"text-align:center\">1:30am LA 8:30am Dublin<\/figcaption><\/figure>\n<figure style=\"float: right; margin-left: 10px\"><img decoding=\"async\" src=\"https:\/\/www.podfeet.com\/blog\/wp-content\/uploads\/2021\/11\/Time-Shifter-Clock-1am-LA-9am-Dublin.png\" alt=\"Time Shifter Clock 1am LA 9am Dublin\" title=\"#title#\" width=\"300 \" height=\"375\"><figcaption style=\"text-align:center\">1am LA 9am Dublin<\/figcaption><\/figure>\n<\/div>\n<p>I put three screenshots in the show notes showing the progression, but I also made a little video showing the success of shifting time over a Daylight Saving Time change in one country with respect to another. I\u2019ve said it before, but I\u2019ll say it again, programming makes me feel like I have made FIRE!<\/p>\n<figure>\n<p><video controls=\"controls\" width=\"500\" height=\"\"><source src=\"https:\/\/www.podfeet.com\/blog\/wp-content\/uploads\/2021\/11\/DST-Change-in-Time-Shifter-Clock.mp4\" type=\"video\/mp4\" \/><\/video><figcaption>DST Change in Time Shifter Clock.mov<\/figcaption><\/figure>\n<h2>One More Thing<\/h2>\n<p>I bet some of you out there have realized that I could have made a virtual machine in a much easier way.  Just 5 months ago I told the story of how I used the free UTM virtual machine software to install Ubuntu on my M1 Mac mini.  I totally forgot I had already solved this exact problem! I was searching for a fix for that pesky caps lock problem in my VM on the Synology when I stumbled across <a href=\"https:\/\/mac.getutm.app\/gallery\/ubuntu-20-04\" target=\"_blank\" rel=\"noopener\">the instructions of how to install Ubuntu on UTM<\/a> that I had followed back in June.<\/p>\n<p>For completion sake, I had to install it again on my M1 MacBook Pro and it worked perfectly, and was a lot faster than running on the Synology.  If I remembered just half of what I\u2019ve done over the years, I\u2019d be brilliant. Oh well, I had fun learning about Virtual Machine Manager on Synology and I had fun experimenting with the different versions of Linux.<\/p>\n<p>In any case, I made fire and Time Shifter Clock is better than ever. I\u2019ll be pushing a release of this new feature soon!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>You\u2019ve probably heard me talk about my amazing web app, Time Shifter Clock that allows you to figure out what time it will be in multiple cities at once in order to schedule times to talk to people around the globe. The Problem to be Solved Last week I had a chance to see how [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":24780,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[147],"tags":[1047,4901,4900,1248,4318,2041,2662,4902],"class_list":["post-24784","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-blog-posts","tag-centos","tag-centos-stream","tag-daylight-saving-time","tag-synology","tag-time-shifter-clock","tag-ubuntu","tag-virtual-machine","tag-virtual-machine-manager"],"jetpack_featured_media_url":"https:\/\/www.podfeet.com\/blog\/wp-content\/uploads\/2021\/11\/Set-Date-and-Time-in-Linux.png","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/www.podfeet.com\/blog\/wp-json\/wp\/v2\/posts\/24784","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.podfeet.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.podfeet.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.podfeet.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.podfeet.com\/blog\/wp-json\/wp\/v2\/comments?post=24784"}],"version-history":[{"count":4,"href":"https:\/\/www.podfeet.com\/blog\/wp-json\/wp\/v2\/posts\/24784\/revisions"}],"predecessor-version":[{"id":24788,"href":"https:\/\/www.podfeet.com\/blog\/wp-json\/wp\/v2\/posts\/24784\/revisions\/24788"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.podfeet.com\/blog\/wp-json\/wp\/v2\/media\/24780"}],"wp:attachment":[{"href":"https:\/\/www.podfeet.com\/blog\/wp-json\/wp\/v2\/media?parent=24784"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.podfeet.com\/blog\/wp-json\/wp\/v2\/categories?post=24784"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.podfeet.com\/blog\/wp-json\/wp\/v2\/tags?post=24784"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}