Date parsing with sub method - parsing

I'm aware of the time package and how you can parse templates based on date/time representation. What I would like to know is how to parse time.Now() one month prior to stdLongMonth.
i.e.
time.Now() // == April, 2013
// Output: March, 2013
In other words, is it possible to parse time.now() with a sub.stdLongMonth() method? Can anyone be kind enough and show some examples?

For example,
package main
import (
"fmt"
"time"
)
func main() {
y, m, _ := time.Now().Date()
t := time.Date(y, m, 1, 0, 0, 0, 0, time.UTC)
fmt.Println(t.Format("January, 2006"))
t = time.Date(y, m-1, 1, 0, 0, 0, 0, time.UTC)
fmt.Println(t.Format("January, 2006"))
}
Output:
April, 2013
March, 2013

use time.AddDate() as this will also free you from timezone considerations:
package main
import (
"fmt"
"time"
)
func main() {
time := time.Now().AddDate(0,-1,0)
fmt.Println(time.Format("January, 2006"))
}

Related

Applying a windowing function to a Dart stream

I am pretty new to Dart, and still wrapping my head around streams. Specifically I am having some difficulty with finding the proper way of making a function that takes a window of N elements from a stream, applies a function to it and restreams the results.
To clarify what I mean, I include an example that I implemented myself which led me to this question. The code takes a byte stream from a file and converts 4 byte chunks to an integer stream. By using an await for I was able to accomplish what I wanted but I am looking for a more idiomatic stream based function that accomplishes the same thing, more succinctly.
Stream<int> loadData(String path) async* {
final f = File(path);
final byteStream = f.openRead();
var buffer = Uint8List(8);
var i = 0;
// This is where I would like to use a windowing function
await for(var bs in byteStream) {
for(var b in bs) {
buffer[i++] = b;
if(i == 8) {
var bytes = new ByteData.view(buffer.buffer);
yield bytes.getUint16(0);
i = 0;
}
}
}
}
Look at bufferCount method from RxDart package.
Buffers a number of values from the source Stream by count then emits the buffer and clears it, and starts a new buffer ...
Here is an example:
import 'dart:typed_data';
import 'package:rxdart/rxdart.dart';
main() {
var bytes = Uint8List.fromList([255, 0, 0, 0, 0, 1, 0, 0, 1, 1, 0, 0, 2, 1, 0, 0]);
Stream<int>.fromIterable(bytes)
.bufferCount(4)
.map((bytes) => Uint8List.fromList(bytes).buffer)
.map((buffer) => ByteData.view(buffer).getInt32(0, Endian.little))
.listen(print); // prints 255 256 257 258
}
It is worth noting that this particular task can be performed much easier:
bytes.buffer.asInt32List();

How to convert number words to int (like three to 3) using Dart

I want to convert number words (like one, two, three and so on) to int (like 1, 2, 3 etc) using Dart
You have to depend on machine learning library or pair every string with the respective number.
int convertStrToNum(String str) {
var number = <String, num>{'one': 1, ...};
return number[str];
}
Of course, machine learning might be the fastest and best way to do this, but I can't really help you there. So, here's an implementation that would work assuming the "number word" follows a certain format, up until 10. (You could implement a RegExp parser to make this easier, but that would get tricky).
int convStrToNum(String str) {
var oneten = <String, num> {
'one': 1,
'two': 2,
'three': 3,
'four': 4,
'five': 5,
'six': 6,
'seven': 7,
'eight': 8,
'nine': 9,
'ten': 10,
}
if (oneten.keys.contains(str)) {
return oneten[str];
}
}
int convStrToInt(String str) {
var list = [
'zero',
'one',
'two',
'three',
'four',
'five',
'six',
'seven',
'eight',
'nine',
'ten',
];
return list.indexOf(str);
}
I just pushed a repo addressing this issue. It's open to open to build upon so feel free to contribute, it would really help improve the package. Heres the link https://github.com/michaelessiet/wordstonumbers.dart

Go: time.Parse() issue

I have the following code:
timeLocal := "01/July/2015:18:12:25 +0900"
inFormat := "02/January/2006:15:04:05 -0700"
parsed, err := time.Parse(inFormat, timeLocal)
if err != nil {
log.Fatal("Time format was not recognized!")
}
Now, parsing works fine. But when I run:
fmt.Println(timeLocal)
fmt.Println(inFormat)
fmt.Println(parsed)
The output is:
01/July/2015:18:12:25 +0900
02/January/2006:15:04:05 -0700
2015-07-01 18:12:25 +0900 +0900
Should the second +0900 be there? What is the stupid thing that I'm doing? Sorry, it was really a long day, I don't see what I'm missing.
Oh, and the whole file is here:
package main
import (
"fmt"
"time"
"log"
)
func main() {
timeLocal := "01/July/2015:18:12:25 +0900"
inFormat := "02/January/2006:15:04:05 -0700"
parsed, err := time.Parse(inFormat, timeLocal)
if err != nil {
log.Fatal("Time format was not recognized!")
}
fmt.Println(timeLocal)
fmt.Println(inFormat)
fmt.Println(parsed)
}
If you look at documentation of time.Time you will see what the default output format is:
String returns the time formatted using the format string:
"2006-01-02 15:04:05.999999999 -0700 MST"
Now you should see what the second +0900 is doing there – this is a location (time zone) name. Since your input format doesn't have a name it will simply repeat an offset.
You can provide name by altering your input format to parse location name. Alternatively you can provide an output format which doesn't print the name, if you don't need it.
Your modified example:
package main
import (
"fmt"
"time"
"log"
)
func main() {
timeLocal := "01/July/2015:18:12:25 +0900 XYZ"
inFormat := "02/January/2006:15:04:05 -0700 MST"
parsed, err := time.Parse(inFormat, timeLocal)
if err != nil {
log.Fatal("Time format was not recognized!")
}
fmt.Println(timeLocal)
fmt.Println(inFormat)
fmt.Println(parsed) // 2015-07-01 18:12:25 +0900 XYZ
fmt.Println(parsed.Format("02/January/2006:15:04:05 -0700"))
}
http://play.golang.org/p/xVGvlt-M5B
The default format used by Time.String is this:
2006-01-02 15:04:05.999999999 -0700 MST
Notice the "MST" part. Since you're not providing the name of the zone, the format just "names" it the same as offset, that is "+0900". If you change that to "+0000", you'll see that this is indeed a time zone name:
2015-07-01 18:12:25 +0000 UTC
If you don't want that, just use a separate format for printing:
myFmt := "2006-01-02 15:04:05.999999999 -0700"
fmt.Println(parsed.Format(myFmt))

Jquery UI Datepicker disabling wrong days

I have run in to issue that days that have to be disabled are shifted to the next day.
The idea is that day that does not exist in our booking object or have a value less than 1 should be disabled on calendar.
here is simplified version of my script and demonstration on jsfiddle:
var bookings = {
"2012-09-01": 24,
"2012-09-03": 31,
"2012-09-05": 27,
"2012-09-06": 9,
"2012-09-07": 18,
"2012-09-08": 0,
"2012-09-10": 20,
"2012-09-12": 19,
"2012-09-13": 0,
"2012-09-14": 9,
"2012-09-15": 24,
"2012-09-17": 19,
"2012-09-19": 28,
"2012-09-20": 15,
"2012-09-21": 12,
"2012-09-22": 25,
"2012-09-24": 19,
"2012-09-26": 0,
"2012-09-27": 0,
"2012-09-28": 0,
"2012-09-29": 0
};
function MyEvent(date)
{
bookings = bookings || {};
this.date = date.toISOString().substr(0, 10);
this.display = (typeof bookings[this.date] == 'number' && bookings[this.date] > 0);
return this;
}
MyEvent.prototype.toArray = function () {
return [this.display, null, null];
};
$(function ()
{
$('#eventCalendar').datepicker({
dateFormat: "yy-mm-dd",
firstDay: 1,
defaultDate: "2012-09-24",
beforeShowDay: function (date)
{
return new MyEvent(date).toArray();
}
}
);
});
Can some one suggest me what am I doing wrong or is it a bug?
It was a little struggle, but is not a bug.
There is a problem using:
date.toISOString()
the operation can return a different date (for example next day) from the original date passed from the plug in, because the function ignores timezone offsets
https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/Date/toISOString
so your condition will not work well.
Here is a working fiddle the is not using that function: http://jsfiddle.net/nBejK/2/

Difficult couchdb query

I have the following query:
view.reduce.group_level(5).keys
which returns:
[["1f9c79a33f399a7937d880c5f31e8dbc", 2011, 12, 29, 13], ["1f9c79a33f399a7937d880c5f31e8dbc", 2011, 12, 29, 14], ["c38332ffc275b6c70bcf06ffc39ddbdd", 2011, 12, 29, 13], ["c38332ffc275b6c70bcf06ffc39ddbdd", 2011, 12, 29, 14]]
The first key is an id and the other keys are year, month, day, hour
I would like all the rows between 2010 and 2013. So I want to ignore the first key.
The problem is that i need to set the first parameter to get the results but i want to get all the results for all the keys.
for example: view.reduce.group_level(5).startkey(["every_possible_key", 2010]).endkey(['every_possible_key", 2013, {}])
If i leave the first key blank than i get nothing. If i give it "\u9999" than i get everything and it ignores the 2nd key.
Somebody knows what I am doing wrong?
Thanks a lot.
map:
function(d) {
if (d['type'] == 'State' && d['driver_id'] && d['name'] && d['created_at']) {
var dt = new Date(d.created_at);
emit([d.driver_id, dt.getFullYear(), dt.getMonth() + 1, dt.getDate(), dt.getHours()], d.name);
}
}
reduce:
function(k,v,r) {
var result = {
'hire': 0, 'hired': 0, 'arrived': 0, 'pick up': 0, 'drop off': 0,
'missed': 0, 'rider cancel': 0, 'driver cancel': 0, 'no show': 0,
'avail': 0, 'unavail': 0, 'other': 0
};
if (r) {
var row = null;
for (i in v) {
row = v[i];
for (j in row) {
result[j] += row[j];
}
}
} else {
for (i in v) {
if (result[v[i]] != null) {
result[v[i]] += 1;
} else {
result['other'] += 1;
}
}
}
return result;
}
What you're "doing wrong" is to use a key you don't need in your query as the first key of your view.
If you need it for another query, create another view.

Resources