Sublime Forum

Datetime.py Sublime Text Python Extension

#1

Enter Sublime Text ( Build 4143 )

Preferences > Browse Packages… > [ User ] folder > [ datetime.py ] file :

import sublime, sublime_plugin, time
from datetime import datetime
class InsertDatetimeCommand(sublime_plugin.TextCommand):
    def run(self, edit):
        sel = self.view.sel();
        for s in sel:
            now__________________ = datetime.now()
            year_________________ = now__________________.strftime('%Y')
            month___without_zeros = now__________________.strftime('%-m')
            day_____without_zeros = now__________________.strftime('%-d')
            hour____without_zeros = now__________________.strftime('%-I')
            minutes_with____zeros = now__________________.strftime("%M")
            minutes_without_zeros = minutes_with____zeros.lstrip('0')
            seconds_with____zeros = now__________________.strftime('%S')
            seconds_without_zeros = seconds_with____zeros.lstrip('0')
            microseconds_________ = now__________________.strftime('%f')
            am_pm________________ = now__________________.strftime('%p')
            timezone_____________ = now__________________.strftime('%z')
            internet_id_and_loc__ = 'richi' + '@' + 'com-vass-nateevo-glonass'
            datetime_____________ = internet_id_and_loc__ + '-' + year_________________ + '-' + month___without_zeros + '-' + day_____without_zeros + '-' + hour____without_zeros + '-' + minutes_without_zeros + '-' + seconds_without_zeros + '-' + microseconds_________ + '-' + am_pm________________ + '-' + timezone_____________
            self.view.replace(edit, s, now__________________.strftime(datetime_____________))

Preferences > Key Bindings :

[
    {
        "keys": ["f5"],
        "command": "insert_datetime"
    }
]

When I press F5 inside Sublime Text Context, Timezone does not work.

Example:

F5

richi@com-vass-nateevo-glonass-2023-6-15-6-7-17-657958-PM-??

Why?

0 Likes

#2

Date objects in Python can be either Naive or Aware with regards to their timezone. An Aware object has been told in what timezone it sits, and can report that information back, while a Naive object has no idea, and so the %z and %Z are empty strings.

So, if you want to include timezone information, you need to tell datetime.now() what timezone “now” is.

1 Like

#3

[context]

now?

0 Likes

#4

Sorry, I do not understand you, OdatNurd!!

0 Likes