CDE
This commit is contained in:
parent
518da4a923
commit
764da6cbbb
6144 changed files with 83610 additions and 11 deletions
3
Task/Date-manipulation/0DESCRIPTION
Normal file
3
Task/Date-manipulation/0DESCRIPTION
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
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.
|
||||
2
Task/Date-manipulation/1META.yaml
Normal file
2
Task/Date-manipulation/1META.yaml
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
---
|
||||
note: Text processing
|
||||
97
Task/Date-manipulation/Ada/date-manipulation.ada
Normal file
97
Task/Date-manipulation/Ada/date-manipulation.ada
Normal file
|
|
@ -0,0 +1,97 @@
|
|||
with Ada.Calendar;
|
||||
with Ada.Calendar.Formatting;
|
||||
with Ada.Calendar.Time_Zones;
|
||||
with Ada.Integer_Text_IO;
|
||||
with Ada.Text_IO;
|
||||
|
||||
procedure Date_Manipulation is
|
||||
|
||||
type Month_Name_T is
|
||||
(January, February, March, April, May, June,
|
||||
July, August, September, October, November, December);
|
||||
|
||||
type Time_Zone_Name_T is (EST, Lisbon);
|
||||
|
||||
type Period_T is (AM, PM);
|
||||
|
||||
package TZ renames Ada.Calendar.Time_Zones;
|
||||
use type TZ.Time_Offset;
|
||||
|
||||
Time_Zone_Offset : array (Time_Zone_Name_T) of TZ.Time_Offset :=
|
||||
(EST => -5 * 60,
|
||||
Lisbon => 0);
|
||||
|
||||
Period_Offset : array (Period_T) of Natural :=
|
||||
(AM => 0,
|
||||
PM => 12);
|
||||
|
||||
package Month_Name_IO is
|
||||
new Ada.Text_IO.Enumeration_IO (Month_Name_T);
|
||||
|
||||
package Time_Zone_Name_IO is
|
||||
new Ada.Text_IO.Enumeration_IO (Time_Zone_Name_T);
|
||||
|
||||
package Period_IO is
|
||||
new Ada.Text_IO.Enumeration_IO (Period_T);
|
||||
|
||||
package Std renames Ada.Calendar;
|
||||
use type Std.Time;
|
||||
|
||||
package Fmt renames Std.Formatting;
|
||||
|
||||
function To_Number (Name : Month_Name_T) return Std.Month_Number is
|
||||
begin
|
||||
return Std.Month_Number (Month_Name_T'Pos (Name) + 1);
|
||||
end;
|
||||
|
||||
function To_Time (S : String) return Std.Time is
|
||||
Month : Month_Name_T;
|
||||
Day : Std.Day_Number;
|
||||
Year : Std.Year_Number;
|
||||
Hour : Fmt.Hour_Number;
|
||||
Minute : Fmt.Minute_Number;
|
||||
Period : Period_T;
|
||||
Time_Zone : Time_Zone_Name_T;
|
||||
I : Natural;
|
||||
begin
|
||||
Month_Name_IO.Get
|
||||
(From => S, Item => Month, Last => I);
|
||||
Ada.Integer_Text_IO.Get
|
||||
(From => S (I + 1 .. S'Last), Item => Day, Last => I);
|
||||
Ada.Integer_Text_IO.Get
|
||||
(From => S (I + 1 .. S'Last), Item => Year, Last => I);
|
||||
Ada.Integer_Text_IO.Get
|
||||
(From => S (I + 1 .. S'Last), Item => Hour, Last => I);
|
||||
Ada.Integer_Text_IO.Get
|
||||
(From => S (I + 2 .. S'Last), Item => Minute, Last => I);
|
||||
-- here we start 2 chars down to skip the ':'
|
||||
Period_IO.Get
|
||||
(From => S (I + 1 .. S'Last), Item => Period, Last => I);
|
||||
Time_Zone_Name_IO.Get
|
||||
(From => S (I + 1 .. S'Last), Item => Time_Zone, Last => I);
|
||||
return Fmt.Time_Of
|
||||
(Year => Year,
|
||||
Month => To_Number (Month),
|
||||
Day => Day,
|
||||
Hour => Hour + Period_Offset (Period),
|
||||
Minute => Minute,
|
||||
Second => 0,
|
||||
Time_Zone => Time_Zone_Offset (Time_Zone));
|
||||
end;
|
||||
|
||||
function Img
|
||||
(Date : Std.Time; Zone : Time_Zone_Name_T) return String is
|
||||
begin
|
||||
return
|
||||
Fmt.Image (Date => Date, Time_Zone => Time_Zone_Offset (Zone)) &
|
||||
" " & Time_Zone_Name_T'Image (Zone);
|
||||
end;
|
||||
|
||||
T1, T2 : Std.Time;
|
||||
use Ada.Text_IO;
|
||||
begin
|
||||
T1 := To_Time ("March 7 2009 7:30pm EST");
|
||||
T2 := T1 + 12.0 * 60.0 * 60.0;
|
||||
Put_Line ("T1 => " & Img (T1, EST) & " = " & Img (T1, Lisbon));
|
||||
Put_Line ("T2 => " & Img (T2, EST) & " = " & Img (T2, Lisbon));
|
||||
end;
|
||||
|
|
@ -0,0 +1,2 @@
|
|||
set x to "March 7 2009 7:30pm EST"
|
||||
return (date x) + 12 * hours
|
||||
|
|
@ -0,0 +1 @@
|
|||
date "Sunday, March 8, 2009 7:30:00 AM"
|
||||
65
Task/Date-manipulation/AutoHotkey/date-manipulation.ahk
Normal file
65
Task/Date-manipulation/AutoHotkey/date-manipulation.ahk
Normal file
|
|
@ -0,0 +1,65 @@
|
|||
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
|
||||
}
|
||||
32
Task/Date-manipulation/BBC-BASIC/date-manipulation.bbc
Normal file
32
Task/Date-manipulation/BBC-BASIC/date-manipulation.bbc
Normal file
|
|
@ -0,0 +1,32 @@
|
|||
INSTALL @lib$+"DATELIB"
|
||||
|
||||
date$ = "March 7 2009 7:30pm EST"
|
||||
|
||||
mjd% = FN_readdate(date$, "mdy", 0)
|
||||
colon% = INSTR(date$, ":")
|
||||
hours% = VAL(MID$(date$, colon%-2))
|
||||
IF INSTR(date$, "am") IF hours%=12 hours% -= 12
|
||||
IF INSTR(date$, "pm") IF hours%<>12 hours% += 12
|
||||
mins% = VAL(MID$(date$, colon%+1))
|
||||
|
||||
now% = mjd% * 1440 + hours% * 60 + mins%
|
||||
new% = now% + 12 * 60 : REM 12 hours later
|
||||
|
||||
PRINT FNformat(new%, "EST")
|
||||
PRINT FNformat(new% + 5 * 60, "GMT")
|
||||
PRINT FNformat(new% - 3 * 60, "PST")
|
||||
END
|
||||
|
||||
DEF FNformat(datetime%, zone$)
|
||||
LOCAL mjd%, hours%, mins%, ampm$
|
||||
mjd% = datetime% DIV 1440
|
||||
hours% = (datetime% DIV 60) MOD 24
|
||||
mins% = datetime% MOD 60
|
||||
|
||||
IF hours% < 12 THEN ampm$ = "am" ELSE ampm$ = "pm"
|
||||
IF hours% = 0 hours% += 12
|
||||
IF hours% > 12 hours% -= 12
|
||||
|
||||
= FN_date$(mjd%, "MMMM d yyyy") + " " + STR$(hours%) + \
|
||||
\ ":" + RIGHT$("0"+STR$(mins%), 2) + ampm$ + " " + zone$
|
||||
ENDPROC
|
||||
67
Task/Date-manipulation/C++/date-manipulation.cpp
Normal file
67
Task/Date-manipulation/C++/date-manipulation.cpp
Normal file
|
|
@ -0,0 +1,67 @@
|
|||
#include <string>
|
||||
#include <iostream>
|
||||
#include <boost/date_time/local_time/local_time.hpp>
|
||||
#include <sstream>
|
||||
#include <boost/date_time/gregorian/gregorian.hpp>
|
||||
#include <vector>
|
||||
#include <boost/algorithm/string.hpp>
|
||||
#include <cstdlib>
|
||||
#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 ;
|
||||
}
|
||||
19
Task/Date-manipulation/C/date-manipulation.c
Normal file
19
Task/Date-manipulation/C/date-manipulation.c
Normal file
|
|
@ -0,0 +1,19 @@
|
|||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#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;
|
||||
}
|
||||
11
Task/Date-manipulation/Clojure/date-manipulation.clj
Normal file
11
Task/Date-manipulation/Clojure/date-manipulation.clj
Normal file
|
|
@ -0,0 +1,11 @@
|
|||
(import java.util.Date
|
||||
java.text.SimpleDateFormat)
|
||||
|
||||
(defn time+12 [s]
|
||||
(let [sdf (SimpleDateFormat. "MMMM d yyyy h:mma zzz")]
|
||||
(-> (.parse sdf s)
|
||||
(.getTime ,)
|
||||
(+ , 43200000)
|
||||
long
|
||||
(Date. ,)
|
||||
(->> , (.format sdf ,)))))
|
||||
81
Task/Date-manipulation/Delphi/date-manipulation.delphi
Normal file
81
Task/Date-manipulation/Delphi/date-manipulation.delphi
Normal file
|
|
@ -0,0 +1,81 @@
|
|||
program DateManipulation;
|
||||
|
||||
{$APPTYPE CONSOLE}
|
||||
|
||||
uses
|
||||
SysUtils,
|
||||
DateUtils;
|
||||
|
||||
function MonthNumber(aMonth: string): Word;
|
||||
begin
|
||||
//Convert a string value representing the month
|
||||
//to its corresponding numerical value
|
||||
if aMonth = 'January' then Result:= 1
|
||||
else if aMonth = 'February' then Result:= 2
|
||||
else if aMonth = 'March' then Result:= 3
|
||||
else if aMonth = 'April' then Result:= 4
|
||||
else if aMonth = 'May' then Result:= 5
|
||||
else if aMonth = 'June' then Result:= 6
|
||||
else if aMonth = 'July' then Result:= 7
|
||||
else if aMonth = 'August' then Result:= 8
|
||||
else if aMonth = 'September' then Result:= 9
|
||||
else if aMonth = 'October' then Result:= 10
|
||||
else if aMonth = 'November' then Result:= 11
|
||||
else if aMonth = 'December' then Result:= 12
|
||||
else Result:= 12;
|
||||
end;
|
||||
|
||||
function ParseString(aDateTime: string): TDateTime;
|
||||
var
|
||||
strDay,
|
||||
strMonth,
|
||||
strYear,
|
||||
strTime: string;
|
||||
iDay,
|
||||
iMonth,
|
||||
iYear: Word;
|
||||
TimePortion: TDateTime;
|
||||
begin
|
||||
//Decode the month from the given string
|
||||
strMonth:= Copy(aDateTime, 1, Pos(' ', aDateTime) - 1);
|
||||
Delete(aDateTime, 1, Pos(' ', aDateTime));
|
||||
iMonth:= MonthNumber(strMonth);
|
||||
|
||||
//Decode the day from the given string
|
||||
strDay:= Copy(aDateTime, 1, Pos(' ', aDateTime) - 1);
|
||||
Delete(aDateTime, 1, Pos(' ', aDateTime));
|
||||
iDay:= StrToIntDef(strDay, 30);
|
||||
|
||||
//Decode the year from the given string
|
||||
strYear:= Copy(aDateTime, 1, Pos(' ', aDateTime) -1);
|
||||
Delete(aDateTime, 1, Pos(' ', aDateTime));
|
||||
iYear:= StrToIntDef(strYear, 1899);
|
||||
|
||||
//Decode the time value from the given string
|
||||
strTime:= Copy(aDateTime, 1, Pos(' ', aDateTime) -1);
|
||||
|
||||
//Encode the date value and assign it to result
|
||||
Result:= EncodeDate(iYear, iMonth, iDay);
|
||||
|
||||
//Encode the time value and add it to result
|
||||
if TryStrToTime(strTime, TimePortion) then
|
||||
Result:= Result + TimePortion;
|
||||
end;
|
||||
|
||||
function Add12Hours(aDateTime: string): string;
|
||||
var
|
||||
tmpDateTime: TDateTime;
|
||||
begin
|
||||
//Adding 12 hours to the given
|
||||
//date time string value
|
||||
tmpDateTime:= ParseString(aDateTime);
|
||||
tmpDateTime:= IncHour(tmpDateTime, 12);
|
||||
|
||||
//Formatting the output
|
||||
Result:= FormatDateTime('mm/dd/yyyy hh:mm AM/PM', tmpDateTime);
|
||||
end;
|
||||
|
||||
begin
|
||||
Writeln(Add12Hours('March 7 2009 7:30pm EST'));
|
||||
Readln;
|
||||
end.
|
||||
10
Task/Date-manipulation/Euphoria/date-manipulation.euphoria
Normal file
10
Task/Date-manipulation/Euphoria/date-manipulation.euphoria
Normal file
|
|
@ -0,0 +1,10 @@
|
|||
--Date Manipulation task from Rosetta Code wiki
|
||||
--User:Lnettnay
|
||||
|
||||
include std/datetime.e
|
||||
|
||||
datetime dt
|
||||
|
||||
dt = new(2009, 3, 7, 19, 30)
|
||||
dt = add(dt, 12, HOURS)
|
||||
printf(1, "%s EST\n", {format(dt, "%B %d %Y %I:%M %p")})
|
||||
31
Task/Date-manipulation/Go/date-manipulation.go
Normal file
31
Task/Date-manipulation/Go/date-manipulation.go
Normal file
|
|
@ -0,0 +1,31 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"time"
|
||||
)
|
||||
|
||||
const taskDate = "March 7 2009 7:30pm EST"
|
||||
const taskFormat = "January 2 2006 3:04pm MST"
|
||||
|
||||
func main() {
|
||||
if etz, err := time.LoadLocation("US/Eastern"); err == nil {
|
||||
time.Local = etz
|
||||
}
|
||||
fmt.Println("Input: ", taskDate)
|
||||
t, err := time.Parse(taskFormat, taskDate)
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
return
|
||||
}
|
||||
t = t.Add(12 * time.Hour)
|
||||
fmt.Println("+12 hrs: ", t)
|
||||
if _, offset := t.Zone(); offset == 0 {
|
||||
fmt.Println("No time zone info.")
|
||||
return
|
||||
}
|
||||
atz, err := time.LoadLocation("US/Arizona")
|
||||
if err == nil {
|
||||
fmt.Println("+12 hrs in Arizona:", t.In(atz))
|
||||
}
|
||||
}
|
||||
9
Task/Date-manipulation/Haskell/date-manipulation.hs
Normal file
9
Task/Date-manipulation/Haskell/date-manipulation.hs
Normal file
|
|
@ -0,0 +1,9 @@
|
|||
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
|
||||
16
Task/Date-manipulation/Java/date-manipulation.java
Normal file
16
Task/Date-manipulation/Java/date-manipulation.java
Normal file
|
|
@ -0,0 +1,16 @@
|
|||
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));
|
||||
}
|
||||
|
||||
}
|
||||
51
Task/Date-manipulation/JavaScript/date-manipulation.js
Normal file
51
Task/Date-manipulation/JavaScript/date-manipulation.js
Normal file
|
|
@ -0,0 +1,51 @@
|
|||
function add12hours(dateString) {
|
||||
|
||||
// Get the parts of the date string
|
||||
var parts = dateString.split(/\s+/);
|
||||
var date = parts[1];
|
||||
var month = parts[0];
|
||||
var year = parts[2];
|
||||
var time = parts[3];
|
||||
var ampm = time && time.match(/[a-z]+$/i)[0];
|
||||
var hr = Number(time.split(':')[0]);
|
||||
var min = Number(time.split(':')[1].replace(/\D/g,''));
|
||||
var zone = parts[4].toUpperCase();
|
||||
var months = ['January','February','March','April','May','June',
|
||||
'July','August','September','October','November','December'];
|
||||
var zones = {'EST': 300, 'AEST': -600}; // Minutes to add to zone time to get UTC
|
||||
|
||||
// Convert month name to number, zero indexed
|
||||
// Could use indexOf but not supported widely
|
||||
for (var i=0, iLen=months.length; i<iLen; i++) {
|
||||
if (months[i] == month) {
|
||||
month = i;
|
||||
}
|
||||
}
|
||||
if (typeof month != 'number') return; // Invalid month name provided
|
||||
|
||||
// Convert hours to 24hr
|
||||
if (ampm && ampm.toLowerCase() == 'pm') {
|
||||
hr += 12;
|
||||
}
|
||||
|
||||
// Add 12 hours to hours
|
||||
hr += 12;
|
||||
|
||||
// Create a date object in local zone
|
||||
var d = new Date(year, month, date);
|
||||
d.setHours(hr, min, 0, 0);
|
||||
|
||||
// Adjust minutes for the time zones
|
||||
d.setMinutes(d.getMinutes() + zones[zone] - d.getTimezoneOffset() );
|
||||
|
||||
// d is now a local date representing the same moment as the
|
||||
// source date plus 12 hours
|
||||
return d;
|
||||
}
|
||||
|
||||
var inputDateString = 'March 7 2009 7:30pm EST';
|
||||
|
||||
alert(
|
||||
'Input: ' + inputDateString + '\n' +
|
||||
'+12hrs in local time: ' + add12hours(inputDateString)
|
||||
);
|
||||
26
Task/Date-manipulation/Lua/date-manipulation.lua
Normal file
26
Task/Date-manipulation/Lua/date-manipulation.lua
Normal file
|
|
@ -0,0 +1,26 @@
|
|||
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 ) )
|
||||
5
Task/Date-manipulation/PHP/date-manipulation.php
Normal file
5
Task/Date-manipulation/PHP/date-manipulation.php
Normal file
|
|
@ -0,0 +1,5 @@
|
|||
<?php
|
||||
$time = new DateTime('March 7 2009 7:30pm EST');
|
||||
$time->modify('+12 hours');
|
||||
echo $time->format('c');
|
||||
?>
|
||||
11
Task/Date-manipulation/Perl/date-manipulation.pl
Normal file
11
Task/Date-manipulation/Perl/date-manipulation.pl
Normal file
|
|
@ -0,0 +1,11 @@
|
|||
use DateTime;
|
||||
use DateTime::Format::Strptime 'strptime';
|
||||
use feature 'say';
|
||||
|
||||
my $input = 'March 7 2009 7:30pm EST';
|
||||
$input =~ s{EST}{America/New_York};
|
||||
|
||||
say strptime('%b %d %Y %I:%M%p %O', $input)
|
||||
->add(hours => 12)
|
||||
->set_time_zone('America/Edmonton')
|
||||
->format_cldr('MMMM d yyyy h:mma zzz');
|
||||
19
Task/Date-manipulation/PicoLisp/date-manipulation.l
Normal file
19
Task/Date-manipulation/PicoLisp/date-manipulation.l
Normal file
|
|
@ -0,0 +1,19 @@
|
|||
(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) ) ) ) )
|
||||
12
Task/Date-manipulation/Python/date-manipulation.py
Normal file
12
Task/Date-manipulation/Python/date-manipulation.py
Normal file
|
|
@ -0,0 +1,12 @@
|
|||
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()
|
||||
5
Task/Date-manipulation/R/date-manipulation.r
Normal file
5
Task/Date-manipulation/R/date-manipulation.r
Normal file
|
|
@ -0,0 +1,5 @@
|
|||
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"
|
||||
13
Task/Date-manipulation/REXX/date-manipulation.rexx
Normal file
13
Task/Date-manipulation/REXX/date-manipulation.rexx
Normal file
|
|
@ -0,0 +1,13 @@
|
|||
/*REXX pgm to add 12 hours to a date & time, showing the before & after.*/
|
||||
aDate = 'March 7 2009 7:30pm EST'
|
||||
parse var aDate mon dd yyyy hhmm tz . /*obtain the various parts&pieces*/
|
||||
mins = time('M',hhmm,'C') /*get the # minutes past midnight*/
|
||||
mins = mins + (12*60) /*add twelve hours to timestamp. */
|
||||
nMins = mins // 1440 /*compute #min into same/next day*/
|
||||
days = mins % 1440 /*compute number of days "added".*/
|
||||
aBdays = date('B',dd left(mon,3) yyyy) /*# of base days since REXX epoch*/
|
||||
nBdays = aBdays + days /*now, add the # of days "added".*/
|
||||
nDate = date(,nBdays,'B') /*calculate the new date (maybe).*/
|
||||
nTime = time('C',nMins,'M') /* " " " time " */
|
||||
say aDate ' + 12 hours ───► ' ndate ntime tz /*display new timestamp.*/
|
||||
/*stick a fork in it, we're done.*/
|
||||
18
Task/Date-manipulation/Ruby/date-manipulation-1.rb
Normal file
18
Task/Date-manipulation/Ruby/date-manipulation-1.rb
Normal file
|
|
@ -0,0 +1,18 @@
|
|||
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
|
||||
|
||||
# another timezone
|
||||
require 'rubygems'
|
||||
require 'active_support'
|
||||
zone = ActiveSupport::TimeZone['Beijing']
|
||||
remote = zone.at(new)
|
||||
# or, remote = new.in_time_zone('Beijing')
|
||||
puts remote.rfc2822
|
||||
puts remote.zone
|
||||
3
Task/Date-manipulation/Ruby/date-manipulation-2.rb
Normal file
3
Task/Date-manipulation/Ruby/date-manipulation-2.rb
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
new = t + 12.hours
|
||||
new = t.in(12.hours)
|
||||
new = t.advance(:hours => 12)
|
||||
17
Task/Date-manipulation/Scala/date-manipulation.scala
Normal file
17
Task/Date-manipulation/Scala/date-manipulation.scala
Normal file
|
|
@ -0,0 +1,17 @@
|
|||
import java.text.SimpleDateFormat
|
||||
import java.util.{Calendar, Locale, TimeZone}
|
||||
|
||||
object DateManipulation {
|
||||
def main(args: Array[String]): Unit = {
|
||||
val input="March 7 2009 7:30pm EST"
|
||||
val df=new SimpleDateFormat("MMMM d yyyy h:mma z", Locale.ENGLISH)
|
||||
val c=Calendar.getInstance()
|
||||
c.setTime(df.parse(input))
|
||||
|
||||
c.add(Calendar.HOUR_OF_DAY, 12)
|
||||
println(df.format(c.getTime))
|
||||
|
||||
df.setTimeZone(TimeZone.getTimeZone("GMT"))
|
||||
println(df.format(c.getTime))
|
||||
}
|
||||
}
|
||||
50
Task/Date-manipulation/Smalltalk/date-manipulation-1.st
Normal file
50
Task/Date-manipulation/Smalltalk/date-manipulation-1.st
Normal file
|
|
@ -0,0 +1,50 @@
|
|||
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 ]
|
||||
|
||||
].
|
||||
29
Task/Date-manipulation/Smalltalk/date-manipulation-2.st
Normal file
29
Task/Date-manipulation/Smalltalk/date-manipulation-2.st
Normal file
|
|
@ -0,0 +1,29 @@
|
|||
|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.
|
||||
5
Task/Date-manipulation/Tcl/date-manipulation.tcl
Normal file
5
Task/Date-manipulation/Tcl/date-manipulation.tcl
Normal file
|
|
@ -0,0 +1,5 @@
|
|||
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
|
||||
Loading…
Add table
Add a link
Reference in a new issue