Re: Date/Time conversion for Grinvich time
Posted in 1995
Michael Z. Sosis (sosism@wwa.com) wrote: : I am looking for the waay to store Greenvich time form Standard and : convert it back to Standard after read from the datbase. At the risk of redundancy, here's a 4gl solution I cooked up a couple months ago. It is fairly but not throughly tested. This is also in the FAQ, I believe. ======================================================================= Dennis J. Pimple dennisp@informix.com Opinions expressed Principal Consultant -------------------- are mine, and do not Informix Software Inc Voice: 303-850-0210 necessarily reflect Denver Colorado USA Fax: 303-779-4025 those of my employer. ########### # INFORMIX PROFESSIONAL SERVICES ####### # ## Denver, Colorado ###### # ### ================================================== ##### # #### File: %M% SCCS: %I% %P% #### # ##### Program: example ### # ###### Client: ## # ####### Author: Dennis J. Pimple # ########### Date: %G% %U% # THE PROBLEM: need to have a local DATETIME converted to Greenwich # Mean Time and vice versa # THE APPROACH: Based on TZ environment and information found in # date and tztab (see Unix manual entries) # Add or subtract hours as required. # tztab may be unique to HPUX, but as long as TZ is of # the same format, this code should work. { # if you have a database with the Daylight Savings Time table in it, # (discussed more below), you can define a modular record like so: DATABASE dbname DEFINE m_dst RECORD LIKE dst.* } # since my (uncommented) code deals not with a database, I do thus: DEFINE m_dst RECORD tz CHAR(10), # timezone with not DST part, eg MST7 st_yy SMALLINT, # start year st_mm SMALLINT, # start month st_dd SMALLINT, # start day; DST actually begins the 1st # Sunday on or after this day st_hh SMALLINT, # start hour, usually 1 (1:00 AM) en_mm SMALLINT, # end month; month DST ends. # If en_mm < st_mm, then DST runs # through the end of the current year # and into en_mm of the next year en_dd SMALLINT, # end day; DST ends 1st Sunday on/after en_hh SMALLINT # end hour, usually 3 END RECORD MAIN DEFINE ldatetime DATETIME YEAR TO MINUTE DEFINE ltz CHAR(18) DEFINE gdatetime DATETIME YEAR TO MINUTE OPTIONS PROMPT LINE 10 CLEAR SCREEN # loop until the user hits INTERRUPT WHILE TRUE LET gdatetime = CURRENT PROMPT "Enter Datetime, or RETURN for ", gdatetime, ":" FOR ldatetime IF ldatetime IS NULL THEN LET ldatetime = gdatetime END IF # this is where you would have your SELECT from the Database LET ltz = fgl_getenv("TZ") PROMPT "Enter Timezone, or RETURN for ", ltz CLIPPED, ":" FOR ltz IF ltz IS NULL THEN LET ltz = fgl_getenv("TZ") END IF LET gdatetime = loc_to_gmt(ldatetime, ltz) DISPLAY ldatetime, " ", ltz CLIPPED, " is ", gdatetime, " GMT", "" AT 5,1 LET ldatetime = gmt_to_loc(gdatetime, ltz) DISPLAY gdatetime, " GMT is ", ldatetime, " ", ltz CLIPPED, "" AT 6,1 END WHILE END MAIN #---------------------------------------------------------------------# FUNCTION loc_to_gmt(ldatetime, ltz) # Arguments: local DATETIME, char of the timezone it came from # Purpose: Convert the passed DATETIME to GMT DATETIME # Returns: DATETIME with GMT #---------------------------------------------------------------------# # comments below will use my local TZ, MST7MDT as example DEFINE ldatetime DATETIME YEAR TO SECOND DEFINE ltz CHAR(18) # MST7MDT DEFINE gdatetime DATETIME YEAR TO SECOND DEFINE ftz CHAR(10) # formatted tz sans DST entry: MST7 DEFINE htm INTERVAL HOUR TO MINUTE DEFINE fdst CHAR(10) # DST entry: MDT or NULL if no DST LET gdatetime = NULL # walk through the ltz sent and break it into ftz, offset (htm) and fdst CALL parse_tz(ltz) RETURNING ftz, htm, fdst IF htm IS NOT NULL THEN # do we need to decrement an hour for DST? # we do if fdst (the DST part of ltz) is not null # and if we're not in most of Indiana (EST5CDT), since there # they go from EST5 (5 hours off GMT) # to CDT (also 5 hours off GMT), so they don't change. IF LENGTH(fdst) > 0 AND ltz != "EST5CDT" THEN IF daylight_savings(ldatetime, ftz) THEN # since during DST we "spring ahead" an hour, # we need to "fall back" the time we're trying to calculate LET ldatetime = ldatetime - 1 UNITS HOUR END IF END IF # Calculate GMT LET gdatetime = ldatetime + htm END IF # htm IS NOT NULL RETURN gdatetime END FUNCTION # loc_to_gmt(ldatetime, ltz) #---------------------------------------------------------------------# FUNCTION gmt_to_loc(gdatetime, ltz) # Arguments: gmt DATETIME, char of the timezone we want to convert to # Purpose: Convert the passed GMT DATETIME to a local DATETIME # Returns: local DATETIME #---------------------------------------------------------------------# # comments below will use my local TZ, MST7MDT as example DEFINE gdatetime DATETIME YEAR TO SECOND DEFINE ltz CHAR(18) # MST7MDT DEFINE ldatetime DATETIME YEAR TO SECOND DEFINE ftz CHAR(10) # formatted tz sans DST entry: MST7 DEFINE htm INTERVAL HOUR TO MINUTE DEFINE fdst CHAR(10) # DST entry: MDT or NULL if no DST LET ldatetime = NULL # walk through the ltz sent and break it into ftz, offset (htm) and fdst CALL parse_tz(ltz) RETURNING ftz, htm, fdst IF htm IS NOT NULL THEN # Calculate local time LET ldatetime = gdatetime - htm # do we need to increment an hour for DST? # we do if fdst (the DST part of ltz) is not null # and if we're not in most of Indiana (EST5CDT), since there # they go from EST5 (5 hours off GMT) # to CDT (also 5 hours off GMT), so they don't change IF LENGTH(fdst) > 0 AND ltz != "EST5CDT" THEN IF daylight_savings(ldatetime, ftz) THEN # we are in DST, so we need to "Spring Ahead" an hour LET ldatetime = ldatetime + 1 UNITS HOUR END IF END IF END IF # htm IS NOT NULL RETURN ldatetime END FUNCTION # gmt_to_loc(gdatetime, ltz) #---------------------------------------------------------------------# FUNCTION parse_tz(ltz) # Arguments: timzone # Purpose: break tz (MST7MDT), # into the local timezone (MST7), the offset (" 07:00"), # and the DST tz if any (MDT) # Returns: the three described above #-------------------------------