Francesco
Francesco

Reputation: 328

Store date values in Google Datastore

I'm trying to store a date value in Google Datastore using the Google API PHP Client but the service replies always with an error:

Invalid value for: Invalid format: "2014-08-18 12:40:52" is malformed at " 12:40:52".

Here's the interesting section of the code I use:

function create_entity($name, $property, $data) {
    $entity = new Google_Service_Datastore_Entity();
    $entity->setKey(createKey($name));

    $string_prop = new Google_Service_Datastore_Property();
    $string_prop->setStringValue($data);
    $string_prop->setIndexed(false);

    $time = date("Y-m-d H:i:s");

    $string_date = new Google_Service_Datastore_Property();
    $string_date->setDateTimeValue($time);
    $string_date->setIndexed(false);

    $property_map = [];
    $property_map[$property] = $string_prop;
    $property_map['date'] = $string_date;

    $entity->setProperties($property_map);

    return $entity;
}

function create_commit($name, $property, $data) {
    $entity = create_entity($name, $property, $data);

    $mutation = new Google_Service_Datastore_Mutation();
    $mutation->setUpsert([$entity]);

    $req = new Google_Service_Datastore_CommitRequest();
    $req->setMode('NON_TRANSACTIONAL');
    $req->setMutation($mutation);
    return $req;
}

Upvotes: 1

Views: 3201

Answers (3)

Andy N
Andy N

Reputation: 1123

Is it possible to store a datetime in local time? I tried storing in iso 8601 format with timezones offset. It records the value as string type. When you create entity in the datastore UI you can store datetime in local time. Can you store datetime in local time using the datastore node.js module?

Upvotes: 0

Tex
Tex

Reputation: 33

Where you have:

$time = date("Y-m-d H:i:s");

Replace with:

$time = date(DATE_RFC3339);

This will give you the date time formatted to comply with RFC 3339 which is the format for the Google Cloud Datastore dateTimeValue

Upvotes: 0

Patrick Costello
Patrick Costello

Reputation: 3626

According to the Cloud Datastore docs, the dateTimeValue property must be a string that is RFC 3339 formatted):

dateTimeValue: string (RFC 3339 formatted, with milliseconds, for instance 2013-05-14T00:01:00.234Z)

Upvotes: 4

Related Questions