Python datetime.utcfromtimestamp() Examples

The following are code examples for showing how to use datetime.utcfromtimestamp(). They are from open source Python projects. You can vote up the examples you like or vote down the ones you don't like.

Example 1
Project: sun-position   Author: s-bear   File: sunposition.py    MIT License 6 votes vote down vote up
def julian_day(dt):
    """Convert UTC datetimes or UTC timestamps to Julian days

    Parameters
    ----------
    dt : array_like
        UTC datetime objects or UTC timestamps (as per datetime.utcfromtimestamp)

    Returns
    -------
    jd : ndarray
        datetimes converted to fractional Julian days
    """
    dts = np.array(dt)
    if len(dts.shape) == 0:
        return _sp.julian_day(dt)

    jds = np.empty(dts.shape)
    for i,d in enumerate(dts.flat):
        jds.flat[i] = _sp.julian_day(d)
    return jds 
Example 2
Project: chan_feed   Author: simon987   File: run.py    GNU General Public License v3.0 6 votes vote down vote up
def publish(item, board, helper, channel, web):
    post_process(item, board, helper, web)

    item_type = helper.item_type(item)
    routing_key = "%s.%s.%s" % (chan, item_type, board)

    _publish_buffered([(channel, routing_key, item)])

    if MONITORING:
        distance = datetime.utcnow() - datetime.utcfromtimestamp(helper.item_mtime(item))
        influxdb.log([{
            "measurement": chan,
            "time": str(datetime.utcnow()),
            "tags": {
                "board": board
            },
            "fields": {
                "distance": distance.total_seconds()
            }
        }]) 
Example 3
Project: RAPIDpy   Author: erdc   File: merge.py    BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def _generate_time_values(self):
        """
        Generates time values for out nc file
        """
        # Populate time values
        log('writing times', 'INFO')
        d1970 = datetime(1970, 1, 1, tzinfo=utc)
        time_array = [[int((self.start_datetime - d1970).total_seconds())]]

        datetime_nc_start_simulation = self.start_datetime
        for raw_nc_index, raw_nc in enumerate(self.raw_nc_list):
            raw_nc_time = raw_nc.get_time_array(
                datetime_simulation_start=datetime_nc_start_simulation,
                simulation_time_step_seconds=self.time_step_array[
                    raw_nc_index])

            time_array.append(raw_nc_time)
            datetime_nc_start_simulation = \
                datetime.utcfromtimestamp(raw_nc_time[-1])

        self.cf_nc.variables['time'][:] = np.concatenate(time_array)
        end_date = datetime.utcfromtimestamp(self.cf_nc.variables['time'][-1])
        self.cf_nc.time_coverage_start = self.start_datetime.isoformat() + 'Z'
        self.cf_nc.time_coverage_end = end_date.isoformat() + 'Z' 
Example 4
Project: bw2python   Author: SoftwareDefinedBuildings   File: client.py    BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def _createMakeEntityFrame(contact, comment, expiry, expiry_delta, revokers,
                               omit_creation_date):
        seq_num = Frame.generateSequenceNumber()
        frame = Frame("make", seq_num)

        if contact is not None:
            frame.addKVPair("contact", contact)
        if comment is not None:
            frame.addKVPair("comment", comment)

        if expiry is not None:
            expiry_time = datetime.utcfromtimestamp(expiry)
            frame.addKVPair("expiry", _utfToRfc3339(expiry_time))
        if expiry_delta is not None:
            frame.addKVPair("expirydelta", "{0}ms".format(expiry_delta))

        if revokers is not None:
            for revoker in reovkers:
                frame.addKVPair("revoker", revoker)
        if omit_creation_date:
            frame.addKVPair("omitcreationdate", "true")
        else:
            frame.addKVPair("omitcreationdate", "false")

        return frame 
Example 5
Project: zschema   Author: zmap   File: leaves.py    Apache License 2.0 6 votes vote down vote up
def _validate(self, name, value, path=_NO_ARG):
        try:
            if isinstance(value, datetime.datetime):
                dt = value
            elif isinstance(value, int):
                dt = datetime.datetime.utcfromtimestamp(value)
            else:
                dt = dateutil.parser.parse(value)
        except (ValueError, TypeError):
            # Either `datetime.utcfromtimestamp` or `dateutil.parser.parse` above
            # may raise on invalid input.
            m = "%s: %s is not valid timestamp" % (name, str(value))
            raise DataValidationException(m, path=path)
        dt = DateTime._ensure_tz_aware(dt)
        if dt > self._max_value_dt:
            m = "%s: %s is greater than allowed maximum (%s)" % (name,
                    str(value), str(self._max_value_dt))
            raise DataValidationException(m, path=path)
        if dt < self._min_value_dt:
            m = "%s: %s is less than allowed minimum (%s)" % (name,
                    str(value), str(self._min_value_dt))
            raise DataValidationException(m, path=path) 
Example 6
Project: cnidaria   Author: sauloal   File: test_timeseries.py    MIT License 6 votes vote down vote up
def test_class_ops_pytz(self):
        tm._skip_if_no_pytz()
        from pytz import timezone

        def compare(x, y):
            self.assertEqual(int(Timestamp(x).value / 1e9), int(Timestamp(y).value / 1e9))

        compare(Timestamp.now(), datetime.now())
        compare(Timestamp.now('UTC'), datetime.now(timezone('UTC')))
        compare(Timestamp.utcnow(), datetime.utcnow())
        compare(Timestamp.today(), datetime.today())
        current_time = calendar.timegm(datetime.now().utctimetuple())
        compare(Timestamp.utcfromtimestamp(current_time),
                datetime.utcfromtimestamp(current_time))
        compare(Timestamp.fromtimestamp(current_time),
                datetime.fromtimestamp(current_time))

        date_component = datetime.utcnow()
        time_component = (date_component + timedelta(minutes=10)).time()
        compare(Timestamp.combine(date_component, time_component),
                datetime.combine(date_component, time_component)) 
Example 7
Project: cnidaria   Author: sauloal   File: test_timeseries.py    MIT License 6 votes vote down vote up
def test_class_ops_dateutil(self):
        tm._skip_if_no_dateutil()
        from dateutil.tz import tzutc

        def compare(x,y):
            self.assertEqual(int(np.round(Timestamp(x).value/1e9)), int(np.round(Timestamp(y).value/1e9)))

        compare(Timestamp.now(),datetime.now())
        compare(Timestamp.now('UTC'), datetime.now(tzutc()))
        compare(Timestamp.utcnow(),datetime.utcnow())
        compare(Timestamp.today(),datetime.today())
        current_time = calendar.timegm(datetime.now().utctimetuple())
        compare(Timestamp.utcfromtimestamp(current_time),
                datetime.utcfromtimestamp(current_time))
        compare(Timestamp.fromtimestamp(current_time),
                datetime.fromtimestamp(current_time))

        date_component = datetime.utcnow()
        time_component = (date_component + timedelta(minutes=10)).time()
        compare(Timestamp.combine(date_component, time_component),
                datetime.combine(date_component, time_component)) 
Example 8
Project: pyblish-win   Author: pyblish   File: test_datetime.py    GNU Lesser General Public License v3.0 5 votes vote down vote up
def test_utcfromtimestamp(self):
        import time

        ts = time.time()
        expected = time.gmtime(ts)
        got = self.theclass.utcfromtimestamp(ts)
        self.verify_field_equality(expected, got) 
Example 9
Project: pyblish-win   Author: pyblish   File: test_datetime.py    GNU Lesser General Public License v3.0 5 votes vote down vote up
def test_insane_utcfromtimestamp(self):
        # It's possible that some platform maps time_t to double,
        # and that this test will fail there.  This test should
        # exempt such platforms (provided they return reasonable
        # results!).
        for insane in -1e200, 1e200:
            self.assertRaises(ValueError, self.theclass.utcfromtimestamp,
                              insane) 
Example 10
Project: pyblish-win   Author: pyblish   File: test_datetime.py    GNU Lesser General Public License v3.0 5 votes vote down vote up
def test_negative_float_utcfromtimestamp(self):
        d = self.theclass.utcfromtimestamp(-1.05)
        self.assertEqual(d, self.theclass(1969, 12, 31, 23, 59, 58, 950000)) 
Example 11
Project: pyblish-win   Author: pyblish   File: test_datetime.py    GNU Lesser General Public License v3.0 5 votes vote down vote up
def test_utcnow(self):
        import time

        # Call it a success if utcnow() and utcfromtimestamp() are within
        # a second of each other.
        tolerance = timedelta(seconds=1)
        for dummy in range(3):
            from_now = self.theclass.utcnow()
            from_timestamp = self.theclass.utcfromtimestamp(time.time())
            if abs(from_timestamp - from_now) <= tolerance:
                break
            # Else try again a few times.
        self.assertLessEqual(abs(from_timestamp - from_now), tolerance) 
Example 12
Project: pyblish-win   Author: pyblish   File: test_datetime.py    GNU Lesser General Public License v3.0 5 votes vote down vote up
def test_tzinfo_fromtimestamp(self):
        import time
        meth = self.theclass.fromtimestamp
        ts = time.time()
        # Ensure it doesn't require tzinfo (i.e., that this doesn't blow up).
        base = meth(ts)
        # Try with and without naming the keyword.
        off42 = FixedOffset(42, "42")
        another = meth(ts, off42)
        again = meth(ts, tz=off42)
        self.assertIs(another.tzinfo, again.tzinfo)
        self.assertEqual(another.utcoffset(), timedelta(minutes=42))
        # Bad argument with and w/o naming the keyword.
        self.assertRaises(TypeError, meth, ts, 16)
        self.assertRaises(TypeError, meth, ts, tzinfo=16)
        # Bad keyword name.
        self.assertRaises(TypeError, meth, ts, tinfo=off42)
        # Too many args.
        self.assertRaises(TypeError, meth, ts, off42, off42)
        # Too few args.
        self.assertRaises(TypeError, meth)

        # Try to make sure tz= actually does some conversion.
        timestamp = 1000000000
        utcdatetime = datetime.utcfromtimestamp(timestamp)
        # In POSIX (epoch 1970), that's 2001-09-09 01:46:40 UTC, give or take.
        # But on some flavor of Mac, it's nowhere near that.  So we can't have
        # any idea here what time that actually is, we can only test that
        # relative changes match.
        utcoffset = timedelta(hours=-15, minutes=39) # arbitrary, but not zero
        tz = FixedOffset(utcoffset, "tz", 0)
        expected = utcdatetime + utcoffset
        got = datetime.fromtimestamp(timestamp, tz)
        self.assertEqual(expected, got.replace(tzinfo=None)) 
Example 13
Project: ironpython2   Author: IronLanguages   File: test_datetime.py    Apache License 2.0 5 votes vote down vote up
def test_utcfromtimestamp(self):
        import time

        ts = time.time()
        expected = time.gmtime(ts)
        got = self.theclass.utcfromtimestamp(ts)
        self.verify_field_equality(expected, got) 
Example 14
Project: ironpython2   Author: IronLanguages   File: test_datetime.py    Apache License 2.0 5 votes vote down vote up
def test_insane_utcfromtimestamp(self):
        # It's possible that some platform maps time_t to double,
        # and that this test will fail there.  This test should
        # exempt such platforms (provided they return reasonable
        # results!).
        for insane in -1e200, 1e200:
            self.assertRaises(ValueError, self.theclass.utcfromtimestamp,
                              insane) 
Example 15
Project: ironpython2   Author: IronLanguages   File: test_datetime.py    Apache License 2.0 5 votes vote down vote up
def test_negative_float_utcfromtimestamp(self):
        d = self.theclass.utcfromtimestamp(-1.05)
        self.assertEqual(d, self.theclass(1969, 12, 31, 23, 59, 58, 950000)) 
Example 16
Project: ironpython2   Author: IronLanguages   File: test_datetime.py    Apache License 2.0 5 votes vote down vote up
def test_utcnow(self):
        import time

        # Call it a success if utcnow() and utcfromtimestamp() are within
        # a second of each other.
        tolerance = timedelta(seconds=1)
        for dummy in range(3):
            from_now = self.theclass.utcnow()
            from_timestamp = self.theclass.utcfromtimestamp(time.time())
            if abs(from_timestamp - from_now) <= tolerance:
                break
            # Else try again a few times.
        self.assertLessEqual(abs(from_timestamp - from_now), tolerance) 
Example 17
Project: ironpython2   Author: IronLanguages   File: test_datetime.py    Apache License 2.0 5 votes vote down vote up
def test_tzinfo_fromtimestamp(self):
        import time
        meth = self.theclass.fromtimestamp
        ts = time.time()
        # Ensure it doesn't require tzinfo (i.e., that this doesn't blow up).
        base = meth(ts)
        # Try with and without naming the keyword.
        off42 = FixedOffset(42, "42")
        another = meth(ts, off42)
        again = meth(ts, tz=off42)
        self.assertIs(another.tzinfo, again.tzinfo)
        self.assertEqual(another.utcoffset(), timedelta(minutes=42))
        # Bad argument with and w/o naming the keyword.
        self.assertRaises(TypeError, meth, ts, 16)
        self.assertRaises(TypeError, meth, ts, tzinfo=16)
        # Bad keyword name.
        self.assertRaises(TypeError, meth, ts, tinfo=off42)
        # Too many args.
        self.assertRaises(TypeError, meth, ts, off42, off42)
        # Too few args.
        self.assertRaises(TypeError, meth)

        # Try to make sure tz= actually does some conversion.
        timestamp = 1000000000
        utcdatetime = datetime.utcfromtimestamp(timestamp)
        # In POSIX (epoch 1970), that's 2001-09-09 01:46:40 UTC, give or take.
        # But on some flavor of Mac, it's nowhere near that.  So we can't have
        # any idea here what time that actually is, we can only test that
        # relative changes match.
        utcoffset = timedelta(hours=-15, minutes=39) # arbitrary, but not zero
        tz = FixedOffset(utcoffset, "tz", 0)
        expected = utcdatetime + utcoffset
        got = datetime.fromtimestamp(timestamp, tz)
        self.assertEqual(expected, got.replace(tzinfo=None)) 
Example 18
Project: sun-position   Author: s-bear   File: sunposition.py    MIT License 5 votes vote down vote up
def calendar_time(dt):
        try:
            x = dt.year, dt.month, dt.day, dt.hour, dt.minute, dt.second, dt.microsecond
            return x
        except AttributeError:
            try:
                return _sp.calendar_time(datetime.utcfromtimestamp(dt)) #will raise OSError if dt is not acceptable
            except:
                raise TypeError('dt must be datetime object or POSIX timestamp') 
Example 19
Project: oss-ftp   Author: aliyun   File: test_datetime.py    MIT License 5 votes vote down vote up
def test_utcfromtimestamp(self):
        import time

        ts = time.time()
        expected = time.gmtime(ts)
        got = self.theclass.utcfromtimestamp(ts)
        self.verify_field_equality(expected, got) 
Example 20
Project: oss-ftp   Author: aliyun   File: test_datetime.py    MIT License 5 votes vote down vote up
def test_insane_utcfromtimestamp(self):
        # It's possible that some platform maps time_t to double,
        # and that this test will fail there.  This test should
        # exempt such platforms (provided they return reasonable
        # results!).
        for insane in -1e200, 1e200:
            self.assertRaises(ValueError, self.theclass.utcfromtimestamp,
                              insane) 
Example 21
Project: oss-ftp   Author: aliyun   File: test_datetime.py    MIT License 5 votes vote down vote up
def test_negative_float_utcfromtimestamp(self):
        d = self.theclass.utcfromtimestamp(-1.05)
        self.assertEqual(d, self.theclass(1969, 12, 31, 23, 59, 58, 950000)) 
Example 22
Project: oss-ftp   Author: aliyun   File: test_datetime.py    MIT License 5 votes vote down vote up
def test_utcnow(self):
        import time

        # Call it a success if utcnow() and utcfromtimestamp() are within
        # a second of each other.
        tolerance = timedelta(seconds=1)
        for dummy in range(3):
            from_now = self.theclass.utcnow()
            from_timestamp = self.theclass.utcfromtimestamp(time.time())
            if abs(from_timestamp - from_now) <= tolerance:
                break
            # Else try again a few times.
        self.assertLessEqual(abs(from_timestamp - from_now), tolerance) 
Example 23
Project: oss-ftp   Author: aliyun   File: test_datetime.py    MIT License 5 votes vote down vote up
def test_tzinfo_fromtimestamp(self):
        import time
        meth = self.theclass.fromtimestamp
        ts = time.time()
        # Ensure it doesn't require tzinfo (i.e., that this doesn't blow up).
        base = meth(ts)
        # Try with and without naming the keyword.
        off42 = FixedOffset(42, "42")
        another = meth(ts, off42)
        again = meth(ts, tz=off42)
        self.assertIs(another.tzinfo, again.tzinfo)
        self.assertEqual(another.utcoffset(), timedelta(minutes=42))
        # Bad argument with and w/o naming the keyword.
        self.assertRaises(TypeError, meth, ts, 16)
        self.assertRaises(TypeError, meth, ts, tzinfo=16)
        # Bad keyword name.
        self.assertRaises(TypeError, meth, ts, tinfo=off42)
        # Too many args.
        self.assertRaises(TypeError, meth, ts, off42, off42)
        # Too few args.
        self.assertRaises(TypeError, meth)

        # Try to make sure tz= actually does some conversion.
        timestamp = 1000000000
        utcdatetime = datetime.utcfromtimestamp(timestamp)
        # In POSIX (epoch 1970), that's 2001-09-09 01:46:40 UTC, give or take.
        # But on some flavor of Mac, it's nowhere near that.  So we can't have
        # any idea here what time that actually is, we can only test that
        # relative changes match.
        utcoffset = timedelta(hours=-15, minutes=39) # arbitrary, but not zero
        tz = FixedOffset(utcoffset, "tz", 0)
        expected = utcdatetime + utcoffset
        got = datetime.fromtimestamp(timestamp, tz)
        self.assertEqual(expected, got.replace(tzinfo=None)) 
Example 24
Project: EvapoTranspiration   Author: linuxha   File: weatherCustom.py    GNU Affero General Public License v3.0 5 votes vote down vote up
def XmmFromLogs(t):
    ydate = (datetime.today() - datetime.utcfromtimestamp(0)).days - 1
    filenames = next(os.walk(logsPath))[2]
    for x in filenames:
        ldate = x
        if ldate == str(ydate):
            fpath = x
        #
    #
    yET = json.load(open(ETPath + '/' + fpath))
    tET = [0] * len(yET)
    logs = json.load(open(logsPath + '/' + fpath))
    if debug != 0: print >>sys.stderr, "E: json load %s/%s (%s)" % (logsPath, fpath, logs)
    l = len(t['mmTime'])
    ydur = [-1] * l
    ymm = [-1] * l
    for x in logs:
        if int(x[0]) == pid:
            ydur[safe_int(x[1])] += safe_int(x[2])
        #
    #
    for x in range(l):
        if t['mmTime'][x]:
            ymm[x] = round(safe_float(yET[safe_int(t['crop'][x])]) - ydur[x] / safe_float(t['mmTime'][x]), 4) * -1
            tET[int(t['crop'][x])] = ymm[x]
        else:
            ymm[x] = 0
        #
    #
    return (ymm, tET)
#

# 
Example 25
Project: webrecorder   Author: webrecorder   File: serializefakeredis.py    Apache License 2.0 5 votes vote down vote up
def load_redis_dict(self, obj):
        new_dict = {}
        redis_type = obj.get('__redis_type')
        exp = None
        for key, value in obj.items():
            if key == '__redis_type':
                continue

            key = key.encode('utf-8')

            if redis_type == 'exp' and isinstance(value, list):
                if isinstance(value[1], int):
                    exp = datetime.utcfromtimestamp(value[1])
                else:
                    exp = None
                value = value[0]

            if isinstance(value, str):
                value = value.encode('utf-8')
            elif isinstance(value, list):
                value = self.load_redis_set(value)
            elif isinstance(value, dict):
                value = self.load_redis_dict(value)

            if redis_type == 'exp':
                new_dict[key] = (value, exp)
            else:
                new_dict[key] = value

        if redis_type == 'zset':
            redis_dict = _ZSet()
        elif redis_type == 'hash':
            redis_dict = _Hash()
        elif redis_type == 'exp':
            redis_dict = _ExpiringDict()
        else:
            raise Exception('Invalid redis_dict: ' + str(redis_type))

        redis_dict._dict = new_dict
        return redis_dict 
Example 26
Project: mldb-tennis   Author: datacratic   File: main.py    Apache License 2.0 5 votes vote down vote up
def print_history(theHistory):
    # print all history
    for theDate in sorted(theHistory):
        print "Player info @ ", datetime.utcfromtimestamp(float(theDate)), " epoch : ", theDate
        for player in theHistory[theDate]:
            print "\t Player: ", player, " info ", theHistory[theDate][player] 
Example 27
Project: mldb-tennis   Author: datacratic   File: main.py    Apache License 2.0 5 votes vote down vote up
def get_player_stats(history, player, ts):

    print " get_player_stats for player  :", player, " at time ", ts, ":", datetime.utcfromtimestamp(ts)
    # look for the history at the specified time
    if history.get(ts) == None:
        print "no history at time " ,ts
        return None
    snapshot = history[ts]
    if snapshot.get(player) == None:
        print "no stats for player ", player
        return None
    playerStats = snapshot[player]
#    print "stats for player ", player, ":", playerStats
    return playerStats 
Example 28
Project: archon   Author: economicnetwork   File: models.py    MIT License 5 votes vote down vote up
def conv_timestamp(ts, exchange):    
    target_format = '%Y-%m-%dT%H:%M:%S'
    if exchange==exc.CRYPTOPIA:
        #local = pytz.timezone("Europe/London") 
        #tsf = datetime.datetime.fromtimestamp(ts)
        tsf = datetime.datetime.utcfromtimestamp(ts)        
        #local_dt = local.localize(tsf, is_dst=None)
        utc_dt = tsf.astimezone(pytz.utc)
        utc_dt = utc_dt + datetime.timedelta(hours=4)
        #dt = utc_dt.strftime(date_broker_format)        
        tsf = utc_dt.strftime(target_format)
        return tsf
    elif exchange==exc.BITTREX:
        ts = ts.split('.')[0]
        tsf = datetime.datetime.strptime(ts,'%Y-%m-%dT%H:%M:%S')
        utc=pytz.UTC
        utc_dt = tsf.astimezone(pytz.utc)
        utc_dt = utc_dt + datetime.timedelta(hours=4)
        tsf = utc_dt.strftime(target_format)
        return tsf

    elif exchange==exc.KUCOIN:
        #dt = conv_timestamp(t/1000,exchange)
        tsf = datetime.datetime.utcfromtimestamp(ts/1000)
        #tsf = datetime.datetime.strptime(ts,'%Y-%m-%dT%H:%M:%S')
        utc=pytz.UTC
        utc_dt = tsf.astimezone(pytz.utc)
        utc_dt = utc_dt + datetime.timedelta(hours=4)
        #tsf = utc_dt.strftime()
        tsf = utc_dt.strftime(target_format)
        return tsf

    elif exchange==exc.BINANCE:
        tsf = datetime.datetime.utcfromtimestamp(int(ts/1000))
        utc=pytz.UTC
        utc_dt = tsf.astimezone(pytz.utc)
        utc_dt = utc_dt + datetime.timedelta(hours=4)
        tsf = utc_dt.strftime(target_format)
        return tsf 
Example 29
Project: Hot   Author: dsolimando   File: test_datetime.py    GNU General Public License v3.0 5 votes vote down vote up
def test_utcfromtimestamp(self):
        import time

        ts = time.time()
        expected = time.gmtime(ts)
        got = self.theclass.utcfromtimestamp(ts)
        self.verify_field_equality(expected, got) 
Example 30
Project: Hot   Author: dsolimando   File: test_datetime.py    GNU General Public License v3.0 5 votes vote down vote up
def test_insane_utcfromtimestamp(self):
        # It's possible that some platform maps time_t to double,
        # and that this test will fail there.  This test should
        # exempt such platforms (provided they return reasonable
        # results!).
        for insane in -1e200, 1e200:
            self.assertRaises(ValueError, self.theclass.utcfromtimestamp,
                              insane) 
Example 31
Project: Hot   Author: dsolimando   File: test_datetime.py    GNU General Public License v3.0 5 votes vote down vote up
def test_negative_float_utcfromtimestamp(self):
        # Windows doesn't accept negative timestamps
        if os.name == "nt":
            return
        d = self.theclass.utcfromtimestamp(-1.05)
        self.assertEquals(d, self.theclass(1969, 12, 31, 23, 59, 58, 950000)) 
Example 32
Project: Hot   Author: dsolimando   File: test_datetime.py    GNU General Public License v3.0 5 votes vote down vote up
def test_utcnow(self):
        import time

        # Call it a success if utcnow() and utcfromtimestamp() are within
        # a second of each other.
        tolerance = timedelta(seconds=1)
        for dummy in range(3):
            from_now = self.theclass.utcnow()
            from_timestamp = self.theclass.utcfromtimestamp(time.time())
            if abs(from_timestamp - from_now) <= tolerance:
                break
            # Else try again a few times.
        self.failUnless(abs(from_timestamp - from_now) <= tolerance) 
Example 33
Project: Hot   Author: dsolimando   File: test_datetime.py    GNU General Public License v3.0 5 votes vote down vote up
def test_tzinfo_fromtimestamp(self):
        import time
        meth = self.theclass.fromtimestamp
        ts = time.time()
        # Ensure it doesn't require tzinfo (i.e., that this doesn't blow up).
        base = meth(ts)
        # Try with and without naming the keyword.
        off42 = FixedOffset(42, "42")
        another = meth(ts, off42)
        again = meth(ts, tz=off42)
        self.failUnless(another.tzinfo is again.tzinfo)
        self.assertEqual(another.utcoffset(), timedelta(minutes=42))
        # Bad argument with and w/o naming the keyword.
        self.assertRaises(TypeError, meth, ts, 16)
        self.assertRaises(TypeError, meth, ts, tzinfo=16)
        # Bad keyword name.
        self.assertRaises(TypeError, meth, ts, tinfo=off42)
        # Too many args.
        self.assertRaises(TypeError, meth, ts, off42, off42)
        # Too few args.
        self.assertRaises(TypeError, meth)

        # Try to make sure tz= actually does some conversion.
        timestamp = 1000000000
        utcdatetime = datetime.utcfromtimestamp(timestamp)
        # In POSIX (epoch 1970), that's 2001-09-09 01:46:40 UTC, give or take.
        # But on some flavor of Mac, it's nowhere near that.  So we can't have
        # any idea here what time that actually is, we can only test that
        # relative changes match.
        utcoffset = timedelta(hours=-15, minutes=39) # arbitrary, but not zero
        tz = FixedOffset(utcoffset, "tz", 0)
        expected = utcdatetime + utcoffset
        got = datetime.fromtimestamp(timestamp, tz)
        self.assertEqual(expected, got.replace(tzinfo=None)) 
Example 34
Project: rawdata   Author: acutesoftware   File: browser_usage.py    MIT License 5 votes vote down vote up
def datetime_from_utc_to_local(self, utc_datetime):
        now_timestamp = time.time()
        offset = datetime.fromtimestamp(now_timestamp) - datetime.utcfromtimestamp(now_timestamp)
        return utc_datetime + offset 
Example 35
Project: bw2python   Author: SoftwareDefinedBuildings   File: client.py    BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def _createSubscribeFrame(uri, primary_access_chain, expiry, expiry_delta,
                              elaborate_pac, unpack, auto_chain, routing_objects):
        seq_num = Frame.generateSequenceNumber()
        frame = Frame("subs", seq_num)
        frame.addKVPair("uri", uri)

        if primary_access_chain is not None:
            frame.addKVPair("primary_access_chain", primary_access_chain)
        if expiry is not None:
            expiry_time = datetime.utcfromtimestamp(expiry)
            frame.addKVPair("expiry", Client._utcToRfc3339(expiry_time))
        if expiry_delta is not None:
            frame.addKVPair("expirydelta", "{0}ms".format(expiry_delta))

        if elaborate_pac is not None:
            if elaborate_pac.lower() == "full":
                frame.addKVPair("elaborate_pac", "full")
            else:
                frame.addKVPair("elaborate_pac", "partial")
        if unpack:
            frame.addKVPair("unpack", "true")
        else:
            frame.addKVPair("unpack", "false")

        if auto_chain:
            frame.addKVPair("autochain", "true")

        if routing_objects is not None:
            frame.addRoutingObjects(routing_objects)

        return frame 
Example 36
Project: bw2python   Author: SoftwareDefinedBuildings   File: client.py    BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def _createPublishFrame(uri, persist, primary_access_chain, expiry, expiry_delta,
                            elaborate_pac, auto_chain, routing_objects, payload_objects):
        seq_num = Frame.generateSequenceNumber()
        if persist:
            frame = Frame("pers", seq_num)
        else:
            frame = Frame("publ", seq_num)
        frame.addKVPair("uri", uri)

        if primary_access_chain is not None:
            frame.addKVPair("primary_access_chain", primary_access_chain)

        if expiry is not None:
            expiry_time = datetime.utcfromtimestamp(expiry)
            frame.addKVPair("expiry", _utcToRfc3339(expiry_time))
        if expiry_delta is not None:
            frame.addKVPair("expirydelta", "{0}ms".format(expiry_delta))

        if elaborate_pac is not None:
            if elaborate_pac.lower() == "full":
                frame.addKVPair("elaborate_pac", "full")
            else:
                frame.addKVPair("elaborate_pac", "partial")

        if auto_chain:
            frame.addKVPair("autochain", "true")

        if routing_objects is not None:
            frame.addRoutingObjects(routing_objects)
        if payload_objects is not None:
            frame.addPayloadObjects(payload_objects)

        return frame 
Example 37
Project: bw2python   Author: SoftwareDefinedBuildings   File: client.py    BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def _createListFrame(uri, primary_access_chain, expiry, expiry_delta,
                         elaborate_pac, auto_chain, routing_objects):
        seq_num = Frame.generateSequenceNumber()
        frame = Frame("list", seq_num)
        frame.addKVPair("uri", uri)

        if primary_access_chain is not None:
            frame.addKVPair("primary_access_chain", primary_access_chain)

        if expiry is not None:
            expiry_time = datetime.utcfromtimestamp(expiry)
            frame.addKVPair("expiry", _utcToRfc3339(expiry_time))
        if expiry_delta is not None:
            frame.addKVPair("expirydelta", "{0}ms".format(expiry_delta))

        if elaborate_pac is not None:
            if elaborate_pac.lower() == "full":
                frame.addKVPair("elaborate_pac", "full")
            else:
                frame.addKVPair("elaborate_pac", "partial")

        if auto_chain:
            frame.addKVPair("autochain", "true")

        if routing_objects is not None:
            for ro in routing_objects:
                frame.addRoutingObject(ro)

        return frame 
Example 38
Project: bw2python   Author: SoftwareDefinedBuildings   File: client.py    BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def _createQueryFrame(uri, primary_access_chain, expiry, expiry_delta,
                         elaborate_pac, unpack, auto_chain, routing_objects):
        seq_num = Frame.generateSequenceNumber()
        frame = Frame("quer", seq_num)
        frame.addKVPair("uri", uri)

        if primary_access_chain is not None:
            frame.addKVPair("primary_access_chain", primary_access_chain)

        if expiry is not None:
            expiry_time = datetime.utcfromtimestamp(expiry)
            frame.addKVPair("expiry", _utcToRfc3339(expiry_time))
        if expiry_delta is not None:
            frame.addKVPair("expirydelta", "{0}ms".format(expiry_delta))

        if elaborate_pac is not None:
            if elaborate_pac.lower() == "full":
                frame.addKVPair("elaborate_pac", "full")
            else:
                frame.addKVPair("elaborate_pac", "partial")

        if unpack:
            frame.addKVPair("unpack", "true")
        else:
            frame.addKVPair("unpack", "false")

        if auto_chain:
            frame.addKVPair("autochain", "true")

        if routing_objects is not None:
            frame.addRoutingObjects(routing_objects)

        return frame 
Example 39
Project: sun-position   Author: s-bear   File: sunposition.py    MIT License 4 votes vote down vote up
def observed_sunpos(dt, latitude, longitude, elevation, temperature=None, pressure=None, delta_t=0, radians=False):
    """Compute the observed coordinates of the sun as viewed at the given time and location.

    Parameters
    ----------
    dt : array_like of datetime or float
        UTC datetime objects or UTC timestamps (as per datetime.utcfromtimestamp) representing the times of observations
    latitude, longitude : array_like of float
        decimal degrees, positive for north of the equator and east of Greenwich
    elevation : array_like of float
        meters, relative to the WGS-84 ellipsoid
    temperature : None or array_like of float, optional
        celcius, default is 14.6 (global average in 2013)
    pressure : None or array_like of float, optional
        millibar, default is 1013 (global average in ??)
    delta_t : array_like of float, optional
        seconds, default is 0, difference between the earth's rotation time (TT) and universal time (UT)
    radians : bool, optional
        return results in radians if True, degrees if False (default)

    Returns
    -------
    coords : ndarray, (...,2)
        The shape of the array is parameters broadcast together, plus a final dimension for the coordinates.
        coords[...,0] = observed azimuth angle, measured eastward from north
        coords[...,1] = observed zenith angle, measured down from vertical
    """
    if temperature is None:
        temperature = 14.6
    if pressure is None:
        pressure = 1013
    
    #6367444 = radius of earth
    #numpy broadcasting
    b = np.broadcast(dt,latitude,longitude,elevation,temperature,pressure,delta_t)
    res = np.empty(b.shape+(2,))
    res_vec = res.reshape((-1,2))
    for i,x in enumerate(b):
        res_vec[i] = _sp.pos(*x)[:2]
    if radians:
        res = np.deg2rad(res)
    return res 
Example 40
Project: sun-position   Author: s-bear   File: sunposition.py    MIT License 4 votes vote down vote up
def topocentric_sunpos(dt, latitude, longitude, temperature=None, pressure=None, delta_t=0, radians=False):
    """Compute the topocentric coordinates of the sun as viewed at the given time and location.

    Parameters
    ----------
    dt : array_like of datetime or float
        UTC datetime objects or UTC timestamps (as per datetime.utcfromtimestamp) representing the times of observations
    latitude, longitude : array_like of float
        decimal degrees, positive for north of the equator and east of Greenwich
    elevation : array_like of float
        meters, relative to the WGS-84 ellipsoid
    temperature : None or array_like of float, optional
        celcius, default is 14.6 (global average in 2013)
    pressure : None or array_like of float, optional
        millibar, default is 1013 (global average in ??)
    delta_t : array_like of float, optional
        seconds, default is 0, difference between the earth's rotation time (TT) and universal time (UT)
    radians : bool, optional
        return results in radians if True, degrees if False (default)

    Returns
    -------
    coords : ndarray, (...,3)
        The shape of the array is parameters broadcast together, plus a final dimension for the coordinates.
        coords[...,0] = topocentric right ascension
        coords[...,1] = topocentric declination
        coords[...,2] = topocentric hour angle
    """
    if temperature is None:
        temperature = 14.6
    if pressure is None:
        pressure = 1013
    
    #6367444 = radius of earth
    #numpy broadcasting
    b = np.broadcast(dt,latitude,longitude,elevation,temperature,pressure,delta_t)
    res = np.empty(b.shape+(2,))
    res_vec = res.reshape((-1,2))
    for i,x in enumerate(b):
        res_vec[i] = _sp.topo_pos(*x)
    if radians:
        res = np.deg2rad(res)
    return res 
Example 41
Project: sun-position   Author: s-bear   File: sunposition.py    MIT License 4 votes vote down vote up
def sunpos(dt, latitude, longitude, elevation, temperature=None, pressure=None, delta_t=0, radians=False):
    """Compute the observed and topocentric coordinates of the sun as viewed at the given time and location.

    Parameters
    ----------
    dt : array_like of datetime or float
        UTC datetime objects or UTC timestamps (as per datetime.utcfromtimestamp) representing the times of observations
    latitude, longitude : array_like of float
        decimal degrees, positive for north of the equator and east of Greenwich
    elevation : array_like of float
        meters, relative to the WGS-84 ellipsoid
    temperature : None or array_like of float, optional
        celcius, default is 14.6 (global average in 2013)
    pressure : None or array_like of float, optional
        millibar, default is 1013 (global average in ??)
    delta_t : array_like of float, optional
        seconds, default is 0, difference between the earth's rotation time (TT) and universal time (UT)
    radians : bool, optional
        return results in radians if True, degrees if False (default)

    Returns
    -------
    coords : ndarray, (...,5)
        The shape of the array is parameters broadcast together, plus a final dimension for the coordinates.
        coords[...,0] = observed azimuth angle, measured eastward from north
        coords[...,1] = observed zenith angle, measured down from vertical
        coords[...,2] = topocentric right ascension
        coords[...,3] = topocentric declination
        coords[...,4] = topocentric hour angle
    """

    if temperature is None:
        temperature = 14.6
    if pressure is None:
        pressure = 1013
    
    #6367444 = radius of earth
    #numpy broadcasting
    b = np.broadcast(dt,latitude,longitude,elevation,temperature,pressure,delta_t)
    res = np.empty(b.shape+(5,))
    res_vec = res.reshape((-1,5))
    for i,x in enumerate(b):
        res_vec[i] = _sp.pos(*x)
    if radians:
        res = np.deg2rad(res)
    return res 
Example 42
Project: fastlane   Author: fastlane-queue   File: job.py    MIT License 4 votes vote down vote up
def schedule_job(self, app, details):
        logger = app.logger.bind(operation="schedule_job")

        if self.image is None or self.command is None:
            logger.warn("No image or command found in job.")

            return None

        start_at = details.get("startAt", None)
        start_in = details.get("startIn", None)
        cron = details.get("cron", None)

        args = [
            str(self.task.task_id),
            str(self.job_id),
            None,
            self.image,
            self.command,
        ]

        queue_job_id = None

        if start_at is not None:
            logger.debug("Enqueuing job execution in the future...", start_at=start_at)
            enqueued_id = app.jobs_queue.enqueue_at(
                int(start_at), Categories.Job, *args
            )
            #  future_date = datetime.utcfromtimestamp(int(start_at))
            #  result = scheduler.enqueue_at(future_date, run_job, *args)
            self.metadata["enqueued_id"] = enqueued_id
            queue_job_id = enqueued_id
            self.save()
            logger.info("Job execution enqueued successfully.", start_at=start_at)
        elif start_in is not None:
            #  future_date = datetime.now(tz=timezone.utc) + start_in
            logger.debug("Enqueuing job execution in the future...", start_in=start_in)
            enqueued_id = app.jobs_queue.enqueue_in(start_in, Categories.Job, *args)
            #  result = scheduler.enqueue_at(future_date, run_job, *args)
            self.metadata["enqueued_id"] = enqueued_id
            queue_job_id = enqueued_id
            self.save()
            logger.info("Job execution enqueued successfully.", start_in=start_in)
        elif cron is not None:
            logger.debug("Enqueuing job execution using cron...", cron=cron)
            enqueued_id = app.jobs_queue.enqueue_cron(cron, Categories.Job, *args)
            self.metadata["enqueued_id"] = enqueued_id
            queue_job_id = enqueued_id
            self.metadata["cron"] = cron
            self.scheduled = True
            self.save()
            logger.info("Job execution enqueued successfully.", cron=cron)

        return queue_job_id 
Example 43
Project: reddit_feed   Author: simon987   File: run.py    GNU General Public License v3.0 4 votes vote down vote up
def stream_thing(prefix, publish_queue, mon_queue=None, start_id=None):
    if start_id is None:
        start_id = read_cursor(prefix)

    logger.info("Starting stream for %s at cursor %s" % (prefix, start_id))

    iterable = reddit_ids(prefix, start_id)

    while True:
        chunk = list(islice(iterable, 100))

        params = {"id": ",".join(chunk)}
        results = reddit.get(API_PATH["info"], params=params)
        if not results:
            logger.warning("No results!")
            continue
        post_time = datetime.utcfromtimestamp(results[0].created_utc)
        now = datetime.utcnow()
        distance = now - post_time

        logger.debug("[+%d] (%s) We are %s away from realtime (%s)" %
                     (len(results), prefix, distance, datetime.fromtimestamp(results[0].created_utc)))
        if distance < REALTIME_DELAY:
            sleep_time = (REALTIME_DELAY - distance).total_seconds()
            logger.info("Sleeping %s seconds" % sleep_time)
            time.sleep(sleep_time)

        update_cursor(results[0].id, prefix)

        for result in results:
            publish_queue.put(result)

        if MONITORING:
            mon_queue.put([{
                "measurement": "reddit",
                "time": str(datetime.utcnow()),
                "tags": {
                    "item_type": prefix,
                },
                "fields": {
                    "item_count": len(results),
                    "distance": distance.total_seconds()
                }
            }]) 
Example 44
Project: archon   Author: economicnetwork   File: models.py    MIT License 4 votes vote down vote up
def conv_timestamp_tx(ts, exchange):   
    target_format = '%Y-%m-%dT%H:%M:%S' 
    if exchange==exc.CRYPTOPIA:
        #tsf = datetime.datetime.utcfromtimestamp(int(ts)/1000)
        tsf = datetime.datetime.strptime(ts,'%Y-%m-%dT%H:%M:%S')
        utc=pytz.UTC
        utc_dt = tsf.astimezone(pytz.utc)
        tsf = utc_dt.strftime(target_format)
        return tsf
        
        #return utc_dt
    elif exchange==exc.BITTREX:
        ts = ts.split('.')[0]
        tsf = datetime.datetime.strptime(ts,'%Y-%m-%dT%H:%M:%S')
        utc=pytz.UTC
        utc_dt = tsf.astimezone(pytz.utc)
        utc_dt = utc_dt + datetime.timedelta(hours=4)
        tsf = utc_dt.strftime(target_format)
        return tsf
    elif exchange==exc.KUCOIN:
        tsf = datetime.datetime.utcfromtimestamp(ts)
        #tsf = datetime.datetime.strptime(ts,'%Y-%m-%dT%H:%M:%S')
        utc=pytz.UTC
        utc_dt = tsf.astimezone(pytz.utc)
        utc_dt = utc_dt + datetime.timedelta(hours=2)
        tsf = utc_dt.strftime(target_format)
        return tsf
        #tsf = utc_dt.strftime('%H:%M:%S')
        #return tsf

    elif exchange==exc.HITBTC:
        #2018-10-18T00:00:00.000Z
        ts = ts[:-5]
        x = datetime.datetime.strptime(ts,'%Y-%m-%dT%H:%M:%S')
        utc_dt = x.astimezone(pytz.utc)
        utc_dt = utc_dt + datetime.timedelta(hours=2)
        tsf = utc_dt.strftime(target_format)
        return tsf

    elif exchange==exc.BINANCE:
        #ts = int(t1/1000)
        #ts = datetime.utcfromtimestamp(ts).strftime('%Y-%m-%d %H:%M:%S')
        
        tsf = datetime.datetime.utcfromtimestamp(int(ts/1000))
        utc=pytz.UTC
        utc_dt = tsf.astimezone(pytz.utc)
        utc_dt = utc_dt + datetime.timedelta(hours=2)
        tsf = utc_dt.strftime(target_format)
        return tsf 
Example 45
Project: archon   Author: economicnetwork   File: models.py    MIT License 4 votes vote down vote up
def conv_timestamp_tx_dt(ts, exchange):   
    target_format = '%Y-%m-%dT%H:%M:%S' 
    if exchange==exc.CRYPTOPIA:
        #tsf = datetime.datetime.utcfromtimestamp(int(ts)/1000)
        tsf = datetime.datetime.strptime(ts,'%Y-%m-%dT%H:%M:%S')
        utc=pytz.UTC
        utc_dt = tsf.astimezone(pytz.utc)
        return utc_dt
        
        #return utc_dt
    elif exchange==exc.BITTREX:
        ts = ts.split('.')[0]
        tsf = datetime.datetime.strptime(ts,'%Y-%m-%dT%H:%M:%S')
        utc=pytz.UTC
        utc_dt = tsf.astimezone(pytz.utc)
        utc_dt = utc_dt + datetime.timedelta(hours=4)
        return utc_dt

    elif exchange==exc.KUCOIN:
        tsf = datetime.datetime.utcfromtimestamp(ts)
        #tsf = datetime.datetime.strptime(ts,'%Y-%m-%dT%H:%M:%S')
        utc=pytz.UTC
        utc_dt = tsf.astimezone(pytz.utc)
        utc_dt = utc_dt + datetime.timedelta(hours=2)
        return utc_dt
        #tsf = utc_dt.strftime('%H:%M:%S')
        #return tsf

    elif exchange==exc.HITBTC:
        #2018-10-18T00:00:00.000Z
        ts = ts[:-5]
        x = datetime.datetime.strptime(ts,'%Y-%m-%dT%H:%M:%S')
        utc_dt = x.astimezone(pytz.utc)
        utc_dt = utc_dt + datetime.timedelta(hours=2)
        return utc_dt

    elif exchange==exc.BINANCE:
        #ts = int(t1/1000)
        #ts = datetime.utcfromtimestamp(ts).strftime('%Y-%m-%d %H:%M:%S')
        
        tsf = datetime.datetime.utcfromtimestamp(int(ts/1000))
        utc=pytz.UTC
        utc_dt = tsf.astimezone(pytz.utc)
        utc_dt = utc_dt + datetime.timedelta(hours=2)
        return utc_dt