php date style date formatting see https www php netdate for format strings usage from datetime import datetime d datetime now df dateformatd printdf format js f y h i 7th october 2003 11 39 timezone is only supported when formatting datetime objects not date objects timezone information not appropriate or time objects against established django policy timezone name if timezone information is not available return an empty string time in 12hour hours and minutes with minutes left off if they re zero examples 1 1 30 2 05 2 proprietary extension difference to greenwich time in hours e g 0200 0430 if timezone information is not available return an empty string time in 12hour hours minutes and a m p m with minutes left off if they re zero and the strings midnight and noon if appropriate examples 1 a m 1 30 p m midnight noon 12 30 p m proprietary extension time zone of this machine e g est or mdt if timezone information is not available return an empty string time zone offset in seconds i e 43200 to 43200 the offset for timezones west of utc is always negative and for those east of utc is always positive if timezone information is not available return an empty string offset is a datetime timedelta for negative values to the west of utc only days can be negative days1 and seconds are always positive e g utc1 timedeltadays1 seconds82800 microseconds0 positive offsets have days0 iso 8601 format example 20080102t10 30 00 000123 assume midnight in default timezone if datetime date provided english ordinal suffix for the day of the month 2 characters i e st nd rd or th year 2 digits with leading zeros e g 99 return 02d self data year 100 def yself day of the year i e 1 to 366 return self data timetuple tmyday def formatvalue formatstring convenience function df dateformatvalue return df formatformatstring def timeformatvalue formatstring convenience function tf timeformatvalue return tf formatformatstring timezone is only supported when formatting datetime objects not date objects timezone information not appropriate or time objects against established django policy timezone name if timezone information is not available return an empty string time in 12 hour hours and minutes with minutes left off if they re zero examples 1 1 30 2 05 2 proprietary extension noqa e743 e741 difference to greenwich time in hours e g 0200 0430 if timezone information is not available return an empty string time in 12 hour hours minutes and a m p m with minutes left off if they re zero and the strings midnight and noon if appropriate examples 1 a m 1 30 p m midnight noon 12 30 p m proprietary extension time zone of this machine e g est or mdt if timezone information is not available return an empty string time zone offset in seconds i e 43200 to 43200 the offset for timezones west of utc is always negative and for those east of utc is always positive if timezone information is not available return an empty string offset is a datetime timedelta for negative values to the west of utc only days can be negative days 1 and seconds are always positive e g utc 1 timedelta days 1 seconds 82800 microseconds 0 positive offsets have days 0 iso 8601 format example 2008 01 02t10 30 00 000123 noqa e743 e741 noqa e743 e741 assume midnight in default timezone if datetime date provided english ordinal suffix for the day of the month 2 characters i e st nd rd or th special case year 2 digits with leading zeros e g 99 year 4 digits with leading zeros e g 1999 day of the year i e 1 to 366
import calendar
from datetime import date, datetime, time
from email.utils import format_datetime as format_datetime_rfc5322

from django.utils.dates import (
    MONTHS,
    MONTHS_3,
    MONTHS_ALT,
    MONTHS_AP,
    WEEKDAYS,
    WEEKDAYS_ABBR,
)
from django.utils.regex_helper import _lazy_re_compile
from django.utils.timezone import (
    _datetime_ambiguous_or_imaginary,
    get_default_timezone,
    is_naive,
    make_aware,
)
from django.utils.translation import gettext as _

re_formatchars = _lazy_re_compile(r"(?<!\\)([aAbcdDeEfFgGhHiIjlLmMnNoOPrsStTUuwWyYzZ])")
re_escaped = _lazy_re_compile(r"\\(.)")


class Formatter:
    def format(self, formatstr):
        pieces = []
        for i, piece in enumerate(re_formatchars.split(str(formatstr))):
            if i % 2:
                if type(self.data) is date and hasattr(TimeFormat, piece):
                    raise TypeError(
                        "The format for date objects may not contain "
                        "time-related format specifiers (found '%s')." % piece
                    )
                pieces.append(str(getattr(self, piece)()))
            elif piece:
                pieces.append(re_escaped.sub(r"\1", piece))
        return "".join(pieces)


class TimeFormat(Formatter):
    def __init__(self, obj):
        self.data = obj
        self.timezone = None

        if isinstance(obj, datetime):
            
            
            
            if is_naive(obj):
                timezone = get_default_timezone()
            else:
                timezone = obj.tzinfo
            if not _datetime_ambiguous_or_imaginary(obj, timezone):
                self.timezone = timezone

    def a(self):
        "'a.m.' or 'p.m.'"
        if self.data.hour > 11:
            return _("p.m.")
        return _("a.m.")

    def A(self):
        "'AM' or 'PM'"
        if self.data.hour > 11:
            return _("PM")
        return _("AM")

    def e(self):
        
        if not self.timezone:
            return ""

        try:
            if getattr(self.data, "tzinfo", None):
                return self.data.tzname() or ""
        except NotImplementedError:
            pass
        return ""

    def f(self):
        
        hour = self.data.hour % 12 or 12
        minute = self.data.minute
        return "%d:%02d" % (hour, minute) if minute else hour

    def g(self):
        "Hour, 12-hour format without leading zeros; i.e. '1' to '12'"
        return self.data.hour % 12 or 12

    def G(self):
        "Hour, 24-hour format without leading zeros; i.e. '0' to '23'"
        return self.data.hour

    def h(self):
        "Hour, 12-hour format; i.e. '01' to '12'"
        return "%02d" % (self.data.hour % 12 or 12)

    def H(self):
        "Hour, 24-hour format; i.e. '00' to '23'"
        return "%02d" % self.data.hour

    def i(self):
        "Minutes; i.e. '00' to '59'"
        return "%02d" % self.data.minute

    def O(self):  
        
        if self.timezone is None:
            return ""

        offset = self.timezone.utcoffset(self.data)
        seconds = offset.days * 86400 + offset.seconds
        sign = "-" if seconds < 0 else "+"
        seconds = abs(seconds)
        return "%s%02d%02d" % (sign, seconds // 3600, (seconds // 60) % 60)

    def P(self):
        
        if self.data.minute == 0 and self.data.hour == 0:
            return _("midnight")
        if self.data.minute == 0 and self.data.hour == 12:
            return _("noon")
        return "%s %s" % (self.f(), self.a())

    def s(self):
        "Seconds; i.e. '00' to '59'"
        return "%02d" % self.data.second

    def T(self):
        
        if self.timezone is None:
            return ""

        return str(self.timezone.tzname(self.data))

    def u(self):
        "Microseconds; i.e. '000000' to '999999'"
        return "%06d" % self.data.microsecond

    def Z(self):
        
        if self.timezone is None:
            return ""

        offset = self.timezone.utcoffset(self.data)

        
        
        
        
        return offset.days * 86400 + offset.seconds


class DateFormat(TimeFormat):
    def b(self):
        "Month, textual, 3 letters, lowercase; e.g. 'jan'"
        return MONTHS_3[self.data.month]

    def c(self):
        
        return self.data.isoformat()

    def d(self):
        "Day of the month, 2 digits with leading zeros; i.e. '01' to '31'"
        return "%02d" % self.data.day

    def D(self):
        "Day of the week, textual, 3 letters; e.g. 'Fri'"
        return WEEKDAYS_ABBR[self.data.weekday()]

    def E(self):
        "Alternative month names as required by some locales. Proprietary extension."
        return MONTHS_ALT[self.data.month]

    def F(self):
        "Month, textual, long; e.g. 'January'"
        return MONTHS[self.data.month]

    def I(self):  
        "'1' if daylight saving time, '0' otherwise."
        if self.timezone is None:
            return ""
        return "1" if self.timezone.dst(self.data) else "0"

    def j(self):
        "Day of the month without leading zeros; i.e. '1' to '31'"
        return self.data.day

    def l(self):  
        "Day of the week, textual, long; e.g. 'Friday'"
        return WEEKDAYS[self.data.weekday()]

    def L(self):
        "Boolean for whether it is a leap year; i.e. True or False"
        return calendar.isleap(self.data.year)

    def m(self):
        "Month; i.e. '01' to '12'"
        return "%02d" % self.data.month

    def M(self):
        "Month, textual, 3 letters; e.g. 'Jan'"
        return MONTHS_3[self.data.month].title()

    def n(self):
        "Month without leading zeros; i.e. '1' to '12'"
        return self.data.month

    def N(self):
        "Month abbreviation in Associated Press style. Proprietary extension."
        return MONTHS_AP[self.data.month]

    def o(self):
        "ISO 8601 year number matching the ISO week number (W)"
        return self.data.isocalendar().year

    def r(self):
        "RFC 5322 formatted date; e.g. 'Thu, 21 Dec 2000 16:01:07 +0200'"
        value = self.data
        if not isinstance(value, datetime):
            
            default_timezone = get_default_timezone()
            value = datetime.combine(value, time.min).replace(tzinfo=default_timezone)
        elif is_naive(value):
            value = make_aware(value, timezone=self.timezone)
        return format_datetime_rfc5322(value)

    def S(self):
        
        if self.data.day in (11, 12, 13):  
            return "th"
        last = self.data.day % 10
        if last == 1:
            return "st"
        if last == 2:
            return "nd"
        if last == 3:
            return "rd"
        return "th"

    def t(self):
        "Number of days in the given month; i.e. '28' to '31'"
        return calendar.monthrange(self.data.year, self.data.month)[1]

    def U(self):
        "Seconds since the Unix epoch (January 1 1970 00:00:00 GMT)"
        value = self.data
        if not isinstance(value, datetime):
            value = datetime.combine(value, time.min)
        return int(value.timestamp())

    def w(self):
        "Day of the week, numeric, i.e. '0' (Sunday) to '6' (Saturday)"
        return (self.data.weekday() + 1) % 7

    def W(self):
        "ISO-8601 week number of year, weeks starting on Monday"
        return self.data.isocalendar().week

    def y(self):
        
        return "%02d" % (self.data.year % 100)

    def Y(self):
        
        return "%04d" % self.data.year

    def z(self):
        
        return self.data.timetuple().tm_yday


def format(value, format_string):
    "Convenience function"
    df = DateFormat(value)
    return df.format(format_string)


def time_format(value, format_string):
    "Convenience function"
    tf = TimeFormat(value)
    return tf.format(format_string)