<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Yogaraj.S</title>
    <description>The latest articles on DEV Community by Yogaraj.S (@syogaraj).</description>
    <link>https://dev.to/syogaraj</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F832370%2Fdf3d1fd6-14ec-4b26-bb7a-f4fc9db6ee90.jpeg</url>
      <title>DEV Community: Yogaraj.S</title>
      <link>https://dev.to/syogaraj</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/syogaraj"/>
    <language>en</language>
    <item>
      <title>ScheduledThreadPoolExecutor in Python</title>
      <dc:creator>Yogaraj.S</dc:creator>
      <pubDate>Tue, 21 Jun 2022 04:24:36 +0000</pubDate>
      <link>https://dev.to/syogaraj/scheduledthreadpoolexecutor-in-python-62e</link>
      <guid>https://dev.to/syogaraj/scheduledthreadpoolexecutor-in-python-62e</guid>
      <description>&lt;p&gt;Similar to Java APIs provided in ScheduledThreadPoolExecutor, I’ve implemented the same in Python. Read further for a detailed explanation.&lt;/p&gt;

&lt;h3&gt;
  
  
  Thread Pool
&lt;/h3&gt;

&lt;p&gt;A thread pool is a design pattern in which multiple threads are maintained to perform the tasks submitted to it. Thread pools are used where multiple short-lived tasks need to be executed and also to increase the performance of the application.&lt;/p&gt;

&lt;h3&gt;
  
  
  Benefits of using a thread pool
&lt;/h3&gt;

&lt;p&gt;Instead of maintaining a thread pool, we can also create a thread for each task as needed. The main benefit of using a thread pool is that we avoid the cost of creating and destroying the threads. From &lt;a href="https://en.wikipedia.org/wiki/Thread_pool#:~:text=In%20computer%20programming%2C%20a%20thread,execution%20by%20the%20supervising%20program."&gt;Wikipedia&lt;/a&gt;,&lt;/p&gt;

&lt;blockquote&gt;
&lt;ol&gt;
&lt;li&gt;Creating too many threads wastes resources and costs time creating the unused threads.&lt;/li&gt;
&lt;li&gt;Destroying too many threads requires more time later when creating them again.&lt;/li&gt;
&lt;li&gt;Creating threads too slowly might result in poor client performance (long wait times).&lt;/li&gt;
&lt;li&gt;Destroying threads too slowly may starve other processes of resources.&lt;/li&gt;
&lt;/ol&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  ThreadPoolExecutor
&lt;/h2&gt;

&lt;p&gt;A ThreadPoolExecutor is a kind of supervisor program using which the tasks are submitted to the thread pool. Each programming language has some kind of concurrency package where the thread pool executors are provided. For example, concurrency is provided by the java.util.concurrent package, whereas in Python, it’s provided by the concurrent.futures package.&lt;/p&gt;

&lt;p&gt;When a task is submitted to a thread pool executor, a thread is assigned to execute the task and return the result. The number of threads in a thread pool is decided by the computational resource available in a computer.&lt;/p&gt;

&lt;h3&gt;
  
  
  ScheduledThreadPoolExecutor
&lt;/h3&gt;

&lt;p&gt;ScheduledThreadPoolExecutor is an executor service where the submitted tasks are executed with an initial delay and a periodic interval repeatedly.&lt;/p&gt;

&lt;p&gt;ScheduledThreadPoolExecutor comes in handy when we need to run a particular task repeatedly at a specific interval. Example: Checking the replication of a component every 10 minutes and triggering a mail if there is slowness or failure.&lt;/p&gt;

&lt;p&gt;The implementation of a ScheduledThreadPoolExecutor is simply done by extending the ThreadPoolExecutor and by maintaining a &lt;a href="https://syogaraj.medium.com/delayqueue-in-python-c61c1eaf4cd3"&gt;delay queue&lt;/a&gt; to provide the tasks based on the interval. Most languages provide the ScheduledThreadPoolExecutor as a part of their concurrency package. Java provides the &lt;a href="https://docs.oracle.com/javase/8/docs/api/java/util/concurrent/ScheduledThreadPoolExecutor.html"&gt;ScheduledThreadPoolExecutor&lt;/a&gt; in its concurrency package by default. Java provides two methods to execute the task periodically.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;&lt;a href="https://docs.oracle.com/javase/8/docs/api/java/util/concurrent/ScheduledThreadPoolExecutor.html#scheduleAtFixedRate-java.lang.Runnable-long-long-java.util.concurrent.TimeUnit-"&gt;scheduleAtFixedRate&lt;/a&gt; — Creates and executes a periodic action that becomes enabled first after the given initial delay, and subsequently with the given period; that is executions will commence after initialDelay then initialDelay+period, then initialDelay + 2 * period, and so on.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;a href="https://docs.oracle.com/javase/8/docs/api/java/util/concurrent/ScheduledThreadPoolExecutor.html#scheduleWithFixedDelay-java.lang.Runnable-long-long-java.util.concurrent.TimeUnit-"&gt;scheduleWithFixedDelay&lt;/a&gt; — Creates and executes a periodic action that becomes enabled first after the given initial delay, and subsequently with the given delay between the termination of one execution and the commencement of the next.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;h3&gt;
  
  
  ScheduledThreadPoolExecutor in Python
&lt;/h3&gt;

&lt;p&gt;Python provides two different modules, sched and concurrent.futures and it is in the hands of the user to implement the ScheduledThreadPoolExecutor. Though there are &lt;a href="https://github.com/dbader/schedule"&gt;packages&lt;/a&gt; that provide scheduling in Python, there is no implementation as close to the one provided by Java. So, I’ve implemented the same with the APIs to closely resemble the ones in Java.&lt;/p&gt;

&lt;p&gt;The Python package for ScheduledThreadPoolExecutor is published in &lt;a href="https://pypi.org/project/scheduled-thread-pool-executor/"&gt;PyPI&lt;/a&gt; as well. You can install it using pip install scheduled-thread-pool-executor . It provides the three main methods schedule, schedule_at_fixed_rate, schedule_at_fixed_delay similar to the one in Java. I believe this will be useful for many who are in search of such implementations in the future.&lt;br&gt;
&lt;/p&gt;
&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;
&lt;br&gt;
&lt;div class="ltag-github-readme-tag"&gt;
  &lt;div class="readme-overview"&gt;
    &lt;h2&gt;
      &lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--566lAguM--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev.to/assets/github-logo-5a155e1f9a670af7944dd5e12375bc76ed542ea80224905ecaf878b9157cdefc.svg" alt="GitHub logo"&gt;
      &lt;a href="https://github.com/syogaraj"&gt;
        syogaraj
      &lt;/a&gt; / &lt;a href="https://github.com/syogaraj/scheduled_thread_pool_executor"&gt;
        scheduled_thread_pool_executor
      &lt;/a&gt;
    &lt;/h2&gt;
    &lt;h3&gt;
      Scheduled Thread Pool Executor implementation in python
    &lt;/h3&gt;
  &lt;/div&gt;
  &lt;div class="ltag-github-body"&gt;
    
&lt;div id="readme" class="rst"&gt;
&lt;a href="https://pyscaffold.org/" rel="nofollow"&gt;&lt;img alt="Project generated with PyScaffold" src="https://camo.githubusercontent.com/4094d114714010902ee0d8554d4b0f4e737e8377180eb20e2245a02c0df1e695/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f2d507953636166666f6c642d3030354341303f6c6f676f3d707973636166666f6c64"&gt;&lt;/a&gt;
&lt;a href="https://pypi.org/project/scheduled_thread_pool_executor/" rel="nofollow"&gt;&lt;img alt="PyPI-Server" src="https://camo.githubusercontent.com/65a31b409e1fd3ccdb438d114cab33a174b718a8cb904ae9244b21eaca6f0a91/68747470733a2f2f696d672e736869656c64732e696f2f707970692f762f7363686564756c65645f7468726561645f706f6f6c5f6578656375746f722e737667"&gt;
&lt;/a&gt;

&lt;h2&gt;
Scheduled Thread Pool Executor&lt;/h2&gt;
&lt;blockquote&gt;
Scheduled Thread Pool Executor implementation in python&lt;/blockquote&gt;
&lt;p&gt;Makes use of delayed queue implementation to submit tasks to the thread pool.&lt;/p&gt;

&lt;h3&gt;
Usage&lt;/h3&gt;
&lt;pre&gt;from scheduled_thread_pool_executor import ScheduledThreadPoolExecutor
scheduled_executor = ScheduledThreadPoolExecutor(max_workers=5)
scheduled_executor.schedule(task, 0)  # equals to schedule once, where task is a callable
scheduled_executor.schedule_at_fixed_rate(task, 0, 5)  # schedule immediately and run periodically for every 5 secs
scheduled_executor.schedule_at_fixed_delay(task, 5, 10)  # schedule after 5secs (initial delay) and run periodically for every 10secs
&lt;/pre&gt;

&lt;h4&gt;
Note&lt;/h4&gt;
&lt;p&gt;This project has been set up using PyScaffold 4.1.1. For details and usage
information on PyScaffold see &lt;a href="https://pyscaffold.org/" rel="nofollow"&gt;https://pyscaffold.org/&lt;/a&gt;.&lt;/p&gt;

&lt;/div&gt;



&lt;/div&gt;
&lt;br&gt;
  &lt;div class="gh-btn-container"&gt;&lt;a class="gh-btn" href="https://github.com/syogaraj/scheduled_thread_pool_executor"&gt;View on GitHub&lt;/a&gt;&lt;/div&gt;
&lt;br&gt;
&lt;/div&gt;
&lt;br&gt;


</description>
      <category>python</category>
      <category>threadpool</category>
      <category>scheduler</category>
      <category>opensource</category>
    </item>
    <item>
      <title>Debugging timezone issue in Java [Linux]</title>
      <dc:creator>Yogaraj.S</dc:creator>
      <pubDate>Fri, 18 Mar 2022 08:28:40 +0000</pubDate>
      <link>https://dev.to/syogaraj/debugging-timezone-issue-in-java-linux-3de0</link>
      <guid>https://dev.to/syogaraj/debugging-timezone-issue-in-java-linux-3de0</guid>
      <description>&lt;p&gt;Have you ever noticed that in some cases, Java will not take the time zone which is configured in a server? This has serious impacts if the server handles scheduling tasks and runs them periodically.&lt;/p&gt;

&lt;p&gt;Before diving deep down, let’s understand what is a time zone?&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Time Zones&lt;/strong&gt; are geographical world globe division of 15 degree each, starting at Greenwich, England. It’s a uniform standard time for legal, commercial and social purposes.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h3&gt;
  
  
  Issue: Timezone difference between the system and java
&lt;/h3&gt;

&lt;p&gt;While executing the below code, It will print the system time along with the timezone. I’ve added the output of the date command as well.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--Fd14WGLD--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/5008/1%2AmKC8gBc2UhmZByRGt3Gujg.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--Fd14WGLD--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/5008/1%2AmKC8gBc2UhmZByRGt3Gujg.png" alt="Example code" width="880" height="292"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--2w0LHJJl--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/3000/1%2AGTe_7g1JfQjtawUbTNDT7w.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--2w0LHJJl--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/3000/1%2AGTe_7g1JfQjtawUbTNDT7w.png" alt="Output" width="880" height="366"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Even though the system timezone is configured as PST, java prints the timezone as GMT.&lt;/p&gt;

&lt;h3&gt;
  
  
  Quick fix:
&lt;/h3&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;Set user.timezonein the command line as &lt;code&gt;-Duser.timezone=America/Los_Angeles&lt;/code&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Set the environmental variable &lt;code&gt;TZ&lt;/code&gt; to provide the timezone.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;The above solutions will work, but to understand and fix it once for all, read further for the actual fix. (All required source code links are embedded in the method names)&lt;/p&gt;

&lt;h3&gt;
  
  
  Detailed explanation on why do we get this timezone difference?
&lt;/h3&gt;

&lt;p&gt;In the example java code, we call &lt;code&gt;System.out.println&lt;/code&gt; of &lt;code&gt;new Date()&lt;/code&gt;. We know that java calls an object’s &lt;code&gt;.toString()&lt;/code&gt; method internally to provide the string representation of an object. Let’s take a look at the &lt;code&gt;toString&lt;/code&gt; method of the &lt;a href="https://hg.openjdk.java.net/jdk8/jdk8/jdk/file/tip/src/share/classes/java/util/Date.java#l999"&gt;Date class&lt;/a&gt; in java.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--gNESPAS6--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/7048/1%2A4Q8Yodid7FJeAlcG5aAKSQ.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--gNESPAS6--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/7048/1%2A4Q8Yodid7FJeAlcG5aAKSQ.png" alt="" width="880" height="509"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The timezone on &lt;code&gt;Line:16&lt;/code&gt; is set by calling the &lt;code&gt;date.getZone()&lt;/code&gt;. The variable &lt;code&gt;date&lt;/code&gt; is initialized by calling the &lt;a href="https://hg.openjdk.java.net/jdk8/jdk8/jdk/file/tip/src/share/classes/java/util/Date.java#l1193"&gt;normalize&lt;/a&gt;() method.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--tk9Uwu_W--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/6232/1%2ALj9TkYNhaQDDeq_z5laRqw.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--tk9Uwu_W--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/6232/1%2ALj9TkYNhaQDDeq_z5laRqw.png" alt="" width="880" height="576"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The timezone in &lt;code&gt;normalize&lt;/code&gt; is set by calling the&lt;a href="https://hg.openjdk.java.net/jdk8u/jdk8u/jdk/file/f940e7a48b72/src/share/classes/java/util/TimeZone.java#l632"&gt;TimeZone.getDefaultRef&lt;/a&gt;() in &lt;code&gt;TimeZone.java&lt;/code&gt; which then calls &lt;a href="https://hg.openjdk.java.net/jdk8u/jdk8u/jdk/file/f940e7a48b72/src/share/classes/java/util/TimeZone.java#l643"&gt;setDefaultZone&lt;/a&gt;()to get the system timezone.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--Fpy9Gjcs--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/4816/1%2A0gCF3uPscMPqzi8Am08NCA.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--Fpy9Gjcs--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/4816/1%2A0gCF3uPscMPqzi8Am08NCA.png" alt="" width="880" height="474"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--UzntTy15--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/5016/1%2Apx4rBDwNG7gydtASZcH0EQ.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--UzntTy15--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/5016/1%2Apx4rBDwNG7gydtASZcH0EQ.png" alt="" width="880" height="1368"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Now, let's break up and understand what the &lt;code&gt;setDefaultZone()&lt;/code&gt; does.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;It checks for the zone by checking the &lt;code&gt;user.timezone&lt;/code&gt; property (which we didn’t set via the command line).&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;If the &lt;code&gt;user.timezone&lt;/code&gt; property is &lt;code&gt;null&lt;/code&gt;, it gets the &lt;code&gt;java.home&lt;/code&gt; property and calls the &lt;code&gt;getSystemTimeZoneID()&lt;/code&gt; which is a &lt;strong&gt;native&lt;/strong&gt; method.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Any &lt;code&gt;NULL&lt;/code&gt; values will set the timezone to default GMT. (So, this is why the timezone is set to GMT!)&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--1hJklNTM--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/7720/1%2A0T_0-rfb7R_NZF1K6fMViQ.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--1hJklNTM--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/7720/1%2A0T_0-rfb7R_NZF1K6fMViQ.png" alt="Java Native method to get the system timezone" width="880" height="702"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Ignoring the unwanted details, let’s focus on what the &lt;a href="https://hg.openjdk.java.net/jdk/jdk/file/f91999057a5a/src/java.base/unix/native/libjava/TimeZone_md.c#l769"&gt;findJavaTZ_md&lt;/a&gt;() does.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--ezA_e7eu--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/3400/1%2A07Xqs4I851c3uaUxVFoavA.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--ezA_e7eu--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/3400/1%2A07Xqs4I851c3uaUxVFoavA.png" alt="" width="880" height="708"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Now, we can understand why the quick search solutions mentioned to set values for &lt;code&gt;user.timezone&lt;/code&gt; and &lt;code&gt;TZ&lt;/code&gt;! In our case, we didn’t set the TZ environmental variable which means &lt;code&gt;TZ = NULL&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;It calls &lt;a href="https://hg.openjdk.java.net/jdk/jdk/file/f91999057a5a/src/java.base/unix/native/libjava/TimeZone_md.c#l221"&gt;getPlatformTimeZoneID&lt;/a&gt;() if TZ is &lt;code&gt;NULL&lt;/code&gt;. (We’re about to reach a conclusion in the next step! Hang on..)&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--zJm0c1hy--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/5424/1%2AFP_4coRTJdsNODPYQbaVSA.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--zJm0c1hy--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/5424/1%2AFP_4coRTJdsNODPYQbaVSA.png" alt="" width="880" height="999"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;As the comment says, it will check for &lt;code&gt;/etc/timezone&lt;/code&gt; file which contains the system timezone information. I checked in my Ubuntu machine for the file and it's straightforward.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--ErM0mVeV--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/3776/1%2AgvC-WyXhr5SezGr9jgGSqg.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--ErM0mVeV--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/3776/1%2AgvC-WyXhr5SezGr9jgGSqg.png" alt="" width="880" height="222"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;If the file is &lt;strong&gt;not&lt;/strong&gt; present, it checks &lt;code&gt;/etc/localtime&lt;/code&gt; to obtain the timezone.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--FI_HDwXu--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/6656/1%2AOmJgJ5utuwb8vpZEIqwPuQ.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--FI_HDwXu--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/6656/1%2AOmJgJ5utuwb8vpZEIqwPuQ.png" alt="" width="880" height="755"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Here’s what happens in the above code.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;Get the file status of &lt;code&gt;/etc/localtime&lt;/code&gt; using &lt;a href="https://linux.die.net/man/2/lstat64"&gt;lstat&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Check whether it's a symlink and read the symlink to obtain the timezone.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;For &lt;code&gt;lstat&lt;/code&gt; to work as expected, it's required to have the executable(&lt;code&gt;x&lt;/code&gt;) permission on the file (as stated in the manpage of lstat). In my case, the /etc/localtime file doesn’t have the executable permission set. Hence, it returns NULL taking the default timezone of GMT.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--kolSX0bq--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/4304/1%2As87IxJH00l5WPJCUrRZEsQ.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--kolSX0bq--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/4304/1%2As87IxJH00l5WPJCUrRZEsQ.png" alt="/etc/localtime without executable permission" width="880" height="188"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Obtaining Timezone via symlink:
&lt;/h3&gt;

&lt;p&gt;If the file has the required permission set, the output will be like the below one. After resolving the symlink, the zone info will be taken using the &lt;a href="https://hg.openjdk.java.net/jdk/jdk/file/f91999057a5a/src/java.base/unix/native/libjava/TimeZone_md.c#l81"&gt;getZoneName&lt;/a&gt;method.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--FwN27S4T--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/7272/1%2AZTn8aUKKFeT-b00YiMpF0w.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--FwN27S4T--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/7272/1%2AZTn8aUKKFeT-b00YiMpF0w.png" alt="/etc/localtime with executable permission" width="880" height="111"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--O3fcefat--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/5440/1%2ASv0ywk7Jc6GCyNMub_yifw.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--O3fcefat--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/5440/1%2ASv0ywk7Jc6GCyNMub_yifw.png" alt="getZoneName — method" width="880" height="467"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;If the &lt;code&gt;/etc/localtimefile&lt;/code&gt; is not a symlink, the &lt;code&gt;getPlatformTimeZoneID&lt;/code&gt; method will try to open and read the file like /etc/timezone. As the last fallback, it will recursively iterate the &lt;code&gt;/usr/share/zoneinfo&lt;/code&gt; directory to find the timezone. When none of them works out, the timezone is set to GMT- the default!&lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;Actual Fix:&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;For my case, I just had to create a symlink of &lt;code&gt;/etc/localtime&lt;/code&gt; from &lt;code&gt;/usr/share/zoneinfo/America/Los_Angeles&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--zAALINo9--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/7080/1%2AtYCITEpgRVvLtjb6lKI28g.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--zAALINo9--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/7080/1%2AtYCITEpgRVvLtjb6lKI28g.png" alt="Symlink creation for /etc/localtime" width="880" height="136"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;After setting the symlink, running the &lt;code&gt;DateExampleprogram&lt;/code&gt; yields the expected result.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--bzKpbDl6--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/3560/1%2ADhCWfN7Lz7279-lrxeNVaQ.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--bzKpbDl6--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/3560/1%2ADhCWfN7Lz7279-lrxeNVaQ.png" alt="" width="880" height="265"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Thank you for reading!&lt;/p&gt;

</description>
      <category>java</category>
      <category>linux</category>
      <category>timezone</category>
      <category>debugging</category>
    </item>
  </channel>
</rss>
