Exemplo n.º 1
0
    def test_parse_date_tz(self):
        "Test timezone parsing in _parse_date"

        # these should succeed and are ISO8601 compliant
        expected_parsed_date = (2017, 5, 1, 0, 0, 0, 60.0)
        for datestr in ("2017-05-01 00:00+01:00", "2017-05-01 00:00+0100",
                        "2017-05-01 00:00+01"):
            d = _parse_date(datestr)
            assert_equal(d, expected_parsed_date)
        # some more tests with non-zero minutes, should all be ISO compliant and work
        expected_parsed_date = (2017, 5, 1, 0, 0, 0, 85.0)
        for datestr in ("2017-05-01 00:00+01:25", "2017-05-01 00:00+0125"):
            d = _parse_date(datestr)
            assert_equal(d, expected_parsed_date)
        # these are NOT ISO8601 compliant and should not even be parseable but will be parsed with timezone anyway
        # because, due to support of other legacy time formats, they are difficult to reject
        # ATTENTION: only the hours part of this will be parsed, single-digit minutes will be ignored!
        expected_parsed_date = (2017, 5, 1, 0, 0, 0, 60.0)
        for datestr in ("2017-05-01 00:00+01:0", "2017-05-01 00:00+01:",
                        "2017-05-01 00:00+01:5"):
            d = _parse_date(datestr)
            assert_equal(d, expected_parsed_date)
        # these should not even be parseable as datestrings but are parseable anyway with ignored timezone
        # this is because the module also supports some legacy, non-standard time strings
        expected_parsed_date = (2017, 5, 1, 0, 0, 0, 0.0)
        for datestr in ("2017-05-01 00:00+1", ):
            d = _parse_date(datestr)
            assert_equal(d, expected_parsed_date)
Exemplo n.º 2
0
    def test_parse_date_tz(self):
        "Test timezone parsing in _parse_date"

        # these should succeed and are ISO8601 compliant
        expected_parsed_date = (2017, 5, 1, 0, 0, 0, 60.0)
        for datestr in ("2017-05-01 00:00+01:00", "2017-05-01 00:00+0100", "2017-05-01 00:00+01"):
            d = _parse_date(datestr)
            assert_equal(d, expected_parsed_date)
        # some more tests with non-zero minutes, should all be ISO compliant and work
        expected_parsed_date = (2017, 5, 1, 0, 0, 0, 85.0)
        for datestr in ("2017-05-01 00:00+01:25", "2017-05-01 00:00+0125"):
            d = _parse_date(datestr)
            assert_equal(d, expected_parsed_date)
        # these are NOT ISO8601 compliant and should not even be parseable but will be parsed with timezone anyway
        # because, due to support of other legacy time formats, they are difficult to reject
        # ATTENTION: only the hours part of this will be parsed, single-digit minutes will be ignored!
        expected_parsed_date = (2017, 5, 1, 0, 0, 0, 60.0)
        for datestr in ("2017-05-01 00:00+01:0", "2017-05-01 00:00+01:", "2017-05-01 00:00+01:5"):
            d = _parse_date(datestr)
            assert_equal(d, expected_parsed_date)
        # these should not even be parseable as datestrings but are parseable anyway with ignored timezone
        # this is because the module also supports some legacy, non-standard time strings
        expected_parsed_date = (2017, 5, 1, 0, 0, 0, 0.0)
        for datestr in ("2017-05-01 00:00+1",):
            d = _parse_date(datestr)
            assert_equal(d, expected_parsed_date)
Exemplo n.º 3
0
def st2elements(date_string):
    '''Parse an ISO 8601 date-time string into a `cftime` object.

    :Parameters:

        date_string: `str`

    :Returns:

        `tuple`

    '''
    if date_string.count('-') != 2:
        raise ValueError(
            "Input date-time string must contain at least a year, a month "
            "and a day"
        )

    _ = cftime._parse_date(date_string)
    if len(_) == 7:
        year, month, day, hour, minute, second, utc_offset = _
        microsecond = 0
    else:
        year, month, day, hour, minute, second, microsecond, utc_offset = _

    if utc_offset:
        raise ValueError("Can't specify a time offset from UTC")

    return (year, month, day, hour, minute,
            second, microsecond)  # round((second % 1 )* 1e6))
Exemplo n.º 4
0
def st2datetime(date_string, calendar=None):
    '''Parse an ISO 8601 date-time string into a `cftime` object.

    :Parameters:

        date_string: `str`

    :Returns:

        subclass of `cftime.datetime`

    '''
    if date_string.count('-') != 2:
        raise ValueError(
            "Input date-time string must contain at least a year, a month "
            "and a day"
        )

    _ = cftime._parse_date(date_string)
    if len(_) == 7:
        year, month, day, hour, minute, second, utc_offset = _
        microsecond = 0
    else:
        year, month, day, hour, minute, second, microsecond, utc_offset = _

    if utc_offset:
        raise ValueError("Can't specify a time offset from UTC")

    #    return Datetime(year, month, day, hour, minute, second)
    return dt(year, month, day, hour, minute, second, microsecond,
              calendar=calendar)
Exemplo n.º 5
0
def get_timeinfo(ncfile):
    """ get_timeinfo(ncfile)
   - function to return datetime objects of initialized time and valid time
   input: ncfile - string to path to netCDF file
   returns: inittime, validtime - datetime objects
            nfhour - integer forecast hour
  """
    import netCDF4 as nc
    import datetime as dt
    from cftime import _parse_date
    ncf = nc.Dataset(ncfile)
    time_units = ncf['time'].units
    date_str = time_units.split('since ')[1]
    initstr = '%04i%02i%02i%02i' % _parse_date(date_str)[0:4]
    inittime = dt.datetime.strptime(initstr, "%Y%m%d%H")
    nfhour = int(ncf['time'][0])
    validtime = inittime + dt.timedelta(hours=nfhour)
    ncf.close()

    return inittime, validtime, nfhour
Exemplo n.º 6
0
from __future__ import print_function
from netCDF4 import Dataset
from cftime import _parse_date
import sys
filename = sys.argv[1]
nc = Dataset(filename)
time_units = nc['time'].units
date_str = time_units.split('since ')[1]
YYYYMMDDHH = '%04i%02i%02i%02i' % _parse_date(date_str)[0:4]
nfhour = int(nc['time'][0])
print('%s %s' % (YYYYMMDDHH, nfhour))