How to make a datetime object aware (not naive)

In general, to make a naive datetime timezone-aware, use the localize method: import datetime import pytz unaware = datetime.datetime(2011, 8, 15, 8, 15, 12, 0) aware = datetime.datetime(2011, 8, 15, 8, 15, 12, 0, pytz.UTC) now_aware = pytz.utc.localize(unaware) assert aware == now_aware For the UTC timezone, it is not really necessary to use localize since … Read more

datetime and timezone conversion with pytz – mind blowing behaviour

The documentation states “Unfortunately using the tzinfo argument of the standard datetime constructors ‘does not work’ with pytz for many timezones.” The code: t = datetime( 2013, 5, 11, hour=11, minute=0, tzinfo=pytz.timezone(‘Europe/Warsaw’) ) doesn’t work according to this, instead you should use the localize method: t = pytz.timezone(‘Europe/Warsaw’).localize( datetime(2013, 5, 11, hour=11, minute=0))

How to deal with the timezone issue when storing dates in utc using mongod?

Aside from the SERVER-6310 mentioned by Matt Johnson, one other workaround is to use the $project operator to add or subtract from the UTC time zone to “shift the time” into the correct local zone. Turns out you can add or subtract time in milliseconds. For example, assuming I have a Date field called orderTime. … Read more

How to convert datetime to timestamp using C#/.NET (ignoring current timezone)

At the moment you’re calling ToUniversalTime() – just get rid of that: private long ConvertToTimestamp(DateTime value) { long epoch = (value.Ticks – 621355968000000000) / 10000000; return epoch; } Alternatively, and rather more readably IMO: private static readonly DateTime Epoch = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc); … private static long ConvertToTimestamp(DateTime value) { … Read more

Convert UTC to “local” time in Go

Keep in mind that the playground has the time set to 2009-11-10 23:00:00 +0000 UTC, so it is working. The proper way is to use time.LoadLocation though, here’s an example: var countryTz = map[string]string{ “Hungary”: “Europe/Budapest”, “Egypt”: “Africa/Cairo”, } func timeIn(name string) time.Time { loc, err := time.LoadLocation(countryTz[name]) if err != nil { panic(err) } … Read more

How to create time in a specific time zone with moment.js

In most cases, you can simply do this:“2013-08-26 16:55:00”, “America/Los_Angeles”) If you require input other than ISO8601, then specify the format string as the second parameter, and the time zone as the third:“8/26/2013 4:55 pm”, “M/D/YYYY h:mm a”, “America/Los_Angeles”) And if you need to use moment’s “strict parsing” mode, then that goes in … Read more

Storing datetime as UTC in PHP/MySQL

MySQL: UTC_TIMESTAMP() Returns the current UTC date and time as a value in ‘YYYY-MM-DD HH:MM:SS’ or YYYYMMDDHHMMSS.uuuuuu format, depending on whether the function is used in a string or numeric context PHP: gmdate() Also PHP date_default_timezone_set() is used in PHP to set the current time zone for the script. You can set it to the … Read more

How to convert between time zones in PHP using the DateTime class?

What you’re looking for is this: $triggerOn = ’04/01/2013 03:08 PM’; $user_tz = ‘America/Los_Angeles’; echo $triggerOn; // echoes 04/01/2013 03:08 PM $schedule_date = new DateTime($triggerOn, new DateTimeZone($user_tz) ); $schedule_date->setTimeZone(new DateTimeZone(‘UTC’)); $triggerOn = $schedule_date->format(‘Y-m-d H:i:s’); echo $triggerOn; // echoes 2013-04-01 22:08:00

How do I print a datetime in the local timezone?

As of python 3.6 calling astimezone() without a timezone object defaults to the local zone (docs). This means you don’t need to import tzlocal and can simply do the following: #!/usr/bin/env python3 from datetime import datetime, timezone utc_dt = print(“Local time {}”.format(utc_dt.astimezone().isoformat())) This script demonstrates a few other ways to show the local timezone … Read more