time.mktime(datetime.timetuple()) seems behaving incorrectly - python-3.x

Hi I have time converted into gmt as below
2016-11-18 13:00:00+00:00
I want to convert this into millis which I am doing as below
epoch = int(time.mktime(datetime_in_gmt.timetuple()))
>>>print(epoch)
1479454200
and then when I do use this link http://www.epochconverter.com/ and paste this epoch i.e 1479454200 I get the result as
GMT: Fri, 18 Nov 2016 07:30:00 GMT
Your time zone: Friday 18 November 2016 01:00:00 PM IST GMT+5:30
I am not getting as why I am getting 18 Nov 7.30 as GMT because my GMT time was 2016-11-18 13:00:00+00:00 ?
any suggestion

Instead of timetuple use timestamp.
Something like
int(datetime_in_gmt.timestamp()) * 1000

Related

Change various strings to timestamp Python 3?

Given all possible time formats I can get from my user (chatbot):
09:03
9:23A.M.
9:23 A.m.
13:44 pm
20:00 P.m
15:40
00:00
12:33
4:33p.M
...
I want to convert them to appropriate time. There won't be one format I can use.
Please advise how to solve such issue?
I have tried with strptime from the datetime package but I need to specify format and it can be different every time.
Find my answer thanks to this amazing source!
Posting for others:
dateutil
The dateutil module is an extension to the datetime module. We don't need to pass any parsing code to parse a string. For example:
from dateutil.parser import parse
datetime = parse('2018-06-29 22:21:41')
print(datetime)
This parse function will parse the string automatically and store it in the datetime variable. Parsing is done automatically. You don't have to mention any format string. Let's try to parse different types of strings using dateutil:
from dateutil.parser import parse
date_array = [
'2018-06-29 08:15:27.243860',
'Jun 28 2018 7:40AM',
'Jun 28 2018 at 7:40AM',
'September 18, 2017, 22:19:55',
'Sun, 05/12/1999, 12:30PM',
'Mon, 21 March, 2015',
'2018-03-12T10:12:45Z',
'2018-06-29 17:08:00.586525+00:00',
'2018-06-29 17:08:00.586525+05:00',
'Tuesday , 6th September, 2017 at 4:30pm'
]
for date in date_array:
print('Parsing: ' + date)
dt = parse(date)
print(dt.date())
print(dt.time())
print(dt.tzinfo)
print('\n')
Output:
$ python3 dateutil-1.py
Parsing: 2018-06-29 08:15:27.243860
2018-06-29
08:15:27.243860
None
Parsing: Jun 28 2018 7:40AM
2018-06-28
07:40:00
None
Parsing: Jun 28 2018 at 7:40AM
2018-06-28
07:40:00
None
Parsing: September 18, 2017, 22:19:55
2017-09-18
22:19:55
None
Parsing: Sun, 05/12/1999, 12:30PM
1999-05-12
12:30:00
None
Parsing: Mon, 21 March, 2015
2015-03-21
00:00:00
None
Parsing: 2018-03-12T10:12:45Z
2018-03-12
10:12:45
tzutc()
Parsing: 2018-06-29 17:08:00.586525+00:00
2018-06-29
17:08:00.586525
tzutc()
Parsing: 2018-06-29 17:08:00.586525+05:00
2018-06-29
17:08:00.586525
tzoffset(None, 18000)
Parsing: Tuesday , 6th September, 2017 at 4:30pm
2017-09-06
16:30:00
None
You can see that almost any type of string can be parsed easily using the dateutil module.
Maya
Maya also makes it very easy to parse a string and for changing timezones. Some simple examples are shown below:
import maya
dt = maya.parse('2018-04-29T17:45:25Z').datetime()
print(dt.date())
print(dt.time())
print(dt.tzinfo)
Output:
$ python3 maya-1.py
2018-04-29
17:45:25
UTC
For converting the time to a different timezone:
import maya
dt = maya.parse('2018-04-29T17:45:25Z').datetime(to_timezone='America/New_York', naive=False)
print(dt.date())
print(dt.time())
print(dt.tzinfo)
Output:
$ python3 maya-2.py
2018-04-29
13:45:25
America/New_York
Now isn't that easy to use? Let's try out maya with the same set of strings we have used with dateutil:
import maya
date_array = [
'2018-06-29 08:15:27.243860',
'Jun 28 2018 7:40AM',
'Jun 28 2018 at 7:40AM',
'September 18, 2017, 22:19:55',
'Sun, 05/12/1999, 12:30PM',
'Mon, 21 March, 2015',
'2018-03-12T10:12:45Z',
'2018-06-29 17:08:00.586525+00:00',
'2018-06-29 17:08:00.586525+05:00',
'Tuesday , 6th September, 2017 at 4:30pm'
]
for date in date_array:
print('Parsing: ' + date)
dt = maya.parse(date).datetime()
print(dt)
print(dt.date())
print(dt.time())
print(dt.tzinfo)
Output:
$ python3 maya-3.py
Parsing: 2018-06-29 08:15:27.243860
2018-06-29 08:15:27.243860+00:00
2018-06-29
08:15:27.243860
UTC
Parsing: Jun 28 2018 7:40AM
2018-06-28 07:40:00+00:00
2018-06-28
07:40:00
UTC
Parsing: Jun 28 2018 at 7:40AM
2018-06-28 07:40:00+00:00
2018-06-28
07:40:00
UTC
Parsing: September 18, 2017, 22:19:55
2017-09-18 22:19:55+00:00
2017-09-18
22:19:55
UTC
Parsing: Sun, 05/12/1999, 12:30PM
1999-05-12 12:30:00+00:00
1999-05-12
12:30:00
UTC
Parsing: Mon, 21 March, 2015
2015-03-21 00:00:00+00:00
2015-03-21
00:00:00
UTC
Parsing: 2018-03-12T10:12:45Z
2018-03-12 10:12:45+00:00
2018-03-12
10:12:45
UTC
Parsing: 2018-06-29 17:08:00.586525+00:00
2018-06-29 17:08:00.586525+00:00
2018-06-29
17:08:00.586525
UTC
Parsing: 2018-06-29 17:08:00.586525+05:00
2018-06-29 12:08:00.586525+00:00
2018-06-29
12:08:00.586525
UTC
Parsing: Tuesday , 6th September, 2017 at 4:30pm
2017-09-06 16:30:00+00:00
2017-09-06
16:30:00
UTC
As you can see, all date formats were parsed, but did you notice the difference? If we are not providing the timezone info it automatically converts it to UTC. So, it is important to note that we need to provide to_timezone and naive parameters if the time is not in UTC.

how to get yesterday starttime in UTC using momentjs

I want to get the epoch seconds using momentjs.
For example, time right now is 2019-04-20T15:07:04.388Z EST, I want to get yesterday start time epoch 2019-04-19T00:00:00.000Z in UTC.
I have tried the below code -
const now = new Date();
const start = moment(now) // get current datetime
.utcOffset(0) // convert to UTC
.subtract(24, "hours") // go 24 hours into the past
.startOf("day") // get the start of the date we landed on
.unix(); // get unix timestamp
console.log(now);
console.log(start);
The output of the above program is -
Sat Apr 20 2019 15:11:23 GMT-0400 (EDT) {}
1555650000
According to https://www.unixtimestamp.com/index.php the 1555650000 translates to Fri, 19 Apr 2019 05:00:00 +0000. But I want it to be Fri, 19 Apr 2019 00:00:00 +0000 in UTC.
The momentjs version used in our code -
"moment": "2.24.0",
"moment-timezone": "^0.5.23"
Any idea how can I get this?
you can try this one line statement
console.log( moment.utc().subtract(1, 'days').startOf('day').toString());
Output : Sun Apr 21 2019 00:00:00 GMT+0000

Why Sorting the timestamp using sort_values is not working?

I have a column of timestamp converted to human readable form.
I have tried to sort it from epochtime as well as after converting. It's giving me
Fri, 08 Feb 2019 17:24:16 IST
Mon, 11 Feb 2019 02:19:40 IST
Sat, 09 Feb 2019 00:22:43 IST
which is not sorted.
I have used sort_values()
each_tracker_df = each_tracker_df.sort_values(["timestamp"],ascending=True)
why it isn't working??
Since all the time is in IST. Replace the string IST with NULL.
>>import datetime
>>times=['Fri, 10 Feb 2010 17:24:16','Fri, 11 Feb 2010 17:24:16','Fri, 11 Feb 2019 17:24:16']
>>change_format=[]
>> for time in times:
change_format.append(datetime.datetime.strptime(time, '%a, %d %b %Y %H:%M:%S'))
>>change_format.sort()

convert date string to timestamp

is there any way to convert date string Tue Feb 23 2016 20:11:42 GMT+0200 (EET) to timestamp in nodejs?
I was trying to use moment.js with this:
moment('Tue Feb 23 2016 20:11:42 GMT+0200 (EET)', 'YYYY-MM-DD HH:mm:ss').valueOf()
but it returned NaN to me. Maybe its impossible?
There's no need for moment.js
var d = new Date("Tue Feb 23 2016 20:11:42 GMT+0200 (EET)");
var timeStamp = d.getTime();
Another way to do it with moment, but a little unnecessary is:
moment(Date.parse('Tue Feb 23 2016 20:11:42 GMT+0200 (EET)')).format('YYYY-MM-DD HH:mm:ss');
change format with any other format you may desire.

NodeJS add two hours to date?

I've got a date string as such:
Tue Jul 29 2014 23:44:06 GMT+0000 (UTC)
How can I add two hours to this?
So I get:
Tue Jul 29 2014 01:44:06 GMT+0000 (UTC)
Here's one solution:
var date = new Date('Tue Jul 29 2014 23:44:06 GMT+0000 (UTC)').getTime();
date += (2 * 60 * 60 * 1000);
console.log(new Date(date).toUTCString());
// displays: Wed, 30 Jul 2014 01:44:06 GMT
Obviously once you have the (new) date object, you can format the output to your liking if the native Date functions do not give you what you need.
Using MomentJS:
var moment = require('moment');
var date1 = moment("Tue Jul 29 2014 23:44:06 GMT+0000 (UTC)");
//sets an internal flag on the moment object.
date1.utc();
console.log(date1.format("ddd MMM DD YYYY HH:mm:ss [GMT]ZZ (UTC)"));
//adds 2 hours
date1.add(2, 'h');
console.log(date1.format("ddd MMM DD YYYY HH:mm:ss [GMT]ZZ (UTC)"));
Prints out the following:
Tue Jul 29 2014 23:44:06 GMT+0000 (UTC)
Wed Jul 30 2014 01:44:06 GMT+0000 (UTC)
This works well:
const epoch = new Date('01-01-2000')
const notBeforeDate = new Date(epoch).setSeconds(notBefore)
const notAfterDate = new Date(epoch).setSeconds(notAfter)
NOTE: the setSeconds() call actually adds seconds to the current Date value, it does not reset the Date to some absolute number of seconds. This detail is poorly addressed in the documentation and causes a lot of heartache when first trying to work with Dates in JavaScript.

Resources