Přejít na obsah
Odoo Menu
  • Přihlásit se
  • Vyzkoušejte zdarma
  • Aplikace
    Finance
    • Účetnictví
    • Fakturace
    • Výdaje
    • Spreadsheet (BI)
    • Dokumenty
    • Podpisy
    Prodej
    • CRM
    • Prodej
    • POS Obchod
    • POS Restaurace
    • Předplatné
    • Pronájem
    Webové stránky
    • Webové stránky
    • E-shop
    • Blog
    • Fórum
    • Živý chat
    • eLearning
    Dodavatelský řetězec
    • Sklad
    • Výroba
    • PLM
    • Nákup
    • Údržba
    • Kvalita
    Lidské zdroje
    • Zaměstnanci
    • Nábor
    • Volno
    • Hodnocení zaměstnanců
    • Doporučení
    • Vozový park
    Marketing
    • Marketing sociálních sítí
    • Emailový marketing
    • SMS Marketing
    • Události
    • Marketingová automatizace
    • Dotazníky
    Služby
    • Projekt
    • Časové výkazy
    • Práce v terénu
    • Helpdesk
    • Plánování
    • Schůzky
    Produktivita
    • Diskuze
    • Schvalování
    • IoT
    • VoIP
    • Znalosti
    • WhatsApp
    Aplikace třetích stran Odoo Studio Odoo cloudová platforma
  • Branže
    Maloobchod
    • Knihkupectví
    • Obchod s oblečením
    • Obchod s nábytkem
    • Potraviny
    • Obchod s hardwarem
    • Hračkářství
    Jídlo a pohostinství
    • Bar a Pub
    • Restaurace
    • Fast Food
    • Penzion
    • Distributor nápojů
    • Hotel
    Nemovitost
    • Realitní kancelář
    • Architektonická firma
    • Stavba
    • Správa nemovitostí
    • Zahradnictví
    • Asociace vlastníků nemovitosti
    Poradenství
    • Účetní firma
    • Odoo Partner
    • Marketingová agentura
    • Právník
    • Akvizice talentů
    • Audit a certifikace
    Výroba
    • Textile
    • Kov
    • Nábytek
    • Jídlo
    • Brewery
    • Korporátní dárky
    Zdraví a fitness
    • Sportovní klub
    • Prodejna brýli
    • Fitness Centrum
    • Wellness praktikové
    • Lékárna
    • Kadeřnictví
    Transakce
    • Údržbář
    • IT hardware a podpora
    • Systémy solární energie
    • Výrobce obuvi
    • Úklidové služby
    • Služby HVAC
    Ostatní
    • Nezisková organizace
    • Agentura pro životní prostředí
    • Pronájem billboardů
    • Fotografování
    • Leasing jízdních kol
    • Prodejce softwaru
    Browse all Industries
  • Komunita
    Edukační program
    • Tutoriály
    • Dokumentace
    • Certifikace
    • Vzdělávání
    • Blog
    • Podcast
    Podpora vzdělávání
    • Vzdělávací program
    • Scale Up! Hra na firmu
    • Navštivte Odoo
    Získat software
    • Stáhnout
    • Porovnejte edice
    • Verze
    Spolupráce
    • Github
    • Fórum
    • Události
    • Překlady
    • Stát se partnerem
    • Services for Partners
    • Registrujte svou účetní firmu
    Získat služby
    • Najít partnera
    • Najít účetní
    • Setkejte se s poradcem
    • Implementační služby
    • Zákaznické reference
    • Podpora
    • Upgrady
    Github Youtube Twitter Linkedin Instagram Facebook Spotify
    +1 (650) 691-3277
    Dohodnout demo
  • Ceník
  • Pomoc

Odoo is the world's easiest all-in-one management software.
It includes hundreds of business apps:

  • CRM
  • e-Commerce
  • Účetnictví
  • Sklad
  • PoS
  • Projekty
  • MRP
All apps
You need to be registered to interact with the community.
All Posts Lidé Odznaky
Štítky (View all)
odoo accounting v14 pos v15
O tomto fóru
You need to be registered to interact with the community.
All Posts Lidé Odznaky
Štítky (View all)
odoo accounting v14 pos v15
O tomto fóru
Pomoc

[Solved] Date/ Time in Odoo with python

Odebírat

Get notified when there's activity on this post

This question has been flagged
pythondatetime
8 Odpovědi
63019 Zobrazení
Avatar
Siobhan

I have an onchange class written :


@api.onchange('res_users_id')

def do_stuff(self):

if self.res_users_id:

self.beschreibung = str(now) + '\n' + str(self.beschreibung)

else:

self.beschreibung = ' '


and a global variable: now = datetime.datetime.now()

I have 2 problems:

1: it is showing about 2 hours behind my current time, how do I fix that? and

2: how do I get rid of the milliseconds at the end - 10:07:15.471708?


4
Avatar
Zrušit
Avatar
Laza Andriamanantena
Nejlepší odpověď

Hi, I know this post is already solved but I has the same issues on Odoo 11, 12 and in my case I use fields.Datetime.context_timestamp , like this:
​

now= datetime.strftime(fields.Datetime.context_timestamp(self, datetime.now()), "%Y-%m-%d %H:%M:%S")

you need to declare it in on methode on models.Models, because the self take the timezone of the current user


5
Avatar
Zrušit
CARLOS ALBERTO GARCIA BRIZUELA

For me works great in Odoo v13 as well !!

Nayar Rodriguez

Thank you so much, works greaaaat! Odoo V13 as well

Avatar
Temur
Nejlepší odpověď

1. I guess you're in UTC+2 time zone? Take in account that server time is in UTC, but that's corrected according timezone of user (timezone of users is configured in settings), so in user interface  every user see time correctly in it's timezone (that's nice solution for a case if several user are logged into the same server from different timezone) consider to use datetime and date fields (also try widget="datetime" widget="date"), finally users will see correct time.

2. consider to use strftime / strptime play.


UPDATE for 1.

if you want add or subtract that 2 hours directly at server side (in python), then you can use (but take in account that it may show wrong time to users in browser, because of what I posted above...):

from datetime import timedelta

...

now = datetime.datetime.now() + timedelta(hours=2) 

...


UPDATE: one more option to get time in user timezone at serverside:

in your case, you may find useful to use server_to_local_timestamp function from openerp.tools.misc

from openerp import tools
...

src_tstamp_str = tools.datetime.now().strftime(tools.misc.DEFAULT_SERVER_DATETIME_FORMAT)

src_format = tools.misc.DEFAULT_SERVER_DATETIME_FORMAT

dst_format = "%c" #format you want to get time in.

dst_tz_name = self.env.user.tz # timezone you want get time in (here used v8 api, otherwise you'll have to browse for user and access tz field using "user_browserecord.tz")

_now = tools.misc.server_to_local_timestamp(src_tstamp_str, src_format, dst_format, dst_tz_name)

note: user should have configured a timezone in settings. if that's not a case, then odoo displays warning (!) at upper right corner in UI in browser.
8
Avatar
Zrušit
Siobhan
Autor

Thank you for the information... I am trying to use %c Locale’s appropriate date and time representation. but it doesn't like the syntax... what is the correct way to use this?

Siobhan
Autor

Is there a way to use timedelta in this piece of code: now = strftime("%a, %d %b %Y %H:%M:%S ", localtime())

Siobhan
Autor

Ok.. got it down to one line of code: now = datetime.datetime.now().strftime("%c") Problem is that I am again 2 hours out even when my time preference in Odoo is set to Central European time.

Temur

you can turn your code into the:

now = ( datetime.datetime.now() + timedelta(hours=2) ).strftime("%c")
this way you can add back that 2 hours difference to the current time. also consider to use another option that I've added to my answer, choose one which better fits your requirements.
Siobhan
Autor

Thank you Temur... timedelta is perfect for now as everyone is in Central Europe so I don't need timezone. Will look at the code above and try get it to work during my quiet time.

CARLOS ALBERTO GARCIA BRIZUELA

Thanks, you are the best, exactly what I need! Cheers

Enjoying the discussion? Don't just read, join in!

Create an account today to enjoy exclusive features and engage with our awesome community!

Přihlásit se
Related Posts Odpovědi Zobrazení Aktivita
DateTime Python Vyřešeno
python datetime
Avatar
Avatar
Avatar
Avatar
4
úno 23
72982
Difference Between Two Dates (result not showing up when diff = 0 Day)
datetime
Avatar
Avatar
Avatar
3
čvn 25
1768
new python env
python
Avatar
0
bře 25
2275
What means "Too many values to unpack" message? Vyřešeno
python
Avatar
Avatar
Avatar
Avatar
Avatar
4
dub 24
175726
have no data in screen. read data in my own module from different model
python
Avatar
0
pro 23
2929
Komunita
  • Tutoriály
  • Dokumentace
  • Fórum
Open Source
  • Stáhnout
  • Github
  • Runbot
  • Překlady
Služby
  • Odoo.sh hostování
  • Podpora
  • Upgrade
  • Nestandardní vývoj
  • Edukační program
  • Najít účetní
  • Najít partnera
  • Stát se partnerem
O nás
  • Naše společnost
  • Podklady značky
  • Kontakujte nás
  • Práce
  • Události
  • Podcast
  • Blog
  • Zákazníci
  • Právní dokumenty • Soukromí
  • Zabezpečení
الْعَرَبيّة Català 简体中文 繁體中文 (台灣) Čeština Dansk Nederlands English Suomi Français Deutsch हिंदी Bahasa Indonesia Italiano 日本語 한국어 (KR) Lietuvių kalba Język polski Português (BR) română русский язык Slovenský jazyk slovenščina Español (América Latina) Español ภาษาไทย Türkçe українська Tiếng Việt

Odoo je balíček open-source aplikací, které pokrývají všechny potřeby vaší společnosti: CRM, e-shop, účetnictví, sklady, kasy, projektové řízení a další.

Unikátní nabídka od Odoo poskytuje velmi jednoduché uživatelské rozhraní a vše je integrované na jednom místě.

Website made with

Odoo Experience on YouTube

1. Use the live chat to ask your questions.
2. The operator answers within a few minutes.

Live support on Youtube
Watch now