Google.Protobuff timestamp.proto in c#
Asked Answered
E

6

9

I have successfully compiled my .proto file with google.proto.Timestamp and generated the .cs file with protoc. The only problem i am having is initialization in my c# code.

I have tried the following:

.proto File

message teststamp
{
    string Name = 1 ;
    string address = 2;
    google.protobuf.Timestamp _timeStamp = 3;
}

C# File

teststamp test = new teststamp();
test.Name = "Test";
test.address = "Test_Test_TEST"
//Example 2 : POSIX
test._timeStamp.Seconds = DateTime.Now.Second;
test._timeStamp.Nanos = DateTime.Now.Second*1000 ;

The above is compiling without errors but giving me this error: Object reference not set to an instance of an object .I have tried few other approaches but due to less help it is unable to fix the error.

Please help me out in this issue

Thanks

Ethiopian answered 6/9, 2016 at 11:43 Comment(0)
A
4

Timestamp is class so you have to create it first like this:

test._timeStamp = new Timestamp() { Seconds = DateTime.Now.Second }
Aimo answered 7/10, 2016 at 20:37 Comment(0)
S
37

Most people will probably just want to use the helper methods:

using Google.Protobuf.WellKnownTypes;

var timestamp = Timestamp.FromDateTime(DateTime.UtcNow)
Springwood answered 12/1, 2020 at 0:52 Comment(2)
+1 if you have a DateTime instance: var timestamp = Timestamp.FromDateTime(_mydatetime.ToUniversalTime())Areola
Simply: var birthday = Timestamp.FromDateTimeOffset(new DateTime(1973,12,25));Reminiscence
M
21

Full example for anyone. More info Official documentation about Google.Protobuf.WellKnownTypes.Timestamp

Proto

syntax = "proto3";

option csharp_namespace = "Test";
import "google/protobuf/timestamp.proto";

package OnlineGrpc;
service OnlineGrpcService {
  rpc SendMessage(SendMessageRequest) returns (SendMessageResponse);
}
message SendMessageRequest{
    google.protobuf.Timestamp requestDate = 1;
}

message SendMessageResponse{
    google.protobuf.Timestamp responseDate = 1;
}

C# send request

var request = new SendMessageRequest();
request.requestDate = DateTime.Now.ToTimestamp();
request.requestDate = DateTimeOffset.Now.ToTimestamp();

C# read response

var response = new SendMessageResponse();
DateTimeOffset dateOffset = response.responseDate.ToDateTimeOffset();
DateTime date = response.responseDate.ToDateTime();
Mythify answered 19/2, 2020 at 22:58 Comment(1)
C# request return Conversion from DateTime to Timestamp requires the DateTime kind to be Utc (Parameter 'dateTime'). I think we need to convert DateTime.Now to UTC and then .ToTimestamp()Tsingyuan
G
5

Here's my attempt to convert an existing DateTime instance to Timestamp:

// doc.CreateOn is a DateTime 
Timestamp.FromDateTime(DateTime.SpecifyKind(doc.CreatedOn, DateTimeKind.Utc));
Grounds answered 15/7, 2020 at 16:26 Comment(0)
A
4

Timestamp is class so you have to create it first like this:

test._timeStamp = new Timestamp() { Seconds = DateTime.Now.Second }
Aimo answered 7/10, 2016 at 20:37 Comment(0)
C
4

In case anyone stumbles across this post.

Here's a code example:

var unixTimeMS = DateTimeOffset
    .UtcNow
    .ToUnixTimeMilliseconds();

var seconds = unixTimeMS / 1000;
var nanos = (int)((unixTimeMS % 1000) * 1e6);

var lul = new ProtoModel
{
    Timestamp = new Google.Protobuf.WellKnownTypes.Timestamp()
    {
        Seconds = seconds,
        Nanos = nanos
    }
};

var unixTimeMSBack = lul.Timestamp
                .ToDateTimeOffset()
                .ToUnixTimeMilliseconds();
Couchant answered 14/3, 2019 at 13:12 Comment(0)
S
0

If you are using Mapster, Use this nuget extension:

Mapster.Protobuf.extension

It adds protobuf mappings like timestamp and others to mapster

Sheehy answered 14/6, 2024 at 4:35 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.