!========================================================================= 
! Copyright (C) GemTalk Systems 1986-2014.  All Rights Reserved.  
! 
! $Id: timezone.txt 32752 2014-02-28 22:11:10Z stever $
!
! 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
! - 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
! 
set user SystemUser pass swordfish gems gs64stone
login
run
| inUSA4 path swap move osName aTimeZone |

"flags currently set to default values"
inUSA4 := true.  "is in 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"

inUSA4 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 notNil ifTrue: [
		path := '$GEMSTONE/pub/zoneinfo/America/' , city.
	].
].
osName := System gemVersionReport at: #osName.
osName = 'Linux' ifTrue: [aTimeZone := TimeZone fromLinux        ].
osName = 'SunOS' ifTrue: [aTimeZone := TimeZone fromSolaris      ].
path notNil      ifTrue: [aTimeZone := TimeZone fromGemPath: path].
aTimeZone isNil  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
