!========================================================================= 
! Copyright (C) GemStone Systems, Inc. 1986-2008.  All Rights Reserved.  
! 
! $Id: timezone.txt,v 1.11 2008-01-09 19:02:37 stever Exp $
!
! Description - Topaz script to update the TimeZone default.
!    This script should be edited and run: 
!       (1) for a new database; and 
!       (2) when the DST rules change.
! 
!========================================================================
!
! This file contains a sample of the code that is used to update
! the default TimeZone object. Modifications might need to be made
! if the database contains other TimeZone instances.
!
! Local edits might be needed for the following:
! - SystemUser password
! - Stone name
! - Path to ZoneInfo binary file
! - Flag for whether we are in one of four primary USA zones and want to
!   automatically convert
! - Flag for whether to swap new with old using #become:
! - Flag for whether to adjust UTC for DateTime instances
!    that have a different local time under the new rules
!
! Please refer to the main documentation available at 
! http://support.gemstone.com/ for details on this new implementation of the
! TimeZone class.
! 
! There are four use cases for this file:
!
! 1. You are in one of the four main US time zones, and wish to automatically
!    create a new default time zone based on your existing default TimeZone
!    instance. This is the default action. No edits (other than login
!    are required.
! 2. You are on a platform that uses the time zone database (Solaris or Linux)
!    and wish to create a new default time zone based on your system setting
!    ($TZ on Solaris or /etc/localtime on Linux). Edit below to set 'USA4auto'
!    to 'false'.
! 3. You know the time zone you wish to use, and you want GemStone to find the
!    time zone database. Edit below to set USA4auto to false and set 'path' to
!    your desired time zone. Valid time zones can be determined by running the
!    interactive script $GEMSTONE/pub/timezone/etc/tzselect.
! 4. You know the time zone you wish to use, and you also have a path to a 
!    time zone database you wish to use. Edit below to set 'USA4auto' to 'false'
!    and set 'path' to the complete path to your time zone.

set user SystemUser pass swordfish gems gemserver61
login
run
| USA4auto path swap move osName aTimeZone |

"flags currently set to default values"
USA4auto := true.  "auto-convert from one of the four primary USA time zones"
swap     := true.  "use #become: to replace old with new"
move     := false. "adjust UTC for selected DateTime instances"
path     := nil.   "set to a valid TZ path here if you know the location of your
                    time zone database and your time zone."

USA4auto ifTrue: [
	| offset city |
	offset := TimeZone default secondsFromGmt / 60 / 60.
	city := 
		offset = -8 ifTrue: ['Los_Angeles'] ifFalse: [
		offset = -7 ifTrue: ['Denver'     ] ifFalse: [
		offset = -6 ifTrue: ['Chicago'    ] ifFalse: [
		offset = -5 ifTrue: ['New_York'   ]]]].
	city ~~ nil ifTrue: [
		path := '$GEMSTONE/pub/timezone/etc/zoneinfo/America/' , city.
	].
].
aTimeZone := TimeZone fromOS.
path ~~ nil      ifTrue: [aTimeZone := TimeZone fromPath: path].
aTimeZone == nil  ifTrue: [aTimeZone := TimeZone sampleLosAngelesB].

swap ifTrue: [
	TimeZone default become: aTimeZone.
	move ifTrue: [
		| new old list |
		new := TimeZone default.
		old := aTimeZone.
		System commitTransaction ifFalse: [nil error: 'commit failed'].
		list := DateTime allInstances.
		list do: [:each | 
			each timeZone == new ifTrue: [
				| offset |
				offset := (each _localOffset: old) - (each _localOffset: new).
				offset ~= 0 ifTrue: [
					(each addSeconds: offset) become: each.
				].
			].
		].
	].
] ifFalse: [
	TimeZone default: aTimeZone.
].
TimeZone default installAsCurrentTimeZone.
System commitTransaction.
%
logout
