keep timezone "CET" from convert into "CEST" in python - python

If I use the following command to localize my timestamp to "CET":
pd.Timestamp('2011-11-06 11:00:00').tz_localize('CET')
output: Timestamp('2011-11-06 11:00:00+0100', tz='CET')
if I use :
pd.Timestamp('2011-06-06 11:00:00').tz_localize('CET')
output: Timestamp('2011-06-06 11:00:00+0200', tz='CET')
So obviously it automatically convert into "CEST" in summer, am I right? what if I would like to keep my timestamp in "CET" through out the whole year (keep it UTC +1:00 all the time), what should I do? Thank you very much!
Updated:
the following seems working:
pd.Timestamp('2011-06-06 11:00:00').tz_localize(tz=pytz.FixedOffset(60))
output:Timestamp('2011-06-06 11:00:00+0100', tz='pytz.FixedOffset(60)')
any other suggestions?

Related

How do I add AM/PM to adatetime.strftime module?

I've incorporated a datetime module that gives the output of the current time, but it does not include AM/PM in the output. Specifically I am using the "Now" and "strftime" features to give the exact time in the output. I am wondering what else I might need to add in order for Am or PM to show but I could not find anything to figure it out. Any help is appreciated.
import datetime
my_datetime = datetime.datetime.now()
print("Checkout at:", my_datetime.strftime("%Y-%m-%d %H:%M:%S"))
For reference, the output right now looks like this:
"Checkout at: 2022-07-28 01:12:07"

Convert Instagram response device time stamp to readable date time

I'm using Instagram-API-python to create an application. I'm getting a JSON response with below value.
'device_timestamp': 607873890651
I tried to convert this value to readable using python.
import time
readable = time.ctime(607873890651)
print(readable)
It gives following result and seems it is not correct.
Sun Oct 3 16:00:51 21232
I'm not much familiar with the Instagram-API-python. Please someone can help me to solve this problem.
The data is very likely to be incorrect.
Timestamp is a very standard way to store a date-time. Counting the seconds that passed since January 1st, 1970, also known as the UNIX Epoch.
I looked for "Instagram 'device_timestamp'" on Google and all the user-provided values made sense, but yours doesn't.
This is probably an error from the database, it happens.
Use the mentioned ctime conversion, but take the 'taken_at' field if available.
Don't use device_timestamp but use taken_at field. Then taken_at need multiply to 1000.
In Java it looks like this
Date data = new Date(taken_at * 1000);

Converting "2013-01-06T22:25:08.733" to "2013-01-06 22:25:08" in python

I have a big .csv file which holds machine log data. One of the fields is timestamp. It stores date and time as shown in the title and I would like to drop the milli seconds and convert it into the format also shown in title. Can anyone help me with that? Im new to Python and Ipython.
Much obliged.
For your special case, this should suffice:
t.replace('T', ' ')[:19]
But I would recommend, that you use the datetime module of the standard library instead, so your time conversion also could be internationalized.
You can use easy_date to make it easy:
import date_converter
new_date = date_converter.string_to_string("2013-01-06T22:25:08.733", "%Y-%m-%dT%H:%M:%S.%f", "%Y-%m-%d %H:%M:%S")

strptime defaulting to 1900

from datetime import datetime
datetime.strptime('%b%d %I:%M%p', 'AUG21 3:26PM')
results with
1900-08-21 15:26:00
how can I write in pythonic way so that when there's no year, take the current year as default (2013)?
I checked and strftime function doesn't have option to change the default.. maybe another time libraries can do?
thx
Parse the date as you are already doing, and then
date= date.replace(2013)
This is one of simplest solution with the modules you are using.
Thinking better about it, you will probably face a problem next Feb 29.
input= 'Aug21 3:26PM'
output= datetime.datetime.strptime('2013 '+ input ,'%Y %b%d %I:%M%p')
You can find out today's date to replace year for dynamic replacement.
datetime.strptime('%b%d %I:%M%p', 'AUG21 3:26PM').replace(year=datetime.today().year)

validate date : python

I want to know how to convert different format dates to expected format in python .
ex : i want to get this format : 2/29/2012
['2012-02-01 // 2012-02-28', '2/15/2012', '2/13/2012', '2/14/2012', '2/23/2012', '2/18/2012', '2/29/2012']
How to check today date in the range '2012-02-01 // 2012-02-28'
Share your suggestions
Use the datetime library in python. You can just compare two different datetime.datetime objects. And you can separate the year, month, date thing to put it in anyform you want.
Check this link for all the library details.
http://docs.python.org/library/datetime.html
Hope that helped.
The dateutil python library parses a wider variety of date formats than the standard datetime module.

Categories