3

I want to get current time in GMT, like 2015-08-21 05:13:13+00:00 Here you can see that correction is made like 00:00 for GMT, In django they are using from django.utils import timezone and date_to = timezone.now() to calculate the current time. How I will get the same functionality in flask, Formate of time should be like 2015-08-21 05:13:13+00:00 not like 2015-03-30 07:19:06.746037+02. I got this link but they are using 2015-03-30 07:19:06+02. I don't want. Just GTM time

Following code is in Django, I want same in Flask, Here they are using from django.utils import timezone. In Flask what is equivalent of it, which gives current time in this format 2015-08-21 05:13:13+00:00

import datetime
import pytz
from django.utils import timezone
def calc_date_args(date_from, date_to, date_options):


        try:
            date_to = timezone.now()
            print 'calc_date_args, date_to', date_to   #calc_date_args, date_to 2015-08-21 05:13:13.615541+00:00

            date_from = date_to - datetime.timedelta(days=float(date_options))

        except Exception:
            raise ValueError(_("The time delta must be a number representing "
                               "the time span in days"))

    return date_from, date_to
Community
  • 1
  • 1
geeks
  • 2,025
  • 6
  • 31
  • 49

1 Answers1

-3

Not sure if you mean UTC time instead of GMT, as most machines consume UTC and most timestamps are in UTC.

In Python 3 to get UTC timezone-aware timestamp:

import datetime

def now():
    """Get timezone-aware UTC timestamp."""
    return datetime.datetime.now(datetime.timezone.utc)
Mikko Ohtamaa
  • 82,057
  • 50
  • 264
  • 435
  • Thanks for reply, I tried your method but getting error `Traceback (most recent call last): File "", line 1, in AttributeError: 'module' object has no attribute 'timezone' ` – geeks Aug 21 '15 at 09:42
  • Your Python version is too old. `datetime.timezone` is available on Python 3.3+ – Mikko Ohtamaa Aug 21 '15 at 10:57