Running Function on all Items in a List (Converting List of Dates to Unix Timestamp)īecause you use the same date over and over again which will always give the same UNIX time ( (), with datetime being the datetime module here). In any case, the result dt_obj.timestamp() Or, as suggested, instead of setting the tzinfo attribute explicitly, you can also modify the input string by adding "+00:00" which is parsed to UTC dt_obj = omisoformat(" 12:51:47" + "+00:00") Since Python's datetime treats naive datetime as local time by default, you need to set the time zone ( tzinfo attribute): from datetime import datetime, timezoneĭt_obj = omisoformat(" 12:51:47").replace(tzinfo=timezone.utc) You should use dateutil.parser instead: > from dateutil.parser import isoparseĭatetime.datetime(2020, 1, 1, 1, 39, 40, tzinfo=tzutc()) Python does not fully support the ISO 8601 standard. News_date_datetime = news_date_datetime.replace(tzinfo=tz) News_date_datetime = datetime.strptime(news_date, '%a, %d %b %Y %H:%M:%S') # get appropriate timezone from string, according to tzmapping: If you have multiple strings with different timezones, you could use a dict to map the abbreviations to time zone names, e.g. News_date = news_date.replace(tzinfo=dateutil.tz.gettz('US/Pacific')) News_date = datetime.strptime(news_date, '%a, %d %b %Y %H:%M:%S') %Z can't parse the timezone name PT - I suggest you skip parsing it and add it "manually" instead: from datetime import datetime if your datetime object is aware of timezones) How can I convert datetime to unix timestamp in python NewDate = time.mktime(datetime.timetuple())Īs an example I did: from datetime import datetimeĬredit to use t.utctimetuple() if you want the result in UTC (e.g. NewDate = time.mktime(datetime.strptime(toDayDate, "%Y-%m-%d %H:%M:%S").timetuple()) Sebastian and I were talking about in the comments should be doable with just the stdlib, and working the same way on both Unix and Windows.) Converting to unix timestamp Python (And if you can wait for Python 3.4, it looks like PEP 341 is likely to make it into the final release, which means all of the stuff J.F. And even if you don't, you may want to consider borrowing its source code. If you have, or can upgrade to, Python 3.3 or later, you can avoid all of these problems by just using the timestamp method instead of trying to figure out how to do it yourself. Or, if you had an aware datetime object, you need to either use a local (aware) epoch on both sides, or explicitly convert to and from UTC. On the other hand, if your original naive datetime was local, you shouldn't have subtracted a UTC timestamp from it in the first place use omtimestamp(0) instead. If your original naive datetime was UTC, the way to recover it is to use utcfromtimestamp instead of fromtimestamp. You need to read the top of the datetime docs, which explain about timezones and "naive" and "aware" objects. Presumably you've five hours off UTC, so T11:00:00 local and are the same time. %z or %Z but again, depends on your use case.Convert datetime to Unix timestamp and convert it back in python but maybe this isn't a problem for your use case.Īnother option may be to ignore the TZ issue, and just include a time zone indicator in your format string e.g. Now the drawback here is of course everyone who can read this search is running this search with the same Splunk role, so no per-user index filtering is happening at search time here. NOT with | savedsearch "searchName" ), the search will then execute as the owner instead of as the user, and magic, standardized TZs. using in SimpleXML or a ds.savedSearch datasource in Dashboard Studio. When your search is loaded in the dashboard by a reference (e.g. This report should be shared in app, readable by all roles who should be able to read and execute the searches on the dashboard, owned by a service account who has the correct timezone in their user preference, and configured to be Run As Owner) So a possible way around this, instead of having your search in your dashboard directly, you save the search as a saved report.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |