Date conversion on server in different timezone

2019-09-14 10:35发布

I have a date string (event occurrence) in local time (scraped from a web page using Scrapy) and I need to convert it to a timestamp. I do this using:

return int(time.mktime(time.strptime(dateString, "%Y-%m-%d")))

That gives me a time stamp that I store in my database. Every day I scrape the page for new data so I need to compare dates. I use the same function as above to convert to a timestamp that I compare with the timestamp in the database. This way I avoid duplicate records.

When I run this script locally (scheduled task) everything works as expected. However, when I run it on a remote server the conversion results in a different timestamp which means I end up with duplicate records. This is because I have pre-populated my database locally.

In my app which needs to display the actual date that the event occurred i have the following:

 /**
 * @summary Format date string
 *
 * @param date {Integer} Unix time stamp
 *
 * @return date {String}
 */
function formatDate( date ) {
    var m_names = new Array( "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" ),
        d = new Date( date ),
        curr_date = d.getUTCDate(),
        curr_month = d.getUTCMonth(),
        curr_year = d.getUTCFullYear();

    return curr_date + "-" + m_names[ curr_month ] + "-" + curr_year;
}

Since my app can display records (events) in several timezones at the same time, I need to ensure that the event date is correctly displayed.

I need some advice on how I can best achieve the desired result.

0条回答
登录 后发表回答