<!DOCTYPE html>
<!-- Generated by pkgdown: do not edit by hand --><html lang="en"><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8"><meta charset="utf-8"><meta http-equiv="X-UA-Compatible" content="IE=edge"><meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no"><title>Convert a Numeric Time to a POSIXct Time — numberAsPOSIXct • oce</title><script src="../deps/jquery-3.6.0/jquery-3.6.0.min.js"></script><meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no"><link href="../deps/bootstrap-5.3.1/bootstrap.min.css" rel="stylesheet"><script src="../deps/bootstrap-5.3.1/bootstrap.bundle.min.js"></script><link href="../deps/font-awesome-6.4.2/css/all.min.css" rel="stylesheet"><link href="../deps/font-awesome-6.4.2/css/v4-shims.min.css" rel="stylesheet"><script src="../deps/headroom-0.11.0/headroom.min.js"></script><script src="../deps/headroom-0.11.0/jQuery.headroom.min.js"></script><script src="../deps/bootstrap-toc-1.0.1/bootstrap-toc.min.js"></script><script src="../deps/clipboard.js-2.0.11/clipboard.min.js"></script><script src="../deps/search-1.0.0/autocomplete.jquery.min.js"></script><script src="../deps/search-1.0.0/fuse.min.js"></script><script src="../deps/search-1.0.0/mark.min.js"></script><!-- pkgdown --><script src="../pkgdown.js"></script><meta property="og:title" content="Convert a Numeric Time to a POSIXct Time — numberAsPOSIXct"><meta name="description" content="This converts numerical values into POSIXct times.  There are many
schemes for doing this, with the type parameter being used
to select between them.  See &amp;#8220;Details&amp;#8221; for a listing, broken
down by scheme."><meta property="og:description" content="This converts numerical values into POSIXct times.  There are many
schemes for doing this, with the type parameter being used
to select between them.  See &amp;#8220;Details&amp;#8221; for a listing, broken
down by scheme."></head><body>
    <a href="#main" class="visually-hidden-focusable">Skip to contents</a>


    <nav class="navbar navbar-expand-lg fixed-top bg-light" data-bs-theme="light" aria-label="Site navigation"><div class="container">

    <a class="navbar-brand me-2" href="../index.html">oce</a>

    <small class="nav-text text-muted me-auto" data-bs-toggle="tooltip" data-bs-placement="bottom" title="">1.8-4</small>


    <button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#navbar" aria-controls="navbar" aria-expanded="false" aria-label="Toggle navigation">
      <span class="navbar-toggler-icon"></span>
    </button>

    <div id="navbar" class="collapse navbar-collapse ms-3">
      <ul class="navbar-nav me-auto"><li class="active nav-item"><a class="nav-link" href="../reference/index.html">Reference</a></li>
<li class="nav-item dropdown">
  <button class="nav-link dropdown-toggle" type="button" id="dropdown-articles" data-bs-toggle="dropdown" aria-expanded="false" aria-haspopup="true">Articles</button>
  <ul class="dropdown-menu" aria-labelledby="dropdown-articles"><li><a class="dropdown-item" href="../articles/A_oce.html">1. Introduction to Oce</a></li>
    <li><a class="dropdown-item" href="../articles/B_ctd.html">2. Analysis of CTD data</a></li>
    <li><a class="dropdown-item" href="../articles/C_adp.html">3. Analysis of acoustic-Doppler data</a></li>
    <li><a class="dropdown-item" href="../articles/D_map_projections.html">4. Using map projections</a></li>
    <li><a class="dropdown-item" href="../articles/E_flags.html">5. Dealing with data-quality flags</a></li>
    <li><a class="dropdown-item" href="../articles/F_subclassing.html">6. Subclassing oce objects</a></li>
    <li><a class="dropdown-item" href="../articles/G_altering_defaults.html">7. Altering oce Defaults</a></li>
    <li><a class="dropdown-item" href="../articles/H_tides.html">8. Tidal Analysis</a></li>
  </ul></li>
<li class="nav-item"><a class="nav-link" href="../news/index.html">Changelog</a></li>
      </ul><ul class="navbar-nav"><li class="nav-item"><form class="form-inline" role="search">
 <input class="form-control" type="search" name="search-input" id="search-input" autocomplete="off" aria-label="Search site" placeholder="Search for" data-search-index="../search.json"></form></li>
<li class="nav-item"><a class="external-link nav-link" href="https://github.com/dankelley/oce/" aria-label="GitHub"><span class="fa fab fa-github fa-lg"></span></a></li>
      </ul></div>


  </div>
</nav><div class="container template-reference-topic">
<div class="row">
  <main id="main" class="col-md-9"><div class="page-header">

      <h1>Convert a Numeric Time to a POSIXct Time</h1>
      <small class="dont-index">Source: <a href="https://github.com/dankelley/oce/blob/HEAD/R/oce.R" class="external-link"><code>R/oce.R</code></a></small>
      <div class="d-none name"><code>numberAsPOSIXct.Rd</code></div>
    </div>

    <div class="ref-description section level2">
    <p>This converts numerical values into POSIXct times.  There are many
schemes for doing this, with the <code>type</code> parameter being used
to select between them.  See “Details” for a listing, broken
down by scheme.</p>
    </div>

    <div class="section level2">
    <h2 id="ref-usage">Usage<a class="anchor" aria-label="anchor" href="#ref-usage"></a></h2>
    <div class="sourceCode"><pre class="sourceCode r"><code><span><span class="fu">numberAsPOSIXct</span><span class="op">(</span><span class="va">t</span>, type <span class="op">=</span> <span class="st">"unix"</span>, tz <span class="op">=</span> <span class="st">"UTC"</span>, leap <span class="op">=</span> <span class="cn">TRUE</span><span class="op">)</span></span></code></pre></div>
    </div>

    <div class="section level2">
    <h2 id="arguments">Arguments<a class="anchor" aria-label="anchor" href="#arguments"></a></h2>


<dl><dt id="arg-t">t<a class="anchor" aria-label="anchor" href="#arg-t"></a></dt>
<dd><p>an integer corresponding to a time, in a way that depends on
<code>type</code>.</p></dd>


<dt id="arg-type">type<a class="anchor" aria-label="anchor" href="#arg-type"></a></dt>
<dd><p>character value indicating the time type.  The permitted values
are <code>"argo"</code>, <code>"epic"</code>, <code>"excel"</code>, <code>"gps"</code>, <code>"matlab"</code>, <code>"ncep1"</code>, <code>"ncep2"</code>,
<code>"sas"</code>, <code>"spss"</code>, <code>"unix"</code>, and <code>"yearday"</code>, the first of these being
the default.</p></dd>


<dt id="arg-tz">tz<a class="anchor" aria-label="anchor" href="#arg-tz"></a></dt>
<dd><p>a string indicating the time zone, by default <code>"UTC"</code>.</p></dd>


<dt id="arg-leap">leap<a class="anchor" aria-label="anchor" href="#arg-leap"></a></dt>
<dd><p>a logical value, TRUE by default, that applies only
if <code>type</code> is <code>"gps"</code>.  If <code>leap</code> is TRUE, then the built-in
dataset named <a href="https://rdrr.io/r/base/DateTimeClasses.html" class="external-link">.leap.seconds</a> is consulted to find
of the number of leap seconds between 1980 (when the
GPS program started) and the time computed from the other
parameters, and the return value is decreased accordingly
(see Example 3).</p></dd>

</dl></div>
    <div class="section level2">
    <h2 id="value">Value<a class="anchor" aria-label="anchor" href="#value"></a></h2>
    <p>A <code><a href="https://rdrr.io/r/base/DateTimeClasses.html" class="external-link">POSIXct()</a></code> time vector.</p>
    </div>
    <div class="section level2">
    <h2 id="details">Details<a class="anchor" aria-label="anchor" href="#details"></a></h2>
    <p>The possible choices for <code>type</code> are as listed below.</p><ul><li><p><code>"unix"</code> handles Unix times, measured in seconds since the start
of the year 1970.</p></li>
<li><p><code>"matlab"</code> handles Matlab times, measured in days since what
MathWorks (reference 1) calls “January 0, 0000” (i.e.  <code>ISOdatetime(0, 1, 1, 0,
0, 0)</code> in R notation).</p></li>
<li><p><code>"gps"</code> handles the Global Positioning System convention. The
scheme is complicated, owing to hardware limitations of
GPS satellites.  As illustrated in Example 3, <code>t</code> may be
a matrix with either 2 or 3 columns. In the 2-column format, the
first column holds the number of weeks after 1999-08-22, modulo
1024 (approximately 19.6 years), and the second column (here
and also in the 3-column format) holds the number of seconds
in the referenced week, with leap seconds being handled with
the <code>leap</code> parameter. The modulo calculation is required
because GPS satellites dedicate only 10 bits to the
week number. The resultant ambiguity (e.g. a rollover in
2019-04-07) is addressed in the 3-column format, in which the
last column holds the number of 1024-week rollover events
since 1980-01-06. Users should set this column to 0 for times
prior to 1999-08-22, to 1 for later times prior
to 2019-04-07, to 2 for later times prior to 2038-11-21,
etc. However, there will be an exception to this rule,
when satellites start dedicating 12 bits to the week value.
For such data, the third column will need to be 0 for all times
prior to 2137-01-06.</p></li>
<li><p><code>"argo"</code> handles Argo times, measured in days since the start of
the year 1900.</p></li>
<li><p><code>"excel"</code> handles Excel times, measured in days since the start of
the year 1900. (Note that excel incorrectly regards 1900 as a leap year,
so 1 day is subtracted from <code>t</code> unless the time is less than or equal
to 1900 Feb 28.  Note that NA is returned for the day 60, which is
what excel codes for "Feb 29, 1900", the non-existing day that excel
accepts.</p></li>
<li><p><code>"ncep1"</code> handles NCEP times, measured in hours since the start
of the year 1800.</p></li>
<li><p><code>"ncep2"</code> handles NCEP times, measured in days since the start of
the year 1. (Note that, for reasons that are unknown at this time, a simple
R expression of this definition is out by two days compared with the UDUNITS
library, which is used by NCEP. Therefore, a two-day offset is applied. See
references 2 and 3.)</p></li>
<li><p><code>"sas"</code> handles SAS times, indicated by <code>type="sas"</code>, have
origin at the start of 1960.</p></li>
<li><p><code>"spss"</code> handles SPSS times, in seconds after 1582-10-14.</p></li>
<li><p><code>"yearday"</code> handles a convention in which <code>t</code> is a
two-column matrix, with the first column being the year, and the second the
yearday (starting at 1 for the first second of January 1, to match the
convention used by Sea-Bird CTD software).</p></li>
<li><p><code>"epic"</code> handles a convention used in the EPIC software library,
from the Pacific Marine Environmental Laboratory, in which <code>t</code> is a
two-column matrix, with the first column being the julian Day (as defined in
<code><a href="julianDay.html">julianDay()</a></code>, for example), and with the second column being the
millisecond within that day. See reference 4.</p></li>
<li><p><code>"vms"</code> handles a convention used in the VMS operating system and
for Modified Julian Day, in which <code>t</code> is the number of seconds
past 1859-11-17T00:00:00 UTC. See reference 5.</p></li>
</ul></div>
    <div class="section level2">
    <h2 id="references">References<a class="anchor" aria-label="anchor" href="#references"></a></h2>

<ol><li><p>Matlab times:
<code>https://www.mathworks.com/help/matlab/ref/datenum.html</code></p></li>
<li><p>NCEP times: <code>https://psl.noaa.gov/data/gridded/faq.html</code></p></li>
<li><p>Problem with NCEP times:
<code>https://github.com/dankelley/oce/issues/738</code></p></li>
<li><p>EPIC times: software and manuals at <code>https://www.pmel.noaa.gov/epic/download/index.html#epslib</code>;
see also Denbo, Donald W., and Nancy N. Soreide. “EPIC.” Oceanography 9 (1996).
<a href="https://doi.org/10.5670/oceanog.1996.10" class="external-link">doi:10.5670/oceanog.1996.10</a></p></li>
<li><p>VMS times: https://en.wikipedia.org/wiki/Epoch_(computing)</p></li>
<li><p>GPS times: https://www.labsat.co.uk/index.php/en/gps-time-calculator</p></li>
</ol></div>
    <div class="section level2">
    <h2 id="see-also">See also<a class="anchor" aria-label="anchor" href="#see-also"></a></h2>
    <div class="dont-index"><p>Other things related to time:
<code><a href="ctimeToSeconds.html">ctimeToSeconds</a>()</code>,
<code><a href="julianCenturyAnomaly.html">julianCenturyAnomaly</a>()</code>,
<code><a href="julianDay.html">julianDay</a>()</code>,
<code><a href="numberAsHMS.html">numberAsHMS</a>()</code>,
<code><a href="secondsToCtime.html">secondsToCtime</a>()</code>,
<code><a href="unabbreviateYear.html">unabbreviateYear</a>()</code></p>
<p>Other things related to time:
<code><a href="ctimeToSeconds.html">ctimeToSeconds</a>()</code>,
<code><a href="julianCenturyAnomaly.html">julianCenturyAnomaly</a>()</code>,
<code><a href="julianDay.html">julianDay</a>()</code>,
<code><a href="numberAsHMS.html">numberAsHMS</a>()</code>,
<code><a href="secondsToCtime.html">secondsToCtime</a>()</code>,
<code><a href="unabbreviateYear.html">unabbreviateYear</a>()</code></p></div>
    </div>
    <div class="section level2">
    <h2 id="author">Author<a class="anchor" aria-label="anchor" href="#author"></a></h2>
    <p>Dan Kelley</p>
    </div>

    <div class="section level2">
    <h2 id="ref-examples">Examples<a class="anchor" aria-label="anchor" href="#ref-examples"></a></h2>
    <div class="sourceCode"><pre class="sourceCode r"><code><span class="r-in"><span><span class="co"># Example 1. default (unix)</span></span></span>
<span class="r-in"><span><span class="fu">numberAsPOSIXct</span><span class="op">(</span><span class="fl">0</span><span class="op">)</span></span></span>
<span class="r-out co"><span class="r-pr">#&gt;</span> [1] "1970-01-01 UTC"</span>
<span class="r-in"><span></span></span>
<span class="r-in"><span><span class="co"># Example 2. Matlab</span></span></span>
<span class="r-in"><span><span class="fu">numberAsPOSIXct</span><span class="op">(</span><span class="fl">1</span>, type <span class="op">=</span> <span class="st">"matlab"</span><span class="op">)</span></span></span>
<span class="r-out co"><span class="r-pr">#&gt;</span> [1] "0000-01-01 UTC"</span>
<span class="r-in"><span></span></span>
<span class="r-in"><span><span class="co"># Example 3. GPS with default week rollover or with no rollover (Canada Day, year 2010)</span></span></span>
<span class="r-in"><span><span class="fu">numberAsPOSIXct</span><span class="op">(</span><span class="fu"><a href="https://rdrr.io/r/base/cbind.html" class="external-link">cbind</a></span><span class="op">(</span><span class="fl">566</span>, <span class="fl">345615</span><span class="op">)</span>, type <span class="op">=</span> <span class="st">"gps"</span><span class="op">)</span></span></span>
<span class="r-out co"><span class="r-pr">#&gt;</span> [1] "2010-07-01 UTC"</span>
<span class="r-in"><span><span class="fu">numberAsPOSIXct</span><span class="op">(</span><span class="fu"><a href="https://rdrr.io/r/base/cbind.html" class="external-link">cbind</a></span><span class="op">(</span><span class="fl">566</span>, <span class="fl">345615</span>, <span class="fl">1</span><span class="op">)</span>, type <span class="op">=</span> <span class="st">"gps"</span><span class="op">)</span></span></span>
<span class="r-out co"><span class="r-pr">#&gt;</span> [1] "2010-07-01 UTC"</span>
<span class="r-in"><span><span class="fu">numberAsPOSIXct</span><span class="op">(</span><span class="fu"><a href="https://rdrr.io/r/base/cbind.html" class="external-link">cbind</a></span><span class="op">(</span><span class="fl">1024</span> <span class="op">+</span> <span class="fl">566</span>, <span class="fl">345615</span>, <span class="fl">0</span><span class="op">)</span>, type <span class="op">=</span> <span class="st">"gps"</span><span class="op">)</span></span></span>
<span class="r-out co"><span class="r-pr">#&gt;</span> [1] "2010-07-01 UTC"</span>
<span class="r-in"><span><span class="co"># Show how to deal with leap seconds (15 of them, in this case)</span></span></span>
<span class="r-in"><span><span class="fu"><a href="https://rdrr.io/r/base/sum.html" class="external-link">sum</a></span><span class="op">(</span><span class="fu"><a href="https://rdrr.io/r/base/as.POSIXlt.html" class="external-link">as.POSIXct</a></span><span class="op">(</span><span class="st">"1980-01-01"</span><span class="op">)</span> <span class="op">&lt;</span> <span class="va">.leap.seconds</span> <span class="op">&amp;</span> <span class="va">.leap.seconds</span> <span class="op">&lt;=</span> <span class="fu"><a href="https://rdrr.io/r/base/as.POSIXlt.html" class="external-link">as.POSIXct</a></span><span class="op">(</span><span class="st">"2010-07-01"</span><span class="op">)</span><span class="op">)</span></span></span>
<span class="r-out co"><span class="r-pr">#&gt;</span> [1] 15</span>
<span class="r-in"><span><span class="op">-</span><span class="fl">15</span> <span class="op">+</span> <span class="fu">numberAsPOSIXct</span><span class="op">(</span><span class="fu"><a href="https://rdrr.io/r/base/cbind.html" class="external-link">cbind</a></span><span class="op">(</span><span class="fl">1024</span> <span class="op">+</span> <span class="fl">566</span>, <span class="fl">345615</span>, <span class="fl">0</span><span class="op">)</span>, type <span class="op">=</span> <span class="st">"gps"</span>, leap <span class="op">=</span> <span class="cn">FALSE</span><span class="op">)</span></span></span>
<span class="r-out co"><span class="r-pr">#&gt;</span> [1] "2010-07-01 UTC"</span>
<span class="r-in"><span></span></span>
<span class="r-in"><span><span class="co"># Example 4. yearday</span></span></span>
<span class="r-in"><span><span class="fu">numberAsPOSIXct</span><span class="op">(</span><span class="fu"><a href="https://rdrr.io/r/base/cbind.html" class="external-link">cbind</a></span><span class="op">(</span><span class="fl">2013</span>, <span class="fl">1</span><span class="op">)</span>, type <span class="op">=</span> <span class="st">"yearday"</span><span class="op">)</span> <span class="co"># start of 2013</span></span></span>
<span class="r-out co"><span class="r-pr">#&gt;</span> [1] "2013-01-01 UTC"</span>
<span class="r-in"><span></span></span>
<span class="r-in"><span><span class="co"># Example 5. Epic time, one hour into Canada Day of year 2018. In computing the</span></span></span>
<span class="r-in"><span><span class="co"># Julian day, note that this starts at noon.</span></span></span>
<span class="r-in"><span><span class="va">jd</span> <span class="op">&lt;-</span> <span class="fu"><a href="julianDay.html">julianDay</a></span><span class="op">(</span><span class="fu"><a href="https://rdrr.io/r/base/as.POSIXlt.html" class="external-link">as.POSIXct</a></span><span class="op">(</span><span class="st">"2018-07-01 12:00:00"</span>, tz <span class="op">=</span> <span class="st">"UTC"</span><span class="op">)</span><span class="op">)</span></span></span>
<span class="r-in"><span><span class="fu">numberAsPOSIXct</span><span class="op">(</span><span class="fu"><a href="https://rdrr.io/r/base/cbind.html" class="external-link">cbind</a></span><span class="op">(</span><span class="va">jd</span>, <span class="fl">1e3</span> <span class="op">*</span> <span class="fl">1</span> <span class="op">*</span> <span class="fl">3600</span><span class="op">)</span>, type <span class="op">=</span> <span class="st">"epic"</span>, tz <span class="op">=</span> <span class="st">"UTC"</span><span class="op">)</span></span></span>
<span class="r-out co"><span class="r-pr">#&gt;</span> [1] "2018-07-01 01:00:00 UTC"</span>
<span class="r-in"><span></span></span>
<span class="r-in"><span><span class="co"># Example 6. Julian day, note that this starts at noon.</span></span></span>
<span class="r-in"><span><span class="va">jd</span> <span class="op">&lt;-</span> <span class="fu"><a href="julianDay.html">julianDay</a></span><span class="op">(</span><span class="fu"><a href="https://rdrr.io/r/base/as.POSIXlt.html" class="external-link">as.POSIXct</a></span><span class="op">(</span><span class="st">"2018-07-01 12:00:00"</span>, tz <span class="op">=</span> <span class="st">"UTC"</span><span class="op">)</span><span class="op">)</span></span></span>
<span class="r-in"><span><span class="fu">numberAsPOSIXct</span><span class="op">(</span><span class="fu"><a href="https://rdrr.io/r/base/cbind.html" class="external-link">cbind</a></span><span class="op">(</span><span class="va">jd</span>, <span class="fl">1e3</span> <span class="op">*</span> <span class="fl">1</span> <span class="op">*</span> <span class="fl">3600</span><span class="op">)</span>, type <span class="op">=</span> <span class="st">"epic"</span>, tz <span class="op">=</span> <span class="st">"UTC"</span><span class="op">)</span></span></span>
<span class="r-out co"><span class="r-pr">#&gt;</span> [1] "2018-07-01 01:00:00 UTC"</span>
<span class="r-in"><span></span></span>
</code></pre></div>
    </div>
  </main><aside class="col-md-3"><nav id="toc" aria-label="Table of contents"><h2>On this page</h2>
    </nav></aside></div>


    <footer><div class="pkgdown-footer-left">
  <p>Developed by Dan Kelley, Clark Richards.</p>
</div>

<div class="pkgdown-footer-right">
  <p>Site built with <a href="https://pkgdown.r-lib.org/" class="external-link">pkgdown</a> 2.1.0.9000.</p>
</div>

    </footer></div>





  </body></html>

