<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DZone Snippets: datetime code</title>
    <link>http://snippets.dzone.com/posts</link>
    <pubDate>Sun, 27 Jul 2008 00:28:25 GMT</pubDate>
    <description>DZone Snippets: datetime code</description>
    <item>
      <title>Calculate Last Day of Last Month</title>
      <link>http://snippets.dzone.com/posts/show/5076</link>
      <description>VB/VBA/VB.NET one-liner to calculate the end of last month. Useful for SSRS/RDL Expressions and Excel/Office Formulas. Note that it does not use string parsing, which can cause localization problems.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;DateAdd("d", -1.0 * DatePart("d", Today), Today)&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Fri, 01 Feb 2008 22:51:12 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/5076</guid>
      <author>jokeyxero (xero)</author>
    </item>
    <item>
      <title>Calculate First Day of Current Month</title>
      <link>http://snippets.dzone.com/posts/show/5075</link>
      <description>VB/VBA/VB.NET one-liner to calculate the start of the current month. Useful for SSRS/RDL Expressions and Excel/Office Formulas. Note that it does not use string parsing, which can cause localization problems.&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;DateAdd("D", -1.0 * DatePart("D", Today) + 1, Today)&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Fri, 01 Feb 2008 22:48:45 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/5075</guid>
      <author>jokeyxero (xero)</author>
    </item>
    <item>
      <title>Calculate First Day of Last Month</title>
      <link>http://snippets.dzone.com/posts/show/5074</link>
      <description>VB/VBA/VB.NET one-liner to calculate the start of the previous month. Useful for SSRS/RDL Expressions and Excel/Office Formulas. Note that it does not use string parsing, which can cause localization problems.&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;DateAdd("D", -1.0 * DatePart("D", Today) + 1, DateAdd("m", -1, Today))&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Fri, 01 Feb 2008 22:47:11 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/5074</guid>
      <author>jokeyxero (xero)</author>
    </item>
    <item>
      <title>DateTime to double and vice versa</title>
      <link>http://snippets.dzone.com/posts/show/4068</link>
      <description>&lt;code&gt;&lt;br /&gt; public static double ToDouble(DateTime what)&lt;br /&gt; {&lt;br /&gt;  return BitConverter.ToDouble(BitConverter.GetBytes(what.Ticks), 0);&lt;br /&gt; }&lt;br /&gt;&lt;br /&gt; public static DateTime ToDateTime(double what)&lt;br /&gt; {&lt;br /&gt;  return new DateTime(BitConverter.ToInt64(BitConverter.GetBytes(what), 0));&lt;br /&gt; }&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Mon, 28 May 2007 15:06:12 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/4068</guid>
      <author>mstampar (Miroslav Stampar)</author>
    </item>
    <item>
      <title>Display DateTime up to milliseconds</title>
      <link>http://snippets.dzone.com/posts/show/4064</link>
      <description>Format string "ffff" is responsible for displaying milliseconds.&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt; string myTime = DateTime.Now.ToString("yyyy.MM.dd HH:mm:ss:ffff");&lt;br /&gt; Console.WriteLine(myTime);&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Fri, 25 May 2007 09:56:44 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/4064</guid>
      <author>mstampar (Miroslav Stampar)</author>
    </item>
    <item>
      <title>Set Date &amp; Time</title>
      <link>http://snippets.dzone.com/posts/show/4048</link>
      <description>&lt;code&gt;&lt;br /&gt;public struct SystemTime&lt;br /&gt;{&lt;br /&gt; public ushort Year;&lt;br /&gt; public ushort Month;&lt;br /&gt; public ushort DayOfWeek;&lt;br /&gt; public ushort Day;&lt;br /&gt; public ushort Hour;&lt;br /&gt; public ushort Minute;&lt;br /&gt; public ushort Second;&lt;br /&gt; public ushort Millisecond;&lt;br /&gt;};&lt;br /&gt;&lt;br /&gt;[DllImport("kernel32.dll", EntryPoint = "GetSystemTime", SetLastError = true)]&lt;br /&gt;public extern static void Win32GetSystemTime(ref SystemTime st);&lt;br /&gt;&lt;br /&gt;[DllImport("kernel32.dll", EntryPoint = "SetSystemTime", SetLastError = true)]&lt;br /&gt;public extern static bool Win32SetSystemTime(ref SystemTime st);&lt;br /&gt;&lt;br /&gt;....&lt;br /&gt;&lt;br /&gt;public static void Test()&lt;br /&gt;{&lt;br /&gt; SystemTime newTime = new SystemTime();&lt;br /&gt; newTime.Year = (ushort)2005;&lt;br /&gt; newTime.Month = (ushort)12;&lt;br /&gt; newTime.Day = (ushort)2;&lt;br /&gt; newTime.Hour = (ushort)12; //UTC time (if you are in UTC+2 zone then you'll put here: time - 2h)&lt;br /&gt; newTime.Minute = (ushort)42;&lt;br /&gt; newTime.Second = (ushort)11;&lt;br /&gt; Win32SetSystemTime(ref newTime);&lt;br /&gt;}&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Mon, 21 May 2007 17:19:01 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/4048</guid>
      <author>mstampar (Miroslav Stampar)</author>
    </item>
    <item>
      <title>SQL Reporting Services Date Conversion</title>
      <link>http://snippets.dzone.com/posts/show/2237</link>
      <description>// A single line function to convert a date from one format to another.  &lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;=System.DateTime.ParseExact(Parameters!lic_period_start_date.Value,"dd/MM/yyyy",System.Globalization.DateTimeFormatInfo.InvariantInfo).ToString("yyyyMMdd")&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Fri, 30 Jun 2006 01:58:09 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/2237</guid>
      <author>rengber (Rob)</author>
    </item>
    <item>
      <title>Fix for ActiveRecord SQL Server adapter dates</title>
      <link>http://snippets.dzone.com/posts/show/2140</link>
      <description>The SQL Server adapter for ActiveRecord uses Time objects to cast dates from the db. This fails for dates before 1970, thus some birthdates come back as nil. This is some kludge to use a DateTime in that case so we still get the value.&lt;br /&gt;&lt;br /&gt;A better approach may be to convert this code to use DateTime objects exclusively, but I'm not sure of the speed implications of doing so. The code below first tries to cast the value to a Time object; if that fails, it tries a DateTime object; if that fails, it returns nil.&lt;br /&gt;&lt;br /&gt;Stick this in a plugin to use with Rails.&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;module ActiveRecord&lt;br /&gt;  module ConnectionAdapters&lt;br /&gt;    class ColumnWithIdentity&lt;br /&gt;      def cast_to_time(value)&lt;br /&gt;        return value if value.is_a?(Time) or value.is_a?(DateTime)&lt;br /&gt;        time_array = ParseDate.parsedate(value)&lt;br /&gt;        time_array[0] ||= 2000&lt;br /&gt;        time_array[1] ||= 1&lt;br /&gt;        time_array[2] ||= 1&lt;br /&gt;        Time.send(Base.default_timezone, *time_array) rescue DateTime.new(*time_array[0..5]) rescue nil&lt;br /&gt;      end&lt;br /&gt;      def cast_to_datetime(value)&lt;br /&gt;        if value.is_a?(Time) or value.is_a?(DateTime)&lt;br /&gt;          if value.year != 0 and value.month != 0 and value.day != 0&lt;br /&gt;            return value&lt;br /&gt;          else&lt;br /&gt;            return Time.mktime(2000, 1, 1, value.hour, value.min, value.sec) rescue nil&lt;br /&gt;          end&lt;br /&gt;        end&lt;br /&gt;        return cast_to_time(value) if value.is_a?(Date) or value.is_a?(String) rescue nil&lt;br /&gt;        value&lt;br /&gt;      end&lt;br /&gt;    end&lt;br /&gt;  end&lt;br /&gt;end&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Mon, 05 Jun 2006 18:26:23 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/2140</guid>
      <author>timmorgan (Tim Morgan)</author>
    </item>
    <item>
      <title>Another simple datetime+struct_time class</title>
      <link>http://snippets.dzone.com/posts/show/1694</link>
      <description>I had shown how datetime can be used in python 2.2&lt;br /&gt;in a &lt;a href=http://www.bigbold.com/snippets/posts/show/1614&gt;previous snippet&lt;/a&gt;. Still, it's sometimes too big.&lt;br /&gt;&lt;br /&gt;Here's a minimal implementation. It works like both&lt;br /&gt;datetime and stuct_time (as returned by localtime() and gmtime()).&lt;br /&gt;&lt;code&gt;&lt;br /&gt;import time&lt;br /&gt;&lt;br /&gt;class datetime(object):&lt;br /&gt;    def __init__(self, *argv):&lt;br /&gt;        self.t = time.struct_time(argv+(0,)*(9-len(argv)))    # append to length 9 &lt;br /&gt;    def __getattr__(self, name):&lt;br /&gt;        try:&lt;br /&gt;            i = ['year', 'month', 'day', 'hour', 'minute', 'second', 'weekday'].index(name)&lt;br /&gt;            return self.t[i]&lt;br /&gt;        except:&lt;br /&gt;            return getattr(self.t, name)&lt;br /&gt;    def __len__(self): return len(self.t)&lt;br /&gt;    def __getitem__(self, key): return self.t[key]&lt;br /&gt;    def __repr__(self): return repr(self.t)&lt;br /&gt;    def now(self=None):&lt;br /&gt;        return datetime(*time.localtime())&lt;br /&gt;    now = staticmethod(now)&lt;br /&gt;    def strftime(self, fmt="%Y-%m-%d %H:%M:%S"):&lt;br /&gt;        return time.strftime(fmt, self.t)&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;Here's its usage&lt;br /&gt;&lt;code&gt;&lt;br /&gt;# here it works like datetime.datetime()&lt;br /&gt;&gt;&gt;&gt; t = datetime.now()&lt;br /&gt;&gt;&gt;&gt; t.year, t.month, t.day&lt;br /&gt;(2006, 3, 13)&lt;br /&gt;&gt;&gt;&gt; t.hour, t.minute, t.second&lt;br /&gt;(23, 3, 28)&lt;br /&gt;&lt;br /&gt;# but also works like localtime()&lt;br /&gt;&gt;&gt;&gt; t&lt;br /&gt;(2006, 3, 13, 23, 3, 28, 0, 72, -1)&lt;br /&gt;&gt;&gt;&gt; t.tm_year, t[0]&lt;br /&gt;(2006, 2006)&lt;br /&gt;&gt;&gt;&gt; mktime(t)&lt;br /&gt;1142265808.0&lt;br /&gt;&lt;br /&gt;# good default for strftime (= ctime)&lt;br /&gt;&gt;&gt;&gt; t.strftime()&lt;br /&gt;'2006-03-13 23:03:28'&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;</description>
      <pubDate>Mon, 13 Mar 2006 21:08:56 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/1694</guid>
      <author>korakot (Korakot Chaovavanich)</author>
    </item>
    <item>
      <title>Using datetime in python 2.2 (e.g. pys60)</title>
      <link>http://snippets.dzone.com/posts/show/1614</link>
      <description>Python 2.2 doesn't have a decend date/time class.&lt;br /&gt;There's a small compatible library from the &lt;a href=http://www.pythonweb.org/projects/webmodules/&gt;Python Web&lt;/a&gt; Project.&lt;br /&gt;&lt;br /&gt;I download the source and extract the datetime.py then&lt;br /&gt;send it to my phone as a pys60 library.&lt;br /&gt;Here's a simple test that works well.&lt;br /&gt;(Taken from &lt;a href=http://online.effbot.org/2003_08_01_archive.htm#librarybook-datetime-module&gt;Effbot&lt;/a&gt;)&lt;br /&gt;&lt;code&gt;&lt;br /&gt;&gt;&gt;&gt; import datetime&lt;br /&gt;&gt;&gt;&gt; now = datetime.datetime(2003, 8, 4, 12, 30, 45)&lt;br /&gt;&gt;&gt;&gt; print now&lt;br /&gt;2003-08-04 12:30:45&lt;br /&gt;&gt;&gt;&gt; print repr(now)&lt;br /&gt;datetime.datetime(2003,8,4,12,30,45)&lt;br /&gt;&gt;&gt;&gt; print type(now)&lt;br /&gt;&lt;type 'instance'&gt;&lt;br /&gt;&gt;&gt;&gt; print now.year, now.month, now.day&lt;br /&gt;2003 8 4&lt;br /&gt;&gt;&gt;&gt; print now.hour, now.minute, now.second&lt;br /&gt;12 30 45&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;See its documentation &lt;a href=http://www.pythonweb.org/projects/webmodules/doc/0.5.3/html_multipage/lib/module-datetime.html&gt;here&lt;/a&gt;.&lt;br /&gt;One limitation as a library is that you need to use&lt;br /&gt;&lt;code&gt;datetime.datetime(2004,1,1).now() # work for any version&lt;/code&gt;&lt;br /&gt;instead of just&lt;br /&gt;&lt;code&gt;datetime.datetime.now() # python 2.3+&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;update&lt;br /&gt;======&lt;br /&gt;I add 'staticmethod' expression. Now it works fine.&lt;br /&gt;&lt;code&gt;&lt;br /&gt;    def now(self=None):&lt;br /&gt;        "Return the current date and time as a datetime."&lt;br /&gt;        now = t.localtime()&lt;br /&gt;        return datetime(now[0],now[1],now[2],now[3],now[4],now[5])&lt;br /&gt;    now = staticmethod(now)&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Thu, 02 Mar 2006 17:06:57 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/1614</guid>
      <author>korakot (Korakot Chaovavanich)</author>
    </item>
  </channel>
</rss>
