# DateTime optionally containing microseconds

**URL:** <https://www.ruby-forum.com/t/datetime-optionally-containing-microseconds/176433>\
**Category:** Rails\
**Created:** [September 30, 2009, 3:09pm UTC](https://www.ruby-forum.com/t/datetime-optionally-containing-microseconds/176433 "2009-09-30T15:09:35Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![ambrice](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/d9b06d/32.png) [@ambrice](https://www.ruby-forum.com/u/ambrice)\
**Post date:** [September 30, 2009, 3:09pm UTC](https://www.ruby-forum.com/t/datetime-optionally-containing-microseconds/176433/1 "2009-09-30T15:09:35Z")

</div>

Hi all,

I’m sending DateTimes over soap to JADE agent system.

If I send DateTime.now over it is accepted fine… and ends up as:

2009-09-30T09:20:52.959452-03:00

However, if I convert a Rails model field to datetime and send it over  
its of class DateTime but loses the microseconds. Which my SOAP  
server inconveniently requires to be present.

@seller.deadline.to\_datetime

ends up as:

2009-09-29T23:20:00-03:00

And is rejected.

I was able to get @seller.deadline.to\_datetime to be accepted by  
adding the microsecond portion of DateTime.now to it. But that seems  
like an awful hack.

How do I ensure a given DateTime such as the one extracted from a  
rails model is alays sent with the microsecond portion present over  
SOAP?

thanks,  
Aaron

```
    obj = EcommercePort.new
    obj.wiredump_dev = STDERR if $DEBUG

    time_periods = ArrayOfSellerTimePeriod.new
    #time_periods[0] = SellerTimePeriod.new

```

(@seller.deadline.to\_datetime, @seller.openingOffer, @seller.alpha,  
@seller.umin, @seller.deadline.to\_datetime)  
time\_periods[0] = SellerTimePeriod.new  
(@seller.deadline.to\_datetime + DateTime.now.sec\_fraction,  
@seller.openingOffer.to\_f, @seller.alpha, @seller.umin, DateTime.now)

```
    p time_periods[0]

    parameters = {
      :sellerID => @seller.sellerID,
      :maxPossPrice => @seller.openingOffer.to_f,
      :itemID => @seller.itemID,
      :deadline => DateTime.now,
      :timePeriods => time_periods
    }

    puts obj.clientGatewayCreateNewSeller(parameters)
```

---

<div class="post-metadata">

**Author:** ![ambrice](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/d9b06d/32.png) [@ambrice](https://www.ruby-forum.com/u/ambrice)\
**Post date:** [September 30, 2009, 5:23pm UTC](https://www.ruby-forum.com/t/datetime-optionally-containing-microseconds/176433/2 "2009-09-30T17:23:39Z")

</div>

On Sep 30, 2009, at 9:08 AM, Aaron B. wrote:

> server inconveniently requires to be present.  
> adding the microsecond portion of DateTime.now to it. But that seems  
> obj.wiredump\_dev = STDERR if $DEBUG
> 
> ```
> parameters = {
> :sellerID => @seller.sellerID,
> :maxPossPrice => @seller.openingOffer.to_f,
> :itemID => @seller.itemID,
> :deadline => DateTime.now,
> :timePeriods => time_periods
> }
> 
> puts obj.clientGatewayCreateNewSeller(parameters)
> 
> ```

If you can use a Time instance rather than a Date, you can get the  
format you want with something like this:

now = Time.now.utc  
now.strftime(“%Y-%m-%dT%H:%M:%S%%sZ”)%[(‘.%03d’%[now.usec/1000]).sub(/  
.?0+\z/,‘’)]

Note that the .sub() takes off trailing 0’s from the fractional time  
which may not be something that you want. Basically, you need to roll  
your own method (as you seem to have sensed). Seems like you can  
create a similar method for your DateTime instance using  
DateTime#sec\_fraction like I’ve used Time#usec (microseconds; µs)  
along with DateTime#strftime.

-Rob

Rob B. [http://agileconsultingllc.com](http://agileconsultingllc.com)  
removed\_email\_address@domain.invalid
