Date manipulation

From Rosetta Code
Revision as of 12:41, 3 February 2011 by 69.217.200.134 (talk) (→‎{{header|Perl}}: Changed to use Olson timezone ID and pattern token so that daylight savings is correctly handled)
Task
Date manipulation
You are encouraged to solve this task according to the task description, using any language you may know.

Given the date string "March 7 2009 7:30pm EST", output the time 12 hours later in any human-readable format.

As extra credit, display the resulting time in a time zone different from your own.

Ada

This example is in need of improvement:

Add input handling for Timezone

Works with: Ada 2005

ignoring input timezone for now (it defaults to UTC). feel free to add it.

<lang Ada>with Ada.Calendar.Formatting; with Ada.Strings.Fixed; with Ada.Text_IO; procedure Add_Hours is

  use type Ada.Calendar.Time;
  Parse_Error : Exception;
  function Date_Value (From : String) return Ada.Calendar.Time is
     use Ada.Strings.Fixed;
     function Month_Value (From : String) return Ada.Calendar.Month_Number is
     begin
        if    From = "January"   then return  1;
        elsif From = "February"  then return  2;
        elsif From = "March"     then return  3;
        elsif From = "April"     then return  4;
        elsif From = "May"       then return  5;
        elsif From = "June"      then return  6;
        elsif From = "July"      then return  7;
        elsif From = "August"    then return  8;
        elsif From = "September" then return  9;
        elsif From = "October"   then return 10;
        elsif From = "November"  then return 11;
        elsif From = "December"  then return 12;
        else raise Parse_Error;
        end if;
     end Month_Value;
     function Time_Value (From : String) return Ada.Calendar.Day_Duration is
        Colon_Index : Positive := Index (From, ":");
        Hours   : Ada.Calendar.Formatting.Hour_Number   := Integer'Value (From (From'First .. Colon_Index - 1));
        Minutes : Ada.Calendar.Formatting.Minute_Number := Integer'Value (From (Colon_Index + 1 .. Colon_Index + 2));
     begin
        if From (From'Last - 1) = 'p' then
           Hours := Hours + 12;
        end if;
        return Ada.Calendar.Formatting.Seconds_Of (Hours, Minutes);
     end Time_Value;
     Month_End : Natural := Index (From, " ", From'First);
     Day_End   : Natural := Index (From, " ", Month_End + 1);
     Year_End  : Natural := Index (From, " ", Day_End   + 1);
     Time_End  : Natural := Index (From, " ", Year_End  + 1);
     The_Month : Ada.Calendar.Month_Number := Month_Value (From (From'First .. Month_End - 1));
     The_Day   : Ada.Calendar.Day_Number   := Integer'Value (From (Month_End + 1 .. Day_End));
     The_Year  : Ada.Calendar.Year_Number  := Integer'Value (From (Day_End + 1 .. Year_End));
     The_Time  : Ada.Calendar.Day_Duration := Time_Value (From (Year_End + 1 .. Time_End - 1));
  begin
     -- ignore time zone (feel free to add)
     return Ada.Calendar.Time_Of (The_Year, The_Month, The_Day, The_Time);
  end Date_Value;
  Given_String : String := "March 7 2009 7:30pm EST";
  Offset       : Duration := 12 * 60 * 60.0; -- seconds
  Date         : Ada.Calendar.Time := Date_Value (Given_String) + Offset;

begin

  Ada.Text_IO.Put_Line (Ada.Calendar.Formatting.Image (Date));

end Add_Hours;</lang>

Output (since input is assumed to be UTC, EST gives wrong time):

$ TZ=UTC ./add_hours
2009-03-08 07:30:00
$ TZ=EST ./add_hours
2009-03-08 12:30:00

AppleScript

AppleScript has a built-in date class and can coerce a string to a date automatically. It also has reserved constants such as hours which are defined in the unit of seconds. There is no built-in support for time zones. <lang AppleScript>set x to "March 7 2009 7:30pm EST" return (date x) + 12 * hours</lang>

Result is: <lang AppleScript>date "Sunday, March 8, 2009 7:30:00 AM"</lang>

AutoHotkey

<lang autohotkey>DateString := "March 7 2009 7:30pm EST"

split the given string with RegExMatch

Needle := "^(?P<mm>\S*) (?P<d>\S*) (?P<y>\S*) (?P<t>\S*) (?P<tz>\S*)$" RegExMatch(DateString, Needle, $)

split the time with RegExMatch

Needle := "^(?P<h>\d+):(?P<min>\d+)(?P<xm>[amp]+)$" RegExMatch($t, Needle, $)

convert am/pm to 24h format

$h += ($xm = "am") ? 0 : 12

knitting YYYYMMDDHH24MI format

_YYYY := $y _MM  := Get_MonthNr($mm) _DD  := SubStr("00" $d, -1) ; last 2 chars _HH24 := SubStr("00" $h, -1) ; last 2 chars _MI  := $min YYYYMMDDHH24MI := _YYYY _MM _DD _HH24 _MI

add 12 hours as requested

EnvAdd, YYYYMMDDHH24MI, 12, Hours FormatTime, HumanReadable, %YYYYMMDDHH24MI%, d/MMM/yyyy HH:mm

add 5 hours to convert to different timezone (GMT)

EnvAdd, YYYYMMDDHH24MI, 5, Hours FormatTime, HumanReadable_GMT, %YYYYMMDDHH24MI%, d/MMM/yyyy HH:mm

output

MsgBox, % "Given: " DateString "`n`n"

       . "12 hours later:`n"
       . "(" $tz "):`t" HumanReadable "h`n"
       . "(GMT):`t" HumanReadable_GMT "h`n"


---------------------------------------------------------------------------

Get_MonthNr(Month) { ; convert named month to 2-digit number

---------------------------------------------------------------------------
   If (Month = "January")
       Result := "01"
   Else If (Month = "February")
       Result := "02"
   Else If (Month = "March")
       Result := "03"
   Else If (Month = "April")
       Result := "04"
   Else If (Month = "May")
       Result := "05"
   Else If (Month = "June")
       Result := "06"
   Else If (Month = "July")
       Result := "07"
   Else If (Month = "August")
       Result := "08"
   Else If (Month = "September")
       Result := "09"
   Else If (Month = "October")
       Result := "10"
   Else If (Month = "November")
       Result := "11"
   Else If (Month = "December")
       Result := "12"
   Return, Result

}</lang> Message box shows:

Given: March 7 2009 7:30pm EST

12 hours later:
(EST):	8/Mar/2009  07:30h
(GMT):	8/Mar/2009  12:30h

C

Works with: POSIX

<lang c>#include <stdio.h>

  1. include <stdlib.h>
  2. include <time.h>

int main() {

 struct tm ts;
 time_t t;
 const char *d = "March 7 2009 7:30pm EST";
 
 strptime(d, "%B %d %Y %I:%M%p %Z", &ts);
 /* ts.tm_hour += 12; instead of t += 12*60*60
    works too. */
 t = mktime(&ts);
 t += 12*60*60;
 printf("%s", ctime(&t));
 return EXIT_SUCCESS;

}</lang>

Note: ctime treats the date as local, so that it is like the timezone information were discarded (to see the passage to daylight saving time I must change the date into March 28... no matter the timezone specified)

C#

<lang csharp>class Program {

   static void Main(string[] args)
   {
       CultureInfo ci=CultureInfo.CreateSpecificCulture("en-US");
       string dateString = "March 7 2009 7:30pm EST";
       string format = "MMMM d yyyy h:mmtt z";
       DateTime myDateTime = DateTime.ParseExact(dateString.Replace("EST","+6"),format,ci) ;
       DateTime newDateTime = myDateTime.AddHours(12).AddDays(1) ;
       Console.WriteLine(newDateTime.ToString(format).Replace("-5","EST")); //probably not the best way to do this
       Console.ReadLine();
   }

}</lang>

C++

Library: Boost

compiled with g++ -lboost_date_time <lang cpp>#include <string>

  1. include <iostream>
  2. include <boost/date_time/local_time/local_time.hpp>
  3. include <sstream>
  4. include <boost/date_time/gregorian/gregorian.hpp>
  5. include <vector>
  6. include <boost/algorithm/string.hpp>
  7. include <cstdlib>
  8. include <locale>


int main( ) {

  std::string datestring ("March 7 2009 7:30pm EST" ) ;
  //we must first parse the date string into a date , a time and a time
  //zone part , to take account of present restrictions in the input facets
  //of the Boost::DateTime library used for this example
  std::vector<std::string> elements ;
  //parsing the date string
  boost::split( elements , datestring , boost::is_any_of( " " ) ) ;
  std::string datepart = elements[ 0 ] + " " + "0" + elements[ 1 ] + " " +
     elements[ 2 ] ; //we must add 0 to avoid trouble with the boost::date_input format strings
  std::string timepart = elements[ 3 ] ;
  std::string timezone = elements[ 4 ] ;
  const char meridians[ ] = { 'a' , 'p' } ;
  //we have to find out if the time is am or pm, to change the hours appropriately
  std::string::size_type found = timepart.find_first_of( meridians, 0 ) ;
  std::string twelve_hour ( timepart.substr( found , 1 ) ) ;
  timepart = timepart.substr( 0 , found ) ; //we chop off am or pm
  elements.clear( ) ;
  boost::split( elements , timepart , boost::is_any_of ( ":" ) ) ;
  long hour = std::atol( (elements.begin( ))->c_str( ) ) ;// hours in the string
  if ( twelve_hour == "p" ) //it's post meridian, we're converting to 24-hour-clock
     hour += 12 ;
  long minute = std::atol( ( elements.begin( ) + 1)->c_str( ) ) ; 
  boost::local_time::tz_database tz_db ;
  tz_db.load_from_file( "/home/ulrich/internetpages/date_time_zonespec.csv" ) ;
  //according to the time zone database, this corresponds to one possible EST time zone
  boost::local_time::time_zone_ptr dyc = tz_db.time_zone_from_region( "America/New_York" ) ;
  //this is the string input format to initialize the date field 
  boost::gregorian::date_input_facet *f =
     new boost::gregorian::date_input_facet( "%B %d %Y"  ) ;
  std::stringstream ss ;
  ss << datepart ;
  ss.imbue( std::locale( std::locale::classic( ) , f ) ) ;
  boost::gregorian::date d ;
  ss >> d ;
  boost::posix_time::time_duration td (  hour , minute , 0  ) ;
  //that's how we initialize the New York local time , by using date and adding
  //time duration with values coming from parsed date input string
  boost::local_time::local_date_time lt ( d , td ,  dyc ,

boost::local_time::local_date_time::NOT_DATE_TIME_ON_ERROR ) ;

  std::cout << "local time: " << lt << '\n' ;
  ss.str( "" ) ;
  ss << lt ;
  //we have to add 12 hours, so a new time duration object is created
  boost::posix_time::time_duration td2 (12 , 0 , 0 , 0 ) ;
  boost::local_time::local_date_time ltlater = lt + td2 ; //local time 12 hours later
  boost::gregorian::date_facet *f2 =
     new boost::gregorian::date_facet( "%B %d %Y , %R %Z" ) ;
  std::cout.imbue( std::locale( std::locale::classic( ) , f2 ) ) ;
  std::cout << "12 hours after " << ss.str( )  << " it is " << ltlater << " !\n" ;
  //what's New York time in the Berlin time zone ?
  boost::local_time::time_zone_ptr bt = tz_db.time_zone_from_region( "Europe/Berlin" ) ;
  std::cout.imbue( std::locale( "de_DE.UTF-8" ) ) ; //choose the output forman appropriate for the time zone
  std::cout << "This corresponds to " << ltlater.local_time_in( bt ) << " in Berlin!\n" ;
  return 0 ;

} </lang> this produces the following output:

local time: 2009-Mar-07 19:30:00 EST
12 hours after 2009-Mar-07 19:30:00 EST it is 2009-Mar-08 08:30:00 EDT !
This corresponds to 2009-Mär-08 13:30:00 CET in Berlin!

F#

The .NET framework does not support parsing of time zone identifiers like "EST". We have to use time zone offsets like "-5".

<lang fsharp>open System

let main() =

 let est = TimeZoneInfo.FindSystemTimeZoneById("Eastern Standard Time")
 let date = DateTime.Parse("March 7 2009 7:30pm -5" )
 let date_est = TimeZoneInfo.ConvertTime( date, est) 
 let date2 = date.AddHours(12.0)
 let date2_est = TimeZoneInfo.ConvertTime( date2, est) 
 Console.WriteLine( "Original date in local time : {0}", date )
 Console.WriteLine( "Original date in EST        : {0}", date_est )
 Console.WriteLine( "12 hours later in local time: {0}", date2 )
 Console.WriteLine( "12 hours later in EST       : {0}", date2_est )

main()</lang>

Output (depends on locale settings):

Original date in local time : 08.03.2009 01:30:00
Original date in EST        : 07.03.2009 19:30:00
12 hours later in local time: 08.03.2009 13:30:00
12 hours later in EST       : 08.03.2009 07:30:00

Go

Library parser uses an easy-to-read prototype. Library formatter offers only two choices for time zone, local and UTC. Otherwise the library formatter has lots of flexibility, but code here just takes the default, "UnixDate" format. <lang go>package main

import (

   "fmt"
   "time"

)

const taskDate = "March 7 2009 7:30pm EST" const taskFormat = "January 2 2006 3:04pm MST"

func main() {

   fmt.Println("Input:", taskDate)
   t, err := time.Parse(taskFormat, taskDate)
   if err != nil {
       fmt.Println(err)
       return
   }
   t2 := t.Seconds() + 12*60*60
   fmt.Println("+12 hrs, local:", time.SecondsToLocalTime(t2))
   fmt.Println("+12 hrs, UTC:  ", time.SecondsToUTC(t2))

}</lang> Output:

Input: March 7 2009 7:30pm EST
+12 hrs, local: Sun Mar  8 08:30:00 EDT 2009
+12 hrs, UTC:   Sun Mar  8 12:30:00 UTC 2009

Haskell

<lang haskell>import Data.Time.Clock.POSIX import Data.Time.Format import System.Locale

main = print t2

 where t1 = readTime defaultTimeLocale
           "%B %e %Y %l:%M%P %Z"
           "March 7 2009 7:30pm EST"
       t2 = posixSecondsToUTCTime $ 12*60*60 + utcTimeToPOSIXSeconds t1</lang>

HicEst

<lang hicest>

  CHARACTER date="March 7 2009 7:30pm EST", am_pm, result*20
  EDIT(Text=date, Parse=cMonth, GetPosition=next)
  month = 1 + EDIT(Text='January,February,March,April,May,June,July,August,September,October,November,December', Right=cMonth, Count=',' )
  READ(Text=date(next:)) day, year, hour, minute, am_pm
  hour = hour + 12*(am_pm == 'p')
  TIME(MOnth=month, Day=day, Year=year, Hour=hour, MInute=minute, TO, Excel=xls_day)
  WRITE(Text=result, Format="UWWW CCYY-MM-DD HH:mm") xls_day + 0.5
                  ! result = "Sun 2009-03-08 07:30"
END

</lang>

Java

<lang Java>import java.util.Date; import java.text.SimpleDateFormat; public class DateManip{

   public static void main(String[] args) throws Exception{

String dateStr = "March 7 2009 7:30pm EST";

SimpleDateFormat sdf = new SimpleDateFormat("MMMM d yyyy h:mma zzz");

Date date = sdf.parse(dateStr);

date.setTime(date.getTime() + 43200000l);

System.out.println(sdf.format(date));

   }

}</lang> Output:

March 8 2009 8:30AM EDT

or using System.out.println(date); as the last line:

Sun Mar 08 08:30:00 EDT 2009

Lua

The following solution is quite ugly, but unfortunately there is not anything like 'strptime'-function in Lua. <lang lua> str = string.lower( "March 7 2009 7:30pm EST" )

month = string.match( str, "%a+" ) if month == "january" then month = 1 elseif month == "february" then month = 2 elseif month == "march" then month = 3 elseif month == "april" then month = 4 elseif month == "may" then month = 5 elseif month == "june" then month = 6 elseif month == "july" then month = 7 elseif month == "august" then month = 8 elseif month == "september" then month = 9 elseif month == "october" then month = 10 elseif month == "november" then month = 11 elseif month == "december" then month = 12 end

strproc = string.gmatch( str, "%d+" ) day = strproc() year = strproc() hour = strproc() min = strproc()

if string.find( str, "pm" ) then hour = hour + 12 end

print( os.date( "%c", os.time{ year=year, month=month, day=day, hour=hour, min=min, sec=0 } + 12 * 3600 ) ) </lang> Output:

Sun Mar  8 07:30:00 2009

Mathematica

<lang Mathematica>dstr = "March 7 2009 7:30pm EST"; DateString[DatePlus[dstr, {12, "Hour"}], {"DayName", " ", "MonthName", " ", "Day", " ", "Year", " ", "Hour24", ":", "Minute", "AMPM"}]</lang>

Perl

We use Mountain Standard Time for output.

<lang perl>use DateTime; use DateTime::Format::Strptime 'strptime'; use feature 'say';

say strptime('%b %d %Y %I:%M%p %O', 'March 7 2009 7:30pm America/New_York')

       ->add(hours => 12)
       ->set_time_zone('America/Edmonton')
       ->format_cldr('MMMM d yyyy h:mma zzzz');</lang>

Using the unambiguous Olson timezone id's ensures daylight savings is correctly handled (which is especially tricky here, since March 7/8 is the DST rollover, and times jump ahead skipping an hour)

PHP

<lang php><?php $time = new DateTime('March 7 2009 7:30pm EST'); $time->modify('+12 hours'); echo $time->format('c'); ?></lang>

PicoLisp

<lang PicoLisp>(de timePlus12 (Str)

  (use (@Mon @Day @Year @Time @Zone)
     (and
        (match
           '(@Mon " " @Day " " @Year " " @Time " " @Zone)
           (chop Str) )
        (setq @Mon (index (pack @Mon) *MonFmt))
        (setq @Day (format @Day))
        (setq @Year (format @Year))
        (setq @Time
           (case (tail 2 @Time)
              (("a" "m") ($tim (head -2 @Time)))
              (("p" "m") (+ `(time 12 0) ($tim (head -2 @Time))))
              (T ($tim @Time)) ) )
        (let? Date (date @Year @Mon @Day)
           (when (>= (inc '@Time `(time 12 0)) 86400)
              (dec '@Time 86400)
              (inc 'Date) )
           (pack (dat$ Date "-") " " (tim$ @Time T) " " @Zone) ) ) ) )</lang>

PL/I

<lang PL/I> /* The PL/I date functions handle dates and time in 49 */ /* different formats, but not that particular one. For any of the */ /* standard formats, the following date manipulation will add */ /* 12 hours to the current date/time. */

seconds = SECS(DATETIME()); seconds = seconds + 12*60*60; put list (SECSTODATE(secs)); </lang>

Python

I don't do anything with timezone here, but it is possible.

<lang python>import datetime

def mt(): datime1="March 7 2009 7:30pm EST" formatting = "%B %d %Y %I:%M%p " datime2 = datime1[:-3] # format can't handle "EST" for some reason tdelta = datetime.timedelta(hours=12) # twelve hours.. s3 = datetime.datetime.strptime(datime2, formatting) datime2 = s3+tdelta print datime2.strftime("%B %d %Y %I:%M%p %Z") + datime1[-3:]

mt()</lang>

R

<lang R>time <- strptime("March 7 2009 7:30pm EST", "%B %d %Y %I:%M%p %Z") # "2009-03-07 19:30:00" isotime <- ISOdatetime(1900 + time$year, time$mon, time$mday,

  time$hour, time$min, time$sec, "EST")                           # "2009-02-07 19:30:00 EST"

twelvehourslater <- isotime + 12 * 60 * 60 # "2009-02-08 07:30:00 EST" timeincentraleurope <- format(isotime, tz="CET", usetz=TRUE) #"2009-02-08 01:30:00 CET"</lang>

REBOL

<lang REBOL>REBOL [ Title: "Date Manipulation" Author: oofoe Date: 2009-12-06 URL: http://rosettacode.org/wiki/Date_Manipulation ]

Only North American zones here -- feel free to extend for your area.

zones: [ NST -3:30 NDT -2:30 AST -4:00 ADT -3:00 EST -5:00 EDT -4:00 CST -6:00 CDT -5:00 MST -7:00 MDT -6:00 PST -8:00 PDT -7:00 AKST -9:00 AKDT -8:00 HAST -10:00 HADT -9:00]

read-time: func [ text /local m d y t z ][ parse load text [ set m word! (m: index? find system/locale/months to-string m) set d integer! set y integer! set t time! set tz word!] to-date reduce [y m d t zones/:tz] ]

print 12:00 + read-time "March 7 2009 7:30pm EST" </lang>

Output:

8-Mar-2009/7:30-5:00

Ruby

The Time package in the standard library adds a parse method to the core Time class.

Library: RubyGems
Library: ActiveSupport

<lang ruby>require 'time' d = "March 7 2009 7:30pm EST" t = Time.parse(d) puts t.rfc2822 puts t.zone

new = t + 12*3600 puts new.rfc2822 puts new.zone

  1. another timezone

require 'rubygems' require 'active_support' zone = ActiveSupport::TimeZone['Beijing'] remote = zone.at(new)

  1. or, remote = new.in_time_zone('Beijing')

puts remote.rfc2822 puts remote.zone</lang> outputs

Sat, 07 Mar 2009 19:30:00 -0500
EST
Sun, 08 Mar 2009 08:30:00 -0400
EDT
Sun, 08 Mar 2009 20:30:00 +0800
CST

Using ActiveSupport, we can add 12 hours with any of: <lang ruby>new = t + 12.hours new = t.in(12.hours) new = t.advance(:hours => 12)</lang>

Smalltalk

Works with: GNU Smalltalk

The aim of the class DateTimeTZ is to provide the ability to understand time with "meridian" (PM/AM, even though no checks are done to assure coherency of the format) and to handle timezones despite the locale (which anyway is gently "ignored", or rather unknown in the format of letters, to Date), providing a proper set of informations to the method readFromWithMeridian:andTimeZone:.

The aDict argument must be a dictionary where keys are the abbreviated timezone code (e.g. EST), and values are three-elements array: difference between the timezone and GMT (as Duration), the DateTime when there's passage between using or not using the daylight saving time (year is ignored), and the "direction" (as Duration) of the change. All data must be filled by hand... As example I've put EST (and there's no way to represent the "new" date and time correctly with the new EDT timezone).

The code also fails when adding a duration that "jumps" beyond two DST changes (e.g from EST to EDT and EST again); (it could be partially fixed by considering intervals instead of single date, and adding a fourth element to link to the "new" timezone abbreviation)

<lang smalltalk>DateTime extend [

 setYear: aNum [ year := aNum ]

].

Object subclass: DateTimeTZ [

 |dateAndTime timeZoneDST timeZoneName timeZoneVar|
 DateTimeTZ class >> new [ ^(super basicNew) ]
 DateTimeTZ class >> readFromWithMeridian: aStream andTimeZone: aDict [
   |me|
   me := self new.
   ^ me initWithMeridian: aStream andTimeZone: aDict
 ]
 initWithMeridian: aStream andTimeZone: aDict [ |s|
   dateAndTime := DateTime readFrom: aStream copy.
   s := aStream collection asString.
   s =~ '[pP][mM]'
     ifMatched: [ :m |
       dateAndTime := dateAndTime + (Duration days: 0 hours: 12 minutes: 0 seconds: 0)
     ].
   aDict keysAndValuesDo: [ :k :v |
     s =~ k
       ifMatched: [ :x |
         dateAndTime := dateAndTime setOffset: (v at: 1).

timeZoneDST := (v at: 2) setOffset: (v at: 1). timeZoneVar := (v at: 3). timeZoneDST setYear: (self year). "ignore the year"

         timeZoneName := k
       ]
   ].
   ^ self
 ]
 setYear: aNum [ dateAndTime setYear: aNum ]
 year [ ^ dateAndTime year ]
 timeZoneName [ ^timeZoneName ]
 + aDuration [ |n|
   n := dateAndTime + aDuration.
   (n > timeZoneDST) ifTrue: [ n := n + timeZoneVar ].
   ^ (self copy dateTime: n)
 ]
 dateTime [ ^dateAndTime ]
 dateTime: aDT [ dateAndTime := aDT ]

].</lang>

Usage example (note: the code is rather rigid, so not all operations possible on DateTime are possible on DateTimeTZ).

<lang smalltalk>|s abbrDict dt|

s := 'March 7 2009 7:30pm EST'.

"Build a abbreviation -> offset for timezones (example)" abbrDict := Dictionary new.

abbrDict at: 'EST'

        put: { (Duration days: 0 hours: -5 minutes: 0 seconds: 0).
               (DateTime year: 2009 month: 3 day: 8 hour: 2 minute: 0 second: 0).

(Duration days: 0 hours: 1 minutes: 0 seconds: 0) }.

dt := DateTimeTZ readFromWithMeridian: (s readStream) andTimeZone: abbrDict.

dt := dt + (Duration days: 0 hours: 12 minutes: 0 seconds: 0).

"let's print it" ('%1 %2 %3 %4:%5%6 %7' % {

 (dt dateTime) monthName asString.
 (dt dateTime) day.
 (dt dateTime) year.
 (dt dateTime) hour12.
 (dt dateTime) minute.
 (dt dateTime) meridianAbbreviation asString.
 dt timeZoneName.

}) displayNl.

(dt dateTime) asUTC displayNl.</lang>

Output example (note that EST should be EDT):

March 8 2009 8:30AM EST
 2009-03-08T13:30:00+00:00

Tcl

Works with: Tcl version 8.5

<lang tcl>set date "March 7 2009 7:30pm EST" set epoch [clock scan $date -format "%B %d %Y %I:%M%p %z"] set later [clock add $epoch 12 hours] puts [clock format $later] ;# Sun Mar 08 08:30:00 EDT 2009 puts [clock format $later -timezone :Asia/Shanghai] ;# Sun Mar 08 20:30:00 CST 2009</lang>

Note the transition into daylight savings time in the interval (in the Eastern timezone).

UNIX Shell

requires GNU date <lang bash>date -d 'March 7 2009 7:30pm EST +12 hours'</lang>