PHP-date <-> Python-date

Sockets, TCP/IP, (XML-)RPC und ähnliche Themen gehören in dieses Forum
Antworten
Benutzeravatar
jens
Python-Forum Veteran
Beiträge: 8502
Registriert: Dienstag 10. August 2004, 09:40
Wohnort: duisburg
Kontaktdaten:

Ich habe von PHP aus eine Formatierungs-Angabe für ein Datum (PHP-Format) Diese möchte ich aber für Python benutzen (Python-Format)

Bsp eines PHP-date-Formats: "j.m.Y G:i"
  • j - Tag des Monats (1-31)
    m - Monat *(01-12)
    Y - Jahreszahl, vierstellig (2001)
    G - Stunde im 24-Stunden-Format (0-23 )
    i - Minuten *(00-59)
In Python müßte also das selbe so aussehen: "%d.%m.%Y - %H:%M"

Muß ich mir jetzt ein dict basten oder gibt es da eine andere Möglichkeit???

Also ungefähr so:

Code: Alles auswählen

date = {
    "j" : "%d",
    "m" : "%m",
    "Y" : "%Y",
    "G" : "%H",
    "i" : "%M"
}
}

GitHub | Open HUB | Xing | Linked in
Bitcoins to: 1JEgSQepxGjdprNedC9tXQWLpS424AL8cd
Benutzeravatar
jens
Python-Forum Veteran
Beiträge: 8502
Registriert: Dienstag 10. August 2004, 09:40
Wohnort: duisburg
Kontaktdaten:

Hab mir nun was gebastelt... Sind allerdings nicht alle Formate erhalten

Code: Alles auswählen

import re

class convertdateformat:
    """
    Wandelt das PHP-date Format in's Python-Format
    z.B. PHP-date "j.m.Y G:i" -> "%d.%m.%Y - %H:%M"

    PHP-Format:
    selfphp.info/funktionsreferenz/datums_und_zeit_funktionen/date.php#beschreibung

    Python-Format:
    docs.python.org/lib/module-time.html#l2h-1941

    nicht eingebaute PHP-Formate:
    --------------------------------------------------------------------
    B - Tage bis Jahresende
    I - (großes i) 1 bei Sommerzeit, 0 bei Winterzeit
    L - Schaltjahr = 1, kein Schaltjahr = 0
    O - Zeitunterschied gegenüber Greenwich (GMT) in Stunden (z.B.: +0100)
    r - Formatiertes Datum (z.B.: Tue, 6 Jul 2004 22:58:15 +0200)
    S - Englische Aufzählung (th für 2(second))
    t - Anzahl der Tage des Monats (28 – 31)
    T - Zeitzoneneinstellung des Rechners (z.B. CEST)
    U - Sekunden seit Beginn der UNIX-Epoche (1.1.1970)
    Z - Offset der Zeitzone gegenüber GTM (-43200 – 43200) in Minuten

    nicht eingebaute Python-Formate:
    --------------------------------------------------------------------
    %c 	Locale's appropriate date and time representation.
    %x 	Locale's appropriate date representation.
    %X 	Locale's appropriate time representation.
    %Z 	Time zone name (no characters if no time zone exists).
    """
    def __init__( self ):
        self.PHP2Python_date = {
            "d" : "%d", # Tag des Monats *( 01 – 31 )
            "j" : "%d", # Tag des Monats (1-31)
            "D" : "%a", # Tag der Woche (3stellig:Mon)
            "l" : "%A", # Tag der Woche (ausgeschrieben:Monday)

            "m" : "%m", # Monat *(01-12)
            "n" : "%m", # Monat (1-12)
            "F" : "%B", # Monatsangabe (December – ganzes Wort)
            "M" : "%b", # Monatsangabe (Feb – 3stellig)

            "y" : "%y", # Jahreszahl, zweistellig (01)
            "Y" : "%Y", # Jahreszahl, vierstellig (2001)

            "g" : "%I", # Stunde im 12-Stunden-Format (1-12 )
            "G" : "%H", # Stunde im 24-Stunden-Format (0-23 )
            "h" : "%I", # Stunde im 12-Stunden-Format *(01-12 )
            "H" : "%H", # Stunde im 24-Stunden-Format *(00-23 )
            "i" : "%M", # Minuten *(00-59)
            "s" : "%S", # Sekunden *(00 – 59)

            "a" : "%p", # "am" oder "pm"
            "A" : "%p", # "AM" oder "PM"

            "w" : "%w", # Wochentag als Zahl (0(Sonntag) bis 6(Samstag))
            "W" : "%W", # Wochennummer des Jahres (z.B. 28)
            "z" : "%j"  # Tag des Jahres als Zahl (z.B. 148 (entspricht 29.05.2001))
        }

    def convert( self, formatDateTime ):
        "PHP-date Format in Python-Format umwandeln"

        for item in re.findall(r"\w", formatDateTime ):
            formatDateTime = formatDateTime.replace( item, self.PHP2Python_date[item] )

        return formatDateTime

formatDateTime = "j.m.Y G:i"
print formatDateTime
formatDateTime = convertdateformat().convert( formatDateTime )
print formatDateTime
Jemand eine schönere Variante?

GitHub | Open HUB | Xing | Linked in
Bitcoins to: 1JEgSQepxGjdprNedC9tXQWLpS424AL8cd
Antworten