Skip to content

Instantly share code, notes, and snippets.

@bzikarsky
Last active August 29, 2015 14:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bzikarsky/c309a55fefbaf9bdaa5d to your computer and use it in GitHub Desktop.
Save bzikarsky/c309a55fefbaf9bdaa5d to your computer and use it in GitHub Desktop.
Test timezone behavior with PHP and Mongo
#!/bin/bash
DB="test"
COL="tz"
ZONES="London Berlin Istanbul"
DATE="2000-01-01"
echo "System timezone: $(date -R)"
echo ""
echo -ne "Drop collection $COL: "
echo "db.${COL}.drop()" | mongo $DB --quiet
echo ""
echo "Inserting $DATE in different timezones"
for ZONE in $ZONES
do
echo "- Europe/$ZONE"
TZ="/usr/share/zoneinfo/Europe/$ZONE" php -r "(new MongoClient())->selectDB('$DB')->selectCollection('$COL')->insert(['tz' => date('e'), 'time' => new MongoDate(strtotime('$DA
TE'))]);"
done
echo ""
echo "DB contents"
echo "db.${COL}.find()" | mongo $DB --quiet
echo ""
echo "Reading in different timezones"
for ZONE in $ZONES
do
echo "- Europe/$ZONE"
TZ="/usr/share/zoneinfo/Europe/$ZONE" php -r "\$c = (new MongoClient)->selectDB('$DB')->selectCollection('$COL')->find(); foreach (\$c as \$d) echo ' => ', \$d['tz'], ' ', d
ate('c', \$d['time']->sec), PHP_EOL;"
done
@bzikarsky
Copy link
Author

$ ~/mongo-tz-test.bash
System timezone: Sat, 29 Nov 2014 12:44:31 -0600

Drop collection tz: true

Inserting 2000-01-01 in different timezones
- Europe/London
- Europe/Berlin
- Europe/Istanbul

DB contents
{ "_id" : ObjectId("547a141028041a00648b4567"), "tz" : "Europe/Helsinki", "time" : ISODate("1999-12-31T22:00:00Z") }
{ "_id" : ObjectId("547a141028041afc638b4567"), "tz" : "UTC", "time" : ISODate("2000-01-01T00:00:00Z") }
{ "_id" : ObjectId("547a141028041afe638b4567"), "tz" : "Europe/Berlin", "time" : ISODate("1999-12-31T23:00:00Z") }

Reading in different timezones
- Europe/London
  => Europe/Helsinki  1999-12-31T22:00:00+00:00
  => UTC  2000-01-01T00:00:00+00:00
  => Europe/Berlin  1999-12-31T23:00:00+00:00
- Europe/Berlin
  => Europe/Helsinki  1999-12-31T23:00:00+01:00
  => UTC  2000-01-01T01:00:00+01:00
  => Europe/Berlin  2000-01-01T00:00:00+01:00
- Europe/Istanbul
  => Europe/Helsinki  2000-01-01T00:00:00+02:00
  => UTC  2000-01-01T02:00:00+02:00
  => Europe/Berlin  2000-01-01T01:00:00+02:00

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment