Just another update

This commit is contained in:
Ingy döt Net 2015-02-20 00:35:01 -05:00
parent a25938f123
commit 00a190b0a6
6591 changed files with 94363 additions and 23227 deletions

View file

@ -1,3 +1,7 @@
Given the date string "March 7 2009 7:30pm EST", output the time 12 hours later in any human-readable format.
{{omit from|ML/I}}
{{omit from|PARI/GP|No real capacity for string manipulation}}
Given the date string "March 7 2009 7:30pm EST", <br>
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.

View file

@ -1,2 +1,4 @@
---
category:
- Date and time
note: Text processing

View file

@ -0,0 +1,27 @@
require'dates'
months=: <;._2 tolower 0 :0
January
February
March
April
May
June
July
August
September
October
November
December
)
numbers=: _".' '"_`(1 I.@:-e.&(":i.10)@])`]}~
words=: [:;:@tolower' '"_`(I.@(tolower = toupper)@])`]}~
getyear=: >./@numbers
getmonth=: 1 + months <./@i. words
getday=: {.@(numbers -. getyear)
gethour=: (2 { numbers) + 12 * (<'pm') e. words
getminsec=: 2 {. 3}. numbers
getts=: getyear, getmonth, getday, gethour, getminsec
timeadd=: 1&tsrep@+&tsrep
deltaT=: (1 tsrep 0)&([ + -@#@[ {. ])

View file

@ -0,0 +1,6 @@
(deltaT 12 0 0) timeadd getts 'March 7 2009 7:30pm EST'
2009 3 8 7 30 0
timestamp (deltaT 12 0 0) timeadd getts 'March 7 2009 7:30pm EST'
08 Mar 2009 07:30:00
isotimestamp (deltaT 12 0 0) timeadd getts 'March 7 2009 7:30pm EST'
2009-03-08 07:30:00.000

View file

@ -1,51 +1,41 @@
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 parts = dateString.split(/\s+/),
date = parts[1],
month = parts[0],
year = parts[2],
time = parts[3];
var hr = Number(time.split(':')[0]),
min = Number(time.split(':')[1].replace(/\D/g,'')),
ampm = time && time.match(/[a-z]+$/i)[0],
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
'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 month name to number, zero indexed. Return if invalid month
month = months.indexOf(month);
if (month === -1) { return; }
// Convert hours to 24hr
if (ampm && ampm.toLowerCase() == 'pm') {
hr += 12;
}
// Add 12 hours to hours
hr += 12;
// Add 12 hours as specified. Add another 12 if pm for 24hr time
hr += (ampm.toLowerCase() === 'pm') ? 24 : 12
// Create a date object in local zone
var d = new Date(year, month, date);
d.setHours(hr, min, 0, 0);
var localTime = new Date(year, month, date);
localTime.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;
// Adjust localTime minutes for the time zones so it is now a local date
// representing the same moment as the source date plus 12 hours
localTime.setMinutes(localTime.getMinutes() + zones[zone] - localTime.getTimezoneOffset() );
return localTime;
}
var inputDateString = 'March 7 2009 7:30pm EST';
alert(
console.log(
'Input: ' + inputDateString + '\n' +
'+12hrs in local time: ' + add12hours(inputDateString)
);

View file

@ -0,0 +1,6 @@
require "date"
puts d1 = DateTime.parse("March 7 2009 7:30pm EST")
# d1 + 1 would add a day, so add half a day:
puts d2 = d1 + 1/2r # 1/2r is a rational; 0.5 would also work
puts d3 = d2.new_offset('+09:00')