001/*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements.  See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License.  You may obtain a copy of the License at
008 *
009 *      http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017package org.apache.juneau.commons.time;
018
019import java.time.*;
020
021/**
022 * Provides access to system time and timezone information.
023 *
024 * <p>
025 * This class abstracts time-related operations to allow for easier testing and customization.
026 * By default, it delegates to the system's time and timezone, but can be extended or replaced
027 * for testing purposes (e.g., using a custom implementation that provides fixed times).
028 *
029 * <h5 class='section'>Usage:</h5>
030 * <p class='bjava'>
031 *    <jc>// Use the default instance</jc>
032 *    ZoneId <jv>zone</jv> = TimeProvider.<jsf>INSTANCE</jsf>.<jsm>getSystemDefaultZoneId</jsm>();
033 *    ZonedDateTime <jv>now</jv> = TimeProvider.<jsf>INSTANCE</jsf>.<jsm>now</jsm>();
034 *
035 *    <jc>// Or create a custom implementation for testing</jc>
036 *    TimeProvider <jv>testProvider</jv> = <jk>new</jk> FakeTimeProvider();
037 *    ZonedDateTime <jv>fixedTime</jv> = <jv>testProvider</jv>.<jsm>now</jsm>();
038 * </p>
039 *
040 * <h5 class='section'>See Also:</h5>
041 * <ul>
042 *    <li class='jm'>{@link GranularZonedDateTime}
043 * </ul>
044 */
045public class TimeProvider {
046
047   /**
048    * The default instance that uses the system's time and timezone.
049    */
050   public static final TimeProvider INSTANCE = new TimeProvider();
051
052   /**
053    * Returns the system default timezone.
054    *
055    * @return The system default {@link ZoneId}.
056    */
057   public ZoneId getSystemDefaultZoneId() {
058      return ZoneId.systemDefault();
059   }
060
061   /**
062    * Returns the current date and time in the system default timezone.
063    *
064    * @return The current {@link ZonedDateTime} in the system default timezone.
065    */
066   public ZonedDateTime now() {
067      return ZonedDateTime.now();
068   }
069
070   /**
071    * Returns the current date and time in the specified timezone.
072    *
073    * @param zoneId The timezone to use. Must not be <jk>null</jk>.
074    * @return The current {@link ZonedDateTime} in the specified timezone.
075    */
076   public ZonedDateTime now(ZoneId zoneId) {
077      return ZonedDateTime.now(zoneId);
078   }
079}