-1

i tried using import datetime and several other but when i change my System time it show according to my current system time instead actual time

  • Does this answer your question? [Display the time in a different time zone](https://stackoverflow.com/questions/1398674/display-the-time-in-a-different-time-zone) – Ivo Mori Jul 26 '20 at 06:46
  • nope that doesn't solve my question, in this [https://stackoverflow.com/questions/1398674/display-the-time-in-a-different-time-zone] it only show time of different time zone which work according to your system time if i alter the system it show different time – Rana Raushan Jul 26 '20 at 06:56
  • Please clarify your problem and question with more details. If you don't want to use your local system time (because why exactly?) then your other option is to query a network service (is that what you want to do?). If so then probably the first choice would be querying a [Network Time Protocol (NTP) server](https://en.wikipedia.org/wiki/Network_Time_Protocol) either by using the [ntplib library](https://pypi.org/project/ntplib/) or by coding it yourself. Otherwise, you can also query an online service like the [World Time API](https://worldtimeapi.org) which offers a JSON and plaintext API. – Ivo Mori Jul 26 '20 at 07:51
  • i did write clearly i want to show time irrespective of my system time,, idk what else do i write to clarify you! – Rana Raushan Jul 26 '20 at 11:29
  • @IvoMori thanks i'll try NTP and world time API – Rana Raushan Jul 26 '20 at 11:43

2 Answers2

1

You can use pytz to do so:

import datetime
import pytz
date = datetime.datetime.now()
timezone = pytz.timezone("Asia/Kolkata")
date_ist = timezone.localize(date)
Huzaifa Ahmad
  • 31
  • 1
  • 4
  • what d in timezone.localize(d) denote ? is it supposed to be date(from date = datetime.datetime.now() )? – Rana Raushan Jul 26 '20 at 06:43
  • You can also do it the other way around, pass the timezone into the `now()` function, like this: `ist = pytz.timezone("Asia/Kolkata"); date_ist = datetime.datetime.now(ist)` – Jiří Baum Jul 26 '20 at 06:46
  • sorry but this give me wrong time, since i have alter my system time and i have clearly said that i want time irrespective of my system time – Rana Raushan Jul 26 '20 at 06:49
0

NTP servers are what this is for. You need a clock to reliably get time data. Usually that's the system clock. But NTP is good when you want your code to be independent of that.

ntplib is a library that makes querying these easy