Thanks Anthony, I didn't know that pytz is deprecated. Now with zoneinfo is
working.
Regards, Andres.
El miércoles, 30 de marzo de 2022 a la(s) 03:02:56 UTC-4, Antonis
Christofides escribió:
> Hi,
>
> not what you asked, but
>
> from settings import TIME_ZONE
>
> is an error. It happens to work in this case, but generally you should
> write
>
> from django.conf import settings
>
> and then use "settings.TIME_ZONE". In Django, "settings" isn't a module,
> it's an object. Django has some logic when populating that object with
> attributes. An obvious example is that you can django the
> DJANGO_SETTINGS_MODULES environment variable to specify a different
> settings file.
>
> Now, regarding what you asked, the pytz documentation has the following
> warning:
>
> Unfortunately using the tzinfo argument of the standard datetime
> constructors ‘’does not work’’ with pytz for many timezones.
>
> I don't remember the correct way to do it, since I'm always confused by
> how pytz works, and also its usage is deprecated because of the existence
> of the new "zoneinfo" Python standard library module.
>
> Regards,
>
> Antonis
>
> Antonis Christofides+30-6979924665 <+30%20697%20992%204665> (mobile)
>
>
> On 29/03/2022 17.51, Andrés Alvarez wrote:
>
> I need to save some fields in my database in the format YYY-MM-MM
> 00:00:00+00. So I decide to do this:
>
> # setting.py
> TIME_ZONE = 'America/Mexico_City'
> USE_I18N = True
> USE_L10N = True
> USE_TZ = True
>
> # models.py
> class Program(models.Model):
> objects = models.Manager()
> objects_custom = ProgramManager()
> class Meta:
> ordering = ['id']
> name = models.CharField(max_length=100, null=False)
> program_budget = models.CharField(max_length=100, null=False)
> company = models.ForeignKey(Company, on_delete=models.CASCADE, null
> =True)
> active = models.BooleanField(default=True)
> uuid4 = models.CharField(max_length=50, unique=True, null=True)
> country_list = models.TextField(null=True)
> method_payment = models.CharField(max_length=100, null=True)
> currency = models.CharField(max_length=4, default='USD', null=True)
> create_at = models.DateTimeField(auto_now_add=True, null=True)
> discharge_program = models.DateTimeField(auto_now_add=True, null=True)
> updated_at = models.DateTimeField(auto_now=True)
> deleted_at = models.DateTimeField(null=True)
> is_saved = models.NullBooleanField(default=False, null=True, blank
> =True)
> is_suspended = models.BooleanField(default=False)
> start_at = models.DateTimeField(null=True)
> finish_at = models.DateTimeField(null=True)
>
> # utils.py
> from datetime import datetime, time
> import pytz
> from settings.py import TIME_ZONE
> def get_start_at(date):
> """Obtain the current day initialize in the first second of the day.
> Parameters:
> date (date): date object
>
> Returns:
> (datetime): YYYY-MM-DD 00:00:00
> """
> return datetime.combine(date, time.min, pytz.timezone(TIME_ZONE))
>
>
> Here are my values to create the register:
> {
> 'has_individual_budget': False,
> 'name': 'plan test create program start_at datetime',
> 'program_budget': '0',
> 'uuid4', 'd6effacf-3c19-48db-ab06-5af1d97df599',
> 'country_list':
> '[{"id":239,"name":"Venezuela","iso_code2":"VE","iso_code3":"VEN","phone_prefix":58,"currency":"VEF","flag":"U+1F1FB
>
>
> U+1F1EA","phone_national_regex":"^[68]00\\\\d{7}|(?:[24]\\\\d|[59]0)\\\\d{8}$","active":1}]',
>
> 'method_payment': '7fb39a7e-a6de-488b-9564-9bd853b598ec',
> 'currency': 'USD',
> 'is_saved': False,
> 'start_at': datetime.datetime(2022, 3, 28, 0, 0, tzinfo=<DstTzInfo
> 'America/Mexico_City' LMT-1 day, 17:23:00 STD>),
> 'finish_at': datetime.datetime(2022, 4, 27, 23, 59, 59, 999999,
> tzinfo=<DstTzInfo 'America/Mexico_City' LMT-1 day, 17:23:00 STD>),
> 'company': <Company: Company object (247)>,
> 'card_provider': <CardProvider: stripe-usa>,
> 'payment_provider': <CardProvider: stripe-usa>
> }
>
> as you can see the field 'start_at' is the datetime that I want to store
> in the database, but when I look the insert query:
> {
> 'sql': """
> INSERT INTO "user_program"
> (
> "name",
> "program_budget",
> "company_id",
> "active",
> "uuid4",
> "country_list",
> "method_payment",
> "currency",
> "create_at",
> "discharge_program",
> "updated_at",
> "deleted_at",
> "is_saved",
> "is_suspended",
> "start_at",
> "finish_at",
> "has_individual_budget",
> "card_provider_id",
> "payment_provider_id"
> )
> VALUES
> (
> \'plan test create program start_at datetime\',
> \'0\',
> 247,
> true,
> \'d6effacf-3c19-48db-ab06-5af1d97df599\',
>
> \'[{"id":239,"name":"Venezuela","iso_code2":"VE","iso_code3":"VEN","phone_prefix":58,"currency":"VEF","flag":"U+1F1FB
>
> U+1F1EA","phone_national_regex":"^[68]00\\\\d{7}|(?:[24]\\\\d|[59]0)\\\\d{8}$","active":1}]\',
>
> \'7fb39a7e-a6de-488b-9564-9bd853b598ec\',
> \'USD\',
> \'2022-03-28T18:47:10.865801+00:00\'::timestamptz,
> \'2022-03-28T18:47:10.865870+00:00\'::timestamptz,
> \'2022-03-28T18:47:10.865913+00:00\'::timestamptz,
> NULL,
> false,
> false,
> \'2022-03-28T00:00:00-06:37\'::timestamptz, --
> start_field, notice that the timezone part should be -06 but it is -06:37
> \'2022-04-27T23:59:59.999999-06:37\'::timestamptz,
> false,
> 1,
> 1
> )
> RETURNING "user_program"."id"',
> 'time': '0.267'
> }
>
>
> Plase tell me how to fix that, if I need to change the TIME_ZONE or
> something.
>
> Regards, Andres.
>
> El martes, 29 de marzo de 2022 a la(s) 01:41:42 UTC-4, Antonis
> Christofides escribió:
>
>> Hello,
>>
>> 1. Please copy your code exactly. Is it really
>> "pytz.timezone(TIME_ZONE)"? Or is it
>> "pytz.timezone(*settings.*TIME_ZONE)"?
>> If it is the first one, please include the definition or importing of
>> TIME_ZONE.
>> 2. Please pretty-print your dictionary. You can use pprint for that.
>> If it doesn't work on OrderedDicts, convert it to a simple dictionary
>> before pretty printing.
>> 3. Likewise, do something so that your SQL statement is more
>> readable, such as manually inserting newlines and indents.
>>
>> Regards,
>>
>> Antonis
>>
>> Antonis Christofides+30-6979924665 <+30%20697%20992%204665> (mobile)
>>
>>
>> On 28/03/2022 22.02, Andrés Alvarez wrote:
>>
>> I need to save some fields in my database in the format YYY-MM-MM
>> 00:00:00+00. So I decide to do this:
>> field_to_save = datetime.combine(date_to_save, time.min,
>> pytz.timezone(TIME_ZONE))
>> in settings.py I have:
>> TIME_ZONE = 'America/Mexico_City'
>> USE_I18N = True
>> USE_L10N = True
>> USE_TZ = True
>>
>> My model:
>> class Program(models.Model):
>> objects = models.Manager()
>> objects_custom = ProgramManager()
>> class Meta:
>> ordering = ['id']
>> # ... others fields
>> start_at = models.DateTimeField(null=True)
>> finish_at = models.DateTimeField(null=True)
>>
>> Here are my values to create the register:
>> OrderedDict([('has_individual_budget', False), ('name', 'plan test create
>> program start_at datetime'), ('program_budget', '0'), ('uuid4',
>> 'd6effacf-3c19-48db-ab06-5af1d97df599'), ('country_list',
>> '[{"id":239,"name":"Venezuela","iso_code2":"VE","iso_code3":"VEN","phone_prefix":58,"currency":"VEF","flag":"U+1F1FB
>>
>> U+1F1EA","phone_national_regex":"^[68]00\\\\d{7}|(?:[24]\\\\d|[59]0)\\\\d{8}$","active":1}]'),
>>
>> ('method_payment', '7fb39a7e-a6de-488b-9564-9bd853b598ec'), ('currency',
>> 'USD'), ('is_saved', False), ('start_at', datetime.datetime(2022, 3, 28, 0,
>> 0, tzinfo=<DstTzInfo 'America/Mexico_City' LMT-1 day, 17:23:00 STD>)),
>> ('finish_at', datetime.datetime(2022, 4, 27, 23, 59, 59, 999999,
>> tzinfo=<DstTzInfo 'America/Mexico_City' LMT-1 day, 17:23:00 STD>)),
>> ('company', <Company: Company object (247)>), ('card_provider',
>> <CardProvider: stripe-usa>), ('payment_provider', <CardProvider:
>> stripe-usa>)])
>> as you can see the field 'start_at' is the datetime that I want to store
>> in the database, but when I look the insert query:
>> {'sql': 'INSERT INTO "user_program" ("name", "program_budget",
>> "company_id", "active", "uuid4", "country_list", "method_payment",
>> "currency", "create_at", "discharge_program", "updated_at", "deleted_at",
>> "is_saved", "is_suspended", "start_at", "finish_at",
>> "has_individual_budget", "card_provider_id", "payment_provider_id") VALUES
>> (\'plan test create program start_at datetime\', \'0\', 247, true,
>> \'d6effacf-3c19-48db-ab06-5af1d97df599\',
>> \'[{"id":239,"name":"Venezuela","iso_code2":"VE","iso_code3":"VEN","phone_prefix":58,"currency":"VEF","flag":"U+1F1FB
>>
>> U+1F1EA","phone_national_regex":"^[68]00\\\\d{7}|(?:[24]\\\\d|[59]0)\\\\d{8}$","active":1}]\',
>>
>> \'7fb39a7e-a6de-488b-9564-9bd853b598ec\', \'USD\',
>> \'2022-03-28T18:47:10.865801+00:00\'::timestamptz,
>> \'2022-03-28T18:47:10.865870+00:00\'::timestamptz,
>> \'2022-03-28T18:47:10.865913+00:00\'::timestamptz, NULL, false, false,
>> \'2022-03-28T00:00:00-06:37\'::timestamptz,
>> \'2022-04-27T23:59:59.999999-06:37\'::timestamptz, false, 1, 1) RETURNING
>> "user_program"."id"', 'time': '0.267'}
>> the timezone part is -06:37 should be only -06.
>> Plase tell me how to fix that, if I need to change the TIME_ZONE or
>> something.
>>
>> Regards, Andres.
>>
>> --
>> You received this message because you are subscribed to the Google Groups
>> "Django users" group.
>> To unsubscribe from this group and stop receiving emails from it, send an
>> email to [email protected].
>> To view this discussion on the web visit
>> https://groups.google.com/d/msgid/django-users/1999ec1e-9170-432e-adc0-71cab3dece6an%40googlegroups.com
>>
>> <https://groups.google.com/d/msgid/django-users/1999ec1e-9170-432e-adc0-71cab3dece6an%40googlegroups.com?utm_medium=email&utm_source=footer>
>> .
>>
>> --
> You received this message because you are subscribed to the Google Groups
> "Django users" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to [email protected].
>
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/django-users/d1cfcde9-3d21-4918-8edf-b9366b1dfa33n%40googlegroups.com
>
> <https://groups.google.com/d/msgid/django-users/d1cfcde9-3d21-4918-8edf-b9366b1dfa33n%40googlegroups.com?utm_medium=email&utm_source=footer>
> .
>
>
--
You received this message because you are subscribed to the Google Groups
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To view this discussion on the web visit
https://groups.google.com/d/msgid/django-users/491602b5-0cca-4267-867f-866abcb05509n%40googlegroups.com.