Timestamp format iso with timezone and special format - python

I was trying some API e-commerce and i followed their example, but I keep getting Timestamp error / mismatch.
The API requires a timestamp in this format:
2017-08-29T09:54:00+07:00
I tried:
datetime.now().isoformat()
But it returns without the timezone:
'2017-08-29T09:54:57.702000'
then I tried:
datetime.now(pytz.timezone( 'Asia/Jakarta' )).isoformat()
but it returns:
2017-08-29T09:54:00.547000+07:00
Basically the python version doesn't use : for the minutes and seconds and it seems they also gives the microsecond which the API does not need. How do i get this format in python 2?
year-month-day then T hour-minute-second+Timezone
2017-08-29T09:54:00+07:00

I got it
datetime.now(pytz.timezone( 'Asia/Jakarta')).replace(microsecond=0).isoformat()

Related

Python DateTime does not match format when use the function inside celery task

I create a function to get the different between two date time and its working fine, But when i use it in celery it returns the following error:
ValueError: time data '2023-02-16T14:38:33.301574' does not match format '%Y-%m-%d %H:%M:%S.%f'
Any advice please
I recommend not doing this. Python standard library gives you everything you need to deal with ISO 8601 formatted date/time data. I advise you to use datetime.datetime.fromisoformat and datetime.datetime.isoformat instead.

querying Elasticsearch for parse date field with format

I am querying Elasticsearch based on date, passing in a date and time string in this format yyyy-mm-dd hh:mm:ss, but Elasticsearch and DateTime are unable to accept this format.
I am writing a script that takes input and queries Elasticsearch based on those inputs, primarily by index and date-time. I've written the script using command line arguments, entering the date-time in the same format, and the script runs perfectly. However, when I try converting the script running with hardcoded inputs, the error appears:
error elasticsearch.exceptions.RequestError: RequestError(400, 'search_phase_execution_exception', 'failed to parse date field
[2019-07-01 00:00:00] with format
[strict_date_optional_time||epoch_millis]')
#this throws the error
runQueryWithoutCommandLine("log4j-*", "2019-07-01 00:00:00", "csv", "json")
#this does not throw error
def runQueryWithCommandLine(*args):
# "yyyy-mm-dd hh:mm:ss" date-time format is given in commandline
Why is this error appearing, and how can I get rid of it? Thank you!
The Date format "strict_date_optional_time||epoch_millis" in elastic uses the ISO date format standards.
As can be seen in link above, the ISO format for string representation of date is :
date-opt-time = date-element ['T' [time-element] [offset]]
In your case, the time portion is separated by a whitespace and not the 'T' and hence the parsing error.
In addition, as I see the time mentioned is 00:00:00, you can simply omit this as this is what that's taken as default is no time portion is specified.
So, any of below date value will work:
1) 2019-07-01T00:00:00
2) 2019-07-01

Redshift COPY Statement Date load error

I am loading the data using COPY command.
My Dates are in the following format.
D/MM/YYYY eg. 1/12/2016
DD/MM/YYYY eg. 23/12/2016
My target table data type is DATE. I am getting the following error "Invalid Date Format - length must be 10 or more"
As per the AWS Redshift documentation,
The default date format is YYYY-MM-DD. The default time stamp without
time zone (TIMESTAMP) format is YYYY-MM-DD HH:MI:SS.
So, as your date is not in the same format and of different length, you are getting this error. Append the following at the end of your COPY command and it should work.
[[COPY command as you are using right now]] + DATEFORMAT 'DD/MM/YYYY'
Not sure about the single digit case though. You might want to pad the incoming values with a 0 in the beginning to match the format length.

storing date into mongodb using python in ISO format

I am trying to store date into mongodb using python(bottle framework).
I want to store it in the below format:
ISODate("2015-06-08 03:38:28")
Currently I am using the following command:
datetime.strptime(DateField, '%m/%d/%Y %H:%M:%S %p')
it is getting stored like this:
ISODate("2015-06-08T03:38:28Z")
How to store it without "T" and "Z" in it??
You are confusing how something is stored vs. how something is displayed.
In MongoDB, dates are stored as 64 bit integers, what you are seeing is the way it is represented so that we can easily determine what date and time the 64bit number represents.
The ISODate is just a helper method, which formats the date in the ISO date format.
So when you pass it in a normal date and time string, it will convert it into the correct format.
The format adds the T (to separate the time part) and the Z (as you have not identified a time zone, it is defaulted to UTC).
In short - you are not storing it with the T and the Z, that's just how it is displayed back to you.

Conversion of datetime in python

Trying to use datetime to get an age of ec2 instance by comparing launch_time to current time. All working fine by using format below:
datetime.datetime.strptime(instance.launch_time, "%Y-%m-%dT%H:%M:%S.%fZ")
Unfortunately I've got an one with 0 microseconds, so getting an error about not matching format:
time data '2015-03-16T03:21:05Z' does not match format '%Y-%m-%dT%H:%M:%S.%fZ')
2015-03-16T03:02:12.910Z
2015-03-16T03:21:05Z - this one is problematic
2015-03-25T09:19:34.018Z
Any idea how to get around this? It looks like datetime is the easiest way to get this sorted but if there are quicker way of doing that, happy to see other options. FYI, comparision has to be done up to hour, so don't care about seconds. Thanks.

Categories