DateTime.java

  1. package org.drip.analytics.date;

  2. /*
  3.  * -*- mode: java; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
  4.  */

  5. /*!
  6.  * Copyright (C) 2020 Lakshmi Krishnamurthy
  7.  * Copyright (C) 2019 Lakshmi Krishnamurthy
  8.  * Copyright (C) 2018 Lakshmi Krishnamurthy
  9.  * Copyright (C) 2017 Lakshmi Krishnamurthy
  10.  * Copyright (C) 2016 Lakshmi Krishnamurthy
  11.  * Copyright (C) 2015 Lakshmi Krishnamurthy
  12.  * Copyright (C) 2014 Lakshmi Krishnamurthy
  13.  * Copyright (C) 2013 Lakshmi Krishnamurthy
  14.  * Copyright (C) 2012 Lakshmi Krishnamurthy
  15.  * Copyright (C) 2011 Lakshmi Krishnamurthy
  16.  *
  17.  *  This file is part of DROP, an open-source library targeting analytics/risk, transaction cost analytics,
  18.  *      asset liability management analytics, capital, exposure, and margin analytics, valuation adjustment
  19.  *      analytics, and portfolio construction analytics within and across fixed income, credit, commodity,
  20.  *      equity, FX, and structured products. It also includes auxiliary libraries for algorithm support,
  21.  *      numerical analysis, numerical optimization, spline builder, model validation, statistical learning,
  22.  *      and computational support.
  23.  *  
  24.  *      https://lakshmidrip.github.io/DROP/
  25.  *  
  26.  *  DROP is composed of three modules:
  27.  *  
  28.  *  - DROP Product Core - https://lakshmidrip.github.io/DROP-Product-Core/
  29.  *  - DROP Portfolio Core - https://lakshmidrip.github.io/DROP-Portfolio-Core/
  30.  *  - DROP Computational Core - https://lakshmidrip.github.io/DROP-Computational-Core/
  31.  *
  32.  *  DROP Product Core implements libraries for the following:
  33.  *  - Fixed Income Analytics
  34.  *  - Loan Analytics
  35.  *  - Transaction Cost Analytics
  36.  *
  37.  *  DROP Portfolio Core implements libraries for the following:
  38.  *  - Asset Allocation Analytics
  39.  *  - Asset Liability Management Analytics
  40.  *  - Capital Estimation Analytics
  41.  *  - Exposure Analytics
  42.  *  - Margin Analytics
  43.  *  - XVA Analytics
  44.  *
  45.  *  DROP Computational Core implements libraries for the following:
  46.  *  - Algorithm Support
  47.  *  - Computation Support
  48.  *  - Function Analysis
  49.  *  - Model Validation
  50.  *  - Numerical Analysis
  51.  *  - Numerical Optimizer
  52.  *  - Spline Builder
  53.  *  - Statistical Learning
  54.  *
  55.  *  Documentation for DROP is Spread Over:
  56.  *
  57.  *  - Main                     => https://lakshmidrip.github.io/DROP/
  58.  *  - Wiki                     => https://github.com/lakshmiDRIP/DROP/wiki
  59.  *  - GitHub                   => https://github.com/lakshmiDRIP/DROP
  60.  *  - Repo Layout Taxonomy     => https://github.com/lakshmiDRIP/DROP/blob/master/Taxonomy.md
  61.  *  - Javadoc                  => https://lakshmidrip.github.io/DROP/Javadoc/index.html
  62.  *  - Technical Specifications => https://github.com/lakshmiDRIP/DROP/tree/master/Docs/Internal
  63.  *  - Release Versions         => https://lakshmidrip.github.io/DROP/version.html
  64.  *  - Community Credits        => https://lakshmidrip.github.io/DROP/credits.html
  65.  *  - Issues Catalog           => https://github.com/lakshmiDRIP/DROP/issues
  66.  *  - JUnit                    => https://lakshmidrip.github.io/DROP/junit/index.html
  67.  *  - Jacoco                   => https://lakshmidrip.github.io/DROP/jacoco/index.html
  68.  *
  69.  *  Licensed under the Apache License, Version 2.0 (the "License");
  70.  *      you may not use this file except in compliance with the License.
  71.  *  
  72.  *  You may obtain a copy of the License at
  73.  *      http://www.apache.org/licenses/LICENSE-2.0
  74.  *  
  75.  *  Unless required by applicable law or agreed to in writing, software
  76.  *      distributed under the License is distributed on an "AS IS" BASIS,
  77.  *      WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  78.  *  
  79.  *  See the License for the specific language governing permissions and
  80.  *      limitations under the License.
  81.  */

  82. /**
  83.  * <i>DateTime</i> provides the representation of the instantiation-time date and time objects. It provides
  84.  * the following functionality:
  85.  *
  86.  *  <br><br>
  87.  *  <ul>
  88.  *      <li>
  89.  *          Instantiation-time and Explicit Date/Time Construction
  90.  *      </li>
  91.  *      <li>
  92.  *          Retrieval of Date/Time Fields
  93.  *      </li>
  94.  *      <li>
  95.  *          Serialization/De-serialization to and from Byte Arrays
  96.  *      </li>
  97.  *  </ul>
  98.  *
  99.  * <br><br>
  100.  *
  101.  * The References are:
  102.  *  <ul>
  103.  *      <li>
  104.  *          Fliegel, H. F., and T. C. van Flandern (1968): A Machine Algorithm for Processing Calendar Dates
  105.  *              <i>Communications of the ACM</i> <b>11</b> 657
  106.  *      </li>
  107.  *      <li>
  108.  *          Fenton, D. (2001): Julian to Calendar Date Conversion
  109.  *              http://mathforum.org/library/drmath/view/51907.html
  110.  *      </li>
  111.  *  </ul>
  112.  *
  113.  *  <br><br>
  114.  *  <ul>
  115.  *      <li><b>Module </b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/ProductCore.md">Product Core Module</a></li>
  116.  *      <li><b>Library</b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/FixedIncomeAnalyticsLibrary.md">Fixed Income Analytics</a></li>
  117.  *      <li><b>Project</b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/src/main/java/org/drip/analytics/README.md">Date, Cash Flow, and Cash Flow Period Measure Generation Utilities</a></li>
  118.  *      <li><b>Package</b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/src/main/java/org/drip/analytics/date/README.md">Date and Time Creation, Manipulation, and Usage</a></li>
  119.  *  </ul>
  120.  *
  121.  * @author Lakshmi Krishnamurthy
  122.  */

  123. public class DateTime {
  124.     private long _lTime = 0L;
  125.     private double _dblDate = java.lang.Double.NaN;

  126.     /**
  127.      * Default constructor initializes the time and date to the current time and current date.
  128.      *
  129.      * @throws java.lang.Exception Thrown if the DateTime Instance cannot be created
  130.      */

  131.     public DateTime()
  132.         throws java.lang.Exception
  133.     {
  134.         _lTime = System.nanoTime();

  135.         java.util.Date dtNow = new java.util.Date();

  136.         _dblDate = org.drip.analytics.date.DateUtil.ToJulian (org.drip.analytics.date.DateUtil.Year (dtNow),
  137.             org.drip.analytics.date.DateUtil.Month (dtNow), org.drip.analytics.date.DateUtil.Day (dtNow));
  138.     }

  139.     /**
  140.      * Constructs DateTime from separate date and time inputs
  141.      *
  142.      * @param dblDate Date
  143.      * @param lTime Time
  144.      *
  145.      * @throws java.lang.Exception Thrown on Invalid Inputs
  146.      */

  147.     public DateTime (
  148.         final double dblDate,
  149.         final long lTime)
  150.         throws java.lang.Exception
  151.     {
  152.         if (!org.drip.numerical.common.NumberUtil.IsValid (dblDate))
  153.             throw new java.lang.Exception ("DateTime ctr: Invalid Inputs!");

  154.         _lTime = lTime;
  155.         _dblDate = dblDate;
  156.     }

  157.     /**
  158.      * Retrieve the Date
  159.      *
  160.      * @return date
  161.      */

  162.     public double date()
  163.     {
  164.         return _dblDate;
  165.     }

  166.     /**
  167.      * Retrieve the time
  168.      *
  169.      * @return time
  170.      */

  171.     public long time()
  172.     {
  173.         return _lTime;
  174.     }
  175. }